Class: TRuby::DeclarationGenerator

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeDeclarationGenerator

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