Class: Rhino::Commands::BlueprintCommand
- Inherits:
-
BaseCommand
- Object
- BaseCommand
- Rhino::Commands::BlueprintCommand
- Defined in:
- lib/rhino/commands/blueprint_command.rb
Overview
Zero-token deterministic code generation from YAML blueprint specs. Port of rhino-server BlueprintCommand.php / rhino-adonis-server blueprint.ts.
Usage: rails rhino:blueprint [OPTIONS]
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ BlueprintCommand
constructor
A new instance of BlueprintCommand.
- #perform ⇒ Object
Constructor Details
#initialize ⇒ BlueprintCommand
Returns a new instance of BlueprintCommand.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rhino/commands/blueprint_command.rb', line 23 def initialize super @options = { dir: ".rhino/blueprints", model: nil, force: false, dry_run: false, skip_tests: false, skip_seeders: false } @migration_timestamp_offset = 0 end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
21 22 23 |
# File 'lib/rhino/commands/blueprint_command.rb', line 21 def @options end |
Instance Method Details
#perform ⇒ 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 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 |
# File 'lib/rhino/commands/blueprint_command.rb', line 36 def perform blueprints_dir = Rails.root.join([:dir]).to_s unless Dir.exist?(blueprints_dir) say "Blueprint directory not found: #{blueprints_dir}", :red say "Run 'rails rhino:install' first, or create the directory manually.", :yellow return end parser = Rhino::Blueprint::BlueprintParser.new validator = Rhino::Blueprint::BlueprintValidator.new manifest = Rhino::Blueprint::ManifestManager.new(blueprints_dir) # 1. Parse roles roles_file = File.join(blueprints_dir, "_roles.yaml") roles = {} if File.exist?(roles_file) begin roles = parser.parse_roles(roles_file) role_result = validator.validate_roles(roles) unless role_result[:valid] say "Role validation errors:", :red role_result[:errors].each { |e| say " • #{e}", :red } return end say " ✓ Parsed #{roles.length} roles", :green rescue => e say " ✗ #{e.}", :red return end else say " ⚠ No _roles.yaml found — role cross-reference disabled", :yellow end # 2. Discover YAML files yaml_files = Dir.glob(File.join(blueprints_dir, "*.yaml")) .reject { |f| File.basename(f).start_with?("_", ".") } .sort if [:model] yaml_files = yaml_files.select { |f| File.basename(f, ".yaml") == [:model] } end if yaml_files.empty? say "No blueprint YAML files found in #{blueprints_dir}", :yellow return end say " Found #{yaml_files.length} blueprint(s)", :cyan # Order so a referenced model's table is migrated before any model that # foreign-keys to it (parents before children). Migration timestamps are # assigned in iteration order, so this is what makes the set runnable. parsed_for_order = [] unparseable = [] yaml_files.each do |f| parsed_for_order << { file: f, blueprint: parser.parse_model(f) } rescue StandardError unparseable << f end sorter = Rhino::Blueprint::Sorter.new ordered = sorter.sort(parsed_for_order.map { |p| p[:blueprint] }) file_by_model = parsed_for_order.each_with_object({}) do |p, h| h[p[:blueprint][:model]] ||= p[:file] end yaml_files = ordered.map { |bp| file_by_model[bp[:model]] }.compact + unparseable if sorter.cycles.any? say " ⚠ Circular foreign-key dependency among: #{sorter.cycles.join(', ')}. " \ "Migration order is best-effort — make one side nullable or add the FK in a later migration.", :yellow end # 3. Process each blueprint is_multi_tenant = multi_tenant_enabled? org_identifier = detect_org_identifier generated_count = 0 skipped_count = 0 all_blueprints = [] all_generated_files = {} yaml_files.each do |yaml_file| filename = File.basename(yaml_file) begin blueprint = parser.parse_model(yaml_file) rescue => e say " ✗ #{filename}: #{e.}", :red next end # Validate result = validator.validate_model(blueprint, roles) unless result[:valid] say " ✗ #{filename}:", :red result[:errors].each { |e| say " • #{e}", :red } next end result[:warnings].each { |w| say " ⚠ #{w}", :yellow } # Check manifest current_hash = parser.compute_file_hash(yaml_file) unless [:force] unless manifest.has_changed?(filename, current_hash) say " ⊘ #{blueprint[:model]} — unchanged, skipping", :light_black skipped_count += 1 all_blueprints << blueprint next end end all_blueprints << blueprint generated_files = [] say " → #{blueprint[:model]}...", :cyan unless [:dry_run] # Generate model model_path = generate_model(blueprint, is_multi_tenant) generated_files << model_path say " ✓ Model: #{model_path}", :green # Generate migration migration_path = generate_migration(blueprint) generated_files << migration_path say " ✓ Migration: #{migration_path}", :green # Generate factory factory_path = generate_factory(blueprint) generated_files << factory_path say " ✓ Factory: #{factory_path}", :green # Generate scope scope_path = generate_scope(blueprint) generated_files << scope_path say " ✓ Scope: #{scope_path}", :green # Generate policy policy_path = generate_policy(blueprint) generated_files << policy_path say " ✓ Policy: #{policy_path}", :green # Generate tests unless [:skip_tests] test_path = generate_tests(blueprint, is_multi_tenant, org_identifier) generated_files << test_path say " ✓ Tests: #{test_path}", :green end # Register in config register_model_in_config(blueprint[:model]) # Record in manifest manifest.record_generation(filename, current_hash, generated_files) all_generated_files[filename] = generated_files end generated_count += 1 end # 4. Generate cross-model seeders unless [:skip_seeders] || [:dry_run] || all_blueprints.empty? generate_seeders(roles, all_blueprints, is_multi_tenant) end # 5. Save manifest manifest.save unless [:dry_run] # 6. Summary say "" say "Blueprint generation complete!", :green say " Generated: #{generated_count} model(s)", :cyan say " Skipped: #{skipped_count} (unchanged)", :light_black say "" end |