Class: Sxn::Core::TemplateManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_managerObject (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_configObject (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

Parameters:

  • Template name

  • (defaults to: nil)

    Template description

  • (defaults to: [])

    Array of project names

Returns:

  • Created template

Raises:



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

Parameters:

  • Template name

Returns:

  • Template configuration

Raises:

  • If template not found



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

Parameters:

  • Template name

  • Default branch to use if not specified per-project

Returns:

  • Array of project configs with resolved branches



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_namesArray<String>

Get list of template names

Returns:

  • 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_templatesArray<Hash>

List all available templates

Returns:

  • Array of template info hashes



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

Parameters:

  • Template name

Returns:

  • True if removed



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

Parameters:

  • Template name

Returns:



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

Parameters:

  • Template name

  • (defaults to: nil)

    New description

  • (defaults to: nil)

    New project list

Returns:

  • Updated 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

Parameters:

  • Template name

Raises:

  • If template is invalid

  • If template not found



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