114 lines
3.6 KiB
Ruby
Raw Normal View History

2020-11-25 17:03:23 +01:00
# typed: strict
# frozen_string_literal: true
require "formula"
2019-04-17 18:25:08 +09:00
require "cli/parser"
module Homebrew
2016-09-26 01:44:51 +02:00
module_function
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def edit_args
Homebrew::CLI::Parser.new do
description <<~EOS
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."
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
named_args [:formula, :cask, :tap], without_api: true
end
end
sig { void }
def fail(path, cask)
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 path.core_formula_tap?
raise TapUnavailableError, CoreCaskTap.instance.name if path.core_cask_tap?
raise TapUnavailableError, "#{tap_match[:user]}/#{tap_match[:repo]}"
elsif cask || path.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 path.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}!
EOS
raise UsageError, message
end
2020-11-20 00:35:48 +01:00
sig { void }
def edit
2020-07-30 18:40:10 +02:00
args = edit_args.parse
2018-03-24 19:38:34 +05:30
unless (HOMEBREW_REPOSITORY/".git").directory?
odie <<~EOS
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
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
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
expanded_paths = args.named.to_paths
expanded_paths.each do |path|
fail(path, args.cask?) unless path.exist?
end
expanded_paths.each do |path|
if (path.core_formula_path? || path.core_cask_path? || path.core_formula_tap? || path.core_cask_tap?) &&
!Homebrew::EnvConfig.no_install_from_api? &&
!Homebrew::EnvConfig.no_env_hints?
opoo <<~EOS
`brew install` ignores locally edited #{(path.core_cask_path? || path.core_cask_tap?) ? "casks" : "formulae"} if
`HOMEBREW_NO_INSTALL_FROM_API` is not set.
EOS
break
end
end
expanded_paths
end
if args.print_path?
paths.each(&method(:puts))
return
end
exec_editor(*paths)
end
end