Class: Sxn::Core::RulesManager

Inherits:
Object
  • Object
show all
Defined in:
lib/sxn/core/rules_manager.rb

Overview

Manages project rules and their application

Instance Method Summary collapse

Constructor Details

#initialize(config_manager = nil, project_manager = nil) ⇒ RulesManager

Returns a new instance of RulesManager.



7
8
9
10
11
# File 'lib/sxn/core/rules_manager.rb', line 7

def initialize(config_manager = nil, project_manager = nil)
  @config_manager = config_manager || ConfigManager.new
  @project_manager = project_manager || ProjectManager.new(@config_manager)
  @rules_engine = Sxn::Rules::RulesEngine.new("/tmp", "/tmp")
end

Instance Method Details

#add_rule(project_name, rule_type, rule_config) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sxn/core/rules_manager.rb', line 13

def add_rule(project_name, rule_type, rule_config)
  project = @project_manager.get_project(project_name)
  raise Sxn::ProjectNotFoundError, "Project '#{project_name}' not found" unless project

  validate_rule_type!(rule_type)
  validate_rule_config!(rule_type, rule_config)

  # Get current config
  config = @config_manager.get_config

  # Initialize project rules if not exists
  config.projects[project_name] ||= {}
  config.projects[project_name]["rules"] ||= {}
  config.projects[project_name]["rules"][rule_type] ||= []

  # Add new rule
  config.projects[project_name]["rules"][rule_type] << rule_config

  # Save updated config
  save_project_config(project_name, config.projects[project_name])

  {
    project: project_name,
    type: rule_type,
    config: rule_config
  }
end

#apply_copy_file_rule(project_path, worktree_path, rule_config) ⇒ Object



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
# File 'lib/sxn/core/rules_manager.rb', line 126

def apply_copy_file_rule(project_path, worktree_path, rule_config)
  source_pattern = rule_config["source"]
  strategy = rule_config["strategy"] || "copy"

  # Handle glob patterns
  source_files = if source_pattern.include?("*")
                   Dir.glob(File.join(project_path, source_pattern))
                 else
                   single_file = File.join(project_path, source_pattern)
                   File.exist?(single_file) ? [single_file] : []
                 end

  source_files.each do |file_path|
    # Calculate relative path from project root
    relative_path = file_path.sub("#{project_path}/", "")
    dest_file = File.join(worktree_path, relative_path)

    # Create destination directory if needed
    FileUtils.mkdir_p(File.dirname(dest_file))

    case strategy
    when "copy"
      FileUtils.cp(file_path, dest_file)
    when "symlink"
      FileUtils.ln_sf(file_path, dest_file)
    end
  end
end

#apply_rules(project_name, session_name = nil) ⇒ Object



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
# File 'lib/sxn/core/rules_manager.rb', line 70

def apply_rules(project_name, session_name = nil)
  project = @project_manager.get_project(project_name)
  raise Sxn::ProjectNotFoundError, "Project '#{project_name}' not found" unless project

  # Get current session if not specified
  session_name ||= @config_manager.current_session
  raise Sxn::NoActiveSessionError, "No active session specified" unless session_name

  session_manager = SessionManager.new(@config_manager)
  session = session_manager.get_session(session_name)
  raise Sxn::SessionNotFoundError, "Session '#{session_name}' not found" unless session

  # Get worktree for this project in the session
  worktree_manager = WorktreeManager.new(@config_manager, session_manager)
  worktree = worktree_manager.get_worktree(project_name, session_name: session_name)
  unless worktree
    raise Sxn::WorktreeNotFoundError,
          "No worktree found for project '#{project_name}' in session '#{session_name}'"
  end

  # Get project rules (format: { "copy_files" => [...], "setup_commands" => [...] })
  rules = @project_manager.get_project_rules(project_name)

  # Transform rules to RulesEngine format and apply
  apply_rules_to_worktree(project, worktree, rules)
end

