Module: Sxn::Rules

Defined in:
lib/sxn/rules.rb,
lib/sxn/rules/errors.rb,
lib/sxn/rules/base_rule.rb,
lib/sxn/rules/rules_engine.rb,
lib/sxn/rules/template_rule.rb,
lib/sxn/rules/copy_files_rule.rb,
lib/sxn/rules/project_detector.rb,
lib/sxn/rules/setup_commands_rule.rb

Overview

The Rules module provides a comprehensive system for automating project setup through configurable rules. It includes secure file copying, command execution, template processing, and intelligent project detection.

Examples:

Basic usage

engine = Rules::RulesEngine.new("/path/to/project", "/path/to/session")
detector = Rules::ProjectDetector.new("/path/to/project")

# Detect project characteristics and suggest rules
suggested_rules = detector.suggest_default_rules

# Apply rules to set up the session
result = engine.apply_rules(suggested_rules)

if result.success?
  puts "Applied #{result.applied_rules.size} rules successfully"
else
  puts "Failed to apply rules: #{result.errors}"
  engine.rollback_rules
end

Defined Under Namespace

Classes: ApplicationError, BaseRule, CommandExecutionError, CopyFilesRule, DependencyError, PathValidationError, ProjectDetector, RollbackError, RulesEngine, RulesError, SetupCommandsRule, TemplateRule, ValidationError

Class Method Summary collapse

Class Method Details

.available_typesArray<String>

Get all available rule types

Returns:

  • (Array<String>)

    Available rule type names



37
38
39
# File 'lib/sxn/rules.rb', line 37

def self.available_types
  RulesEngine::RULE_TYPES.keys
end

.create_rule(name, type, config, project_path, session_path, dependencies: []) ⇒ BaseRule

Create a rule instance from configuration

Parameters:

  • name (String)

    Rule name

  • type (String)

    Rule type

  • config (Hash)

    Rule configuration

  • project_path (String)

    Project root path

  • session_path (String)

    Session directory path

  • dependencies (Array<String>) (defaults to: [])

    Rule dependencies

Returns:

Raises:

  • (ArgumentError)

    if rule type is invalid



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sxn/rules.rb', line 51

def self.create_rule(name, type, config, project_path, session_path, dependencies: [])
  # Convert symbol to string for consistent lookup
  type_key = type.to_s
  rule_class = RulesEngine::RULE_TYPES[type_key]
  raise ArgumentError, "Invalid rule type: #{type}" unless rule_class

  # Create rule instance - rule classes all follow BaseRule constructor pattern
  case type_key
  when "copy_files"
    CopyFilesRule.new(name, config, project_path, session_path, dependencies: dependencies)
  when "setup_commands"
    SetupCommandsRule.new(name, config, project_path, session_path, dependencies: dependencies)
  when "template"
    TemplateRule.new(name, config, project_path, session_path, dependencies: dependencies)
  else
    raise ArgumentError, "Invalid rule type: #{type}"
  end
end

.rule_type_infoHash

Get rule type information

Returns:

  • (Hash)

    Rule type information including descriptions and supported options



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

def self.rule_type_info
  {
    "copy_files" => {
      description: "Securely copy or symlink files with permission control and optional encryption",
      config_schema: {
        "files" => {
          type: "array",
          required: true,
          description: "List of files to copy",
          items: {
            "source" => { type: "string", required: true, description: "Source file path" },
            "destination" => { type: "string", required: false,
                               description: "Destination path (defaults to source)" },
            "strategy" => { type: "string", required: false, enum: %w[copy symlink], default: "copy" },
            "permissions" => { type: "string", required: false, description: "File permissions (e.g., '0600')" },
            "encrypt" => { type: "boolean", required: false, default: false },
            "required" => { type: "boolean", required: false, default: true }
          }
        }
      }
    },
    "setup_commands" => {
      description: "Execute project setup commands securely with environment control",
      config_schema: {
        "commands" => {
          type: "array",
          required: true,
          description: "List of commands to execute",
          items: {
            "command" => { type: "array", required: true, description: "Command and arguments" },
            "env" => { type: "object", required: false, description: "Environment variables" },
            "timeout" => { type: "integer", required: false, default: 60, maximum: 1800 },
            "condition" => { type: "string", required: false, description: "Execution condition" },
            "working_directory" => { type: "string", required: false, description: "Working directory" },
            "description" => { type: "string", required: false, description: "Command description" },
            "required" => { type: "boolean", required: false, default: true }
          }
        },
        "continue_on_failure" => { type: "boolean", required: false, default: false }
      }
    },
    "template" => {
      description: "Process and apply template files with variable substitution",
      config_schema: {
        "templates" => {
          type: "array",
          required: true,
          description: "List of templates to process",
          items: {
            "source" => { type: "string", required: true, description: "Template file path" },
            "destination" => { type: "string", required: true, description: "Output file path" },
            "variables" => { type: "object", required: false, description: "Additional template variables" },
            "engine" => { type: "string", required: false, enum: ["liquid"], default: "liquid" },
            "required" => { type: "boolean", required: false, default: true },
            "overwrite" => { type: "boolean", required: false, default: false }
          }
        }
      }
    }
  }
end

.validate_configuration(rules_config, project_path, session_path) ⇒ Boolean

Validate a rules configuration hash

Parameters:

  • rules_config (Hash)

    Rules configuration

  • project_path (String)

    Project root path

  • session_path (String)

    Session directory path

Returns:

  • (Boolean)

    true if valid

Raises:



77
78
79
80
81
# File 'lib/sxn/rules.rb', line 77

def self.validate_configuration(rules_config, project_path, session_path)
  engine = RulesEngine.new(project_path, session_path)
  engine.validate_rules_config(rules_config)
  true
end