Module: Kumi::Dev::Codegen
- Defined in:
- lib/kumi/dev/codegen.rb
Class Method Summary collapse
- .available_schemas ⇒ Object
- .build!(schema_name, targets: %w[ruby],, output_dir: nil) ⇒ Object
- .build_ruby!(schema_path, output_dir) ⇒ Object
- .find_expected_output(schema_name) ⇒ Object
- .find_schema_path(name) ⇒ Object
- .find_test_input(schema_name) ⇒ Object
- .list ⇒ Object
- .schema_paths ⇒ Object
- .test!(*schema_names) ⇒ Object
- .test_schema!(schema_name) ⇒ Object
- .verify!(schema_name) ⇒ Object
Class Method Details
.available_schemas ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/kumi/dev/codegen.rb', line 87 def available_schemas schema_paths.map do |path| if path.start_with?("golden/") # Extract schema name from golden/SCHEMA_NAME/schema.kumi path.split("/")[1] else # For standalone schemas, use the filename without extension File.basename(path, ".kumi") end end.uniq.sort end |
.build!(schema_name, targets: %w[ruby],, output_dir: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/kumi/dev/codegen.rb', line 17 def build!(schema_name, targets: %w[ruby], output_dir: nil) schema_path = find_schema_path(schema_name) raise "Schema '#{schema_name}' not found" unless schema_path output_dir ||= "codegen/#{schema_name}" FileUtils.mkdir_p(output_dir) targets.each do |target| case target when "ruby" build_ruby!(schema_path, output_dir) else puts "Warning: Unknown target '#{target}'" end end puts "Generated code in #{output_dir}" end |
.build_ruby!(schema_path, output_dir) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/kumi/dev/codegen.rb', line 109 def build_ruby!(schema_path, output_dir) schema, = Kumi::Frontends.load(path: schema_path) result = Kumi::Analyzer.analyze!(schema) result.state[:ruby_codegen_files]["codegen.rb"] File.write("#{output_dir}/schema_ruby.rb", ruby_code) puts " ✓ Ruby code generated" ruby_code end |
.find_expected_output(schema_name) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/kumi/dev/codegen.rb', line 184 def find_expected_output(schema_name) candidates = [ "golden/#{schema_name}/expected.json", "codegen/#{schema_name}/expected.json", "test/outputs/#{schema_name}.json" ] candidates.find { |path| File.exist?(path) } end |
.find_schema_path(name) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/kumi/dev/codegen.rb', line 99 def find_schema_path(name) candidates = [ "golden/#{name}/schema.kumi", "schemas/#{name}.kumi", name.end_with?(".kumi") ? name : "#{name}.kumi" ] candidates.find { |path| File.exist?(path) } end |
.find_test_input(schema_name) ⇒ Object
175 176 177 178 179 180 181 182 |
# File 'lib/kumi/dev/codegen.rb', line 175 def find_test_input(schema_name) candidates = [ "golden/#{schema_name}/input.json", "codegen/#{schema_name}/input.json", "test/inputs/#{schema_name}.json" ] candidates.find { |path| File.exist?(path) } end |
.list ⇒ Object
11 12 13 14 15 |
# File 'lib/kumi/dev/codegen.rb', line 11 def list available_schemas.each do |name| puts name end end |
.schema_paths ⇒ Object
83 84 85 |
# File 'lib/kumi/dev/codegen.rb', line 83 def schema_paths Dir.glob("golden/*/schema.kumi") + Dir.glob("schemas/*.kumi") end |
.test!(*schema_names) ⇒ Object
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 |
# File 'lib/kumi/dev/codegen.rb', line 36 def test!(*schema_names) names = schema_names.empty? ? available_schemas : schema_names.flatten success = true total_tests = 0 passed_tests = 0 names.each do |name| puts "Testing #{name}..." begin schema_success, schema_total, schema_passed = test_schema!(name) success &&= schema_success total_tests += schema_total passed_tests += schema_passed rescue StandardError => e puts " ✗ #{name}: ERROR - #{e.message}" success = false end end puts "=== Test Summary ===" puts "Schemas: #{names.length}" puts "Total: #{total_tests}" puts "Passed: #{passed_tests}" puts "Failed: #{total_tests - passed_tests}" success_rate = total_tests > 0 ? (passed_tests.to_f / total_tests * 100).round(1) : 0 puts "Success rate: #{success_rate}%" success end |
.test_schema!(schema_name) ⇒ Object
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 |
# File 'lib/kumi/dev/codegen.rb', line 121 def test_schema!(schema_name) output_dir = "codegen/#{schema_name}" generated_file = "#{output_dir}/schema_ruby.rb" input_file = find_test_input(schema_name) expected_file = find_expected_output(schema_name) unless File.exist?(generated_file) puts " ✗ No generated code found" return [false, 0, 0] end unless input_file && File.exist?(input_file) puts " ✗ No test input found" return [false, 0, 0] end unless expected_file && File.exist?(expected_file) puts " ✗ No expected output found" return [false, 0, 0] end code = File.read(generated_file) input_data = JSON.parse(File.read(input_file)) expected_outputs = JSON.parse(File.read(expected_file)) temp_file = "#{output_dir}/test_runner.rb" File.write(temp_file, code) begin load temp_file registry = Kumi::KernelRegistry.load_ruby program = Generated::Program.new(registry: registry) bound = program.from(input_data) total = expected_outputs.size passed = 0 expected_outputs.each do |decl_name, expected_value| actual = bound[decl_name.to_sym] if actual == expected_value puts " ✓ #{decl_name}: #{actual.inspect}" passed += 1 else puts " ✗ #{decl_name}: got #{actual.inspect}, expected #{expected_value.inspect}" end end [passed == total, total, passed] ensure FileUtils.rm_f(temp_file) end end |
.verify!(schema_name) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/kumi/dev/codegen.rb', line 67 def verify!(schema_name) schema_path = find_schema_path(schema_name) raise "Schema '#{schema_name}' not found" unless schema_path output_dir = "codegen/#{schema_name}" expected_file = "#{output_dir}/expected.json" unless File.exist?(expected_file) puts "No expected outputs for #{schema_name}" return false end build!(schema_name, output_dir: output_dir) test_schema!(schema_name) end |