#apply_rules_to_worktree(project, worktree, rules) ⇒ Object



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
# File 'lib/sxn/core/rules_manager.rb', line 97

def apply_rules_to_worktree(project, worktree, rules)
  project_path = project[:path]
  worktree_path = worktree[:path]

  # Ensure paths exist
  raise Sxn::InvalidProjectPathError, "Project path does not exist: #{project_path}" unless File.directory?(project_path)
  raise Sxn::WorktreeNotFoundError, "Worktree path does not exist: #{worktree_path}" unless File.directory?(worktree_path)

  applied_count = 0
  errors = []

  # Apply copy_files rules
  rules["copy_files"]&.each do |rule_config|
    apply_copy_file_rule(project_path, worktree_path, rule_config)
    applied_count += 1
  rescue StandardError => e
    errors << "copy_files: #{e.message}"
  end

  # Apply setup_commands rules (skip for now as they can be slow)
  # Users can run these manually if needed

  {
    success: errors.empty?,
    applied_count: applied_count,
    errors: errors
  }
end

#generate_rule_template(rule_type, project_type = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/sxn/core/rules_manager.rb', line 186

def generate_rule_template(rule_type, project_type = nil)
  case rule_type
  when "copy_files"
    generate_copy_files_template(project_type)
  when "setup_commands"
    generate_setup_commands_template(project_type)
  when "template"
    generate_template_rule_template(project_type)
  else
    raise Sxn::InvalidRuleTypeError, "Unknown rule type: #{rule_type}"
  end
end

#get_available_rule_typesObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sxn/core/rules_manager.rb', line 199

def get_available_rule_types
  [
    {
      name: "copy_files",
      description: "Copy files from source project to worktree",
      example: { "source" => "config/master.key", "strategy" => "copy" }
    },
    {
      name: "setup_commands",
      description: "Run setup commands in the worktree",
      example: { "command" => %w[bundle install] }
    },
    {
      name: "template",
      description: "Process template files with variable substitution",
      example: { "source" => ".sxn/templates/README.md", "destination" => "README.md" }
    }
  ]
end

#list_rules(project_name = nil) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/sxn/core/rules_manager.rb', line 62

def list_rules(project_name = nil)
  if project_name
    list_project_rules(project_name)
  else
    list_all_rules
  end
end

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sxn/core/rules_manager.rb', line 41

def remove_rule(project_name, rule_type, rule_index = nil)
  project = @project_manager.get_project(project_name)
  raise Sxn::ProjectNotFoundError, "Project '#{project_name}' not found" unless project

  config = @config_manager.get_config
  project_rules = config.projects.dig(project_name, "rules", rule_type)

  raise Sxn::RuleNotFoundError, "No #{rule_type} rules found for project '#{project_name}'" unless project_rules

  if rule_index
    raise Sxn::RuleNotFoundError, "Rule index #{rule_index} not found" if rule_index >= project_rules.size

    removed_rule = project_rules.delete_at(rule_index)
  else
    removed_rule = project_rules.clear
  end

  save_project_config(project_name, config.projects[project_name])
  removed_rule
end

#validate_rules(project_name) ⇒ Object



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
# File 'lib/sxn/core/rules_manager.rb', line 155

def validate_rules(project_name)
  project = @project_manager.get_project(project_name)
  raise Sxn::ProjectNotFoundError, "Project '#{project_name}' not found" unless project

  rules = @project_manager.get_project_rules(project_name)
  validation_results = []

  rules.each do |rule_type, rule_configs|
    Array(rule_configs).each_with_index do |rule_config, index|
      validate_rule_config!(rule_type, rule_config)
      validation_results << {
        type: rule_type,
        index: index,
        config: rule_config,
        valid: true,
        errors: []
      }
    rescue StandardError => e
      validation_results << {
        type: rule_type,
        index: index,
        config: rule_config,
        valid: false,
        errors: [e.message]
      }
    end
  end

  validation_results
end