Method: Compose::CLI#edit

Defined in:
lib/compose/cli.rb

#edit(*files) ⇒ Object



12
13
14
15
16
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
# File 'lib/compose/cli.rb', line 12

def edit(*files)
  puts 'Welcome to Compose!'.green

  if files.empty?
    puts 'Error: No files or folders to process'.red
    exit(1)
  end

  if options[:clear_cache]
    FileUtils.rm_rf(FileProcessor::CACHE_DIR)
    puts 'Cache cleared'.green
  end

  # Set up configuration
  Compose::Config.configure do |config|
    config.code_model = Model.find(options[:model])
    config.verifier_model = Model.preferred_verifier_model
  end

  ApiKeyUtils.setup(Compose::Config.code_model)
  ApiKeyUtils.setup(Compose::Config.verifier_model)

  file_processor = FileProcessor.new(files, include_imports: options[:include_imports])
  puts "Loaded #{file_processor.files.count} file(s): #{file_processor.files.keys.map { |path| File.exist?(path) ? Pathname.new(path).relative_path_from(Pathname.pwd).to_s : path }.join(', ')}"

  task = ask('What do you need me to do? (Type \'ask\' followed by your question to ask a question instead):')

  if task.downcase.start_with?('ask ')
    question = task[4..-1].strip
    answer = file_processor.ask_files(question)
    puts answer
  else
    edits = file_processor.edit_files(task)

    edit_verifier = EditVerifier.new(edits)
    verified_edits = edit_verifier.verify_edits

    edit_processor = EditProcessor.new(verified_edits)
    edit_processor.process_edits
  end

  puts 'Thank you for using Compose!'.green
end