Class: Sxn::Core::TemplateManager
- Inherits:
-
Object
- Object
- Sxn::Core::TemplateManager
- Defined in:
- lib/sxn/core/template_manager.rb
Overview
Manages session templates - collections of projects that can be applied when creating a session to automatically create multiple worktrees.
Instance Attribute Summary collapse
-
#config_manager ⇒ Object
readonly
Returns the value of attribute config_manager.
-
#templates_config ⇒ Object
readonly
Returns the value of attribute templates_config.
Instance Method Summary collapse
-
#create_template(name, description: nil, projects: []) ⇒ Hash
Create a new template.
-
#get_template(name) ⇒ Hash
Get a specific template by name.
-
#get_template_projects(name, default_branch:) ⇒ Array<Hash>
Get project configurations for a template with branch resolution.
-
#initialize(config_manager) ⇒ TemplateManager
constructor
A new instance of TemplateManager.
-
#list_template_names ⇒ Array<String>
Get list of template names.
-
#list_templates ⇒ Array<Hash>
List all available templates.
-
#remove_template(name) ⇒ Boolean
Remove a template.
-
#template_exists?(name) ⇒ Boolean
Check if a template exists.
-
#update_template(name, description: nil, projects: nil) ⇒ Hash
Update an existing template.
-
#validate_template(name) ⇒ Object
Validate a template before use.
Constructor Details
#initialize(config_manager) ⇒ TemplateManager
Returns a new instance of TemplateManager.
12 13 14 15 |
# File 'lib/sxn/core/template_manager.rb', line 12 def initialize(config_manager) @config_manager = config_manager @templates_config = Config::TemplatesConfig.new(config_manager.sxn_folder_path) end |
Instance Attribute Details
#config_manager ⇒ Object (readonly)
Returns the value of attribute config_manager.
10 11 12 |
# File 'lib/sxn/core/template_manager.rb', line 10 def config_manager @config_manager end |
#templates_config ⇒ Object (readonly)
Returns the value of attribute templates_config.
10 11 12 |
# File 'lib/sxn/core/template_manager.rb', line 10 def templates_config @templates_config end |
Instance Method Details
#create_template(name, description: nil, projects: []) ⇒ Hash
Create a new template
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sxn/core/template_manager.rb', line 82 def create_template(name, description: nil, projects: []) validate_template_name!(name) raise SessionTemplateValidationError.new(name, "Template already exists") if template_exists?(name) # Validate all projects exist projects.each do |project_name| project = config_manager.get_project(project_name) raise SessionTemplateValidationError.new(name, "Project '#{project_name}' not found") unless project end template = { "description" => description, "projects" => projects.map { |p| { "name" => p } } } templates_config.set_template(name, template) get_template(name) end |
#get_template(name) ⇒ Hash
Get a specific template by name
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sxn/core/template_manager.rb', line 36 def get_template(name) template = templates_config.get_template(name) unless template available = list_template_names raise SessionTemplateNotFoundError.new(name, available: available) end template end |
#get_template_projects(name, default_branch:) ⇒ Array<Hash>
Get project configurations for a template with branch resolution
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/sxn/core/template_manager.rb', line 158 def get_template_projects(name, default_branch:) template = get_template(name) projects = template["projects"] || [] projects.map do |project_config| project_name = project_config["name"] project = config_manager.get_project(project_name) { name: project_name, path: project[:path], branch: project_config["branch"] || default_branch, rules: project_config["rules"] } end end |
#list_template_names ⇒ Array<String>
Get list of template names
49 50 51 |
# File 'lib/sxn/core/template_manager.rb', line 49 def list_template_names templates_config.list_template_names end |
#list_templates ⇒ Array<Hash>
List all available templates
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sxn/core/template_manager.rb', line 19 def list_templates config = templates_config.load templates = config["templates"] || {} templates.map do |name, template| { name: name, description: template["description"], project_count: (template["projects"] || []).size } end end |
#remove_template(name) ⇒ Boolean
Remove a template
140 141 142 143 144 145 |
# File 'lib/sxn/core/template_manager.rb', line 140 def remove_template(name) # Verify template exists get_template(name) templates_config.remove_template(name) end |
#template_exists?(name) ⇒ Boolean
Check if a template exists
150 151 152 |
# File 'lib/sxn/core/template_manager.rb', line 150 def template_exists?(name) templates_config.get_template(name) != nil end |
#update_template(name, description: nil, projects: nil) ⇒ Hash
Update an existing template
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 |
# File 'lib/sxn/core/template_manager.rb', line 108 def update_template(name, description: nil, projects: nil) template = get_template(name) # Update description if provided template["description"] = description if description # Update projects if provided if projects # Validate all projects exist projects.each do |project_name| project = config_manager.get_project(project_name) raise SessionTemplateValidationError.new(name, "Project '#{project_name}' not found") unless project end template["projects"] = projects.map { |p| { "name" => p } } end # Save back to config config = templates_config.load config["templates"] ||= {} config["templates"][name] = { "description" => template["description"], "projects" => template["projects"] } templates_config.save(config) get_template(name) end |
#validate_template(name) ⇒ Object
Validate a template before use
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sxn/core/template_manager.rb', line 57 def validate_template(name) template = get_template(name) projects = template["projects"] || [] errors = [] errors << "Template has no projects defined" if projects.empty? # Validate each project exists in config projects.each do |project_config| project_name = project_config["name"] project = config_manager.get_project(project_name) errors << "Project '#{project_name}' not found in configuration" unless project end raise SessionTemplateValidationError.new(name, errors.join("; ")) if errors.any? true end |