Class: Sxn::Commands::Rules

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sxn/commands/rules.rb

Overview

Manage project setup rules

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, local_options = {}, config = {}) ⇒ Rules

Returns a new instance of Rules.



12
13
14
15
16
17
18
19
20
# File 'lib/sxn/commands/rules.rb', line 12

def initialize(args = ARGV, local_options = {}, config = {})
  super
  @ui = Sxn::UI::Output.new
  @prompt = Sxn::UI::Prompt.new
  @table = Sxn::UI::Table.new
  @config_manager = Sxn::Core::ConfigManager.new
  @project_manager = Sxn::Core::ProjectManager.new(@config_manager)
  @rules_manager = Sxn::Core::RulesManager.new(@config_manager, @project_manager)
end

Instance Method Details

#add(project_name = nil, rule_type = nil, rule_config = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
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
# File 'lib/sxn/commands/rules.rb', line 25

def add(project_name = nil, rule_type = nil, rule_config = nil)
  ensure_initialized!

  # Interactive mode
  if options[:interactive] || project_name.nil?
    project_name = select_project("Select project for rule:")
    return if project_name.nil?
  end

  rule_type = @prompt.rule_type if options[:interactive] || rule_type.nil?

  if options[:interactive] || rule_config.nil?
    rule_config = prompt_rule_config(rule_type)
  else
    # Parse JSON config from command line
    begin
      rule_config = JSON.parse(rule_config)
    rescue JSON::ParserError => e
      @ui.error("Invalid JSON config: #{e.message}")
      exit(1)
    end
  end

  begin
    @ui.progress_start("Adding #{rule_type} rule for #{project_name}")

    rule = @rules_manager.add_rule(project_name, rule_type, rule_config)

    @ui.progress_done
    @ui.success("Added #{rule_type} rule for #{project_name}")

    display_rule_info(rule)
  rescue Sxn::Error => e
    @ui.progress_failed
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#apply(project_name = nil) ⇒ Object



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
# File 'lib/sxn/commands/rules.rb', line 153

def apply(project_name = nil)
  ensure_initialized!

  session_name = options[:session] || @config_manager.current_session
  unless session_name
    @ui.error("No active session")
    @ui.recovery_suggestion("Use 'sxn use <session>' or specify --session")
    exit(1)
  end

  # Interactive selection if project not provided
  if project_name.nil?
    worktree_manager = Sxn::Core::WorktreeManager.new(@config_manager)
    worktrees = worktree_manager.list_worktrees(session_name: session_name)

    if worktrees.empty?
      @ui.empty_state("No worktrees in current session")
      @ui.recovery_suggestion("Add worktrees with 'sxn worktree add <project>'")
      exit(1)
    end

    choices = worktrees.map do |w|
      { name: "#{w[:project]} (#{w[:branch]})", value: w[:project] }
    end
    project_name = @prompt.select("Select project to apply rules:", choices)
  end

  begin
    if options[:dry_run]
      @ui.info("Dry run mode - showing rules that would be applied")
      show_rules_preview(project_name)
    else
      @ui.progress_start("Applying rules for #{project_name}")

      results = @rules_manager.apply_rules(project_name, session_name)

      @ui.progress_done

      if results[:success]
        @ui.success("Applied #{results[:applied_count]} rules successfully")
      else
        @ui.warning("Some rules failed to apply")
        results[:errors].each { |error| @ui.error("  #{error}") }
      end
    end
  rescue Sxn::Error => e
    @ui.progress_failed if options[:dry_run]
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#list(project_name = nil) ⇒ Object



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
# File 'lib/sxn/commands/rules.rb', line 118

def list(project_name = nil)
  ensure_initialized!

  begin
    rules = @rules_manager.list_rules(project_name)

    # Filter by type if specified
    rules = rules.select { |r| r[:type] == options[:type] } if options[:type]

    @ui.section("Project Rules")

    if rules.empty?
      if project_name
        @ui.empty_state("No rules configured for project #{project_name}")
      else
        @ui.empty_state("No rules configured")
      end
      suggest_add_rule
    elsif options[:validate]
      list_with_validation(rules, project_name)
    else
      @table.rules(rules, project_name)
      @ui.newline
      @ui.info("Total: #{rules.size} rules")
    end
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#remove(project_name = nil, rule_type = nil, rule_index = nil) ⇒ Object



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
# File 'lib/sxn/commands/rules.rb', line 67

