Class: AIRefactor::Cli
- Inherits:
-
Object
- Object
- AIRefactor::Cli
- Defined in:
- lib/ai_refactor/cli.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .load_options_from_config_file ⇒ Object
- .request_file_inputs(prompt, multiple: true) ⇒ Object
- .request_input_with_autocomplete(prompt, completion_list) ⇒ Object
- .request_switch(prompt) ⇒ Object
- .request_text_input(prompt) ⇒ Object
Instance Method Summary collapse
- #ai_client ⇒ Object
-
#initialize(configuration, logger:) ⇒ Cli
constructor
A new instance of Cli.
- #inputs ⇒ Object
- #refactoring_type ⇒ Object
- #run ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(configuration, logger:) ⇒ Cli
Returns a new instance of Cli.
51 52 53 54 |
# File 'lib/ai_refactor/cli.rb', line 51 def initialize(configuration, logger:) @configuration = configuration @logger = logger end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
56 57 58 |
# File 'lib/ai_refactor/cli.rb', line 56 def configuration @configuration end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
56 57 58 |
# File 'lib/ai_refactor/cli.rb', line 56 def logger @logger end |
Class Method Details
.load_options_from_config_file ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ai_refactor/cli.rb', line 8 def # Load config from ~/.ai_refactor or .ai_refactor home_config_file_path = File.("~/.ai_refactor") local_config_file_path = File.join(Dir.pwd, ".ai_refactor") config_file_path = if File.exist?(local_config_file_path) local_config_file_path elsif File.exist?(home_config_file_path) home_config_file_path end return unless config_file_path config_string = File.read(config_file_path) config_lines = config_string.split(/\n+/).reject { |s| s =~ /\A\s*#/ }.map(&:strip) config_lines.flat_map(&:shellsplit) end |
.request_file_inputs(prompt, multiple: true) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/ai_refactor/cli.rb', line 38 def request_file_inputs(prompt, multiple: true) Readline.completion_append_character = multiple ? " " : nil Readline.completion_proc = Readline::FILENAME_COMPLETION_PROC paths = Readline.readline(prompt, true) multiple ? paths.gsub(/[^\\] /, ",") : paths end |
.request_input_with_autocomplete(prompt, completion_list) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/ai_refactor/cli.rb', line 30 def request_input_with_autocomplete(prompt, completion_list) Readline.completion_append_character = nil Readline.completion_proc = proc do |str| completion_list.grep(/^#{Regexp.escape(str)}/) end Readline.readline(prompt, true) end |
.request_switch(prompt) ⇒ Object
46 47 48 |
# File 'lib/ai_refactor/cli.rb', line 46 def request_switch(prompt) (Readline.readline(prompt, true) =~ /^y/i) ? true : false end |
.request_text_input(prompt) ⇒ Object
25 26 27 28 |
# File 'lib/ai_refactor/cli.rb', line 25 def request_text_input(prompt) puts prompt gets.chomp end |
Instance Method Details
#ai_client ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ai_refactor/cli.rb', line 66 def ai_client @ai_client ||= AIRefactor::AIClient.new( platform: configuration.ai_platform, model: configuration.ai_model, temperature: configuration.ai_temperature, max_tokens: configuration.ai_max_tokens, timeout: configuration.ai_timeout, verbose: configuration.verbose ) end |
#inputs ⇒ Object
62 63 64 |
# File 'lib/ai_refactor/cli.rb', line 62 def inputs configuration.input_file_paths end |
#refactoring_type ⇒ Object
58 59 60 |
# File 'lib/ai_refactor/cli.rb', line 58 def refactoring_type configuration.refactor || raise(StandardError, "No refactor provided") end |
#run ⇒ Object
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 133 134 135 136 137 138 |
# File 'lib/ai_refactor/cli.rb', line 83 def run return false unless valid? if refactorer.takes_input_files? = inputs.map do |path| File.exist?(path) ? path : Dir.glob(path) end.flatten logger.info "AI Refactor #{.size} files(s)/dir(s) '#{}' with #{refactorer.refactor_name} refactor\n" logger.info "====================\n" if configuration.description logger.info "Description: #{configuration.description}\n" end return_values = .map do |file| logger.info "Processing #{file}..." refactor = refactorer.new(ai_client, file, configuration, logger) refactor_returned = refactor.run failed = refactor_returned == false if failed logger.warn "Refactor failed on #{file}\nFailed due to: #{refactor.}\n" else logger.success "Refactor succeeded on #{file}\n" if refactor_returned.is_a?(String) logger.info "Refactor #{file} output:\n\n#{refactor_returned}\n\n" end end failed ? [file, refactor.] : true end if return_values.all?(true) logger.success "All files processed successfully!" else files = return_values.select { |v| v != true } logger.warn "Some files failed to process:\n#{files.map { |f| "#{f[0]} :\n > #{f[1]}" }.join("\n")}" end logger.info "Done processing all files!" else name = refactorer.refactor_name logger.info "AI Refactor - #{name} refactor\n" logger.info "====================\n" refactor = refactorer.new(ai_client, nil, configuration, logger) refactor_returned = refactor.run failed = refactor_returned == false if failed logger.warn "Refactor failed with #{name}\nFailed due to: #{refactor.}\n" else logger.success "Refactor succeeded with #{name}\n" if refactor_returned.is_a?(String) logger.info "Refactor output:\n\n#{refactor_returned}\n\n" end end end end |
#valid? ⇒ Boolean
77 78 79 80 81 |
# File 'lib/ai_refactor/cli.rb', line 77 def valid? return false unless refactorer inputs_valid = refactorer.takes_input_files? ? !(inputs.nil? || inputs.empty?) : true AIRefactor::Refactors.supported?(refactoring_type) && inputs_valid end |