Class: Sublayer::Commands::Generator

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sublayer/cli/commands/generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



14
15
16
# File 'lib/sublayer/cli/commands/generator.rb', line 14

def self.banner
  "sublayer generate:generator"
end

.source_rootObject



18
19
20
# File 'lib/sublayer/cli/commands/generator.rb', line 18

def self.source_root
  File.expand_path("../../templates", __FILE__)
end

Instance Method Details

#ask_for_generator_detailsObject



36
37
38
39
40
41
42
43
44
# File 'lib/sublayer/cli/commands/generator.rb', line 36

def ask_for_generator_details
  @description = options[:description] || ask("Enter a description for the Sublayer Generator you'd like to create:")
  @ai_provider = options[:provider] || ask("Select an AI provider:", default: "OpenAI", limited_to: @available_providers)
  @ai_model = options[:model] || select_ai_model

  if is_cli_project? && options[:generate_command].nil?
    @generate_command = yes?("Would you like to create a corresponding CLI command for this generator?")
  end
end

#confirm_usage_of_ai_apiObject



22
23
24
25
26
# File 'lib/sublayer/cli/commands/generator.rb', line 22

def confirm_usage_of_ai_api
  puts "You are about to generate a new generator that uses an AI API to generate content."
  puts "Please ensure you have the necessary API keys and that you are aware of the costs associated with using the API."
  exit unless yes?("Do you want to continue?")
end

#determine_available_providersObject



28
29
30
31
32
33
34
# File 'lib/sublayer/cli/commands/generator.rb', line 28

def determine_available_providers
  @available_providers = []

  @available_providers << "OpenAI" if ENV["OPENAI_API_KEY"]
  @available_providers << "Claude" if ENV["ANTHROPIC_API_KEY"]
  @available_providers << "Gemini" if ENV["GEMINI_API_KEY"]
end

#determine_destination_folderObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/sublayer/cli/commands/generator.rb', line 54

def determine_destination_folder
  # Find either a ./generators folder or a generators folder nested one level below ./lib
  @destination_folder = if File.directory?("./generators")
                          "./generators"
                        elsif Dir.glob("./lib/**/generators").any?
                          Dir.glob("./lib/**/generators").first
                        else
                          "./"
                        end
end

#generate_command_if_requestedObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sublayer/cli/commands/generator.rb', line 69

def generate_command_if_requested
  return unless @generate_command

  say "Generating command..."

  generator_code = File.read(File.join(@destination_folder, @results.filename))
  command_results = SublayerCommandGenerator.new(generator_code: generator_code).generate
  @command_class_name = command_results.class_name
  @command_description = command_results.description
  @command_execute_body = command_results.execute_body
  @command_filename = command_results.filename

  commands_folder = File.join("lib", @project_name, "commands")

  destination_path = File.join(commands_folder, @command_filename)
  template("utilities/cli/command.rb.tt", destination_path)
end

#generate_generatorObject



46
47
48
49
50
51
52
# File 'lib/sublayer/cli/commands/generator.rb', line 46

def generate_generator
  Sublayer.configuration.ai_provider = Object.const_get("Sublayer::Providers::#{@ai_provider}")
  Sublayer.configuration.ai_model = @ai_model

  say "Generating Sublayer Generator..."
  @results = SublayerGeneratorGenerator.new(description: @description).generate
end

#save_generator_to_destination_folderObject



65
66
67
# File 'lib/sublayer/cli/commands/generator.rb', line 65

def save_generator_to_destination_folder
  create_file File.join(@destination_folder, @results.filename), @results.code
end