Class: Hbc::CLI::InternalAuditModifiedCasks

Inherits:
AbstractInternalCommand show all
Defined in:
Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb

Constant Summary collapse

RELEVANT_STANZAS =
[:version, :sha256, :url, :appcast].freeze

Instance Attribute Summary collapse

Attributes inherited from AbstractCommand

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractInternalCommand

command_name, visible

Methods inherited from AbstractCommand

abstract?, command_name, run, visible

Methods included from Options

included, #process_arguments

Constructor Details

#initializeInternalAuditModifiedCasks

Returns a new instance of InternalAuditModifiedCasks



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 15

def initialize(*)
  super

  if args.count != 1
    raise ArgumentError, <<~EOS
      This command requires exactly one argument.

      #{self.class.usage}
    EOS
  end

  @commit_range = args.first
end

Instance Attribute Details

#commit_rangeObject

Returns the value of attribute commit_range



12
13
14
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 12

def commit_range
  @commit_range
end

Class Method Details

.helpObject



29
30
31
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 29

def self.help
  "audit all modified Casks in a given commit range"
end

.needs_init?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 8

def self.needs_init?
  true
end

.usageObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 33

def self.usage
  <<~EOS
    Usage: brew cask _audit_modified_casks [options...] <commit range>

    Given a range of Git commits, find any Casks that were modified and run `brew
    cask audit' on them. If the `url', `version', or `sha256' stanzas were modified,
    run with the `--download' flag to verify the hash.

    Options:
      -c, --cleanup
        Remove all cached downloads. Use with care.
  EOS
end

Instance Method Details

#added_cask_filesObject



65
66
67
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 65

def added_cask_files
  @added_cask_files ||= git_filter_cask_files("A")
end

#audit(cask, cask_file) ⇒ Object



85
86
87
88
89
90
91
92
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 85

def audit(cask, cask_file)
  audit_download = audit_download?(cask, cask_file)
  check_token_conflicts = added_cask_files.include?(cask_file)
  success = Auditor.audit(cask, audit_download:        audit_download,
                                check_token_conflicts: check_token_conflicts,
                                commit_range: commit_range)
  failed_casks << cask unless success
end

#audit_download?(cask, cask_file) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 98

def audit_download?(cask, cask_file)
  cask.sha256 != :no_check && relevant_stanza_modified?(cask_file)
end

#failed_casksObject



94
95
96
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 94

def failed_casks
  @failed_casks ||= []
end

#git(*args) ⇒ Object



107
108
109
110
111
112
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 107

def git(*args)
  odebug ["git", *args].join(" ")
  out, err, status = Open3.capture3("git", *args)
  return out.chomp if status.success?
  odie err.chomp
end

#git_filter_cask_files(filter) ⇒ Object



69
70
71
72
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 69

def git_filter_cask_files(filter)
  git("diff", "--name-only", "--diff-filter=#{filter}", commit_range,
      "--", Pathname.new(git_root).join("Casks", "*.rb").to_s).split("\n")
end

#git_rootObject



57
58
59
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 57

def git_root
  @git_root ||= git("rev-parse", "--show-toplevel")
end

#modified_cask_filesObject



61
62
63
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 61

def modified_cask_files
  @modified_cask_files ||= git_filter_cask_files("AMR")
end

#modified_casksObject



74
75
76
77
78
79
80
81
82
83
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 74

def modified_casks
  return @modified_casks if defined? @modified_casks
  @modified_casks = modified_cask_files.map { |f| CaskLoader.load(f) }
  if @modified_casks.any?
    num_modified = @modified_casks.size
    ohai "#{Formatter.pluralize(num_modified, "modified cask")}: " \
      "#{@modified_casks.join(" ")}"
  end
  @modified_casks
end

#relevant_stanza_modified?(cask_file) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 102

def relevant_stanza_modified?(cask_file)
  out = git("diff", commit_range, "--", cask_file)
  out =~ /^\+\s*(#{RELEVANT_STANZAS.join('|')})/
end

#report_failuresObject



114
115
116
117
118
119
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 114

def report_failures
  return if failed_casks.empty?
  num_failed = failed_casks.size
  odie "audit failed for #{Formatter.pluralize(num_failed, "cask")}: " \
    "#{failed_casks.join(" ")}"
end

#runObject



47
48
49
50
51
52
53
54
55
# File 'Library/Homebrew/cask/lib/hbc/cli/internal_audit_modified_casks.rb', line 47

def run
  Dir.chdir git_root do
    modified_cask_files.zip(modified_casks).each do |cask_file, cask|
      audit(cask, cask_file)
      Cleanup.run(cask) if cleanup?
    end
  end
  report_failures
end