Class: TRuby::DeclarationGenerator
- Inherits:
-
Object
- Object
- TRuby::DeclarationGenerator
- Defined in:
- lib/t_ruby/declaration_generator.rb
Overview
Generator for .d.trb type declaration files Similar to TypeScript’s .d.ts files
Constant Summary collapse
- DECLARATION_EXTENSION =
".d.trb"
Instance Method Summary collapse
-
#generate(source) ⇒ Object
Generate declaration content from source code.
-
#generate_file(input_path, output_dir = nil) ⇒ Object
Generate declaration file from a .trb source file.
-
#generate_file_to_path(input_path, output_path) ⇒ Object
Generate declaration file to a specific output path.
-
#initialize ⇒ DeclarationGenerator
constructor
A new instance of DeclarationGenerator.
Constructor Details
#initialize ⇒ DeclarationGenerator
Returns a new instance of DeclarationGenerator.
9 10 11 |
# File 'lib/t_ruby/declaration_generator.rb', line 9 def initialize @parser = nil end |
Instance Method Details
#generate(source) ⇒ Object
Generate declaration content from source code
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 |
# File 'lib/t_ruby/declaration_generator.rb', line 14 def generate(source) @parser = Parser.new(source) result = @parser.parse declarations = [] # Add header comment declarations << "# Auto-generated type declaration file" declarations << "# Do not edit manually" declarations << "" # Generate type alias declarations (result[:type_aliases] || []).each do |type_alias| declarations << generate_type_alias(type_alias) end # Generate interface declarations (result[:interfaces] || []).each do |interface| declarations << generate_interface(interface) end # Generate function declarations (result[:functions] || []).each do |function| declarations << generate_function(function) end declarations.join("\n") end |
#generate_file(input_path, output_dir = nil) ⇒ Object
Generate declaration file from a .trb source file
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/t_ruby/declaration_generator.rb', line 44 def generate_file(input_path, output_dir = nil) unless File.exist?(input_path) raise ArgumentError, "File not found: #{input_path}" end unless input_path.end_with?(".trb") raise ArgumentError, "Expected .trb file, got: #{input_path}" end source = File.read(input_path) content = generate(source) # Determine output path output_dir ||= File.dirname(input_path) FileUtils.mkdir_p(output_dir) base_name = File.basename(input_path, ".trb") output_path = File.join(output_dir, "#{base_name}#{DECLARATION_EXTENSION}") File.write(output_path, content) output_path end |
#generate_file_to_path(input_path, output_path) ⇒ Object
Generate declaration file to a specific output path
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/t_ruby/declaration_generator.rb', line 68 def generate_file_to_path(input_path, output_path) unless File.exist?(input_path) raise ArgumentError, "File not found: #{input_path}" end unless input_path.end_with?(".trb") raise ArgumentError, "Expected .trb file, got: #{input_path}" end source = File.read(input_path) content = generate(source) File.write(output_path, content) output_path end |