Class: Uniword::TemplateCLI

Inherits:
Thor
  • Object
show all
Includes:
CLIHelpers
Defined in:
lib/uniword/cli/template_cli.rb

Overview

Template subcommands for Uniword CLI.

Manages a library of .docx template files – create templates from existing documents, list the library, and apply templates with data to generate new documents.

Uses TemplateCLI (not Template) to avoid collision with the Uniword::Template module.

Instance Method Summary collapse

Methods included from CLIHelpers

included

Instance Method Details

#apply(template_name, output_path) ⇒ Object



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
# File 'lib/uniword/cli/template_cli.rb', line 129

def apply(template_name, output_path)
  data = load_template_data

  say "Applying template '#{template_name}'...", :green if options[:verbose]
  if options[:verbose] && data.any?
    say "  Data keys: #{data.keys.join(', ')}",
        :cyan
  end

  Uniword::TemplateManager.apply(
    template_name,
    data,
    output_path,
    template_dir: options[:dir],
  )

  say "Applied template '#{template_name}' -> #{output_path}", :green
rescue ArgumentError => e
  say "Error: #{e.message}", :red
  exit 1
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#create(name, source) ⇒ Object



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
104
105
106
107
# File 'lib/uniword/cli/template_cli.rb', line 75

def create(name, source)
  unless File.exist?(source)
    say "Source file not found: #{source}", :red
    exit 1
  end

  if options[:verbose]
    say "Creating template '#{name}' from #{source}...",
        :green
  end

   = Uniword::TemplateManager.create(
    name,
    source,
    options[:dir],
    description: options[:description],
  )

  say "Created template '#{name}' in #{options[:dir]}/", :green

  if options[:verbose]
    say "  Description: #{[:description] || '(none)'}", :cyan
    say "  Markers: #{[:markers]}", :cyan
    say "  Created: #{[:created_at]}", :cyan
  end
rescue ArgumentError => e
  say "Error: #{e.message}", :red
  exit 1
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#listObject



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
# File 'lib/uniword/cli/template_cli.rb', line 33

def list
  templates = Uniword::TemplateManager.list(options[:dir])

  if templates.empty?
    say "No templates found in #{options[:dir]}/", :yellow
    say "Use 'uniword template create' to add templates.", :yellow
    return
  end

  say "Available templates (#{templates.count}):", :green
  templates.each do |t|
    if options[:verbose]
      say "  #{t[:name]}:", :cyan
      say "    Path: #{t[:path]}"
      say "    Description: #{t[:description] || '(none)'}"
      say "    Source: #{t[:source] || '(unknown)'}"
      say "    Markers: #{t[:markers] || 0}"
      say "    Created: #{t[:created_at] || '(unknown)'}"
    else
      label = t[:description] ? " - #{t[:description]}" : ""
      say "  - #{t[:name]}#{label}"
    end
  end
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end