Skip to content

Commit 89bdde6

Browse files
committed
feat(cli): add version check and update commands
- Add `trc -v` to check for new versions from RubyGems.org - Add `trc update` command to update t-ruby to latest version - Create VersionChecker class for API communication
1 parent 9c4590f commit 89bdde6

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

lib/t_ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "t_ruby/version"
4+
require_relative "t_ruby/version_checker"
45
require_relative "t_ruby/config"
56

67
# Core infrastructure (must be loaded first)

lib/t_ruby/cli.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class CLI
1313
trc --watch, -w Watch input files and recompile on change
1414
trc --decl <file.trb> Generate .d.trb declaration file
1515
trc --lsp Start LSP server (for IDE integration)
16-
trc --version, -v Show version
16+
trc update Update t-ruby to the latest version
17+
trc --version, -v Show version (and check for updates)
1718
trc --help, -h Show this help
1819
1920
Examples:
@@ -44,6 +45,12 @@ def run
4445

4546
if @args.include?("--version") || @args.include?("-v")
4647
puts "trc #{VERSION}"
48+
check_for_updates
49+
return
50+
end
51+
52+
if @args.include?("update")
53+
update_gem
4754
return
4855
end
4956

@@ -78,6 +85,24 @@ def run
7885

7986
private
8087

88+
def check_for_updates
89+
result = VersionChecker.check
90+
return unless result
91+
92+
puts ""
93+
puts "New version available: #{result[:latest]} (current: #{result[:current]})"
94+
puts "Run 'trc update' to update"
95+
end
96+
97+
def update_gem
98+
puts "Updating t-ruby..."
99+
if VersionChecker.update
100+
puts "Successfully updated t-ruby!"
101+
else
102+
puts "Update failed. Try: gem install t-ruby"
103+
end
104+
end
105+
81106
def init_project
82107
config_file = "trbconfig.yml"
83108
src_dir = "src"

lib/t_ruby/version_checker.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "net/http"
4+
require "json"
5+
require "uri"
6+
7+
module TRuby
8+
class VersionChecker
9+
GEM_NAME = "t-ruby"
10+
RUBYGEMS_API = "https://rubygems.org/api/v1/gems/#{GEM_NAME}.json".freeze
11+
12+
def self.check
13+
new.check
14+
end
15+
16+
def self.update
17+
new.update
18+
end
19+
20+
def check
21+
latest = fetch_latest_version
22+
return nil unless latest
23+
24+
current = Gem::Version.new(VERSION)
25+
latest_version = Gem::Version.new(latest)
26+
27+
return nil if current >= latest_version
28+
29+
{ current: VERSION, latest: latest }
30+
end
31+
32+
def update
33+
system("gem install #{GEM_NAME}")
34+
end
35+
36+
private
37+
38+
def fetch_latest_version
39+
uri = URI(RUBYGEMS_API)
40+
response = Net::HTTP.get_response(uri)
41+
42+
return nil unless response.is_a?(Net::HTTPSuccess)
43+
44+
data = JSON.parse(response.body)
45+
data["version"]
46+
rescue StandardError
47+
nil
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)