Add without_api specifier for CLI named args

This commit is contained in:
Bo Anderson 2023-06-19 03:57:52 +01:00
parent 1c081f379d
commit 68289f1165
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65
6 changed files with 129 additions and 99 deletions

View File

@ -176,4 +176,20 @@ module Homebrew
Tap.fetch(org, repo)
end
end
# @api private
sig { params(block: T.proc.returns(T.untyped)).returns(T.untyped) }
def self.with_no_api_env(&block)
return yield if Homebrew::EnvConfig.no_install_from_api?
with_env(HOMEBREW_NO_INSTALL_FROM_API: "1", HOMEBREW_AUTOMATICALLY_SET_NO_INSTALL_FROM_API: "1", &block)
end
# @api private
sig { params(condition: T::Boolean, block: T.proc.returns(T.untyped)).returns(T.untyped) }
def self.with_no_api_env_if_needed(condition, &block)
return yield unless condition
with_no_api_env(&block)
end
end

View File

@ -32,7 +32,7 @@ module Homebrew
self[:remaining] = remaining_args.freeze
end
def freeze_named_args!(named_args, cask_options:)
def freeze_named_args!(named_args, cask_options:, without_api:)
options = {}
options[:force_bottle] = true if self[:force_bottle?]
options[:override_spec] = :head if self[:HEAD?]
@ -41,6 +41,7 @@ module Homebrew
*named_args.freeze,
parent: self,
cask_options: cask_options,
without_api: without_api,
**options,
)
end

View File

@ -19,6 +19,7 @@ module Homebrew
force_bottle: T::Boolean,
flags: T::Array[String],
cask_options: T::Boolean,
without_api: T::Boolean,
).void
}
def initialize(
@ -27,7 +28,8 @@ module Homebrew
override_spec: T.unsafe(nil),
force_bottle: T.unsafe(nil),
flags: T.unsafe(nil),
cask_options: false
cask_options: false,
without_api: false
)
require "cask/cask"
require "cask/cask_loader"
@ -40,6 +42,7 @@ module Homebrew
@force_bottle = force_bottle
@flags = flags
@cask_options = cask_options
@without_api = without_api
@parent = parent
super(@args)
@ -112,6 +115,7 @@ module Homebrew
end
def load_formula_or_cask(name, only: nil, method: nil, warn: nil)
Homebrew.with_no_api_env_if_needed(@without_api) do
unreadable_error = nil
if only != :cask
@ -199,6 +203,7 @@ module Homebrew
raise FormulaOrCaskUnavailableError, name
end
end
private :load_formula_or_cask
def resolve_formula(name)

View File

@ -138,6 +138,7 @@ module Homebrew
@named_args_type = nil
@max_named_args = nil
@min_named_args = nil
@named_args_without_api = false
@description = nil
@usage_banner = nil
@hide_from_man_page = false
@ -346,7 +347,7 @@ module Homebrew
check_named_args(named_args)
end
@args.freeze_named_args!(named_args, cask_options: @cask_options)
@args.freeze_named_args!(named_args, cask_options: @cask_options, without_api: @named_args_without_api)
@args.freeze_remaining_args!(non_options.empty? ? remaining : [*remaining, "--", non_options])
@args.freeze_processed_options!(@processed_options)
@args.freeze
@ -396,9 +397,10 @@ module Homebrew
number: T.nilable(Integer),
min: T.nilable(Integer),
max: T.nilable(Integer),
without_api: T::Boolean,
).void
}
def named_args(type = nil, number: nil, min: nil, max: nil)
def named_args(type = nil, number: nil, min: nil, max: nil, without_api: false)
if number.present? && (min.present? || max.present?)
raise ArgumentError, "Do not specify both `number` and `min` or `max`"
end
@ -417,6 +419,8 @@ module Homebrew
@min_named_args = min
@max_named_args = max
end
@named_args_without_api = without_api
end
sig { void }

View File

@ -123,7 +123,7 @@ module Homebrew
ENV.activate_extensions!
ENV.setup_build_environment
audit_formulae, audit_casks = without_api do # audit requires full Ruby source
audit_formulae, audit_casks = with_no_api_env do # audit requires full Ruby source
if args.tap
Tap.fetch(args.tap).then do |tap|
[
@ -217,7 +217,7 @@ module Homebrew
# 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?
without_api(&audit_proc)
with_no_api_env(&audit_proc)
else
audit_proc.call
end
@ -347,10 +347,4 @@ module Homebrew
"* #{location}#{message.chomp.gsub("\n", "\n ")}#{status}"
end
end
def self.without_api(&block)
return yield if Homebrew::EnvConfig.no_install_from_api?
with_env(HOMEBREW_NO_INSTALL_FROM_API: "1", HOMEBREW_AUTOMATICALLY_SET_NO_INSTALL_FROM_API: "1", &block)
end
end

View File

@ -87,13 +87,19 @@ class FormulaOrCaskUnavailableError < RuntimeError
super()
@name = name
# Store the state of these envs at the time the exception is thrown.
# This is so we do the fuzzy search for "did you mean" etc under that same mode,
# in case the list of formulae are different.
@without_api = Homebrew::EnvConfig.no_install_from_api?
@auto_without_api = Homebrew::EnvConfig.automatically_set_no_install_from_api?
end
sig { returns(String) }
def did_you_mean
require "formula"
similar_formula_names = Formula.fuzzy_search(name)
similar_formula_names = Homebrew.with_no_api_env_if_needed(@without_api) { Formula.fuzzy_search(name) }
return "" if similar_formula_names.blank?
"Did you mean #{similar_formula_names.to_sentence two_words_connector: " or ", last_word_connector: " or "}?"
@ -101,7 +107,11 @@ class FormulaOrCaskUnavailableError < RuntimeError
sig { returns(String) }
def to_s
"No available formula or cask with the name \"#{name}\". #{did_you_mean}".strip
s = "No available formula or cask with the name \"#{name}\". #{did_you_mean}".strip
if @auto_without_api && !CoreTap.instance.installed?
s += "\nA full git tap clone is required to use this command on core packages."
end
s
end
end