Class: Docforge::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/docforge/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/docforge/cli.rb', line 17

def self.exit_on_failure?
  true
end

Instance Method Details

#configObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/docforge/cli.rb', line 27

def config
  cfg = build_config
  data = cfg.to_h
  puts "docforge configuration"
  puts "" * 50
  puts "  API key set:    #{data[:api_key_set] ? "yes" : "NO — set ANTHROPIC_API_KEY"}"
  puts "  Model:          #{data[:model]}"
  puts "  Output dir:     #{data[:output_dir]}"
  puts "  Author:         #{data[:author]}"
  puts "  Input files:    prd=#{data[:input_filenames]["prd"]}  spec=#{data[:input_filenames]["spec"]}  notes=#{data[:input_filenames]["notes"]}"
  puts "  System prompt:  #{data[:system_prompt_path]}"
  puts "  Interview qs:   #{data[:interview_question_count]}"
  puts "  Loaded configs: #{data[:loaded_config_files].empty? ? "(none — using built-in defaults)" : data[:loaded_config_files].join(", ")}"
end

#generate(folder) ⇒ Object



61
62
63
64
65
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
104
105
106
107
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
# File 'lib/docforge/cli.rb', line 61

def generate(folder)
  cfg = build_config
  cfg.instance_variable_set(:@model, options[:model]) if options[:model]
  cfg.validate! unless options[:dry_run] || cache_hit?(folder)

  puts "→ Reading inputs from #{File.expand_path(folder)}"
  if cfg.loaded_config_files.any?
    puts "  · Config: #{cfg.loaded_config_files.join(", ")}"
  end
  inputs = Inputs.read(folder, config: cfg)
  f = cfg.input_filenames
  puts "#{f["prd"]}  (#{inputs.prd.length} chars)"
  puts "#{f["spec"]} (#{inputs.spec.length} chars)"
  puts "  #{inputs.notes ? "" : "·"} #{f["notes"]}  #{inputs.notes ? "(#{inputs.notes.length} chars)" : "(none)"}"
  puts "  #{inputs.assets.any? ? "" : "·"} _assets/  (#{inputs.assets.size} files)"

  cache_path = File.join(inputs.folder, ".brief-cache.json")
  cached     = !options[:fresh] && File.exist?(cache_path)

  if cached
    puts "\n→ Using cached brief (#{cache_path})"
    puts "  · pass --fresh to re-call the API"
    cache_data = JSON.parse(File.read(cache_path, encoding: "UTF-8"))
    payload    = cache_data["payload"]
    puts "  ✓ Cached on #{cache_data["generated_at"]} via #{cache_data["model"]}"
  else
    interview_answers = options[:no_interview] ? {} : run_interview(cfg.interview_questions)
    user_message = Prompt.user_message(inputs: inputs, interview_answers: interview_answers)

    if options[:dry_run]
      puts "\n→ DRY RUN — would call Anthropic with:"
      puts "  Model:                 #{cfg.model}"
      puts "  System prompt length:  #{Prompt.system_prompt(config: cfg).length} chars"
      puts "  User message length:   #{user_message.length} chars"
      puts "  Output would land at:  #{File.join(cfg.output_dir, inputs.slug)}.docx"
      puts "\nSet ANTHROPIC_API_KEY and re-run without --dry-run to actually generate."
      return
    end

    spinner = TTY::Spinner.new("[:spinner] Calling Anthropic (#{cfg.model})...", format: :dots)
    spinner.auto_spin
    begin
      client  = Client.new(cfg)
      payload = client.generate_brief(
        system_prompt: Prompt.system_prompt(config: cfg),
        user_message: user_message
      )
      spinner.success("(done)")
    rescue StandardError => e
      spinner.error("(failed)")
      raise e
    end

    File.write(cache_path, JSON.pretty_generate(
      "generated_at" => Time.now.utc.iso8601,
      "model"        => cfg.model,
      "payload"      => payload
    ))
    puts "  ✓ Cached response → #{cache_path}"
  end

  brief = Brief.new(payload)
  puts "  ✓ Brief — #{brief.title.inspect}"

  puts "\n→ Rendering .docx..."
  path = Renderers::Docx.new(brief: brief, config: cfg, slug: inputs.slug).render
  puts "  ✓ Wrote #{path}"
  puts "\nDone. Open it: open \"#{path}\""
rescue Error => e
  warn "ERROR: #{e.message}"
  exit 1
end

#initObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/docforge/cli.rb', line 44

def init
  target = File.join(Dir.pwd, ".docforge.yml")
  if File.exist?(target) && !options[:force]
    warn "#{target} already exists — pass --force to overwrite."
    exit 1
  end

  File.write(target, starter_yaml)
  puts "✓ Wrote #{target}"
  puts "  Edit it to point at your project's PRD/SPEC filenames and (optionally) a custom prompt."
end

#versionObject



22
23
24
# File 'lib/docforge/cli.rb', line 22

def version
  puts Docforge::VERSION
end