Merge pull request #16921 from Homebrew/ported-cmds

Convert some dev commands to use AbstractCommand
This commit is contained in:
Douglas Eichelberger 2024-03-20 10:47:44 -07:00 committed by GitHub
commit 0ac23c0690
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 2927 additions and 2890 deletions

View File

@ -16,21 +16,21 @@ module Homebrew
abstract!
class << self
sig { returns(T.nilable(CLI::Parser)) }
attr_reader :parser
sig { returns(String) }
def command_name = T.must(name).split("::").fetch(-1).downcase
def command_name = Utils.underscore(T.must(name).split("::").fetch(-1)).tr("_", "-")
# @return the AbstractCommand subclass associated with the brew CLI command name.
sig { params(name: String).returns(T.nilable(T.class_of(AbstractCommand))) }
def command(name) = subclasses.find { _1.command_name == name }
sig { returns(CLI::Parser) }
def parser = CLI::Parser.new(self, &@parser_block)
private
sig { params(block: T.proc.bind(CLI::Parser).void).void }
def cmd_args(&block)
@parser = T.let(CLI::Parser.new(&block), T.nilable(CLI::Parser))
@parser_block = T.let(block, T.nilable(T.proc.void))
end
end
@ -39,10 +39,7 @@ module Homebrew
sig { params(argv: T::Array[String]).void }
def initialize(argv = ARGV.freeze)
parser = self.class.parser
raise "Commands must include a `cmd_args` block" if parser.nil?
@args = T.let(parser.parse(argv), CLI::Args)
@args = T.let(self.class.parser.parse(argv), CLI::Args)
end
sig { abstract.void }

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "abstract_command"
require "env_config"
require "cask/config"
require "cli/args"
@ -19,9 +20,18 @@ module Homebrew
def self.from_cmd_path(cmd_path)
cmd_args_method_name = Commands.args_method_name(cmd_path)
cmd_name = cmd_args_method_name.to_s.delete_suffix("_args").tr("_", "-")
begin
Homebrew.send(cmd_args_method_name) if require?(cmd_path)
if require?(cmd_path)
cmd = Homebrew::AbstractCommand.command(cmd_name)
if cmd
cmd.parser
else
# FIXME: remove once commands are all subclasses of `AbstractCommand`:
Homebrew.send(cmd_args_method_name)
end
end
rescue NoMethodError => e
raise if e.name.to_sym != cmd_args_method_name
@ -109,8 +119,10 @@ module Homebrew
]
end
sig { params(block: T.nilable(T.proc.bind(Parser).void)).void }
def initialize(&block)
sig {
params(cmd: T.nilable(T.class_of(Homebrew::AbstractCommand)), block: T.nilable(T.proc.bind(Parser).void)).void
}
def initialize(cmd = nil, &block)
@parser = OptionParser.new
@parser.summary_indent = " " * 2
@ -123,12 +135,18 @@ module Homebrew
@args = Homebrew::CLI::Args.new
# Filter out Sorbet runtime type checking method calls.
cmd_location = T.must(caller_locations).select do |location|
T.must(location.path).exclude?("/gems/sorbet-runtime-")
end.fetch(1)
@command_name = T.must(cmd_location.label).chomp("_args").tr("_", "-")
@is_dev_cmd = T.must(cmd_location.absolute_path).start_with?(Commands::HOMEBREW_DEV_CMD_PATH)
if cmd
@command_name = cmd.command_name
@is_dev_cmd = cmd.name&.start_with?("Homebrew::DevCmd")
else
# FIXME: remove once commands are all subclasses of `AbstractCommand`:
# Filter out Sorbet runtime type checking method calls.
cmd_location = T.must(caller_locations).select do |location|
T.must(location.path).exclude?("/gems/sorbet-runtime-")
end.fetch(1)
@command_name = T.must(cmd_location.label).chomp("_args").tr("_", "-")
@is_dev_cmd = T.must(cmd_location.absolute_path).start_with?(Commands::HOMEBREW_DEV_CMD_PATH)
end
@constraints = []
@conflicts = []

View File

