Class: Ace::PromptPrep::Molecules::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/prompt_prep/molecules/template_manager.rb

Overview

Manages template file operations (copy, archive, restore)

Class Method Summary collapse

Class Method Details

.archive_file(source_path:, archive_dir:) ⇒ Hash

Archive existing file with timestamp

Parameters:

  • source_path (String)

    File to archive

  • archive_dir (String)

    Archive directory path

Returns:

  • (Hash)

    Result with :success, :archive_path, :skipped, :error keys



66
67
68
69
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
96
97
98
99
100
101
102
103
# File 'lib/ace/prompt_prep/molecules/template_manager.rb', line 66

def archive_file(source_path:, archive_dir:)
  # Check if source exists
  unless File.exist?(source_path)
    return {
      success: true,
      archive_path: nil,
      skipped: true,
      error: nil
    }
  end

  # Create archive directory
  FileUtils.mkdir_p(archive_dir) unless Dir.exist?(archive_dir)

  # Generate archive filename with timestamp
  timestamp = Time.now.strftime("%Y%m%d-%H%M%S")
  basename = File.basename(source_path, ".*")
  ext = File.extname(source_path)
  archive_filename = "#{basename}-#{timestamp}#{ext}"
  archive_path = File.join(archive_dir, archive_filename)

  # Copy to archive
  FileUtils.cp(source_path, archive_path)

  {
    success: true,
    archive_path: archive_path,
    skipped: false,
    error: nil
  }
rescue => e
  {
    success: false,
    archive_path: nil,
    skipped: false,
    error: "Failed to archive file: #{e.message}"
  }
end

.copy_template(template_path:, target_path:, force: false) ⇒ Hash

Copy template content to target path

Parameters:

  • template_path (String)

    Source template file path

  • target_path (String)

    Destination file path

  • force (Boolean) (defaults to: false)

    Overwrite if target exists

Returns:

  • (Hash)

    Result with :success, :path, :skipped, :error keys



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ace/prompt_prep/molecules/template_manager.rb', line 17

def copy_template(template_path:, target_path:, force: false)
  # Check if template exists
  unless File.exist?(template_path)
    return {
      success: false,
      path: nil,
      skipped: false,
      error: "Template file not found: #{template_path}"
    }
  end

  # Check if target already exists
  if File.exist?(target_path) && !force
    return {
      success: true,
      path: target_path,
      skipped: true,
      error: nil
    }
  end

  # Create target directory if needed
  target_dir = File.dirname(target_path)
  FileUtils.mkdir_p(target_dir) unless Dir.exist?(target_dir)

  # Copy template content
  template_content = File.read(template_path, encoding: "utf-8")
  File.write(target_path, template_content, encoding: "utf-8")

  {
    success: true,
    path: target_path,
    skipped: false,
    error: nil
  }
rescue => e
  {
    success: false,
    path: nil,
    skipped: false,
    error: "Failed to copy template: #{e.message}"
  }
end

.restore_template(template_path:, target_path:, archive_dir:, force: false) ⇒ Hash

Restore template to target path (archive current if exists)

Parameters:

  • template_path (String)

    Source template file path

  • target_path (String)

    Destination file path

  • archive_dir (String)

    Archive directory path

  • force (Boolean) (defaults to: false)

    Skip archiving if true

Returns:

  • (Hash)

    Result with :success, :path, :archive_path, :error keys



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
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ace/prompt_prep/molecules/template_manager.rb', line 112

def restore_template(template_path:, target_path:, archive_dir:, force: false)
  # Archive current file unless force
  archive_result = if force || !File.exist?(target_path)
    {success: true, archive_path: nil, skipped: true}
  else
    archive_file(source_path: target_path, archive_dir: archive_dir)
  end

  unless archive_result[:success]
    return {
      success: false,
      path: nil,
      archive_path: nil,
      error: archive_result[:error]
    }
  end

  # Copy template (force overwrite since we archived)
  copy_result = copy_template(
    template_path: template_path,
    target_path: target_path,
    force: true
  )

  unless copy_result[:success]
    return {
      success: false,
      path: nil,
      archive_path: archive_result[:archive_path],
      error: copy_result[:error]
    }
  end

  {
    success: true,
    path: copy_result[:path],
    archive_path: archive_result[:archive_path],
    error: nil
  }
rescue => e
  {
    success: false,
    path: nil,
    archive_path: nil,
    error: "Failed to restore template: #{e.message}"
  }
end