Class: Sxn::Rules::TemplateRule

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/sxn/rules/template_rule.rb

Overview

TemplateRule processes and applies template files using the secure template processor. It supports variable substitution, multiple template engines, and safe file generation.

Configuration format: {

"templates" => [
  {
    "source" => ".sxn/templates/session-info.md.liquid",
    "destination" => "README.md",
    "variables" => { "custom_var" => "value" },
    "engine" => "liquid",
    "required" => true,
    "overwrite" => false
  }
]

}

Examples:

Basic usage

rule = TemplateRule.new(
  "generate_docs",
  {
    "templates" => [
      {
        "source" => ".sxn/templates/CLAUDE.md.liquid",
        "destination" => "CLAUDE.md"
      }
    ]
  },
  "/path/to/project",
  "/path/to/session"
)
rule.validate
rule.apply

Constant Summary collapse

SUPPORTED_ENGINES =

Supported template engines

%w[liquid].freeze

Constants included from BaseRule::States

BaseRule::States::APPLIED, BaseRule::States::APPLYING, BaseRule::States::FAILED, BaseRule::States::PENDING, BaseRule::States::ROLLED_BACK, BaseRule::States::ROLLING_BACK, BaseRule::States::VALIDATED, BaseRule::States::VALIDATING

Instance Attribute Summary

Attributes inherited from BaseRule

#changes, #config, #dependencies, #errors, #name, #project_path, #session_path, #state

Instance Method Summary collapse

Methods inherited from BaseRule

#applied?, #can_execute?, #description, #duration, #failed?, #required?, #rollback, #rollbackable?, #to_h, #type, #validate, #validate_config_hash

Constructor Details

#initialize(arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, dependencies: []) ⇒ TemplateRule

Initialize the template rule



48
49
50
51
52
# File 'lib/sxn/rules/template_rule.rb', line 48

def initialize(arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, dependencies: [])
  super
  @template_processor = Templates::TemplateProcessor.new
  @template_variables = Templates::TemplateVariables.new
end

Instance Method Details

#applyObject

Apply the template processing operations



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sxn/rules/template_rule.rb', line 57

def apply
  change_state!(APPLYING)

  begin
    @config["templates"].each_with_index do |template_config, index|
      apply_template(template_config, index)
    end

    change_state!(APPLIED)
    log(:info, "Successfully processed #{@config["templates"].size} templates")
    true
  rescue StandardError => e
    @errors << e
    change_state!(FAILED)
    raise ApplicationError, "Failed to process templates: #{e.message}"
  end
end