Port Homebrew::DevCmd::Edit
This commit is contained in:
parent
21c84553cf
commit
2cc70549d8
@ -1,15 +1,14 @@
|
||||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "abstract_command"
|
||||
require "formula"
|
||||
require "cli/parser"
|
||||
|
||||
module Homebrew
|
||||
module_function
|
||||
|
||||
sig { returns(CLI::Parser) }
|
||||
def edit_args
|
||||
Homebrew::CLI::Parser.new do
|
||||
module DevCmd
|
||||
class Edit < AbstractCommand
|
||||
cmd_args 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.
|
||||
@ -26,8 +25,57 @@ module Homebrew
|
||||
|
||||
named_args [:formula, :cask, :tap], without_api: true
|
||||
end
|
||||
|
||||
sig { override.void }
|
||||
def run
|
||||
ENV["COLORTERM"] = ENV.fetch("HOMEBREW_COLORTERM", nil)
|
||||
|
||||
unless (HOMEBREW_REPOSITORY/".git").directory?
|
||||
odie <<~EOS
|
||||
Changes will be lost!
|
||||
The first time you `brew update`, all local changes will be lost; you should
|
||||
thus `brew update` before you `brew edit`!
|
||||
EOS
|
||||
end
|
||||
|
||||
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"
|
||||
["--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|
|
||||
raise_with_message!(path, args.cask?) unless path.exist?
|
||||
end
|
||||
|
||||
if expanded_paths.any? do |path|
|
||||
!Homebrew::EnvConfig.no_install_from_api? &&
|
||||
!Homebrew::EnvConfig.no_env_hints? &&
|
||||
(core_formula_path?(path) || core_cask_path?(path) || core_formula_tap?(path) || core_cask_tap?(path))
|
||||
end
|
||||
opoo <<~EOS
|
||||
`brew install` ignores locally edited casks and formulae if
|
||||
HOMEBREW_NO_INSTALL_FROM_API is not set.
|
||||
EOS
|
||||
end
|
||||
expanded_paths
|
||||
end
|
||||
|
||||
if args.print_path?
|
||||
paths.each { puts _1 }
|
||||
return
|
||||
end
|
||||
|
||||
exec_editor(*paths)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
sig { params(path: Pathname).returns(T::Boolean) }
|
||||
def core_formula_path?(path)
|
||||
path.fnmatch?("**/homebrew-core/Formula/**.rb", File::FNM_DOTMATCH)
|
||||
@ -80,54 +128,6 @@ module Homebrew
|
||||
Run #{Formatter.identifier(command)} to #{action}!
|
||||
EOS
|
||||
end
|
||||
|
||||
sig { void }
|
||||
def edit
|
||||
args = edit_args.parse
|
||||
|
||||
ENV["COLORTERM"] = ENV.fetch("HOMEBREW_COLORTERM", nil)
|
||||
|
||||
unless (HOMEBREW_REPOSITORY/".git").directory?
|
||||
odie <<~EOS
|
||||
Changes will be lost!
|
||||
The first time you `brew update`, all local changes will be lost; you should
|
||||
thus `brew update` before you `brew edit`!
|
||||
EOS
|
||||
end
|
||||
|
||||
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"
|
||||
["--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|
|
||||
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)) &&
|
||||
!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
|
||||
end
|
||||
expanded_paths
|
||||
end
|
||||
|
||||
if args.print_path?
|
||||
paths.each(&method(:puts))
|
||||
return
|
||||
end
|
||||
|
||||
exec_editor(*paths)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "cmd/shared_examples/args_parse"
|
||||
require "dev-cmd/edit"
|
||||
|
||||
RSpec.describe "brew edit" do
|
||||
RSpec.describe Homebrew::DevCmd::Edit do
|
||||
it_behaves_like "parseable arguments"
|
||||
|
||||
it "opens a given Formula in an editor", :integration_test do
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user