Class: Tng::Services::DirectGeneration
- Inherits:
-
Object
- Object
- Tng::Services::DirectGeneration
- Includes:
- ExtractMethods
- Defined in:
- lib/tng/services/direct_generation.rb
Instance Method Summary collapse
- #display_audit_results(result) ⇒ Object
- #extract_methods_for_type(file_object, type) ⇒ Object
- #find_similar_files(base_name) ⇒ Object
- #generate_test_for_file(file_path, method_name, type) ⇒ Object
- #generate_test_result(file_object, method_info, type, progress) ⇒ Object
-
#initialize(pastel, testng, http_client, params, show_post_generation_menu_proc, go_ui) ⇒ DirectGeneration
constructor
A new instance of DirectGeneration.
- #perform_clone_analysis(file_object, progress) ⇒ Object
- #perform_trace_analysis(file_object, method_info, progress) ⇒ Object
- #run ⇒ Object
- #run_clones_for_file(file_path, type) ⇒ Object
- #suggest_similar_files(file_path) ⇒ Object
Methods included from ExtractMethods
#extract_controller_methods, #extract_model_methods, #extract_other_methods, #extract_service_methods
Constructor Details
#initialize(pastel, testng, http_client, params, show_post_generation_menu_proc, go_ui) ⇒ DirectGeneration
Returns a new instance of DirectGeneration.
12 13 14 15 16 17 18 19 |
# File 'lib/tng/services/direct_generation.rb', line 12 def initialize(pastel, testng, http_client, params, , go_ui) @pastel = pastel @testng = testng @http_client = http_client @params = params = @go_ui = go_ui end |
Instance Method Details
#display_audit_results(result) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tng/services/direct_generation.rb', line 21 def display_audit_results(result) # debug logs removed audit_results = result[:audit_results] || result["audit_results"] || result[:result] || result["result"] || result if audit_results.is_a?(String) begin audit_results = JSON.parse(audit_results) rescue JSON::ParserError audit_results = nil end end # debug logs removed return unless audit_results.is_a?(Hash) @go_ui.show_audit_results(audit_results, "issues") end |
#extract_methods_for_type(file_object, type) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/tng/services/direct_generation.rb', line 189 def extract_methods_for_type(file_object, type) case type when "controller" then extract_controller_methods(file_object) when "model" then extract_model_methods(file_object) when "service" then extract_service_methods(file_object) else extract_other_methods(file_object) end end |
#find_similar_files(base_name) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/tng/services/direct_generation.rb', line 91 def find_similar_files(base_name) unless Tng::Utils.rails_project?(Dir.pwd) root = Dir.pwd files = Tng::Services::FileDiscovery.list_ruby_files(root) return files .select { |f| File.basename(f, ".rb").include?(base_name) } .map { |f| f.gsub(%r{^#{Regexp.escape(root)}/}, "") } end rails_root = defined?(::Rails) && ::Rails.root ? ::Rails.root.to_s : Dir.pwd similar_files = [] %w[app/controllers app/models app/services app/service].each do |dir| next unless Dir.exist?(File.join(rails_root, dir)) Dir.glob(File.join(rails_root, dir, "**", "*#{base_name}*.rb")).each do |file| next if Tng::Utils.ignored_path?(file) similar_files << file.gsub(%r{^#{Regexp.escape(rails_root)}/}, "") end end similar_files end |
#generate_test_for_file(file_path, method_name, type) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/tng/services/direct_generation.rb', line 115 def generate_test_for_file(file_path, method_name, type) file_object = FileTypeDetector.build_file_object(file_path, type) methods = extract_methods_for_type(file_object, type) if methods.empty? @go_ui.display_warning(@pastel.yellow("⚠️ No methods found in #{file_object[:name]}")) return end method_info = methods.find { |m| m[:name].downcase == method_name.downcase } if method_info && @params[:class_name] method_info = method_info.merge(class_name: @params[:class_name]) end unless method_info @go_ui.display_error(@pastel.red("❌ Method '#{method_name}' not found in #{file_object[:name]}")) @go_ui.display_list(@pastel.yellow("Available methods:"), methods.first(10).map { |m| m[:name] }) return end action = if @params[:trace] "Tracing" elsif @params[:audit] "Auditing" else "Generating test for" end @go_ui.display_info(@pastel.bright_white("🎯 #{action} #{file_object[:name]}##{method_info[:name]}...")) result = @go_ui.show_progress("Processing...") do |progress| generate_test_result(file_object, method_info, type, progress) end # debug logs removed if result&.dig(:error) label = if @params[:trace] "Trace" elsif @params[:audit] "Audit" else "Test generation" end @go_ui.display_error(@pastel.red("❌ #{label} failed: #{result[:message]}")) elsif result&.dig(:trace_generated) if result[:file_path] @go_ui.show_trace_results(result[:file_path]) File.unlink(result[:file_path]) if File.exist?(result[:file_path]) end elsif @params[:audit] display_audit_results(result) elsif result && result[:file_path] .call(result) else @go_ui.display_error(@pastel.red("❌ Failed to generate test")) end end |
#generate_test_result(file_object, method_info, type, progress) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/tng/services/direct_generation.rb', line 198 def generate_test_result(file_object, method_info, type, progress) generator = Tng::Services::TestGenerator.new(@http_client) if !Tng::Utils.rails_project?(Dir.pwd) method_info = method_info.merge(test_type: method_info[:test_type] || "other") return generator.run_for_ruby_method(file_object, method_info, progress: progress) unless @params[:trace] || @params[:audit] if @params[:audit] return generator.run_audit_for_ruby_method(file_object, method_info, progress: progress) end end if @params[:trace] perform_trace_analysis(file_object, method_info, progress) elsif @params[:audit] case type when "controller" then generator.run_audit_for_controller_method(file_object, method_info, progress: progress) when "model" then generator.run_audit_for_model_method(file_object, method_info, progress: progress) when "service" then generator.run_audit_for_service_method(file_object, method_info, progress: progress) else generator.run_audit_for_other_method(file_object, method_info, progress: progress) end else case type when "controller" then generator.run_for_controller_method(file_object, method_info, progress: progress) when "model" then generator.run_for_model_method(file_object, method_info, progress: progress) when "service" then generator.run_for_service_method(file_object, method_info, progress: progress) else generator.run_for_other_method(file_object, method_info, progress: progress) end end end |
#perform_clone_analysis(file_object, progress) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/tng/services/direct_generation.rb', line 229 def perform_clone_analysis(file_object, progress) progress.update("Analyzing file for duplicates...", 50) project_root = Dir.pwd path = File.(file_object[:path]) level = @params[:level] || "all" begin matches = Tng::Analyzer::Context.analyze_clones(project_root, path, level) progress.update("Analysis complete!", 100) { matches: matches } rescue => e { error: true, message: e. } end end |
#perform_trace_analysis(file_object, method_info, progress) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/tng/services/direct_generation.rb', line 245 def perform_trace_analysis(file_object, method_info, progress) progress.update("Tracing method execution...", 50) project_root = Dir.pwd path = File.(file_object[:path]) begin trace_json = Tng::Analyzer::Context.analyze_symbolic_trace( project_root, path, method_info[:name], nil ) f = Tempfile.new(['trace', '.json']) f.write(JSON.generate(trace_json)) f.close result_path = f.path progress.update("Trace generated!", 100) { trace_generated: true, file_path: result_path } rescue => e { error: true, message: e. } end end |
#run ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tng/services/direct_generation.rb', line 37 def run file_path = @params[:file] method_name = @params[:method] unless file_path && (method_name || @params[:clones]) @go_ui.display_error(@pastel.red("❌ File parameter is required#{@params[:clones] ? "" : " along with method"}")) @go_ui.display_warning(@pastel.yellow("Usage: bundle exec tng app/controllers/users_controller.rb index")) @go_ui.display_warning(@pastel.yellow(" or: bundle exec tng --file=users_controller.rb --clones [--level=1|2|all]")) return end if Tng::Utils.ignored_path?(file_path) @go_ui.display_error(@pastel.red("❌ Ignored by TNG config (ignore_files/ignore_folders). Remove it from config to proceed.")) return end resolved_result = FileTypeDetector.resolve_file_path(file_path) if resolved_result.is_a?(Hash) && resolved_result[:ignored] @go_ui.display_error(@pastel.red("❌ Ignored by TNG config (ignore_files/ignore_folders). Remove it from config to proceed.")) return end resolved_path = resolved_result.is_a?(Hash) ? resolved_result[:path] : resolved_result unless resolved_path && File.exist?(resolved_path) @go_ui.display_error(@pastel.red("❌ File not found: #{file_path}")) suggest_similar_files(file_path) return end if Tng::Utils.ignored_path?(resolved_path) @go_ui.display_error(@pastel.red("❌ Ignored by TNG config (ignore_files/ignore_folders). Remove it from config to proceed.")) return end type = FileTypeDetector.detect_type(resolved_path) if @params[:clones] run_clones_for_file(resolved_path, type) else generate_test_for_file(resolved_path, method_name, type) end end |
#run_clones_for_file(file_path, type) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/tng/services/direct_generation.rb', line 174 def run_clones_for_file(file_path, type) file_object = FileTypeDetector.build_file_object(file_path, type) @go_ui.display_info(@pastel.bright_white("🎯 Analyzing clones in #{file_object[:name]}...")) result = @go_ui.show_progress("Processing...") do |progress| perform_clone_analysis(file_object, progress) end if result&.dig(:error) @go_ui.display_error(@pastel.red("❌ Clone analysis failed: #{result[:message]}")) else @go_ui.show_clones(file_object[:path], result) end end |
#suggest_similar_files(file_path) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/tng/services/direct_generation.rb', line 79 def suggest_similar_files(file_path) base_name = File.basename(file_path, ".rb") similar_files = find_similar_files(base_name) if similar_files.empty? @go_ui.display_info(@pastel.dim(" No similar files found")) else @go_ui.display_list(@pastel.yellow("💡 Did you mean one of these?"), similar_files.first(5)) end end |