Skip to content

Commit 174cc84

Browse files
committed
feat(compiler): add .rb file support with rbs prototype
- Copy .rb files from src to build directory - Generate .rbs signatures using 'rbs prototype rb' - Update watcher to monitor both .trb and .rb files - Bump version to 0.0.10
1 parent 3b58dd1 commit 174cc84

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

lib/t_ruby/cli.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class CLI
77
88
Usage:
99
trc <file.trb> Compile a .trb file to .rb
10+
trc <file.rb> Copy .rb file to build/ and generate .rbs
1011
trc --init Initialize a new t-ruby project
1112
trc --watch, -w Watch input files and recompile on change
1213
trc --decl <file.trb> Generate .d.trb declaration file
@@ -16,9 +17,10 @@ class CLI
1617
1718
Examples:
1819
trc hello.trb Compile hello.trb to build/hello.rb
20+
trc utils.rb Copy utils.rb to build/ and generate utils.rbs
1921
trc --init Create trbconfig.yml and src/, build/ directories
20-
trc -w Watch all .trb files in current directory
21-
trc -w src/ Watch all .trb files in src/ directory
22+
trc -w Watch all .trb and .rb files in current directory
23+
trc -w src/ Watch all .trb and .rb files in src/ directory
2224
trc --watch hello.trb Watch specific file for changes
2325
trc --decl hello.trb Generate hello.d.trb declaration file
2426
trc --lsp Start language server for VS Code

lib/t_ruby/compiler.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ def compile(input_path)
2020
raise ArgumentError, "File not found: #{input_path}"
2121
end
2222

23+
# Handle .rb files separately
24+
if input_path.end_with?(".rb")
25+
return copy_ruby_file(input_path)
26+
end
27+
2328
unless input_path.end_with?(".trb")
24-
raise ArgumentError, "Expected .trb file, got: #{input_path}"
29+
raise ArgumentError, "Expected .trb or .rb file, got: #{input_path}"
2530
end
2631

2732
source = File.read(input_path)
@@ -214,6 +219,36 @@ def generate_dtrb_file(input_path, out_dir)
214219
generator = DeclarationGenerator.new
215220
generator.generate_file(input_path, out_dir)
216221
end
222+
223+
# Copy .rb file to output directory and generate .rbs signature
224+
def copy_ruby_file(input_path)
225+
unless File.exist?(input_path)
226+
raise ArgumentError, "File not found: #{input_path}"
227+
end
228+
229+
out_dir = @config.out_dir
230+
FileUtils.mkdir_p(out_dir)
231+
232+
base_filename = File.basename(input_path, ".rb")
233+
output_path = File.join(out_dir, base_filename + ".rb")
234+
235+
# Copy the .rb file to output directory
236+
FileUtils.cp(input_path, output_path)
237+
238+
# Generate .rbs file if enabled in config
239+
if @config.emit["rbs"]
240+
generate_rbs_from_ruby(base_filename, out_dir, input_path)
241+
end
242+
243+
output_path
244+
end
245+
246+
# Generate RBS from Ruby file using rbs prototype
247+
def generate_rbs_from_ruby(base_filename, out_dir, input_path)
248+
rbs_path = File.join(out_dir, base_filename + ".rbs")
249+
result = `rbs prototype rb #{input_path} 2>/dev/null`
250+
File.write(rbs_path, result) unless result.strip.empty?
251+
end
217252
end
218253

219254
# IR-aware code generator for source-preserving transformation

lib/t_ruby/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module TRuby
4-
VERSION = "0.0.9"
4+
VERSION = "0.0.10"
55
end

lib/t_ruby/watcher.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def watch
5959
compile_all
6060
@stats[:total_time] += Time.now - start_time
6161

62-
# Start watching
63-
listener = Listen.to(*watch_directories, only: /\.trb$/) do |modified, added, removed|
62+
# Start watching (.trb and .rb files)
63+
listener = Listen.to(*watch_directories, only: /\.(trb|rb)$/) do |modified, added, removed|
6464
handle_changes(modified, added, removed)
6565
end
6666

@@ -91,7 +91,7 @@ def watch_directories
9191
end
9292

9393
def handle_changes(modified, added, removed)
94-
changed_files = (modified + added).select { |f| f.end_with?(".trb") }
94+
changed_files = (modified + added).select { |f| f.end_with?(".trb") || f.end_with?(".rb") }
9595
return if changed_files.empty? && removed.empty?
9696

9797
puts
@@ -120,25 +120,33 @@ def compile_all
120120
errors = []
121121

122122
trb_files = find_trb_files
123-
@file_count = trb_files.size
123+
rb_files = find_rb_files
124+
all_files = trb_files + rb_files
125+
@file_count = all_files.size
124126

125127
if @incremental && @cross_file_check
126128
# Use enhanced incremental compiler with cross-file checking
127129
result = @incremental_compiler.compile_all_with_checking(trb_files)
128130
errors = result[:errors].map { |e| format_error(e[:file], e[:error] || e[:message]) }
129131
@error_count = errors.size
130132
@stats[:total_compilations] += trb_files.size
131-
elsif @parallel && trb_files.size > 1
133+
134+
# Also compile .rb files
135+
rb_files.each do |file|
136+
result = compile_file(file)
137+
errors.concat(result[:errors]) if result[:errors].any?
138+
end
139+
elsif @parallel && all_files.size > 1
132140
# Parallel compilation
133-
results = @parallel_processor.process_files(trb_files) do |file|
141+
results = @parallel_processor.process_files(all_files) do |file|
134142
compile_file(file)
135143
end
136144
results.each do |result|
137145
errors.concat(result[:errors]) if result[:errors]&.any?
138146
end
139147
else
140148
# Sequential compilation
141-
trb_files.each do |file|
149+
all_files.each do |file|
142150
result = compile_file(file)
143151
errors.concat(result[:errors]) if result[:errors].any?
144152
end
@@ -234,6 +242,18 @@ def find_trb_files
234242
files.uniq
235243
end
236244

245+
def find_rb_files
246+
files = []
247+
@paths.each do |path|
248+
if File.directory?(path)
249+
files.concat(Dir.glob(File.join(path, "**", "*.rb")))
250+
elsif File.file?(path) && path.end_with?(".rb")
251+
files << path
252+
end
253+
end
254+
files.uniq
255+
end
256+
237257
def format_error(file, message)
238258
# Parse error message for line/column info if available
239259
# Format: file:line:col - error TRB0001: message

spec/t_ruby/cli_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
end
151151

152152
context "with wrong file extension" do
153-
it "displays error message for .rb file" do
153+
it "compiles .rb file successfully (copies to build and generates rbs)" do
154154
Dir.mktmpdir do |tmpdir|
155155
input_file = File.join(tmpdir, "test.rb")
156156
File.write(input_file, "puts 'hello'")
@@ -159,7 +159,7 @@
159159

160160
expect {
161161
cli.run
162-
}.to raise_error(SystemExit)
162+
}.to output(/Compiled: .* -> /).to_stdout
163163
end
164164
end
165165

0 commit comments

Comments
 (0)