-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.rb
More file actions
263 lines (220 loc) · 7.1 KB
/
cli.rb
File metadata and controls
263 lines (220 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# frozen_string_literal: true
module TRuby
class CLI
HELP_TEXT = <<~HELP.freeze
t-ruby compiler (trc) v#{VERSION}
Usage:
trc <file.trb> Compile a .trb file to .rb
trc <file.rb> Copy .rb file to build/ and generate .rbs
trc --init Initialize a new t-ruby project
trc --config, -c <path> Use custom config file
trc --watch, -w Watch input files and recompile on change
trc --decl <file.trb> Generate .d.trb declaration file
trc --lsp Start LSP server (for IDE integration)
trc update Update t-ruby to the latest version
trc --version, -v Show version (and check for updates)
trc --help, -h Show this help
Examples:
trc hello.trb Compile hello.trb to build/hello.rb
trc utils.rb Copy utils.rb to build/ and generate utils.rbs
trc --init Create trbconfig.yml and src/, build/ directories
trc -c custom.yml file.trb Compile with custom config file
trc -w Watch all .trb and .rb files in current directory
trc -w src/ Watch all .trb and .rb files in src/ directory
trc --watch hello.trb Watch specific file for changes
trc --decl hello.trb Generate hello.d.trb declaration file
trc --lsp Start language server for VS Code
HELP
def self.run(args)
new(args).run
end
def initialize(args)
@args = args
end
def run
if @args.empty? || @args.include?("--help") || @args.include?("-h")
puts HELP_TEXT
return
end
if @args.include?("--version") || @args.include?("-v")
puts "trc #{VERSION}"
check_for_updates
return
end
if @args.include?("update")
update_gem
return
end
if @args.include?("--init")
init_project
return
end
if @args.include?("--lsp")
start_lsp_server
return
end
if @args.include?("--watch") || @args.include?("-w")
start_watch_mode
return
end
if @args.include?("--decl")
input_file = @args[@args.index("--decl") + 1]
generate_declaration(input_file)
return
end
# Extract config path if --config or -c flag is present
config_path = extract_config_path
# Get input file (first non-flag argument)
input_file = find_input_file
compile(input_file, config_path: config_path)
end
private
def check_for_updates
result = VersionChecker.check
return unless result
puts ""
puts "New version available: #{result[:latest]} (current: #{result[:current]})"
puts "Run 'trc update' to update"
end
def update_gem
puts "Updating t-ruby..."
if VersionChecker.update
puts "Successfully updated t-ruby!"
else
puts "Update failed. Try: gem install t-ruby"
end
end
def init_project
config_file = "trbconfig.yml"
src_dir = "src"
build_dir = "build"
created = []
skipped = []
# Create trbconfig.yml with new schema
if File.exist?(config_file)
skipped << config_file
else
File.write(config_file, <<~YAML)
# T-Ruby configuration file
# See: https://type-ruby.github.io/docs/getting-started/project-configuration
source:
include:
- #{src_dir}
exclude: []
extensions:
- ".trb"
- ".rb"
output:
ruby_dir: #{build_dir}
# rbs_dir: sig # Optional: separate directory for .rbs files
# clean_before_build: false
compiler:
strictness: standard # strict | standard | permissive
generate_rbs: true
target_ruby: "#{RubyVersion.current.major}.#{RubyVersion.current.minor}"
# experimental: []
# checks:
# no_implicit_any: false
# no_unused_vars: false
# strict_nil: false
watch:
# paths: [] # Additional paths to watch
debounce: 100
# clear_screen: false
# on_success: "bundle exec rspec"
YAML
created << config_file
end
# Create src/ directory
if Dir.exist?(src_dir)
skipped << "#{src_dir}/"
else
Dir.mkdir(src_dir)
created << "#{src_dir}/"
end
# Create build/ directory
if Dir.exist?(build_dir)
skipped << "#{build_dir}/"
else
Dir.mkdir(build_dir)
created << "#{build_dir}/"
end
# Output results
if created.any?
puts "Created: #{created.join(", ")}"
end
if skipped.any?
puts "Skipped (already exists): #{skipped.join(", ")}"
end
if created.empty? && skipped.any?
puts "Project already initialized."
else
puts "t-ruby project initialized successfully!"
end
end
def start_lsp_server
server = LSPServer.new
server.run
end
def start_watch_mode
# Get paths to watch (everything after --watch or -w flag)
watch_index = @args.index("--watch") || @args.index("-w")
paths = @args[(watch_index + 1)..]
# Default to current directory if no paths specified
paths = ["."] if paths.empty?
config = Config.new
watcher = Watcher.new(paths: paths, config: config)
watcher.watch
end
def generate_declaration(input_file)
config = Config.new
generator = DeclarationGenerator.new
output_path = generator.generate_file(input_file, config.out_dir)
puts "Generated: #{input_file} -> #{output_path}"
rescue ArgumentError => e
puts "Error: #{e.message}"
exit 1
end
def compile(input_file, config_path: nil)
config = Config.new(config_path)
compiler = Compiler.new(config)
result = compiler.compile_with_diagnostics(input_file)
if result[:success]
puts "Compiled: #{input_file} -> #{result[:output_path]}"
else
formatter = DiagnosticFormatter.new(use_colors: $stdout.tty?)
result[:diagnostics].each do |diagnostic|
puts formatter.format(diagnostic)
end
puts
puts formatter.send(:format_summary, result[:diagnostics])
exit 1
end
end
# Extract config path from --config or -c flag
def extract_config_path
config_index = @args.index("--config") || @args.index("-c")
return nil unless config_index
@args[config_index + 1]
end
# Find the input file (first non-flag argument)
def find_input_file
skip_next = false
@args.each do |arg|
if skip_next
skip_next = false
next
end
# Skip known flags with arguments
if %w[--config -c --decl].include?(arg)
skip_next = true
next
end
# Skip flags without arguments
next if arg.start_with?("-")
return arg
end
nil
end
end
end