def remove(project_name = nil, rule_type = nil, rule_index = nil)
  ensure_initialized!

  # Interactive selection
  if project_name.nil?
    project_name = select_project("Select project:")
    return if project_name.nil?
  end

  if rule_type.nil?
    rules = @rules_manager.list_rules(project_name)
    if rules.empty?
      @ui.empty_state("No rules configured for project #{project_name}")
      return
    end

    rule_types = rules.map { |r| r[:type] }.uniq
    rule_type = @prompt.select("Select rule type to remove:", rule_types)
  end

  # Convert index to integer
  rule_index = rule_index.to_i if rule_index && !options[:all]

  unless @prompt.confirm_deletion("#{rule_type} rule(s)", "rule")
    @ui.info("Cancelled")
    return
  end

  begin
    @ui.progress_start("Removing #{rule_type} rule(s)")

    if options[:all]
      @rules_manager.remove_rule(project_name, rule_type)
      @ui.progress_done
      @ui.success("Removed all #{rule_type} rules for #{project_name}")
    else
      @rules_manager.remove_rule(project_name, rule_type, rule_index)
      @ui.progress_done
      @ui.success("Removed #{rule_type} rule ##{rule_index} for #{project_name}")
    end
  rescue Sxn::Error => e
    @ui.progress_failed
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#template(rule_type = nil, project_type = nil) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/sxn/commands/rules.rb', line 255

def template(rule_type = nil, project_type = nil)
  ensure_initialized!

  if rule_type.nil?
    available_types = @rules_manager.get_available_rule_types
    choices = available_types.map do |type|
      { name: "#{type[:name]} - #{type[:description]}", value: type[:name] }
    end
    rule_type = @prompt.select("Select rule type:", choices)
  end

  begin
    template_data = @rules_manager.generate_rule_template(rule_type, project_type)

    @ui.section("Rule Template: #{rule_type}")

    puts JSON.pretty_generate(template_data)

    @ui.newline
    @ui.info("Copy this template and customize for your project")
    @ui.command_example(
      "sxn rules add <project> #{rule_type} '#{JSON.generate(template_data.first)}'",
      "Add this rule to a project"
    )
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#typesObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/sxn/commands/rules.rb', line 286

def types
  available_types = @rules_manager.get_available_rule_types

  @ui.section("Available Rule Types")

  available_types.each do |type|
    @ui.subsection(type[:name])
    @ui.info(type[:description])
    @ui.newline

    puts "Example:"
    puts JSON.pretty_generate(type[:example])
    @ui.newline
  end
end

#validate(project_name = nil) ⇒ Object



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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/sxn/commands/rules.rb', line 206

def validate(project_name = nil)
  ensure_initialized!

  if project_name.nil?
    project_name = select_project("Select project to validate:")
    return if project_name.nil?
  end

  begin
    results = @rules_manager.validate_rules(project_name)

    @ui.section("Rule Validation: #{project_name}")

    if results.nil?
      @ui.error("No validation results returned")
      return
    end

    valid_count = 0
    invalid_count = 0

    results.each do |result|
      if result[:valid]
        status = "✅"
        valid_count += 1
      else
        status = "❌"
        invalid_count += 1
      end
      @ui.list_item("#{status} #{result[:type]} ##{result[:index]}")

      # Show errors for invalid rules
      result[:errors].each { |error| @ui.list_item("  #{error}") } unless result[:valid]
    end

    @ui.newline
    @ui.info("Valid: #{valid_count}, Invalid: #{invalid_count}")

    @ui.success("All rules are valid") if invalid_count.zero?
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  rescue NoMethodError => e
    # Handle case where validate_rules is not fully implemented
    @ui.warning("Validation not yet fully implemented: #{e.message}")
  end
end