lib/packwerk/cli.rb in packwerk-1.1.0 vs lib/packwerk/cli.rb in packwerk-1.1.1

- old
+ new

@@ -1,20 +1,23 @@ # typed: true # frozen_string_literal: true require "benchmark" require "sorbet-runtime" +require "packwerk/application_load_paths" require "packwerk/application_validator" require "packwerk/configuration" require "packwerk/files_for_processing" +require "packwerk/formatters/offenses_formatter" require "packwerk/formatters/progress_formatter" require "packwerk/inflector" require "packwerk/output_styles" require "packwerk/run_context" require "packwerk/updating_deprecated_references" require "packwerk/checking_deprecated_references" require "packwerk/commands/detect_stale_violations_command" +require "packwerk/commands/update_deprecations_command" require "packwerk/commands/offense_progress_marker" module Packwerk class Cli extend T::Sig @@ -99,11 +102,11 @@ application_validation && generate_configurations end def generate_configs configuration_file = Packwerk::Generators::ConfigurationFile.generate( - load_paths: @configuration.load_paths, + load_paths: Packwerk::ApplicationLoadPaths.extract_relevant_paths, root: @configuration.root_path, out: @out ) inflections_file = Packwerk::Generators::InflectionsFile.generate(root: @configuration.root_path, out: @out) root_package = Packwerk::Generators::RootPackage.generate(root: @configuration.root_path, out: @out) @@ -132,37 +135,20 @@ warn("`packwerk update` is deprecated in favor of `packwerk update-deprecations`.") update_deprecations(paths) end def update_deprecations(paths) - updating_deprecated_references = ::Packwerk::UpdatingDeprecatedReferences.new(@configuration.root_path) - @run_context = Packwerk::RunContext.from_configuration( - @configuration, - reference_lister: updating_deprecated_references + update_deprecations = Commands::UpdateDeprecationsCommand.new( + files: fetch_files_to_process(paths), + configuration: @configuration, + offenses_formatter: offenses_formatter, + progress_formatter: @progress_formatter ) - - files = fetch_files_to_process(paths) - - @progress_formatter.started(files) - - all_offenses = T.let([], T.untyped) - execution_time = Benchmark.realtime do - all_offenses = files.flat_map do |path| - @run_context.process_file(file: path).tap do |offenses| - mark_progress(offenses: offenses, progress_formatter: @progress_formatter) - end - end - - updating_deprecated_references.dump_deprecated_references_files - end - - @out.puts # put a new line after the progress dots - show_offenses(all_offenses) - @progress_formatter.finished(execution_time) - @out.puts("✅ `deprecated_references.yml` has been updated.") - - all_offenses.empty? + result = update_deprecations.run + @out.puts + @out.puts(result.message) + result.status end def check(paths) files = fetch_files_to_process(paths) @@ -179,19 +165,19 @@ rescue Interrupt @out.puts @out.puts("Manually interrupted. Violations caught so far are listed below:") end - @out.puts # put a new line after the progress dots - show_offenses(all_offenses) @progress_formatter.finished(execution_time) + @out.puts + @out.puts(offenses_formatter.show_offenses(all_offenses)) all_offenses.empty? end def detect_stale_violations(paths) - detect_stale_violations = DetectStaleViolationsCommand.new( + detect_stale_violations = Commands::DetectStaleViolationsCommand.new( files: fetch_files_to_process(paths), configuration: @configuration, progress_formatter: @progress_formatter ) result = detect_stale_violations.run @@ -223,23 +209,10 @@ return result.ok? end end - def show_offenses(offenses) - if offenses.empty? - @out.puts("No offenses detected 🎉") - else - offenses.each do |offense| - @out.puts(offense.to_s(@style)) - end - - offenses_string = Inflector.default.pluralize("offense", offenses.length) - @out.puts("#{offenses.length} #{offenses_string} detected") - end - end - def list_validation_errors(result) @out.puts if result.ok? @out.puts("Validation successful 🎉") else @@ -253,8 +226,12 @@ if File.exist?("config/application.rb") && File.exist?("bin/rails") File.foreach("Gemfile").any? { |line| line.match?(/['"]rails['"]/) } else false end + end + + def offenses_formatter + @offenses_formatter ||= Formatters::OffensesFormatter.new(style: @style) end end end