Class: Legion::CLI::Lex

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/lex_command.rb

Constant Summary collapse

DEFAULT_CATEGORIES =
{
  core:    { type: :list, tier: 1 },
  ai:      { type: :list, tier: 2 },
  gaia:    { type: :list, tier: 3 },
  agentic: { type: :prefix, tier: 4 }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/legion/cli/lex_command.rb', line 18

def self.exit_on_failure?
  true
end

Instance Method Details

#approve_fix(fix_id) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/legion/cli/lex_command.rb', line 254

def approve_fix(fix_id)
  out = formatter
  with_data do
    require 'legion/extensions/codegen/runners/auto_fix'
    result = Legion::Extensions::Codegen::Runners::AutoFix.approve_fix(fix_id: fix_id)
    if result[:success]
      out.success("Fix #{fix_id} approved. Merge the branch manually.")
    else
      out.error("Failed to approve: #{result[:reason]}")
    end
  end
end

#create(name = nil) ⇒ Object



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
# File 'lib/legion/cli/lex_command.rb', line 104

def create(name = nil)
  out = formatter

  if options[:list_templates]
    render_template_list(out)
    return
  end

  unless name
    out.error('NAME is required. Usage: legion lex create NAME [--template TEMPLATE]')
    return
  end

  if options[:category] && options[:category] !~ /\A[a-z][a-z0-9_-]*\z/
    out.error('--category must be lowercase letters, numbers, underscores, or hyphens')
    return
  end

  template_name = options[:template] || 'basic'
  unless LexTemplates.valid?(template_name)
    out.warn("Unknown template '#{template_name}', falling back to 'basic'. Run `legion lex create --list-templates` to see available templates.")
    template_name = 'basic'
  end

  gem_name = options[:category] ? "lex-#{options[:category]}-#{name}" : "lex-#{name}"
  target_dir = gem_name

  if Dir.exist?(target_dir)
    out.error("Directory #{target_dir} already exists")
    raise SystemExit, 1
  end

  if Dir.pwd.include?('lex-')
    out.error('Already inside a LEX directory. Move to a parent directory first.')
    raise SystemExit, 1
  end

  Legion::Extensions.check_reserved_words(gem_name, known_org: false)

  out.success("Creating #{gem_name} (template: #{template_name})...")

  vars = { filename: target_dir, class_name: name.split('_').map(&:capitalize).join, lex: name }

  generator = LexGenerator.new(name, vars, options, gem_name: gem_name, template: template_name)
  generator.generate(out)

  out.spacer
  out.success("Extension #{gem_name} created in ./#{target_dir}")
  out.spacer
  puts '  Next steps:'
  puts "    cd #{target_dir}"
  puts '    bundle install' unless options[:bundle_install]
  puts '    # Add runners:  legion generate runner my_runner'
  puts '    # Add actors:   legion generate actor my_actor'
end

#disable(name) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/legion/cli/lex_command.rb', line 177

def disable(name)
  out = formatter
  Connection.ensure_settings(resolve_secrets: false)

  extensions = Legion::Settings[:extensions] || {}
  if extensions.key?(name.to_sym)
    extensions[name.to_sym][:enabled] = false
    out.success("Extension '#{name}' disabled")
  else
    out.warn("Extension '#{name}' not found in settings (may not be configured)")
  end
  out.warn('Restart Legion for changes to take effect') unless options[:json]
end

#enable(name) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/legion/cli/lex_command.rb', line 161

def enable(name)
  out = formatter
  Connection.ensure_settings(resolve_secrets: false)

  extensions = Legion::Settings[:extensions] || {}
  if extensions.key?(name.to_sym)
    extensions[name.to_sym][:enabled] = true
  else
    extensions[name.to_sym] = { enabled: true }
  end

  out.success("Extension '#{name}' enabled")
  out.warn('Restart Legion for changes to take effect') unless options[:json]
end

#fixesObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/legion/cli/lex_command.rb', line 234

def fixes
  out = formatter
  with_data do
    require 'legion/extensions/codegen/runners/auto_fix'
    result = Legion::Extensions::Codegen::Runners::AutoFix.list_fixes(status: options[:status])
    if options[:json]
      out.json(result)
    elsif result[:fixes].empty?
      out.warn('No fixes found')
    else
      rows = result[:fixes].map do |f|
        [f[:fix_id][0..7], f[:gem_name], f[:status], f[:specs_passed] ? 'PASS' : 'FAIL',
         f[:branch], f[:created_at]]
      end
      out.table(%w[ID Gem Status Specs Branch Created], rows)
    end
  end
end

#info(name) ⇒ Object



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
# File 'lib/legion/cli/lex_command.rb', line 44

def info(name)
  out = formatter
  lex = find_lex(name)

  unless lex
    out.error("Extension '#{name}' not found. Run `legion lex list` to see installed extensions.")
    raise SystemExit, 1
  end

  if options[:json]
    out.json(lex)
    return
  end

  out.header("lex-#{lex[:name]} v#{lex[:version]}")
  out.spacer
  out.detail({
               name:    lex[:name],
               version: lex[:version],
               status:  lex[:status],
               gem_dir: lex[:gem_dir],
               class:   lex[:extension_class]
             })

  if lex[:runners].is_a?(Array) && lex[:runners].any?
    out.spacer
    out.header('Runners')
    lex[:runners].each do |runner|
      puts "  #{out.colorize(runner, :cyan)}"
    end
  end

  if lex[:actors].is_a?(Array) && lex[:actors].any?
    out.spacer
    out.header('Actors')
    lex[:actors].each do |actor|
      puts "  #{out.colorize(actor[:name], :cyan)}  #{out.colorize(actor[:type], :gray)}"
    end
  end

  return unless lex[:dependencies].is_a?(Array) && lex[:dependencies].any?

  out.spacer
  out.header('Dependencies')
  lex[:dependencies].each do |dep|
    puts "  #{dep}"
  end
end

#invoke_ext(ext_name, command = nil, method_name = nil) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/legion/cli/lex_command.rb', line 193

def invoke_ext(ext_name, command = nil, method_name = nil)
  out = formatter
  manifest = LexCliManifest.new
  gem_name = manifest.resolve_alias(ext_name) || "lex-#{ext_name}"
  gem_manifest = manifest.read_manifest(gem_name)

  unless gem_manifest
    out.error("Unknown extension: #{ext_name}. Run `legion lex list` to see installed extensions.")
    return
  end

  unless command && gem_manifest.dig('commands', command)
    out.header("Available commands for #{ext_name}:")
    gem_manifest['commands'].each do |cmd, info|
      methods = info['methods'].map { |m, d| "#{m} - #{d['desc']}" }.join(', ')
      puts "  #{out.colorize(cmd, :cyan)}: #{methods}"
    end
    return
  end

  unless method_name && gem_manifest.dig('commands', command, 'methods', method_name)
    methods = gem_manifest.dig('commands', command, 'methods')
    out.header("Available methods for #{command}:")
    methods.each do |m, d|
      puts "  #{out.colorize(m, :cyan)} - #{d['desc']}"
    end
    return
  end

  require gem_name.tr('-', '/').tr('_', '/')
  klass = Object.const_get(gem_manifest.dig('commands', command, 'class'))
  instance = klass.new
  instance.public_send(method_name.to_sym)
rescue LoadError => e
  out.error("Failed to load #{gem_name}: #{e.message}")
rescue StandardError => e
  out.error("Error running #{ext_name} #{command} #{method_name}: #{e.message}")
end

#list(category = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/cli/lex_command.rb', line 28

def list(category = nil)
  out  = formatter
  lexs = discover_all

  rows = options[:all] ? lexs : lexs.reject { |l| l[:status] == 'disabled' }
  rows = rows.select { |l| l[:category] == category } if category

  if options[:flat] || category
    render_flat_table(out, rows)
  else
    render_grouped_table(out, rows)
  end
end

#reject_fix(fix_id) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/legion/cli/lex_command.rb', line 269

def reject_fix(fix_id)
  out = formatter
  with_data do
    require 'legion/extensions/codegen/runners/auto_fix'
    result = Legion::Extensions::Codegen::Runners::AutoFix.reject_fix(fix_id: fix_id)
    if result[:success]
      out.success("Fix #{fix_id} rejected.")
    else
      out.error("Failed to reject: #{result[:reason]}")
    end
  end
end