Module: UsePackwerk::Private::PackwerkWrapper

Extended by:
T::Sig
Defined in:
lib/use_packwerk/private/packwerk_wrapper.rb,
lib/use_packwerk/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



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

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



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

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



66
67
68
# File 'lib/use_packwerk/private/packwerk_wrapper.rb', line 66

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

.packwerk_cli(formatter) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/use_packwerk/private/packwerk_wrapper.rb', line 40

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



22
23
24
25
26
# File 'lib/use_packwerk/private/packwerk_wrapper.rb', line 22

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

.packwerk_cli_run_safely(argv, formatter) ⇒ Object



12
13
14
15
16
# File 'lib/use_packwerk/private/packwerk_wrapper.rb', line 12

def self.packwerk_cli_run_safely(argv, formatter)
  with_safe_exit_if_no_files_found do
    packwerk_cli(formatter).run(argv)
  end
end

.with_safe_exit_if_no_files_found(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/use_packwerk/private/packwerk_wrapper.rb', line 29

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