Merge pull request #2278 from reitermarkus/cask-_audit_modified_casks

Refactor `brew cask _audit_modified_casks`.
This commit is contained in:
Markus Reiter 2017-03-07 01:35:10 +01:00 committed by GitHub
commit 0a0c305111

View File

@ -3,28 +3,27 @@ module Hbc
class InternalAuditModifiedCasks < InternalUseBase class InternalAuditModifiedCasks < InternalUseBase
RELEVANT_STANZAS = [:version, :sha256, :url, :appcast].freeze RELEVANT_STANZAS = [:version, :sha256, :url, :appcast].freeze
class << self def self.needs_init?
def needs_init?
true true
end end
def run(*args) def self.run(*args)
commit_range = commit_range(args) commit_range = commit_range(args)
cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i } cleanup = args.any? { |a| a =~ /^-+c(leanup)?$/i }
new(commit_range, cleanup: cleanup).run new(commit_range, cleanup: cleanup).run
end end
def commit_range(args) def self.commit_range(args)
posargs = args.reject { |a| a.empty? || a.chars.first == "-" } posargs = args.reject { |a| a.empty? || a.chars.first == "-" }
odie usage unless posargs.size == 1 odie usage unless posargs.size == 1
posargs.first posargs.first
end end
def posargs(args) def self.posargs(args)
args.reject { |a| a.empty? || a.chars.first == "-" } args.reject { |a| a.empty? || a.chars.first == "-" }
end end
def usage def self.usage
<<-EOS.undent <<-EOS.undent
Usage: brew cask _audit_modified_casks [options...] <commit range> Usage: brew cask _audit_modified_casks [options...] <commit range>
@ -37,7 +36,6 @@ module Hbc
Remove all cached downloads. Use with care. Remove all cached downloads. Use with care.
EOS EOS
end end
end
def initialize(commit_range, cleanup: false) def initialize(commit_range, cleanup: false)
@commit_range = commit_range @commit_range = commit_range
@ -85,7 +83,7 @@ module Hbc
@modified_casks = modified_cask_files.map { |f| Hbc.load(f) } @modified_casks = modified_cask_files.map { |f| Hbc.load(f) }
if @modified_casks.any? if @modified_casks.any?
num_modified = @modified_casks.size num_modified = @modified_casks.size
ohai "#{num_modified} modified #{pluralize("cask", num_modified)}: " \ ohai "#{num_modified} modified cask#{plural(num_modified)}: " \
"#{@modified_casks.join(" ")}" "#{@modified_casks.join(" ")}"
end end
@modified_casks @modified_casks
@ -122,15 +120,10 @@ module Hbc
def report_failures def report_failures
return if failed_casks.empty? return if failed_casks.empty?
num_failed = failed_casks.size num_failed = failed_casks.size
cask_pluralized = pluralize("cask", num_failed) odie "audit failed for #{num_failed} cask#{plural(num_failed)}: " \
odie "audit failed for #{num_failed} #{cask_pluralized}: " \
"#{failed_casks.join(" ")}" "#{failed_casks.join(" ")}"
end end
def pluralize(str, num)
num == 1 ? str : "#{str}s"
end
def cleanup def cleanup
Cleanup.run if cleanup? Cleanup.run if cleanup?
end end