Class: AgentCode::Commands::BlueprintCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/agentcode/commands/blueprint_command.rb

Overview

Zero-token deterministic code generation from YAML blueprint specs. Port of agentcode-server BlueprintCommand.php / agentcode-adonis-server blueprint.ts.

Usage: rails agentcode:blueprint [OPTIONS]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlueprintCommand

Returns a new instance of BlueprintCommand.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agentcode/commands/blueprint_command.rb', line 22

def initialize
  super
  @options = {
    dir: ".agentcode/blueprints",
    model: nil,
    force: false,
    dry_run: false,
    skip_tests: false,
    skip_seeders: false
  }
  @migration_timestamp_offset = 0
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/agentcode/commands/blueprint_command.rb', line 20

def options
  @options
end

Instance Method Details

#performObject



35
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
# File 'lib/agentcode/commands/blueprint_command.rb', line 35

def perform
  print_banner

  blueprints_dir = Rails.root.join(options[:dir]).to_s

  unless Dir.exist?(blueprints_dir)
    say "Blueprint directory not found: #{blueprints_dir}", :red
    say "Run 'rails agentcode:install' first, or create the directory manually.", :yellow
    return
  end

  parser = AgentCode::Blueprint::BlueprintParser.new
  validator = AgentCode::Blueprint::BlueprintValidator.new
  manifest = AgentCode::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.message}", :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 options[:model]
    yaml_files = yaml_files.select { |f| File.basename(f, ".yaml") == options[: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

  # 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.message}", :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 options[: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 options[: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 options[: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 options[:skip_seeders] || options[:dry_run] || all_blueprints.empty?
    generate_seeders(roles, all_blueprints, is_multi_tenant)
  end

  # 5. Save manifest
  manifest.save unless options[: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