diff --git a/lib/packwerk/offense_collection.rb b/lib/packwerk/offense_collection.rb index 6dfed32a3..0a4d27324 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/parse_run.rb b/lib/packwerk/parse_run.rb index ebce79b32..eda11fa88 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 @@ -60,7 +63,12 @@ def update_todo 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,27 +84,35 @@ 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) - 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) + 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) + offenses = T.let([], T::Array[Offense]) + process_file = if block_given? + 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? + offenses = if @configuration.parallel? Parallel.flat_map(@relative_file_set, &process_file) else serial_find_offenses(&process_file) end end - all_offenses.each { |offense| offense_collection.add_offense(offense) } - offense_collection + offenses end sig { params(block: ProcessFileProc).returns(T::Array[Offense]) }