@ -1,6 +1,7 @@
# typed: true
# frozen_string_literal: true
require "abstract_command"
require "formula"
require "formula_versions"
require "utils/curl"
@ -20,341 +21,345 @@ require "formula_auditor"
require "tap_auditor"
module Homebrew
sig { returns(CLI::Parser) }
def self.audit_args
Homebrew::CLI::Parser.new do
description <<~EOS
Check <formula> for Homebrew coding style violations. This should be run before
submitting a new formula or cask. If no <formula>|<cask> are provided, check all
locally available formulae and casks and skip style checks. Will exit with a
non-zero status if any errors are found.
EOS
flag "--os=",
description: "Audit the given operating system. (Pass `all` to audit all operating systems.)"
flag "--arch=",
description: "Audit the given CPU architecture. (Pass `all` to audit all architectures.)"
switch "--strict",
description: "Run additional, stricter style checks."
switch "--git",
description: "Run additional, slower style checks that navigate the Git repository."
switch "--online",
description: "Run additional, slower style checks that require a network connection."
switch "--installed",
description: "Only check formulae and casks that are currently installed."
switch "--eval-all",
description: "Evaluate all available formulae and casks, whether installed or not, to audit them. " \
"Implied if `HOMEBREW_EVAL_ALL` is set."
switch "--new",
description: "Run various additional style checks to determine if a new formula or cask is eligible " \
"for Homebrew. This should be used when creating new formulae or casks and implies " \
"`--strict` and `--online`."
switch "--new-formula",
replacement: "--new",
# odeprecated: change this to true on disable and remove `args.new_formula?` calls
disable: false,
hidden: true
switch "--new-cask",
replacement: "--new",
# odeprecated: change this to true on disable and remove `args.new_formula?` calls
disable: false,
hidden: true
switch "--[no-]signing",
description: "Audit for signed apps, which are required on ARM"
switch "--token-conflicts",
description: "Audit for token conflicts."
flag "--tap=",
description: "Check the formulae within the given tap, specified as <user>`/`<repo>."
switch "--fix",
description: "Fix style violations automatically using RuboCop's auto-correct feature."
switch "--display-cop-names",
description: "Include the RuboCop cop name for each violation in the output. This is the default.",
hidden: true
switch "--display-filename",
description: "Prefix every line of output with the file or formula name being audited, to " \
"make output easy to grep."
switch "--skip-style",
description: "Skip running non-RuboCop style checks. Useful if you plan on running " \
"`brew style` separately. Enabled by default unless a formula is specified by name."
switch "-D", "--audit-debug",
description: "Enable debugging and profiling of audit methods."
comma_array "--only",
description: "Specify a comma-separated <method> list to only run the methods named " \
"`audit_`<method>."
comma_array "--except",
description: "Specify a comma-separated <method> list to skip running the methods named " \
"`audit_`<method>."
comma_array "--only-cops",
description: "Specify a comma-separated <cops> list to check for violations of only the listed " \
"RuboCop cops."
comma_array "--except-cops",
description: "Specify a comma-separated <cops> list to skip checking for violations of the " \
"listed RuboCop cops."
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
module DevCmd
class Audit < AbstractCommand
cmd_args do
description <<~EOS
Check <formula> for Homebrew coding style violations. This should be run before
submitting a new formula or cask. If no <formula>|<cask> are provided, check all
locally available formulae and casks and skip style checks. Will exit with a
non-zero status if any errors are found.
EOS
flag "--os=",
description: "Audit the given operating system. (Pass `all` to audit all operating systems.)"
flag "--arch=",
description: "Audit the given CPU architecture. (Pass `all` to audit all architectures.)"
switch "--strict",
description: "Run additional, stricter style checks."
switch "--git",
description: "Run additional, slower style checks that navigate the Git repository."
switch "--online",
description: "Run additional, slower style checks that require a network connection."
switch "--installed",
description: "Only check formulae and casks that are currently installed."
switch "--eval-all",
description: "Evaluate all available formulae and casks, whether installed or not, to audit them. " \
"Implied if `HOMEBREW_EVAL_ALL` is set."
switch "--new",
description: "Run various additional style checks to determine if a new formula or cask is eligible " \
"for Homebrew. This should be used when creating new formulae or casks and implies " \
"`--strict` and `--online`."
switch "--new-formula",
replacement: "--new",
# odeprecated: change this to true on disable and remove `args.new_formula?` calls
disable: false,
hidden: true
switch "--new-cask",
replacement: "--new",
# odeprecated: change this to true on disable and remove `args.new_formula?` calls
disable: false,
hidden: true
switch "--[no-]signing",
description: "Audit for signed apps, which are required on ARM"
switch "--token-conflicts",
description: "Audit for token conflicts."
flag "--tap=",
description: "Check the formulae within the given tap, specified as <user>`/`<repo>."
switch "--fix",
description: "Fix style violations automatically using RuboCop's auto-correct feature."
switch "--display-cop-names",
description: "Include the RuboCop cop name for each violation in the output. This is the default.",
hidden: true
switch "--display-filename",
description: "Prefix every line of output with the file or formula name being audited, to " \
"make output easy to grep."
switch "--skip-style",
description: "Skip running non-RuboCop style checks. Useful if you plan on running " \
"`brew style` separately. Enabled by default unless a formula is specified by name."
switch "-D", "--audit-debug",
description: "Enable debugging and profiling of audit methods."
comma_array "--only",
description: "Specify a comma-separated <method> list to only run the methods named " \
"`audit_`<method>."
comma_array "--except",
description: "Specify a comma-separated <method> list to skip running the methods named " \
"`audit_`<method>."
comma_array "--only-cops",
description: "Specify a comma-separated <cops> list to check for violations of only the listed " \
"RuboCop cops."
comma_array "--except-cops",
description: "Specify a comma-separated <cops> list to skip checking for violations of the " \
"listed RuboCop cops."
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
conflicts "--only", "--except"
conflicts "--only-cops", "--except-cops", "--strict"
conflicts "--only-cops", "--except-cops", "--only"
conflicts "--formula", "--cask"
conflicts "--installed", "--all"
conflicts "--only", "--except"
conflicts "--only-cops", "--except-cops", "--strict"
conflicts "--only-cops", "--except-cops", "--only"
conflicts "--formula", "--cask"
conflicts "--installed", "--all"
named_args [:formula, :cask], without_api: true
end
end
sig { void }
def self.audit
args = audit_args.parse
new_cask = args.new? || args.new_cask?
new_formula = args.new? || args.new_formula?
Formulary.enable_factory_cache!
os_arch_combinations = args.os_arch_combinations
Homebrew.auditing = true
inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
strict = new_formula || args.strict?
online = new_formula || args.online?
tap_audit = args.tap.present?
skip_style = args.skip_style? || args.no_named? || tap_audit
no_named_args = T.let(false, T::Boolean)
ENV.activate_extensions!
ENV.setup_build_environment
audit_formulae, audit_casks = Homebrew.with_no_api_env do # audit requires full Ruby source
if args.tap
Tap.fetch(args.tap).then do |tap|
[
tap.formula_files.map { |path| Formulary.factory(path) },
tap.cask_files.map { |path| Cask::CaskLoader.load(path) },
]
end
elsif args.installed?
no_named_args = true
[Formula.installed, Cask::Caskroom.casks]
elsif args.no_named?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
# This odisabled should probably stick around indefinitely.
odisabled "brew audit",
"brew audit --eval-all or HOMEBREW_EVAL_ALL"
end
no_named_args = true
[
Formula.all(eval_all: args.eval_all?),
Cask::Cask.all(eval_all: args.eval_all?),
]
else
if args.named.any? { |named_arg| named_arg.end_with?(".rb") }
# This odisabled should probably stick around indefinitely,
# until at least we have a way to exclude error on these in the CLI parser.
odisabled "brew audit [path ...]",
"brew audit [name ...]"
end
args.named.to_formulae_and_casks
.partition { |formula_or_cask| formula_or_cask.is_a?(Formula) }
named_args [:formula, :cask], without_api: true
end
end
if audit_formulae.empty? && audit_casks.empty? && !args.tap
ofail "No matching formulae or casks to audit!"
return
end
sig { override.void }
def run
new_cask = args.new? || args.new_cask?
new_formula = args.new? || args.new_formula?
gem_groups = ["audit"]
gem_groups << "style" unless skip_style
Homebrew.install_bundler_gems!(groups: gem_groups)
Formulary.enable_factory_cache!
style_files = args.named.to_paths unless skip_style
os_arch_combinations = args.os_arch_combinations
only_cops = args.only_cops
except_cops = args.except_cops
style_options = { fix: args.fix?, debug: args.debug?, verbose: args.verbose? }
Homebrew.auditing = true
Homebrew.inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?
if only_cops
style_options[:only_cops] = only_cops
elsif new_formula || new_cask
nil
elsif except_cops
style_options[:except_cops] = except_cops
elsif !strict
style_options[:except_cops] = [:FormulaAuditStrict]
end
strict = new_formula || args.strict?
online = new_formula || args.online?
tap_audit = args.tap.present?
skip_style = args.skip_style? || args.no_named? || tap_audit
no_named_args = T.let(false, T::Boolean)
# Run tap audits first
named_arg_taps = [*audit_formulae, *audit_casks].map(&:tap).uniq if !args.tap && !no_named_args
tap_problems = Tap.installed.each_with_object({}) do |tap, problems|
next if args.tap && tap != args.tap
next if named_arg_taps&.exclude?(tap)
ENV.activate_extensions!
ENV.setup_build_environment
ta = TapAuditor.new(tap, strict: args.strict?)
ta.audit
problems[[tap.name, tap.path]] = ta.problems if ta.problems.any?
end
# Check style in a single batch run up front for performance
style_offenses = Style.check_style_json(style_files, **style_options) if style_files
# load licenses
spdx_license_data = SPDX.license_data
spdx_exception_data = SPDX.exception_data
formula_problems = audit_formulae.sort.each_with_object({}) do |f, problems|
path = f.path
only = only_cops ? ["style"] : args.only
options = {
new_formula:,
strict:,
online:,
git: args.git?,
only:,
except: args.except,
spdx_license_data:,
spdx_exception_data:,
style_offenses: style_offenses&.for_path(f.path),
tap_audit:,
}.compact
errors = os_arch_combinations.flat_map do |os, arch|
SimulateSystem.with(os:, arch:) do
odebug "Auditing Formula #{f} on os #{os} and arch #{arch}"
audit_proc = proc { FormulaAuditor.new(Formulary.factory(path), **options).tap(&:audit) }
# Audit requires full Ruby source so disable API.
# We shouldn't do this for taps however so that we don't unnecessarily require a full Homebrew/core clone.
fa = if f.core_formula?
Homebrew.with_no_api_env(&audit_proc)
audit_formulae, audit_casks = Homebrew.with_no_api_env do # audit requires full Ruby source
if args.tap
Tap.fetch(T.must(args.tap)).then do |tap|
[
tap.formula_files.map { |path| Formulary.factory(path) },
tap.cask_files.map { |path| Cask::CaskLoader.load(path) },
]
end
elsif args.installed?
no_named_args = true
[Formula.installed, Cask::Caskroom.casks]
elsif args.no_named?
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
# This odisabled should probably stick around indefinitely.
odisabled "brew audit",
"brew audit --eval-all or HOMEBREW_EVAL_ALL"
end
no_named_args = true
[
Formula.all(eval_all: args.eval_all?),
Cask::Cask.all(eval_all: args.eval_all?),
]
else
audit_proc.call
if args.named.any? { |named_arg| named_arg.end_with?(".rb") }
# This odisabled should probably stick around indefinitely,
# until at least we have a way to exclude error on these in the CLI parser.
odisabled "brew audit [path ...]",
"brew audit [name ...]"
end
args.named.to_formulae_and_casks
.partition { |formula_or_cask| formula_or_cask.is_a?(Formula) }
end
end
if audit_formulae.empty? && audit_casks.empty? && !args.tap
ofail "No matching formulae or casks to audit!"
return
end
gem_groups = ["audit"]
gem_groups << "style" unless skip_style
Homebrew.install_bundler_gems!(groups: gem_groups)
style_files = args.named.to_paths unless skip_style
only_cops = args.only_cops
except_cops = args.except_cops
style_options = { fix: args.fix?, debug: args.debug?, verbose: args.verbose? }
if only_cops
style_options[:only_cops] = only_cops
elsif new_formula || new_cask
nil
elsif except_cops
style_options[:except_cops] = except_cops
elsif !strict
style_options[:except_cops] = [:FormulaAuditStrict]
end
# Run tap audits first
named_arg_taps = [*audit_formulae, *audit_casks].map(&:tap).uniq if !args.tap && !no_named_args
tap_problems = Tap.installed.each_with_object({}) do |tap, problems|
next if args.tap && tap != args.tap
next if named_arg_taps&.exclude?(tap)
ta = TapAuditor.new(tap, strict: args.strict?)
ta.audit
problems[[tap.name, tap.path]] = ta.problems if ta.problems.any?
end
# Check style in a single batch run up front for performance
style_offenses = Style.check_style_json(style_files, **style_options) if style_files
# load licenses
spdx_license_data = SPDX.license_data
spdx_exception_data = SPDX.exception_data
formula_problems = audit_formulae.sort.each_with_object({}) do |f, problems|
path = f.path
only = only_cops ? ["style"] : args.only
options = {
new_formula:,
strict:,
online:,
git: args.git?,
only:,
except: args.except,
spdx_license_data:,
spdx_exception_data:,
style_offenses: style_offenses&.for_path(f.path),
tap_audit:,
}.compact
errors = os_arch_combinations.flat_map do |os, arch|
SimulateSystem.with(os:, arch:) do
odebug "Auditing Formula #{f} on os #{os} and arch #{arch}"
audit_proc = proc { FormulaAuditor.new(Formulary.factory(path), **options).tap(&:audit) }
# Audit requires full Ruby source so disable API. We shouldn't do this for taps however so that we
# don't unnecessarily require a full Homebrew/core clone.
fa = if f.core_formula?
Homebrew.with_no_api_env(&audit_proc)
else
audit_proc.call
end
fa.problems + fa.new_formula_problems
end
end.uniq
problems[[f.full_name, path]] = errors if errors.any?
end
require "cask/auditor" if audit_casks.any?
cask_problems = audit_casks.each_with_object({}) do |cask, problems|
path = cask.sourcefile_path
errors = os_arch_combinations.flat_map do |os, arch|
next [] if os == :linux
SimulateSystem.with(os:, arch:) do
odebug "Auditing Cask #{cask} on os #{os} and arch #{arch}"
Cask::Auditor.audit(
Cask::CaskLoader.load(path),
# For switches, we add `|| nil` so that `nil` will be passed
# instead of `false` if they aren't set.
# This way, we can distinguish between "not set" and "set to false".
audit_online: args.online? || nil,
audit_strict: args.strict? || nil,
# No need for `|| nil` for `--[no-]signing`
# because boolean switches are already `nil` if not passed
audit_signing: args.signing?,
audit_new_cask: new_cask || nil,
audit_token_conflicts: args.token_conflicts? || nil,
quarantine: true,
any_named_args: !no_named_args,
only: args.only,
except: args.except,
).to_a
end
end.uniq
problems[[cask.full_name, path]] = errors if errors.any?
end
print_problems(tap_problems)
print_problems(formula_problems)
print_problems(cask_problems)
tap_count = tap_problems.keys.count
formula_count = formula_problems.keys.count
cask_count = cask_problems.keys.count
corrected_problem_count = (formula_problems.values + cask_problems.values)
.sum { |problems| problems.count { |problem| problem.fetch(:corrected) } }
tap_problem_count = tap_problems.sum { |_, problems| problems.count }
formula_problem_count = formula_problems.sum { |_, problems| problems.count }
cask_problem_count = cask_problems.sum { |_, problems| problems.count }
total_problems_count = formula_problem_count + cask_problem_count + tap_problem_count
if total_problems_count.positive?
errors_summary = Utils.pluralize("problem", total_problems_count, include_count: true)
error_sources = []
if formula_count.positive?
error_sources << Utils.pluralize("formula", formula_count, plural: "e", include_count: true)
end
error_sources << Utils.pluralize("cask", cask_count, include_count: true) if cask_count.positive?
error_sources << Utils.pluralize("tap", tap_count, include_count: true) if tap_count.positive?
errors_summary += " in #{error_sources.to_sentence}" if error_sources.any?
errors_summary += " detected"
if corrected_problem_count.positive?
errors_summary +=
", #{Utils.pluralize("problem", corrected_problem_count, include_count: true)} corrected"
end
fa.problems + fa.new_formula_problems
ofail "#{errors_summary}."
end
end.uniq
problems[[f.full_name, path]] = errors if errors.any?
end
return unless ENV["GITHUB_ACTIONS"]
require "cask/auditor" if audit_casks.any?
annotations = formula_problems.merge(cask_problems).flat_map do |(_, path), problems|
problems.map do |problem|
GitHub::Actions::Annotation.new(
:error,
problem[:message],
file: path,
line: problem[:location]&.line,
column: problem[:location]&.column,
)
end
end.compact
cask_problems = audit_casks.each_with_object({}) do |cask, problems|
path = cask.sourcefile_path
errors = os_arch_combinations.flat_map do |os, arch|
next [] if os == :linux
SimulateSystem.with(os:, arch:) do
odebug "Auditing Cask #{cask} on os #{os} and arch #{arch}"
Cask::Auditor.audit(
Cask::CaskLoader.load(path),
# For switches, we add `|| nil` so that `nil` will be passed
# instead of `false` if they aren't set.
# This way, we can distinguish between "not set" and "set to false".
audit_online: args.online? || nil,
audit_strict: args.strict? || nil,
# No need for `|| nil` for `--[no-]signing`
# because boolean switches are already `nil` if not passed
audit_signing: args.signing?,
audit_new_cask: new_cask || nil,
audit_token_conflicts: args.token_conflicts? || nil,
quarantine: true,
any_named_args: !no_named_args,
only: args.only,
except: args.except,
).to_a
annotations.each do |annotation|
puts annotation if annotation.relevant?
end
end.uniq
problems[[cask.full_name, path]] = errors if errors.any?
end
print_problems(tap_problems, display_filename: args.display_filename?)
print_problems(formula_problems, display_filename: args.display_filename?)
print_problems(cask_problems, display_filename: args.display_filename?)
tap_count = tap_problems.keys.count
formula_count = formula_problems.keys.count
cask_count = cask_problems.keys.count
corrected_problem_count = (formula_problems.values + cask_problems.values)
.sum { |problems| problems.count { |problem| problem.fetch(:corrected) } }
tap_problem_count = tap_problems.sum { |_, problems| problems.count }
formula_problem_count = formula_problems.sum { |_, problems| problems.count }
cask_problem_count = cask_problems.sum { |_, problems| problems.count }
total_problems_count = formula_problem_count + cask_problem_count + tap_problem_count
if total_problems_count.positive?
errors_summary = Utils.pluralize("problem", total_problems_count, include_count: true)
error_sources = []
if formula_count.positive?
error_sources << Utils.pluralize("formula", formula_count, plural: "e", include_count: true)
end
error_sources << Utils.pluralize("cask", cask_count, include_count: true) if cask_count.positive?
error_sources << Utils.pluralize("tap", tap_count, include_count: true) if tap_count.positive?
errors_summary += " in #{error_sources.to_sentence}" if error_sources.any?
errors_summary += " detected"
if corrected_problem_count.positive?
errors_summary += ", #{Utils.pluralize("problem", corrected_problem_count, include_count: true)} corrected"
end
ofail "#{errors_summary}."
end
private
return unless ENV["GITHUB_ACTIONS"]
def print_problems(results)
results.each do |(name, path), problems|
problem_lines = format_problem_lines(problems)
annotations = formula_problems.merge(cask_problems).flat_map do |(_, path), problems|
problems.map do |problem|
GitHub::Actions::Annotation.new(
:error,
problem[:message],
file: path,
line: problem[:location]&.line,
column: problem[:location]&.column,
)
end
end.compact
annotations.each do |annotation|
puts annotation if annotation.relevant?
end
end
def self.print_problems(results, display_filename:)
results.each do |(name, path), problems|
problem_lines = format_problem_lines(problems)
if display_filename
problem_lines.each do |l|
puts "#{path}: #{l}"
if args.display_filename?
problem_lines.each do |l|
puts "#{path}: #{l}"
end
else
puts name, problem_lines.map { |l| l.dup.prepend(" ") }
end
end
else
puts name, problem_lines.map { |l| l.dup.prepend(" ") }
end
end
end
def self.format_problem_lines(problems)
problems.map do |problem|
status = " #{Formatter.success("[corrected]")}" if problem.fetch(:corrected)
location = problem.fetch(:location)
location = "#{location.line&.to_s&.prepend("line ")}#{location.column&.to_s&.prepend(", col ")}: " if location
message = problem.fetch(:message)
"* #{location}#{message.chomp.gsub("\n", "\n ")}#{status}"
def format_problem_lines(problems)
problems.map do |problem|
status = " #{Formatter.success("[corrected]")}" if problem.fetch(:corrected)
location = problem.fetch(:location)
if location
location = "#{location.line&.to_s&.prepend("line ")}#{location.column&.to_s&.prepend(", col ")}: "
end
message = problem.fetch(:message)
"* #{location}#{message.chomp.gsub("\n", "\n ")}#{status}"
end
end
end
end
end

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
# typed: strict
# frozen_string_literal: true
require "abstract_command"
require "bump_version_parser"
require "cask"
require "cask/download"
@ -8,320 +9,319 @@ require "cli/parser"
require "utils/tar"
module Homebrew
module_function
module DevCmd
class BumpCaskPr < AbstractCommand
cmd_args do
description <<~EOS
Create a pull request to update <cask> with a new version.
sig { returns(CLI::Parser) }
def bump_cask_pr_args
Homebrew::CLI::Parser.new do
description <<~EOS
Create a pull request to update <cask> with a new version.
A best effort to determine the <SHA-256> will be made if the value is not
supplied by the user.
EOS
switch "-n", "--dry-run",
description: "Print what would be done rather than doing it."
switch "--write-only",
description: "Make the expected file modifications without taking any Git actions."
switch "--commit",
depends_on: "--write-only",
description: "When passed with `--write-only`, generate a new commit after writing changes " \
"to the cask file."
switch "--no-audit",
description: "Don't run `brew audit` before opening the PR."
switch "--online",
hidden: true
switch "--no-style",
description: "Don't run `brew style --fix` before opening the PR."
switch "--no-browse",
description: "Print the pull request URL instead of opening in a browser."
switch "--no-fork",
description: "Don't try to fork the repository."
flag "--version=",
description: "Specify the new <version> for the cask."
flag "--version-arm=",
description: "Specify the new cask <version> for the ARM architecture."
flag "--version-intel=",
description: "Specify the new cask <version> for the Intel architecture."
flag "--message=",
description: "Prepend <message> to the default pull request message."
flag "--url=",
description: "Specify the <URL> for the new download."
flag "--sha256=",
description: "Specify the <SHA-256> checksum of the new download."
flag "--fork-org=",
description: "Use the specified GitHub organization for forking."
switch "-f", "--force",
hidden: true
A best effort to determine the <SHA-256> will be made if the value is not
supplied by the user.
EOS
switch "-n", "--dry-run",
description: "Print what would be done rather than doing it."
switch "--write-only",
description: "Make the expected file modifications without taking any Git actions."
switch "--commit",
depends_on: "--write-only",
description: "When passed with `--write-only`, generate a new commit after writing changes " \
"to the cask file."
switch "--no-audit",
description: "Don't run `brew audit` before opening the PR."
switch "--online",
hidden: true
switch "--no-style",
description: "Don't run `brew style --fix` before opening the PR."
switch "--no-browse",
description: "Print the pull request URL instead of opening in a browser."
switch "--no-fork",
description: "Don't try to fork the repository."
flag "--version=",
description: "Specify the new <version> for the cask."
flag "--version-arm=",
description: "Specify the new cask <version> for the ARM architecture."
flag "--version-intel=",
description: "Specify the new cask <version> for the Intel architecture."
flag "--message=",
description: "Prepend <message> to the default pull request message."
flag "--url=",
description: "Specify the <URL> for the new download."
flag "--sha256=",
description: "Specify the <SHA-256> checksum of the new download."
flag "--fork-org=",
description: "Use the specified GitHub organization for forking."
switch "-f", "--force",
hidden: true
conflicts "--dry-run", "--write"
conflicts "--no-audit", "--online"
conflicts "--version=", "--version-arm="
conflicts "--version=", "--version-intel="
conflicts "--dry-run", "--write"
conflicts "--no-audit", "--online"
conflicts "--version=", "--version-arm="
conflicts "--version=", "--version-intel="
named_args :cask, number: 1, without_api: true
end
end
sig { void }
def bump_cask_pr
args = bump_cask_pr_args.parse
odeprecated "brew bump-cask-pr --online" if args.online?
odisabled "brew bump-cask-pr --force" if args.force?
# This will be run by `brew audit` or `brew style` later so run it first to
# not start spamming during normal output.
gem_groups = []
gem_groups << "style" if !args.no_audit? || !args.no_style?
gem_groups << "audit" unless args.no_audit?
Homebrew.install_bundler_gems!(groups: gem_groups) unless gem_groups.empty?
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
# Use the user's browser, too.
ENV["BROWSER"] = EnvConfig.browser
cask = args.named.to_casks.first
odie "This cask is not in a tap!" if cask.tap.blank?
odie "This cask's tap is not a Git repository!" unless cask.tap.git?
odie <<~EOS unless cask.tap.allow_bump?(cask.token)
Whoops, the #{cask.token} cask has its version update
pull requests automatically opened by BrewTestBot!
We'd still love your contributions, though, so try another one
that's not in the autobump list:
#{Formatter.url("#{cask.tap.remote}/blob/master/.github/autobump.txt")}
EOS
new_version = BumpVersionParser.new(
general: args.version,
intel: args.version_intel,
arm: args.version_arm,
)
new_hash = unless (new_hash = args.sha256).nil?
raise UsageError, "`--sha256` must not be empty." if new_hash.blank?
["no_check", ":no_check"].include?(new_hash) ? :no_check : new_hash
end
new_base_url = unless (new_base_url = args.url).nil?
raise UsageError, "`--url` must not be empty." if new_base_url.blank?
begin
URI(new_base_url)
rescue URI::InvalidURIError
raise UsageError, "`--url` is not valid."
named_args :cask, number: 1, without_api: true
end
end
if new_version.blank? && new_base_url.nil? && new_hash.nil?
raise UsageError, "No `--version`, `--url` or `--sha256` argument specified!"
end
sig { override.void }
def run
odeprecated "brew bump-cask-pr --online" if args.online?
odisabled "brew bump-cask-pr --force" if args.force?
check_pull_requests(cask, args:, new_version:)
# This will be run by `brew audit` or `brew style` later so run it first to
# not start spamming during normal output.
gem_groups = []
gem_groups << "style" if !args.no_audit? || !args.no_style?
gem_groups << "audit" unless args.no_audit?
Homebrew.install_bundler_gems!(groups: gem_groups) unless gem_groups.empty?
replacement_pairs ||= []
branch_name = "bump-#{cask.token}"
commit_message = nil
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
old_contents = File.read(cask.sourcefile_path)
# Use the user's browser, too.
ENV["BROWSER"] = EnvConfig.browser
if new_base_url
commit_message ||= "#{cask.token}: update URL"
cask = args.named.to_casks.first
m = /^ +url "(.+?)"\n/m.match(old_contents)
odie "Could not find old URL in cask!" if m.nil?
odie "This cask is not in a tap!" if cask.tap.blank?
odie "This cask's tap is not a Git repository!" unless cask.tap.git?
old_base_url = m.captures.fetch(0)
odie <<~EOS unless cask.tap.allow_bump?(cask.token)
Whoops, the #{cask.token} cask has its version update
pull requests automatically opened by BrewTestBot!
We'd still love your contributions, though, so try another one
that's not in the autobump list:
#{Formatter.url("#{cask.tap.remote}/blob/master/.github/autobump.txt")}
EOS
replacement_pairs << [
/#{Regexp.escape(old_base_url)}/,
new_base_url.to_s,
]
end
new_version = BumpVersionParser.new(
general: args.version,
intel: args.version_intel,
arm: args.version_arm,
)
if new_version.present?
# For simplicity, our naming defers to the arm version if we multiple architectures are specified
branch_version = new_version.arm || new_version.general
if branch_version.is_a?(Cask::DSL::Version)
commit_version = shortened_version(branch_version, cask:)
branch_name = "bump-#{cask.token}-#{branch_version.tr(",:", "-")}"
commit_message ||= "#{cask.token} #{commit_version}"
end
replacement_pairs = replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
end
# Now that we have all replacement pairs, we will replace them further down
new_hash = unless (new_hash = args.sha256).nil?
raise UsageError, "`--sha256` must not be empty." if new_hash.blank?
commit_message ||= "#{cask.token}: update checksum" if new_hash
["no_check", ":no_check"].include?(new_hash) ? :no_check : new_hash
end
# Remove nested arrays where elements are identical
replacement_pairs = replacement_pairs.reject { |pair| pair[0] == pair[1] }.uniq.compact
Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
replacement_pairs,
read_only_run: args.dry_run?,
silent: args.quiet?)
new_base_url = unless (new_base_url = args.url).nil?
raise UsageError, "`--url` must not be empty." if new_base_url.blank?
run_cask_audit(cask, old_contents, args:)
run_cask_style(cask, old_contents, args:)
pr_info = {
branch_name:,
commit_message:,
old_contents:,
pr_message: "Created with `brew bump-cask-pr`.",
sourcefile_path: cask.sourcefile_path,
tap: cask.tap,
}
GitHub.create_bump_pr(pr_info, args:)
end
sig { params(version: Cask::DSL::Version, cask: Cask::Cask).returns(Cask::DSL::Version) }
def shortened_version(version, cask:)
if version.before_comma == cask.version.before_comma
version
else
version.before_comma
end
end
sig {
params(
cask: Cask::Cask,
new_hash: T.any(NilClass, String, Symbol),
new_version: BumpVersionParser,
replacement_pairs: T::Array[[T.any(Regexp, String), T.any(Regexp, String)]],
).returns(T::Array[[T.any(Regexp, String), T.any(Regexp, String)]])
}
def replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
# When blocks are absent, arch is not relevant. For consistency, we simulate the arm architecture.
arch_options = cask.on_system_blocks_exist? ? OnSystem::ARCH_OPTIONS : [:arm]
arch_options.each do |arch|
SimulateSystem.with(arch:) do
old_cask = Cask::CaskLoader.load(cask.sourcefile_path)
old_version = old_cask.version
bump_version = new_version.send(arch) || new_version.general
old_version_regex = old_version.latest? ? ":latest" : %Q(["']#{Regexp.escape(old_version.to_s)}["'])
replacement_pairs << [/version\s+#{old_version_regex}/m,
"version #{bump_version.latest? ? ":latest" : %Q("#{bump_version}")}"]
# We are replacing our version here so we can get the new hash
tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
replacement_pairs.uniq.compact,
read_only_run: true,
silent: true)
tmp_cask = Cask::CaskLoader.load(tmp_contents)
old_hash = tmp_cask.sha256
if tmp_cask.version.latest? || new_hash == :no_check
opoo "Ignoring specified `--sha256=` argument." if new_hash.is_a?(String)
replacement_pairs << [/"#{old_hash}"/, ":no_check"] if old_hash != :no_check
elsif old_hash == :no_check && new_hash != :no_check
replacement_pairs << [":no_check", "\"#{new_hash}\""] if new_hash.is_a?(String)
elsif new_hash && !cask.on_system_blocks_exist? && cask.languages.empty?
replacement_pairs << [old_hash.to_s, new_hash.to_s]
elsif old_hash != :no_check
opoo "Multiple checksum replacements required; ignoring specified `--sha256` argument." if new_hash
languages = if cask.languages.empty?
[nil]
else
cask.languages
begin
URI(new_base_url)
rescue URI::InvalidURIError
raise UsageError, "`--url` is not valid."
end
languages.each do |language|
new_cask = Cask::CaskLoader.load(tmp_contents)
new_cask.config = if language.blank?
tmp_cask.config
else
tmp_cask.config.merge(Cask::Config.new(explicit: { languages: [language] }))
end
download = Cask::Download.new(new_cask, quarantine: true).fetch(verify_download_integrity: false)
Utils::Tar.validate_file(download)
end
if new_cask.sha256.to_s != download.sha256
replacement_pairs << [new_cask.sha256.to_s,
download.sha256]
if new_version.blank? && new_base_url.nil? && new_hash.nil?
raise UsageError, "No `--version`, `--url` or `--sha256` argument specified!"
end
check_pull_requests(cask, new_version:)
replacement_pairs ||= []
branch_name = "bump-#{cask.token}"
commit_message = nil
old_contents = File.read(cask.sourcefile_path)
if new_base_url
commit_message ||= "#{cask.token}: update URL"
m = /^ +url "(.+?)"\n/m.match(old_contents)
odie "Could not find old URL in cask!" if m.nil?
old_base_url = m.captures.fetch(0)
replacement_pairs << [
/#{Regexp.escape(old_base_url)}/,
new_base_url.to_s,
]
end
if new_version.present?
# For simplicity, our naming defers to the arm version if we multiple architectures are specified
branch_version = new_version.arm || new_version.general
if branch_version.is_a?(Cask::DSL::Version)
commit_version = shortened_version(branch_version, cask:)
branch_name = "bump-#{cask.token}-#{branch_version.tr(",:", "-")}"
commit_message ||= "#{cask.token} #{commit_version}"
end
replacement_pairs = replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
end
# Now that we have all replacement pairs, we will replace them further down
commit_message ||= "#{cask.token}: update checksum" if new_hash
# Remove nested arrays where elements are identical
replacement_pairs = replacement_pairs.reject { |pair| pair[0] == pair[1] }.uniq.compact
Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
replacement_pairs,
read_only_run: args.dry_run?,
silent: args.quiet?)
run_cask_audit(cask, old_contents)
run_cask_style(cask, old_contents)
pr_info = {
branch_name:,
commit_message:,
old_contents:,
pr_message: "Created with `brew bump-cask-pr`.",
sourcefile_path: cask.sourcefile_path,
tap: cask.tap,
}
GitHub.create_bump_pr(pr_info, args:)
end
private
sig { params(version: Cask::DSL::Version, cask: Cask::Cask).returns(Cask::DSL::Version) }
def shortened_version(version, cask:)
if version.before_comma == cask.version.before_comma
version
else
version.before_comma
end
end
sig {
params(
cask: Cask::Cask,
new_hash: T.any(NilClass, String, Symbol),
new_version: BumpVersionParser,
replacement_pairs: T::Array[[T.any(Regexp, String), T.any(Regexp, String)]],
).returns(T::Array[[T.any(Regexp, String), T.any(Regexp, String)]])
}
def replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
# When blocks are absent, arch is not relevant. For consistency, we simulate the arm architecture.
arch_options = cask.on_system_blocks_exist? ? OnSystem::ARCH_OPTIONS : [:arm]
arch_options.each do |arch|
SimulateSystem.with(arch:) do
old_cask = Cask::CaskLoader.load(cask.sourcefile_path)
old_version = old_cask.version
bump_version = new_version.send(arch) || new_version.general
old_version_regex = old_version.latest? ? ":latest" : %Q(["']#{Regexp.escape(old_version.to_s)}["'])
replacement_pairs << [/version\s+#{old_version_regex}/m,
"version #{bump_version.latest? ? ":latest" : %Q("#{bump_version}")}"]
# We are replacing our version here so we can get the new hash
tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
replacement_pairs.uniq.compact,
read_only_run: true,
silent: true)
tmp_cask = Cask::CaskLoader.load(tmp_contents)
old_hash = tmp_cask.sha256
if tmp_cask.version.latest? || new_hash == :no_check
opoo "Ignoring specified `--sha256=` argument." if new_hash.is_a?(String)
replacement_pairs << [/"#{old_hash}"/, ":no_check"] if old_hash != :no_check
elsif old_hash == :no_check && new_hash != :no_check
replacement_pairs << [":no_check", "\"#{new_hash}\""] if new_hash.is_a?(String)
elsif new_hash && !cask.on_system_blocks_exist? && cask.languages.empty?
replacement_pairs << [old_hash.to_s, new_hash.to_s]
elsif old_hash != :no_check
opoo "Multiple checksum replacements required; ignoring specified `--sha256` argument." if new_hash
languages = if cask.languages.empty?
[nil]
else
cask.languages
end
languages.each do |language|
new_cask = Cask::CaskLoader.load(tmp_contents)
new_cask.config = if language.blank?
tmp_cask.config
else
tmp_cask.config.merge(Cask::Config.new(explicit: { languages: [language] }))
end
download = Cask::Download.new(new_cask, quarantine: true).fetch(verify_download_integrity: false)
Utils::Tar.validate_file(download)
if new_cask.sha256.to_s != download.sha256
replacement_pairs << [new_cask.sha256.to_s,
download.sha256]
end
end
end
end
end
replacement_pairs
end
sig { params(cask: Cask::Cask, new_version: BumpVersionParser).void }
def check_pull_requests(cask, new_version:)
tap_remote_repo = cask.tap.full_name || cask.tap.remote_repo
GitHub.check_for_duplicate_pull_requests(cask.token, tap_remote_repo,
state: "open",
version: nil,
file: cask.sourcefile_path.relative_path_from(cask.tap.path).to_s,
quiet: args.quiet?)
# if we haven't already found open requests, try for an exact match across closed requests
new_version.instance_variables.each do |version_type|
version = new_version.instance_variable_get(version_type)
next if version.blank?
GitHub.check_for_duplicate_pull_requests(
cask.token,
tap_remote_repo,
state: "closed",
version: shortened_version(version, cask:),
file: cask.sourcefile_path.relative_path_from(cask.tap.path).to_s,
quiet: args.quiet?,
)
end
end
sig { params(cask: Cask::Cask, old_contents: String).void }
def run_cask_audit(cask, old_contents)
if args.dry_run?
if args.no_audit?
ohai "Skipping `brew audit`"
else
ohai "brew audit --cask --online #{cask.full_name}"
end
return
end
failed_audit = false
if args.no_audit?
ohai "Skipping `brew audit`"
else
system HOMEBREW_BREW_FILE, "audit", "--cask", "--online", cask.full_name
failed_audit = !$CHILD_STATUS.success?
end
return unless failed_audit
cask.sourcefile_path.atomic_write(old_contents)
odie "`brew audit` failed!"
end
sig { params(cask: Cask::Cask, old_contents: String).void }
def run_cask_style(cask, old_contents)
if args.dry_run?
if args.no_style?
ohai "Skipping `brew style --fix`"
else
ohai "brew style --fix #{cask.sourcefile_path.basename}"
end
return
end
failed_style = false
if args.no_style?
ohai "Skipping `brew style --fix`"
else
system HOMEBREW_BREW_FILE, "style", "--fix", cask.sourcefile_path
failed_style = !$CHILD_STATUS.success?
end
return unless failed_style
cask.sourcefile_path.atomic_write(old_contents)
odie "`brew style --fix` failed!"
end
end
replacement_pairs
end
sig { params(cask: Cask::Cask, args: CLI::Args, new_version: BumpVersionParser).void }
def check_pull_requests(cask, args:, new_version:)
tap_remote_repo = cask.tap.full_name || cask.tap.remote_repo
GitHub.check_for_duplicate_pull_requests(cask.token, tap_remote_repo,
state: "open",
version: nil,
file: cask.sourcefile_path.relative_path_from(cask.tap.path).to_s,
quiet: args.quiet?)
# if we haven't already found open requests, try for an exact match across closed requests
new_version.instance_variables.each do |version_type|
version = new_version.instance_variable_get(version_type)
next if version.blank?
GitHub.check_for_duplicate_pull_requests(
cask.token,
tap_remote_repo,
state: "closed",
version: shortened_version(version, cask:),
file: cask.sourcefile_path.relative_path_from(cask.tap.path).to_s,
quiet: args.quiet?,
)
end
end
sig { params(cask: Cask::Cask, old_contents: String, args: T.untyped).void }
def run_cask_audit(cask, old_contents, args:)
if args.dry_run?
if args.no_audit?
ohai "Skipping `brew audit`"
else
ohai "brew audit --cask --online #{cask.full_name}"
end
return
end
failed_audit = false
if args.no_audit?
ohai "Skipping `brew audit`"
else
system HOMEBREW_BREW_FILE, "audit", "--cask", "--online", cask.full_name
failed_audit = !$CHILD_STATUS.success?
end
return unless failed_audit
cask.sourcefile_path.atomic_write(old_contents)
odie "`brew audit` failed!"
end
sig { params(cask: Cask::Cask, old_contents: String, args: T.untyped).void }
def run_cask_style(cask, old_contents, args:)
if args.dry_run?
if args.no_style?
ohai "Skipping `brew style --fix`"
else
ohai "brew style --fix #{cask.sourcefile_path.basename}"
end
return
end
failed_style = false
if args.no_style?
ohai "Skipping `brew style --fix`"
else
system HOMEBREW_BREW_FILE, "style", "--fix", cask.sourcefile_path
failed_style = !$CHILD_STATUS.success?
end
return unless failed_style
cask.sourcefile_path.atomic_write(old_contents)
odie "`brew style --fix` failed!"
end
end

File diff suppressed because it is too large Load Diff

View File

@ -1,76 +1,75 @@
# typed: true
# typed: strict
# frozen_string_literal: true
require "abstract_command"
require "formula"
require "cli/parser"
module Homebrew
module_function
module DevCmd
class BumpRevision < AbstractCommand
cmd_args do
description <<~EOS
Create a commit to increment the revision of <formula>. If no revision is
present, "revision 1" will be added.
EOS
switch "-n", "--dry-run",
description: "Print what would be done rather than doing it."
switch "--remove-bottle-block",
description: "Remove the bottle block in addition to bumping the revision."
switch "--write-only",
description: "Make the expected file modifications without taking any Git actions."
flag "--message=",
description: "Append <message> to the default commit message."
sig { returns(CLI::Parser) }
def bump_revision_args
Homebrew::CLI::Parser.new do
description <<~EOS
Create a commit to increment the revision of <formula>. If no revision is
present, "revision 1" will be added.
EOS
switch "-n", "--dry-run",
description: "Print what would be done rather than doing it."
switch "--remove-bottle-block",
description: "Remove the bottle block in addition to bumping the revision."
switch "--write-only",
description: "Make the expected file modifications without taking any Git actions."
flag "--message=",
description: "Append <message> to the default commit message."
conflicts "--dry-run", "--write-only"
conflicts "--dry-run", "--write-only"
named_args :formula, min: 1, without_api: true
end
end
def bump_revision
args = bump_revision_args.parse
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
args.named.to_formulae.each do |formula|
current_revision = formula.revision
new_revision = current_revision + 1
if args.dry_run?
unless args.quiet?
old_text = "revision #{current_revision}"
new_text = "revision #{new_revision}"
if current_revision.zero?
ohai "add #{new_text.inspect}"
else
ohai "replace #{old_text.inspect} with #{new_text.inspect}"
end
end
else
Homebrew.install_bundler_gems!(groups: ["ast"])
require "utils/ast"
formula_ast = Utils::AST::FormulaAST.new(formula.path.read)
if current_revision.zero?
formula_ast.add_stanza(:revision, new_revision)
else
formula_ast.replace_stanza(:revision, new_revision)
end
formula_ast.remove_stanza(:bottle) if args.remove_bottle_block?
formula.path.atomic_write(formula_ast.process)
named_args :formula, min: 1, without_api: true
end
message = "#{formula.name}: revision bump #{args.message}"
if args.dry_run?
ohai "git commit --no-edit --verbose --message=#{message} -- #{formula.path}"
elsif !args.write_only?
formula.path.parent.cd do
safe_system "git", "commit", "--no-edit", "--verbose",
"--message=#{message}", "--", formula.path
sig { override.void }
def run
# As this command is simplifying user-run commands then let's just use a
# user path, too.
ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s
args.named.to_formulae.each do |formula|
current_revision = formula.revision
new_revision = current_revision + 1
if args.dry_run?
unless args.quiet?
old_text = "revision #{current_revision}"
new_text = "revision #{new_revision}"
if current_revision.zero?
ohai "add #{new_text.inspect}"
else
ohai "replace #{old_text.inspect} with #{new_text.inspect}"
end
end
else
Homebrew.install_bundler_gems!(groups: ["ast"])
require "utils/ast"
formula_ast = Utils::AST::FormulaAST.new(formula.path.read)
if current_revision.zero?
formula_ast.add_stanza(:revision, new_revision)
else
formula_ast.replace_stanza(:revision, new_revision)
end
formula_ast.remove_stanza(:bottle) if args.remove_bottle_block?
formula.path.atomic_write(formula_ast.process)
end
message = "#{formula.name}: revision bump #{args.message}"
if args.dry_run?
ohai "git commit --no-edit --verbose --message=#{message} -- #{formula.path}"
elsif !args.write_only?
formula.path.parent.cd do
safe_system "git", "commit", "--no-edit", "--verbose",
"--message=#{message}", "--", formula.path
end
end
end
end
end

View File

@ -1,4 +1,4 @@
# typed: true
# typed: strict
# frozen_string_literal: true
require "timeout"
@ -11,167 +11,168 @@ require "tap"
require "unversioned_cask_checker"
module Homebrew
extend SystemCommand::Mixin
module DevCmd
class BumpUnversionedCask < AbstractCommand
include SystemCommand::Mixin
sig { returns(CLI::Parser) }
def self.bump_unversioned_casks_args
Homebrew::CLI::Parser.new do
description <<~EOS
Check all casks with unversioned URLs in a given <tap> for updates.
EOS
switch "-n", "--dry-run",
description: "Do everything except caching state and opening pull requests."
flag "--limit=",
description: "Maximum runtime in minutes."
flag "--state-file=",
description: "File for caching state."
cmd_args do
description <<~EOS
Check all casks with unversioned URLs in a given <tap> for updates.
EOS
switch "-n", "--dry-run",
description: "Do everything except caching state and opening pull requests."
flag "--limit=",
description: "Maximum runtime in minutes."
flag "--state-file=",
description: "File for caching state."
named_args [:cask, :tap], min: 1, without_api: true
end
end
sig { void }
def self.bump_unversioned_casks
args = bump_unversioned_casks_args.parse
Homebrew.install_bundler_gems!(groups: ["bump_unversioned_casks"])
state_file = if args.state_file.present?
Pathname(args.state_file).expand_path
else
HOMEBREW_CACHE/"bump_unversioned_casks.json"
end
state_file.dirname.mkpath
state = state_file.exist? ? JSON.parse(state_file.read) : {}
casks = args.named.to_paths(only: :cask, recurse_tap: true).map { |path| Cask::CaskLoader.load(path) }
unversioned_casks = casks.select do |cask|
cask.url&.unversioned? && !cask.livecheckable? && !cask.discontinued?
end
ohai "Unversioned Casks: #{unversioned_casks.count} (#{state.size} cached)"
checked, unchecked = unversioned_casks.partition { |c| state.key?(c.full_name) }
queue = Queue.new
# Start with random casks which have not been checked.
unchecked.shuffle.each do |c|
queue.enq c
end
# Continue with previously checked casks, ordered by when they were last checked.
checked.sort_by { |c| state.dig(c.full_name, "check_time") }.each do |c|
queue.enq c
end
limit = args.limit.presence&.to_i
end_time = Time.now + (limit * 60) if limit
until queue.empty? || (end_time && end_time < Time.now)
cask = queue.deq
key = cask.full_name
new_state = bump_unversioned_cask(cask, state: state.fetch(key, {}), dry_run: args.dry_run?)
next unless new_state
state[key] = new_state
state_file.atomic_write JSON.pretty_generate(state) unless args.dry_run?
end
end
sig {
params(cask: Cask::Cask, state: T::Hash[String, T.untyped], dry_run: T.nilable(T::Boolean))
.returns(T.nilable(T::Hash[String, T.untyped]))
}
def self.bump_unversioned_cask(cask, state:, dry_run:)
ohai "Checking #{cask.full_name}"
unversioned_cask_checker = UnversionedCaskChecker.new(cask)
if !unversioned_cask_checker.single_app_cask? &&
!unversioned_cask_checker.single_pkg_cask? &&
!unversioned_cask_checker.single_qlplugin_cask?
opoo "Skipping, not a single-app or PKG cask."
return
end
last_check_time = state["check_time"]&.then { |t| Time.parse(t) }
check_time = Time.now
if last_check_time && (check_time - last_check_time) / 3600 < 24
opoo "Skipping, already checked within the last 24 hours."
return
end
last_sha256 = state["sha256"]
last_time = state["time"]&.then { |t| Time.parse(t) }
last_file_size = state["file_size"]
download = Cask::Download.new(cask)
time, file_size = begin
download.time_file_size
rescue
[nil, nil]
end
if last_time != time || last_file_size != file_size
sha256 = begin
Timeout.timeout(5 * 60) do
unversioned_cask_checker.installer.download.sha256
end
rescue => e
onoe e
named_args [:cask, :tap], min: 1, without_api: true
end
if sha256.present? && last_sha256 != sha256
version = begin
Timeout.timeout(60) do
unversioned_cask_checker.guess_cask_version
end
rescue Timeout::Error
onoe "Timed out guessing version for cask '#{cask}'."
sig { override.void }
def run
Homebrew.install_bundler_gems!(groups: ["bump_unversioned_casks"])
state_file = if args.state_file.present?
Pathname(T.must(args.state_file)).expand_path
else
HOMEBREW_CACHE/"bump_unversioned_casks.json"
end
state_file.dirname.mkpath
state = state_file.exist? ? JSON.parse(state_file.read) : {}
casks = args.named.to_paths(only: :cask, recurse_tap: true).map { |path| Cask::CaskLoader.load(path) }
unversioned_casks = casks.select do |cask|
cask.url&.unversioned? && !cask.livecheckable? && !cask.discontinued?
end
if version
if cask.version == version
oh1 "Cask #{cask} is up-to-date at #{version}"
else
bump_cask_pr_args = [
"bump-cask-pr",
"--version", version.to_s,
"--sha256", ":no_check",
"--message", "Automatic update via `brew bump-unversioned-casks`.",
cask.sourcefile_path
]
ohai "Unversioned Casks: #{unversioned_casks.count} (#{state.size} cached)"
if dry_run
bump_cask_pr_args << "--dry-run"
oh1 "Would bump #{cask} from #{cask.version} to #{version}"
else
oh1 "Bumping #{cask} from #{cask.version} to #{version}"
checked, unchecked = unversioned_casks.partition { |c| state.key?(c.full_name) }
queue = Queue.new
# Start with random casks which have not been checked.
unchecked.shuffle.each do |c|
queue.enq c
end
# Continue with previously checked casks, ordered by when they were last checked.
checked.sort_by { |c| state.dig(c.full_name, "check_time") }.each do |c|
queue.enq c
end
limit = args.limit.presence&.to_i
end_time = Time.now + (limit * 60) if limit
until queue.empty? || (end_time && end_time < Time.now)
cask = queue.deq
key = cask.full_name
new_state = bump_unversioned_cask(cask, state: state.fetch(key, {}))
next unless new_state
state[key] = new_state
state_file.atomic_write JSON.pretty_generate(state) unless args.dry_run?
end
end
private
sig {
params(cask: Cask::Cask, state: T::Hash[String, T.untyped])
.returns(T.nilable(T::Hash[String, T.untyped]))
}
def bump_unversioned_cask(cask, state:)
ohai "Checking #{cask.full_name}"
unversioned_cask_checker = UnversionedCaskChecker.new(cask)
if !unversioned_cask_checker.single_app_cask? &&
!unversioned_cask_checker.single_pkg_cask? &&
!unversioned_cask_checker.single_qlplugin_cask?
opoo "Skipping, not a single-app or PKG cask."
return
end
last_check_time = state["check_time"]&.then { |t| Time.parse(t) }
check_time = Time.now
if last_check_time && (check_time - last_check_time) / 3600 < 24
opoo "Skipping, already checked within the last 24 hours."
return
end
last_sha256 = state["sha256"]
last_time = state["time"]&.then { |t| Time.parse(t) }
last_file_size = state["file_size"]
download = Cask::Download.new(cask)
time, file_size = begin
download.time_file_size
rescue
[nil, nil]
end
if last_time != time || last_file_size != file_size
sha256 = begin
Timeout.timeout(5 * 60) do
unversioned_cask_checker.installer.download.sha256
end
rescue => e
onoe e
end
if sha256.present? && last_sha256 != sha256
version = begin
Timeout.timeout(60) do
unversioned_cask_checker.guess_cask_version
end
rescue Timeout::Error
onoe "Timed out guessing version for cask '#{cask}'."
end
begin
system_command! HOMEBREW_BREW_FILE, args: bump_cask_pr_args
rescue ErrorDuringExecution => e
onoe e
if version
if cask.version == version
oh1 "Cask #{cask} is up-to-date at #{version}"
else
bump_cask_pr_args = [
"bump-cask-pr",
"--version", version.to_s,
"--sha256", ":no_check",
"--message", "Automatic update via `brew bump-unversioned-casks`.",
cask.sourcefile_path
]
if args.dry_run?
bump_cask_pr_args << "--dry-run"
oh1 "Would bump #{cask} from #{cask.version} to #{version}"
else
oh1 "Bumping #{cask} from #{cask.version} to #{version}"
end
begin
system_command! HOMEBREW_BREW_FILE, args: bump_cask_pr_args
rescue ErrorDuringExecution => e
onoe e
end
end
end
end
end
{
"sha256" => sha256,
"check_time" => check_time.iso8601,
"time" => time&.iso8601,
"file_size" => file_size,
}
end
end
{
"sha256" => sha256,
"check_time" => check_time.iso8601,
"time" => time&.iso8601,
"file_size" => file_size,
}
end
end

File diff suppressed because it is too large Load Diff

View File

@ -1,62 +1,65 @@
# typed: true
# typed: strict
# frozen_string_literal: true
require "abstract_command"
require "cli/parser"
module Homebrew
sig { returns(CLI::Parser) }
def self.cat_args
Homebrew::CLI::Parser.new do
description <<~EOS
Display the source of a <formula> or <cask>.
EOS
module DevCmd
class Cat < AbstractCommand
include FileUtils
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
cmd_args do
description <<~EOS
Display the source of a <formula> or <cask>.
EOS
conflicts "--formula", "--cask"
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
named_args [:formula, :cask], min: 1, without_api: true
end
end
conflicts "--formula", "--cask"
def self.cat
args = cat_args.parse
cd HOMEBREW_REPOSITORY do
pager = if Homebrew::EnvConfig.bat?
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme
ensure_formula_installed!(
"bat",
reason: "displaying <formula>/<cask> source",
# The user might want to capture the output of `brew cat ...`
# Redirect stdout to stderr
output_to_stderr: true,
).opt_bin/"bat"
else
"cat"
named_args [:formula, :cask], min: 1, without_api: true
end
args.named.to_paths.each do |path|
next path if path.exist?
sig { override.void }
def run
cd HOMEBREW_REPOSITORY do
pager = if Homebrew::EnvConfig.bat?
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme
ensure_formula_installed!(
"bat",
reason: "displaying <formula>/<cask> source",
# The user might want to capture the output of `brew cat ...`
# Redirect stdout to stderr
output_to_stderr: true,
).opt_bin/"bat"
else
"cat"
end
path = path.basename(".rb") if args.cask?
args.named.to_paths.each do |path|
next path if path.exist?
ofail "#{path}'s source doesn't exist on disk."
path = path.basename(".rb") if args.cask?
ofail "#{path}'s source doesn't exist on disk."
end
if Homebrew.failed?
$stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:"
treat_as = "--cask " if args.cask?
treat_as = "--formula " if args.formula?
$stderr.puts " brew info --github #{treat_as}#{args.named.join(" ")}"
return
end
safe_system pager, *args.named.to_paths
end
end
if Homebrew.failed?
$stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:"
treat_as = "--cask " if args.cask?
treat_as = "--formula " if args.formula?
$stderr.puts " brew info --github #{treat_as}#{args.named.join(" ")}"
return
end
safe_system pager, *args.named.to_paths
end
end
end

View File

@ -1,30 +1,29 @@
# typed: true
# typed: strict
# frozen_string_literal: true
require "abstract_command"
require "commands"
require "cli/parser"
module Homebrew
module_function
module DevCmd
class Command < AbstractCommand
cmd_args do
description <<~EOS
Display the path to the file being used when invoking `brew` <cmd>.
EOS
sig { returns(CLI::Parser) }
def command_args
Homebrew::CLI::Parser.new do
description <<~EOS
Display the path to the file being used when invoking `brew` <cmd>.
EOS
named_args :command, min: 1
end
named_args :command, min: 1
end
end
def command
args = command_args.parse
args.named.each do |cmd|
path = Commands.path(cmd)
odie "Unknown command: #{cmd}" unless path
puts path
sig { override.void }
def run
args.named.each do |cmd|
path = Commands.path(cmd)
odie "Unknown command: #{cmd}" unless path
puts path
end
end
end
end
end

View File

@ -5,209 +5,209 @@ require "cli/parser"
require "csv"
module Homebrew
module_function
module DevCmd
class Contributions < AbstractCommand
PRIMARY_REPOS = %w[brew core cask].freeze
SUPPORTED_REPOS = [
PRIMARY_REPOS,
OFFICIAL_CMD_TAPS.keys.map { |t| t.delete_prefix("homebrew/") },
OFFICIAL_CASK_TAPS.reject { |t| t == "cask" },
].flatten.freeze
MAX_REPO_COMMITS = 1000
PRIMARY_REPOS = %w[brew core cask].freeze
SUPPORTED_REPOS = [
PRIMARY_REPOS,
OFFICIAL_CMD_TAPS.keys.map { |t| t.delete_prefix("homebrew/") },
OFFICIAL_CASK_TAPS.reject { |t| t == "cask" },
].flatten.freeze
MAX_REPO_COMMITS = 1000
cmd_args do
usage_banner "`contributions` [--user=<email|username>] [<--repositories>`=`] [<--csv>]"
description <<~EOS
Summarise contributions to Homebrew repositories.
EOS
sig { returns(CLI::Parser) }
def contributions_args
Homebrew::CLI::Parser.new do
usage_banner "`contributions` [--user=<email|username>] [<--repositories>`=`] [<--csv>]"
description <<~EOS
Summarise contributions to Homebrew repositories.
EOS
comma_array "--repositories",
description: "Specify a comma-separated list of repositories to search. " \
"Supported repositories: #{SUPPORTED_REPOS.map { |t| "`#{t}`" }.to_sentence}. " \
"Omitting this flag, or specifying `--repositories=primary`, searches only the " \
"main repositories: brew,core,cask. " \
"Specifying `--repositories=all`, searches all repositories. "
flag "--from=",
description: "Date (ISO-8601 format) to start searching contributions. " \
"Omitting this flag searches the last year."
comma_array "--repositories",
description: "Specify a comma-separated list of repositories to search. " \
"Supported repositories: #{SUPPORTED_REPOS.map { |t| "`#{t}`" }.to_sentence}. " \
"Omitting this flag, or specifying `--repositories=primary`, searches only the " \
"main repositories: brew,core,cask. " \
"Specifying `--repositories=all`, searches all repositories. "
flag "--from=",
description: "Date (ISO-8601 format) to start searching contributions. " \
"Omitting this flag searches the last year."
flag "--to=",
description: "Date (ISO-8601 format) to stop searching contributions."
flag "--to=",
description: "Date (ISO-8601 format) to stop searching contributions."
comma_array "--user=",
description: "Specify a comma-separated list of GitHub usernames or email addresses to find " \
"contributions from. Omitting this flag searches maintainers."
comma_array "--user=",
description: "Specify a comma-separated list of GitHub usernames or email addresses to find " \
"contributions from. Omitting this flag searches maintainers."
switch "--csv",
description: "Print a CSV of contributions across repositories over the time period."
end
end
sig { void }
def contributions
args = contributions_args.parse
results = {}
grand_totals = {}
repos = if args.repositories.blank? || args.repositories.include?("primary")
PRIMARY_REPOS
elsif args.repositories.include?("all")
SUPPORTED_REPOS
else
args.repositories
end
from = args.from.presence || Date.today.prev_year.iso8601
contribution_types = [:author, :committer, :coauthorship, :review]
users = args.user.presence || GitHub.members_by_team("Homebrew", "maintainers").keys
users.each do |username|
# TODO: Using the GitHub username to scan the `git log` undercounts some
# contributions as people might not always have configured their Git
# committer details to match the ones on GitHub.
# TODO: Switch to using the GitHub APIs instead of `git log` if
# they ever support trailers.
results[username] = scan_repositories(repos, username, args, from:)
grand_totals[username] = total(results[username])
contributions = contribution_types.filter_map do |type|
type_count = grand_totals[username][type]
next if type_count.to_i.zero?
"#{Utils.pluralize("time", type_count, include_count: true)} (#{type})"
end
contributions << "#{Utils.pluralize("time", grand_totals[username].values.sum, include_count: true)} (total)"
puts [
"#{username} contributed",
*contributions.to_sentence,
"#{time_period(from:, to: args.to)}.",
].join(" ")
end
return unless args.csv?
puts
puts generate_csv(grand_totals)
end
sig { params(repo: String).returns(Pathname) }
def find_repo_path_for_repo(repo)
return HOMEBREW_REPOSITORY if repo == "brew"
Tap.fetch("homebrew", repo).path
end
sig { params(from: T.nilable(String), to: T.nilable(String)).returns(String) }
def time_period(from:, to:)
if from && to
"between #{from} and #{to}"
elsif from
"after #{from}"
elsif to
"before #{to}"
else
"in all time"
end
end
sig { params(totals: Hash).returns(String) }
def generate_csv(totals)
CSV.generate do |csv|
csv << %w[user repo author committer coauthorship review total]
totals.sort_by { |_, v| -v.values.sum }.each do |user, total|
csv << grand_total_row(user, total)
end
end
end
sig { params(user: String, grand_total: Hash).returns(Array) }
def grand_total_row(user, grand_total)
[
user,
"all",
grand_total[:author],
grand_total[:committer],
grand_total[:coauthorship],
grand_total[:review],
grand_total.values.sum,
]
end
def scan_repositories(repos, person, args, from:)
data = {}
repos.each do |repo|
if SUPPORTED_REPOS.exclude?(repo)
return ofail "Unsupported repository: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}."
switch "--csv",
description: "Print a CSV of contributions across repositories over the time period."
end
repo_path = find_repo_path_for_repo(repo)
tap = Tap.fetch("homebrew", repo)
unless repo_path.exist?
opoo "Repository #{repo} not yet tapped! Tapping it now..."
tap.install
sig { override.void }
def run
results = {}
grand_totals = {}
repos = if args.repositories.blank? || T.must(args.repositories).include?("primary")
PRIMARY_REPOS
elsif T.must(args.repositories).include?("all")
SUPPORTED_REPOS
else
args.repositories
end
from = args.from.presence || Date.today.prev_year.iso8601
contribution_types = [:author, :committer, :coauthorship, :review]
users = args.user.presence || GitHub.members_by_team("Homebrew", "maintainers").keys
users.each do |username|
# TODO: Using the GitHub username to scan the `git log` undercounts some
# contributions as people might not always have configured their Git
# committer details to match the ones on GitHub.
# TODO: Switch to using the GitHub APIs instead of `git log` if
# they ever support trailers.
results[username] = scan_repositories(repos, username, from:)
grand_totals[username] = total(results[username])
contributions = contribution_types.filter_map do |type|
type_count = grand_totals[username][type]
next if type_count.to_i.zero?
"#{Utils.pluralize("time", type_count, include_count: true)} (#{type})"
end
contributions <<
"#{Utils.pluralize("time", grand_totals[username].values.sum, include_count: true)} (total)"
puts [
"#{username} contributed",
*contributions.to_sentence,
"#{time_period(from:, to: args.to)}.",
].join(" ")
end
return unless args.csv?
puts
puts generate_csv(grand_totals)
end
repo_full_name = if repo == "brew"
"homebrew/brew"
else
tap.full_name
private
sig { params(repo: String).returns(Pathname) }
def find_repo_path_for_repo(repo)
return HOMEBREW_REPOSITORY if repo == "brew"
Tap.fetch("homebrew", repo).path
end
puts "Determining contributions for #{person} on #{repo_full_name}..." if args.verbose?
sig { params(from: T.nilable(String), to: T.nilable(String)).returns(String) }
def time_period(from:, to:)
if from && to
"between #{from} and #{to}"
elsif from
"after #{from}"
elsif to
"before #{to}"
else
"in all time"
end
end
author_commits, committer_commits = GitHub.count_repo_commits(repo_full_name, person, args,
max: MAX_REPO_COMMITS)
data[repo] = {
author: author_commits,
committer: committer_commits,
coauthorship: git_log_trailers_cmd(T.must(repo_path), person, "Co-authored-by", from:, to: args.to),
review: count_reviews(repo_full_name, person, args),
sig { params(totals: Hash).returns(String) }
def generate_csv(totals)
CSV.generate do |csv|
csv << %w[user repo author committer coauthorship review total]
totals.sort_by { |_, v| -v.values.sum }.each do |user, total|
csv << grand_total_row(user, total)
end
end
end
sig { params(user: String, grand_total: Hash).returns(Array) }
def grand_total_row(user, grand_total)
[
user,
"all",
grand_total[:author],
grand_total[:committer],
grand_total[:coauthorship],
grand_total[:review],
grand_total.values.sum,
]
end
def scan_repositories(repos, person, from:)
data = {}
repos.each do |repo|
if SUPPORTED_REPOS.exclude?(repo)
return ofail "Unsupported repository: #{repo}. Try one of #{SUPPORTED_REPOS.join(", ")}."
end
repo_path = find_repo_path_for_repo(repo)
tap = Tap.fetch("homebrew", repo)
unless repo_path.exist?
opoo "Repository #{repo} not yet tapped! Tapping it now..."
tap.install
end
repo_full_name = if repo == "brew"
"homebrew/brew"
else
tap.full_name
end
puts "Determining contributions for #{person} on #{repo_full_name}..." if args.verbose?
author_commits, committer_commits = GitHub.count_repo_commits(repo_full_name, person, args,
max: MAX_REPO_COMMITS)
data[repo] = {
author: author_commits,
committer: committer_commits,
coauthorship: git_log_trailers_cmd(T.must(repo_path), person, "Co-authored-by", from:, to: args.to),
review: count_reviews(repo_full_name, person),
}
end
data
end
sig { params(results: Hash).returns(Hash) }
def total(results)
totals = { author: 0, committer: 0, coauthorship: 0, review: 0 }
results.each_value do |counts|
counts.each do |kind, count|
totals[kind] += count
end
end
totals
end
sig {
params(repo_path: Pathname, person: String, trailer: String, from: T.nilable(String),
to: T.nilable(String)).returns(Integer)
}
end
def git_log_trailers_cmd(repo_path, person, trailer, from:, to:)
cmd = ["git", "-C", repo_path, "log", "--oneline"]
cmd << "--format='%(trailers:key=#{trailer}:)'"
cmd << "--before=#{to}" if to
cmd << "--after=#{from}" if from
data
end
Utils.safe_popen_read(*cmd).lines.count { |l| l.include?(person) }
end
sig { params(results: Hash).returns(Hash) }
def total(results)
totals = { author: 0, committer: 0, coauthorship: 0, review: 0 }
results.each_value do |counts|
counts.each do |kind, count|
totals[kind] += count
sig { params(repo_full_name: String, person: String).returns(Integer) }
def count_reviews(repo_full_name, person)
GitHub.count_issues("", is: "pr", repo: repo_full_name, reviewed_by: person, review: "approved", args:)
rescue GitHub::API::ValidationFailedError
if args.verbose?
onoe "Couldn't search GitHub for PRs by #{person}. Their profile might be private. Defaulting to 0."
end
0 # Users who have made their contributions private are not searchable to determine counts.
end
end
totals
end
sig {
params(repo_path: Pathname, person: String, trailer: String, from: T.nilable(String),
to: T.nilable(String)).returns(Integer)
}
def git_log_trailers_cmd(repo_path, person, trailer, from:, to:)
cmd = ["git", "-C", repo_path, "log", "--oneline"]
cmd << "--format='%(trailers:key=#{trailer}:)'"
cmd << "--before=#{to}" if to
cmd << "--after=#{from}" if from
Utils.safe_popen_read(*cmd).lines.count { |l| l.include?(person) }
end
sig { params(repo_full_name: String, person: String, args: Homebrew::CLI::Args).returns(Integer) }
def count_reviews(repo_full_name, person, args)
GitHub.count_issues("", is: "pr", repo: repo_full_name, reviewed_by: person, review: "approved", args:)
rescue GitHub::API::ValidationFailedError
if args.verbose?
onoe "Couldn't search GitHub for PRs by #{person}. Their profile might be private. Defaulting to 0."
end
0 # Users who have made their contributions private are not searchable to determine counts.
end
end

View File

@ -2,23 +2,25 @@
# frozen_string_literal: true
module Homebrew
class << self
undef tar_args
module DevCmd
class Bottle < AbstractCommand
undef tar_args
sig { returns(T::Array[String]) }
def tar_args
if MacOS.version >= :catalina
["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze
else
[].freeze
sig { returns(T::Array[String]) }
def tar_args
if MacOS.version >= :catalina
["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze
else
[].freeze
end
end
end
undef gnu_tar
undef gnu_tar
sig { params(gnu_tar_formula: Formula).returns(String) }
def gnu_tar(gnu_tar_formula)
"#{gnu_tar_formula.opt_bin}/gtar"
sig { params(gnu_tar_formula: Formula).returns(String) }
def gnu_tar(gnu_tar_formula)
"#{gnu_tar_formula.opt_bin}/gtar"
end
end
end
end

View File

@ -49,7 +49,8 @@ module Tapioca
end
else
root.create_path(Homebrew::CLI::Args) do |klass|
create_args_methods(klass, T.must(T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser))
parser = T.cast(constant, T.class_of(Homebrew::AbstractCommand)).parser
create_args_methods(klass, parser)
end
end
end

View File

@ -583,7 +583,7 @@ RSpec.describe Homebrew::CLI::Parser do
# commands for formulae and casks on Linux.
it "succeeds for developer commands" do
require "dev-cmd/cat"
args = Homebrew.cat_args.parse(["--cask", "cask_name"])
args = Homebrew::DevCmd::Cat.new(["--cask", "cask_name"]).args
expect(args.cask?).to be(true)
end
end

View File

@ -5,7 +5,7 @@ require "formulary"
require "cmd/shared_examples/args_parse"
require "utils/spdx"
RSpec.describe "brew audit" do
RSpec.describe Homebrew::DevCmd::Audit do
it_behaves_like "parseable arguments"
end

View File

@ -3,7 +3,7 @@
require "cmd/shared_examples/args_parse"
require "dev-cmd/bottle"
RSpec.describe "brew bottle" do
RSpec.describe Homebrew::DevCmd::Bottle do
def stub_hash(parameters)
<<~EOS
{
@ -30,7 +30,7 @@ RSpec.describe "brew bottle" do
EOS
end
it_behaves_like "parseable arguments"
it_behaves_like "parseable arguments", argv: ["foo"]
it "builds a bottle for the given Formula", :integration_test do
install_test_formula "testball", build_bottle: true
@ -308,8 +308,8 @@ RSpec.describe "brew bottle" do
end
end
describe Homebrew do
subject(:homebrew) { described_class }
describe "bottle_cmd" do
subject(:homebrew) { described_class.new(["foo"]) }
let(:hello_hash_big_sur) do
JSON.parse stub_hash(

View File

@ -3,6 +3,6 @@
require "cmd/shared_examples/args_parse"
require "dev-cmd/bump-cask-pr"
RSpec.describe "brew bump-cask-pr" do
it_behaves_like "parseable arguments"
RSpec.describe Homebrew::DevCmd::BumpCaskPr do
it_behaves_like "parseable arguments", argv: ["foo"]
end

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/bump-formula-pr"
RSpec.describe "brew bump-formula-pr" do
RSpec.describe Homebrew::DevCmd::BumpFormulaPr do
it_behaves_like "parseable arguments"
end

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/bump-revision"
RSpec.describe "brew bump-revision" do
it_behaves_like "parseable arguments"
RSpec.describe Homebrew::DevCmd::BumpRevision do
it_behaves_like "parseable arguments", argv: ["foo"]
end

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/bump-unversioned-casks"
RSpec.describe "brew bump-unversioned-casks" do
it_behaves_like "parseable arguments"
RSpec.describe Homebrew::DevCmd::BumpUnversionedCask do
it_behaves_like "parseable arguments", argv: ["foo"]
end

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/bump"
RSpec.describe "brew bump" do
RSpec.describe Homebrew::DevCmd::Bump do
it_behaves_like "parseable arguments"
describe "formula", :integration_test, :needs_homebrew_curl, :needs_network do

View File

@ -1,9 +1,10 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/cat"
RSpec.describe "brew cat" do
it_behaves_like "parseable arguments"
RSpec.describe Homebrew::DevCmd::Cat do
it_behaves_like "parseable arguments", argv: ["foo"]
it "prints the content of a given Formula", :integration_test do
formula_file = setup_test_formula "testball"

View File

@ -1,9 +1,10 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/command"
RSpec.describe "brew command" do
it_behaves_like "parseable arguments"
RSpec.describe Homebrew::DevCmd::Command do
it_behaves_like "parseable arguments", argv: ["foo"]
it "returns the file for a given command", :integration_test do
expect { brew "command", "info" }

View File

@ -1,7 +1,8 @@
# frozen_string_literal: true
require "cmd/shared_examples/args_parse"
require "dev-cmd/contributions"
RSpec.describe "brew contributions" do
RSpec.describe Homebrew::DevCmd::Contributions do
it_behaves_like "parseable arguments"
end