Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev-cmd/edit: Move path specific functions to Pathname #16029

Merged
merged 16 commits into from Sep 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 68 additions & 49 deletions Library/Homebrew/dev-cmd/edit.rb
Expand Up @@ -28,6 +28,59 @@
end
end

sig { params(path: Pathname).returns(T::Boolean) }
def core_formula_path?(path)
path.fnmatch?("**/homebrew-core/Formula/**.rb", File::FNM_DOTMATCH)

Check warning on line 33 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L33

Added line #L33 was not covered by tests
end

sig { params(path: Pathname).returns(T::Boolean) }
def core_cask_path?(path)
path.fnmatch?("**/homebrew-cask/Casks/**.rb", File::FNM_DOTMATCH)

Check warning on line 38 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L38

Added line #L38 was not covered by tests
end

sig { params(path: Pathname).returns(T::Boolean) }
def core_formula_tap?(path)
path == CoreTap.instance.path

Check warning on line 43 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L43

Added line #L43 was not covered by tests
end

sig { params(path: Pathname).returns(T::Boolean) }
def core_cask_tap?(path)
path == CoreCaskTap.instance.path

Check warning on line 48 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L48

Added line #L48 was not covered by tests
end

sig { params(path: Pathname, cask: T::Boolean).returns(T.noreturn) }
def raise_with_message!(path, cask)
name = path.basename(".rb").to_s

Check warning on line 53 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L53

Added line #L53 was not covered by tests

if (tap_match = Regexp.new("#{HOMEBREW_TAP_DIR_REGEX.source}$").match(path.to_s))

Check warning on line 55 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L55

Added line #L55 was not covered by tests
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]}"

Check warning on line 59 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L59

Added line #L59 was not covered by tests
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}"

Check warning on line 63 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L63

Added line #L63 was not covered by tests
else
command = "brew create --cask --set-name #{name} $URL"
action = "create a new cask"

Check warning on line 66 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L66

Added line #L66 was not covered by tests
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}"

Check warning on line 72 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L72

Added line #L72 was not covered by tests
else
command = "brew create --set-name #{name} $URL"
action = "create a new formula"

Check warning on line 75 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L75

Added line #L75 was not covered by tests
end

raise UsageError, <<~EOS

Check warning on line 78 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L78

Added line #L78 was not covered by tests
#{name} doesn't exist on disk.
Run #{Formatter.identifier(command)} to #{action}!
EOS
end

sig { void }
def edit
args = edit_args.parse
Expand All @@ -50,56 +103,22 @@
[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

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}!
expanded_paths = args.named.to_paths
expanded_paths.each do |path|

Check warning on line 107 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L107

Added line #L107 was not covered by tests
raise_with_message!(path, args.cask?) unless path.exist?
end

if expanded_paths.any? do |path|
(core_formula_path?(path) || core_cask_path?(path) || core_formula_tap?(path) || core_cask_tap?(path)) &&

Check warning on line 112 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L112

Added line #L112 was not covered by tests
!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

Check warning on line 121 in Library/Homebrew/dev-cmd/edit.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/edit.rb#L121

Added line #L121 was not covered by tests
end

if args.print_path?
Expand Down