Merge pull request #16029 from abitrolly/edit-simplify
dev-cmd/edit: Move path specific functions to Pathname
This commit is contained in:
commit
67be775fd9
@ -28,6 +28,59 @@ module Homebrew
|
||||
end
|
||||
end
|
||||
|
||||
sig { params(path: Pathname).returns(T::Boolean) }
|
||||
def core_formula_path?(path)
|
||||
path.fnmatch?("**/homebrew-core/Formula/**.rb", File::FNM_DOTMATCH)
|
||||
end
|
||||
|
||||
sig { params(path: Pathname).returns(T::Boolean) }
|
||||
def core_cask_path?(path)
|
||||
path.fnmatch?("**/homebrew-cask/Casks/**.rb", File::FNM_DOTMATCH)
|
||||
end
|
||||
|
||||
sig { params(path: Pathname).returns(T::Boolean) }
|
||||
def core_formula_tap?(path)
|
||||
path == CoreTap.instance.path
|
||||
end
|
||||
|
||||
sig { params(path: Pathname).returns(T::Boolean) }
|
||||
def core_cask_tap?(path)
|
||||
path == CoreCaskTap.instance.path
|
||||
end
|
||||
|
||||
sig { params(path: Pathname, cask: T::Boolean).returns(T.noreturn) }
|
||||
def raise_with_message!(path, cask)
|
||||
name = path.basename(".rb").to_s
|
||||
|
||||
if (tap_match = Regexp.new("#{HOMEBREW_TAP_DIR_REGEX.source}$").match(path.to_s))
|
||||
raise TapUnavailableError, CoreTap.instance.name if core_formula_tap?(path)
|
||||
raise TapUnavailableError, CoreCaskTap.instance.name if core_cask_tap?(path)
|
||||
|
||||
raise TapUnavailableError, "#{tap_match[:user]}/#{tap_match[:repo]}"
|
||||
elsif cask || core_cask_path?(path)
|
||||
if !CoreCaskTap.instance.installed? && Homebrew::API::Cask.all_casks.key?(name)
|
||||
command = "brew tap --force #{CoreCaskTap.instance.name}"
|
||||
action = "tap #{CoreCaskTap.instance.name}"
|
||||
else
|
||||
command = "brew create --cask --set-name #{name} $URL"
|
||||
action = "create a new cask"
|
||||
end
|
||||
elsif core_formula_path?(path) &&
|
||||
!CoreTap.instance.installed? &&
|
||||
Homebrew::API::Formula.all_formulae.key?(name)
|
||||
command = "brew tap --force #{CoreTap.instance.name}"
|
||||
action = "tap #{CoreTap.instance.name}"
|
||||
else
|
||||
command = "brew create --set-name #{name} $URL"
|
||||
action = "create a new formula"
|
||||
end
|
||||
|
||||
raise UsageError, <<~EOS
|
||||
#{name} doesn't exist on disk.
|
||||
Run #{Formatter.identifier(command)} to #{action}!
|
||||
EOS
|
||||
end
|
||||
|
||||
sig { void }
|
||||
def edit
|
||||
args = edit_args.parse
|
||||
@ -50,56 +103,22 @@ module Homebrew
|
||||
[HOMEBREW_REPOSITORY]
|
||||
end
|
||||
else
|
||||
edit_api_message_displayed = T.let(false, T::Boolean)
|
||||
args.named.to_paths.select do |path|
|
||||
core_formula_path = path.fnmatch?("**/homebrew-core/Formula/**.rb", File::FNM_DOTMATCH)
|
||||
core_cask_path = path.fnmatch?("**/homebrew-cask/Casks/**.rb", File::FNM_DOTMATCH)
|
||||
core_formula_tap = path == CoreTap.instance.path
|
||||
core_cask_tap = path == CoreCaskTap.instance.path
|
||||
expanded_paths = args.named.to_paths
|
||||
expanded_paths.each do |path|
|
||||
raise_with_message!(path, args.cask?) unless path.exist?
|
||||
end
|
||||
|
||||
if path.exist?
|
||||
if (core_formula_path || core_cask_path || core_formula_tap || core_cask_tap) &&
|
||||
!edit_api_message_displayed &&
|
||||
!Homebrew::EnvConfig.no_install_from_api? &&
|
||||
!Homebrew::EnvConfig.no_env_hints?
|
||||
opoo <<~EOS
|
||||
`brew install` ignores locally edited #{(core_cask_path || core_cask_tap) ? "casks" : "formulae"} if
|
||||
`HOMEBREW_NO_INSTALL_FROM_API` is not set.
|
||||
EOS
|
||||
edit_api_message_displayed = true
|
||||
end
|
||||
next path
|
||||
end
|
||||
|
||||
name = path.basename(".rb").to_s
|
||||
|
||||
if (tap_match = Regexp.new(HOMEBREW_TAP_DIR_REGEX.source + /$/.source).match(path.to_s))
|
||||
raise TapUnavailableError, CoreTap.instance.name if core_formula_tap
|
||||
raise TapUnavailableError, CoreCaskTap.instance.name if core_cask_tap
|
||||
|
||||
raise TapUnavailableError, "#{tap_match[:user]}/#{tap_match[:repo]}"
|
||||
elsif args.cask? || core_cask_path
|
||||
if !CoreCaskTap.instance.installed? && Homebrew::API::Cask.all_casks.key?(name)
|
||||
command = "brew tap --force #{CoreCaskTap.instance.name}"
|
||||
action = "tap #{CoreCaskTap.instance.name}"
|
||||
else
|
||||
command = "brew create --cask --set-name #{name} $URL"
|
||||
action = "create a new cask"
|
||||
end
|
||||
elsif core_formula_path && !CoreTap.instance.installed? && Homebrew::API::Formula.all_formulae.key?(name)
|
||||
command = "brew tap --force #{CoreTap.instance.name}"
|
||||
action = "tap #{CoreTap.instance.name}"
|
||||
else
|
||||
command = "brew create --set-name #{name} $URL"
|
||||
action = "create a new formula"
|
||||
end
|
||||
|
||||
message = <<~EOS
|
||||
#{name} doesn't exist on disk.
|
||||
Run #{Formatter.identifier(command)} to #{action}!
|
||||
if expanded_paths.any? do |path|
|
||||
(core_formula_path?(path) || core_cask_path?(path) || core_formula_tap?(path) || core_cask_tap?(path)) &&
|
||||
!Homebrew::EnvConfig.no_install_from_api? &&
|
||||
!Homebrew::EnvConfig.no_env_hints?
|
||||
end
|
||||
opoo <<~EOS
|
||||
`brew install` ignores locally edited casks and formulae if
|
||||
HOMEBREW_NO_INSTALL_FROM_API is not set.
|
||||
EOS
|
||||
raise UsageError, message
|
||||
end.presence
|
||||
end
|
||||
expanded_paths
|
||||
end
|
||||
|
||||
if args.print_path?
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user