Module: Headdesk::CliCommands::Blame

Includes:
Headdesk::CliCommand
Defined in:
lib/headdesk/cli_commands/blame.rb

Overview

Find out which JAR/AAR is bringing in symbols

:reek:NilCheck, :reek:NestedIterators

Class Method Summary collapse

Methods included from Headdesk::CliCommand

each

Class Method Details

.included(thor) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/headdesk/cli_commands/blame.rb', line 16

def self.included(thor)
  thor.class_eval do
    desc 'blame SYMBOL [PATH]', 'Recursivly search for JARs and AARs which define a given symbol.'
    method_option :json, type: :boolean
    def blame(in_symbol, path = '.')
      unless Dir.exist?(path)
        STDERR.puts "Could not find path: #{path}"
        CLI.command_help(Thor::Base.shell.new, 'blame')
        exit 1
      end

      symbol_matcher = /(.*)(#{Regexp.escape(in_symbol.tr('.', '/'))})([^\.]*)?(\.class)?/

      # Match inside JAR files
      jar_matches = {}
      Dir[File.join(path, '**', '*.jar')].each do |jar|
        stdout = `jar -tf #{jar}`
        exit $CHILD_STATUS.to_i unless $CHILD_STATUS.success?

        matches = Blame.match_lines(stdout, symbol_matcher)
        jar_matches[jar] = matches unless matches.empty?
      end

      # Match inside AAR files
      aar_matches = {}
      Dir[File.join(path, '**', '*.aar')].each do |aar|
        stdout = `unzip -p -j #{aar} classes.jar | jar -t`
        exit $CHILD_STATUS.to_i unless $CHILD_STATUS.success?

        matches = Blame.match_lines(stdout, symbol_matcher)
        aar_matches[aar] = matches unless matches.empty?
      end

      # JSON output if requested
      if options[:json]
        STDOUT.puts({ jar: jar_matches, aar: aar_matches }.to_json)
        exit 0
      end

      # Pretty output
      all_matches = jar_matches.merge(aar_matches)
      all_matches.each do |package, symbols|
        STDOUT.puts '📦 ' + package
        symbols.each do |match|
          STDOUT.puts "  ↳ #{match[0]}#{match[1].green}#{match[2]&.chomp('.')}"
        end
      end

      print_update_message unless Headdesk::Versions.latest_version?

      exit !all_matches.empty?
    end
  end
end

.match_lines(stdout, matcher) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/headdesk/cli_commands/blame.rb', line 71

def self.match_lines(stdout, matcher)
  stdout.lines
        .each(&:strip!)
        .map { |line| matcher.match(line) }
        .compact
        .map { |match| match.captures.map { |capture| capture&.tr('/', '.') } }
end