diff --git a/USAGE.md b/USAGE.md index 51803b9d0..c7407eacf 100644 --- a/USAGE.md +++ b/USAGE.md @@ -20,7 +20,6 @@ * [Understanding how to respond to new violations](#understanding-how-to-respond-to-new-violations) * [Recording existing violations](#recording-existing-violations) * [Understanding the package todo file](#understanding-the-package-todo-file) - * [Understanding the list of deprecated references](#understanding-the-list-of-deprecated-references) * [Loading extensions](#loading-extensions) ## What problem does Packwerk solve? diff --git a/lib/packwerk/cli.rb b/lib/packwerk/cli.rb index 707cea0b7..2ddc749ed 100644 --- a/lib/packwerk/cli.rb +++ b/lib/packwerk/cli.rb @@ -57,6 +57,8 @@ def execute_command(args) output_result(parse_run(args).check) when "update-todo", "update" output_result(parse_run(args).update_todo) + when "show-offenses", "show" + output_result(parse_run(args).show_offenses) when "validate" validate(args) when "version" @@ -118,6 +120,7 @@ def usage Subcommands: init - set up packwerk check - run all checks + show-offenses - displays all offenses update-todo - update package_todo.yml files validate - verify integrity of packwerk and package configuration version - output packwerk version diff --git a/lib/packwerk/offense_collection.rb b/lib/packwerk/offense_collection.rb index 38c9a86d3..6bc8aa67f 100644 --- a/lib/packwerk/offense_collection.rb +++ b/lib/packwerk/offense_collection.rb @@ -42,6 +42,11 @@ def listed?(offense) package_todo_for(reference.package).listed?(reference, violation_type: offense.violation_type) end + sig { params(offenses: T::Array[Offense]).void } + def add_offenses(offenses) + offenses.each { |offense| add_offense(offense) } + end + sig do params(offense: Packwerk::Offense).void end diff --git a/lib/packwerk/package.rb b/lib/packwerk/package.rb index 95988aa57..ccf978598 100644 --- a/lib/packwerk/package.rb +++ b/lib/packwerk/package.rb @@ -25,7 +25,6 @@ def initialize(name:, config: nil) @name = name @config = T.let(config || {}, T::Hash[String, T.untyped]) @dependencies = T.let(Array(@config["dependencies"]).freeze, T::Array[String]) - @public_path = T.let(nil, T.nilable(String)) end sig { returns(T::Boolean) } diff --git a/lib/packwerk/parse_run.rb b/lib/packwerk/parse_run.rb index ebce79b32..d5548e601 100644 --- a/lib/packwerk/parse_run.rb +++ b/lib/packwerk/parse_run.rb @@ -46,7 +46,10 @@ def update_todo end run_context = RunContext.from_configuration(@configuration) - offense_collection = find_offenses(run_context) + offenses = find_offenses(run_context) { update_progress } + + offense_collection = OffenseCollection.new(@configuration.root_path) + offense_collection.add_offenses(offenses) offense_collection.persist_package_todo_files(run_context.package_set) message = <<~EOS @@ -57,10 +60,25 @@ def update_todo Cli::Result.new(message: message, status: offense_collection.errors.empty?) end + sig { returns(Cli::Result) } + def show_offenses + run_context = RunContext.from_configuration(@configuration) + all_offenses = find_offenses(run_context) + + message = @offenses_formatter.show_offenses(all_offenses) + + Cli::Result.new(message: message, status: true) + end + sig { returns(Cli::Result) } def check run_context = RunContext.from_configuration(@configuration) - offense_collection = find_offenses(run_context, show_errors: true) + offense_collection = OffenseCollection.new(@configuration.root_path) + offenses = find_offenses(run_context) do |offenses| + failed = offenses.any? { |offense| !offense_collection.listed?(offense) } + update_progress(failed: failed) + end + offense_collection.add_offenses(offenses) messages = [ @offenses_formatter.show_offenses(offense_collection.outstanding_offenses), @@ -76,16 +94,25 @@ def check private - sig { params(run_context: RunContext, show_errors: T::Boolean).returns(OffenseCollection) } - def find_offenses(run_context, show_errors: false) - offense_collection = OffenseCollection.new(@configuration.root_path) + sig do + params( + run_context: RunContext, + block: T.nilable(T.proc.params( + offenses: T::Array[Packwerk::Offense], + ).void) + ).returns(T::Array[Offense]) + end + def find_offenses(run_context, &block) all_offenses = T.let([], T::Array[Offense]) - process_file = T.let(->(relative_file) do - run_context.process_file(relative_file: relative_file).tap do |offenses| - failed = show_errors && offenses.any? { |offense| !offense_collection.listed?(offense) } - update_progress(failed: failed) - end - end, ProcessFileProc) + process_file = if block + T.let(proc do |relative_file| + run_context.process_file(relative_file: relative_file).tap(&block) + end, ProcessFileProc) + else + T.let(proc do |relative_file| + run_context.process_file(relative_file: relative_file) + end, ProcessFileProc) + end @progress_formatter.started_inspection(@relative_file_set) do all_offenses = if @configuration.parallel? @@ -95,8 +122,7 @@ def find_offenses(run_context, show_errors: false) end end - all_offenses.each { |offense| offense_collection.add_offense(offense) } - offense_collection + all_offenses end sig { params(block: ProcessFileProc).returns(T::Array[Offense]) }