2020-11-25 17:03:23 +01:00
|
|
|
# typed: strict
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
require "formula"
|
2019-04-17 18:25:08 +09:00
|
|
|
require "cli/parser"
|
2010-09-11 20:22:54 +01:00
|
|
|
|
2014-06-18 22:41:47 -05:00
|
|
|
module Homebrew
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { returns(CLI::Parser) }
|
2018-07-30 18:25:38 +05:30
|
|
|
def edit_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
2021-01-15 15:04:02 -05:00
|
|
|
description <<~EOS
|
2023-07-31 20:26:29 -04:00
|
|
|
Open a <formula>, <cask> or <tap> in the editor set by `EDITOR` or `HOMEBREW_EDITOR`,
|
|
|
|
or open the Homebrew repository for editing if no argument is provided.
|
2018-09-28 21:39:52 +05:30
|
|
|
EOS
|
2020-11-20 00:35:48 +01:00
|
|
|
|
|
|
|
switch "--formula", "--formulae",
|
|
|
|
description: "Treat all named arguments as formulae."
|
|
|
|
switch "--cask", "--casks",
|
|
|
|
description: "Treat all named arguments as casks."
|
2021-10-13 10:27:12 -06:00
|
|
|
switch "--print-path",
|
|
|
|
description: "Print the file path to be edited, without opening an editor."
|
2020-11-27 11:41:08 -05:00
|
|
|
|
2020-11-20 00:35:48 +01:00
|
|
|
conflicts "--formula", "--cask"
|
2021-01-10 14:26:40 -05:00
|
|
|
|
2023-07-31 20:26:29 -04:00
|
|
|
named_args [:formula, :cask, :tap], without_api: true
|
2018-03-25 17:48:22 +05:30
|
|
|
end
|
2018-07-30 18:25:38 +05:30
|
|
|
end
|
|
|
|
|
2023-09-27 12:39:03 +03:00
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def core_formula_path?(path)
|
|
|
|
path.fnmatch?("**/homebrew-core/Formula/**.rb", File::FNM_DOTMATCH)
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def core_cask_path?(path)
|
|
|
|
path.fnmatch?("**/homebrew-cask/Casks/**.rb", File::FNM_DOTMATCH)
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def core_formula_tap?(path)
|
|
|
|
path == CoreTap.instance.path
|
|
|
|
end
|
|
|
|
|
|
|
|
sig { returns(T::Boolean) }
|
|
|
|
def core_cask_tap?(path)
|
|
|
|
path == CoreCaskTap.instance.path
|
|
|
|
end
|
|
|
|
|
2023-09-27 08:27:13 +03:00
|
|
|
sig { params(path: Pathname, cask: T::Boolean).returns(T.noreturn) }
|
|
|
|
def raise_with_message!(path, cask)
|
2023-09-25 08:09:03 +03:00
|
|
|
name = path.basename(".rb").to_s
|
|
|
|
|
2023-09-25 16:13:16 +03:00
|
|
|
if (tap_match = Regexp.new("#{HOMEBREW_TAP_DIR_REGEX.source}$").match(path.to_s))
|
2023-09-27 12:39:03 +03:00
|
|
|
raise TapUnavailableError, CoreTap.instance.name if core_formula_tap?(path)
|
|
|
|
raise TapUnavailableError, CoreCaskTap.instance.name if core_cask_tap?(path)
|
2023-09-25 08:09:03 +03:00
|
|
|
|
|
|
|
raise TapUnavailableError, "#{tap_match[:user]}/#{tap_match[:repo]}"
|
2023-09-27 12:39:03 +03:00
|
|
|
elsif cask || core_cask_path?(path)
|
2023-09-25 08:09:03 +03:00
|
|
|
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
|
2023-09-27 12:39:03 +03:00
|
|
|
elsif core_formula_path?(path) &&
|
2023-09-25 08:09:03 +03:00
|
|
|
!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
|
|
|
|
|
2023-09-25 18:03:57 +03:00
|
|
|
raise UsageError, <<~EOS
|
2023-09-25 08:09:03 +03:00
|
|
|
#{name} doesn't exist on disk.
|
|
|
|
Run #{Formatter.identifier(command)} to #{action}!
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2020-11-20 00:35:48 +01:00
|
|
|
sig { void }
|
2018-07-30 18:25:38 +05:30
|
|
|
def edit
|
2020-07-30 18:40:10 +02:00
|
|
|
args = edit_args.parse
|
2018-03-24 19:38:34 +05:30
|
|
|
|
2015-08-03 13:09:07 +01:00
|
|
|
unless (HOMEBREW_REPOSITORY/".git").directory?
|
2020-12-03 00:02:22 -05:00
|
|
|
odie <<~EOS
|
2012-08-01 15:56:52 -04:00
|
|
|
Changes will be lost!
|
2019-04-05 12:24:10 -04:00
|
|
|
The first time you `brew update`, all local changes will be lost; you should
|
|
|
|
thus `brew update` before you `brew edit`!
|
2018-06-06 23:34:19 -04:00
|
|
|
EOS
|
2012-08-01 15:56:52 -04:00
|
|
|
end
|
|
|
|
|
2021-04-02 15:36:29 -07:00
|
|
|
paths = if args.named.empty?
|
|
|
|
# Sublime requires opting into the project editing path,
|
|
|
|
# as opposed to VS Code which will infer from the .vscode path
|
2023-02-11 00:16:11 -08:00
|
|
|
if which_editor(silent: true) == "subl"
|
2021-04-02 15:36:29 -07:00
|
|
|
["--project", "#{HOMEBREW_REPOSITORY}/.sublime/homebrew.sublime-project"]
|
|
|
|
else
|
|
|
|
# If no formulae are listed, open the project root in an editor.
|
|
|
|
[HOMEBREW_REPOSITORY]
|
|
|
|
end
|
|
|
|
else
|
2023-09-24 21:05:37 +03:00
|
|
|
expanded_paths = args.named.to_paths
|
|
|
|
expanded_paths.each do |path|
|
2023-09-27 08:27:13 +03:00
|
|
|
raise_with_message(path, args.cask?) unless path.exist?
|
2023-09-25 13:09:11 +03:00
|
|
|
end
|
|
|
|
|
2023-09-26 10:08:54 +03:00
|
|
|
if expanded_paths.any? do |path|
|
2023-09-27 12:39:03 +03:00
|
|
|
(core_formula_path?(path) || core_cask_path?(path) || core_formula_tap?(path) || core_cask_tap?(path)) &&
|
2023-09-25 13:09:11 +03:00
|
|
|
!Homebrew::EnvConfig.no_install_from_api? &&
|
|
|
|
!Homebrew::EnvConfig.no_env_hints?
|
2023-09-26 10:08:54 +03:00
|
|
|
end
|
|
|
|
opoo <<~EOS
|
|
|
|
`brew install` ignores locally edited casks and formulae if
|
|
|
|
HOMEBREW_NO_INSTALL_FROM_API is not set.
|
|
|
|
EOS
|
2023-09-24 21:05:37 +03:00
|
|
|
end
|
2023-09-25 13:09:11 +03:00
|
|
|
expanded_paths
|
2023-02-10 12:07:58 +01:00
|
|
|
end
|
|
|
|
|
2021-10-13 10:27:12 -06:00
|
|
|
if args.print_path?
|
|
|
|
paths.each(&method(:puts))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-09-06 12:31:29 +01:00
|
|
|
exec_editor(*paths)
|
2011-08-24 14:45:01 +01:00
|
|
|
end
|
2010-09-11 20:22:54 +01:00
|
|
|
end
|