Module: UsePacks::Private::PackwerkWrapper

Extended by:
T::Sig
Defined in:
lib/use_packs/private/packwerk_wrapper.rb,
lib/use_packs/private/packwerk_wrapper/offenses_aggregator_formatter.rb

Defined Under Namespace

Classes: OffensesAggregatorFormatter

Class Method Summary collapse

Class Method Details

.get_offenses_for_files(files) ⇒ Object



43
44
45
46
47
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 43

def self.get_offenses_for_files(files)
  formatter = OffensesAggregatorFormatter.new
  packwerk_cli_execute_safely(['check', *files], formatter)
  formatter.aggregated_offenses.compact
end

.get_offenses_for_files_by_package(files) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 56

def self.get_offenses_for_files_by_package(files)
  packages = package_names_for_files(files)
  argv = ['check', '--packages', packages.join(',')]
  formatter = OffensesAggregatorFormatter.new
  packwerk_cli_execute_safely(argv, formatter)
  formatter.aggregated_offenses.compact
end

.package_names_for_files(files) ⇒ Object



65
66
67
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 65

def self.package_names_for_files(files)
  files.map { |f| ParsePackwerk.package_from_path(f).name }.compact.uniq
end

.packwerk_cli(formatter) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 33

def self.packwerk_cli(formatter)
  # This is mostly copied from exe/packwerk within the packwerk gem, but we use our own formatters
  # Note that packwerk does not allow you to pass in your own progress formatter currently
  ENV['RAILS_ENV'] = 'test'

  style = Packwerk::OutputStyles::Coloured.new
  Packwerk::Cli.new(style: style, offenses_formatter: formatter)
end

.packwerk_cli_execute_safely(argv, formatter = nil) ⇒ Object



15
16
17
18
19
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 15

def self.packwerk_cli_execute_safely(argv, formatter = nil)
  with_safe_exit_if_no_files_found do
    packwerk_cli(formatter).execute_command(argv)
  end
end

.validate!Object



50
51
52
53
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 50

def self.validate!
  formatter = OffensesAggregatorFormatter.new
  packwerk_cli_execute_safely(['validate'], formatter)
end

.with_safe_exit_if_no_files_found(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/use_packs/private/packwerk_wrapper.rb', line 22

def self.with_safe_exit_if_no_files_found(&block)
  block.call
rescue SystemExit => e
  # Packwerk should probably exit positively here rather than raising an error -- there should be no
  # errors if the user has excluded all files being checked.
  unless e.message == 'No files found or given. Specify files or check the include and exclude glob in the config file.'
    raise
  end
end