57 lines
1.4 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
2020-10-20 12:03:48 +02:00
extend T::Sig
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
2020-11-20 00:35:48 +01:00
Open a <formula> or <cask> in the editor set by `EDITOR` or `HOMEBREW_EDITOR`,
or open the Homebrew repository for editing if no formula 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."
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]
end
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
2020-12-17 21:14:18 +09:00
paths = args.named.to_paths.select do |path|
2020-10-20 20:48:43 +09:00
next path if path.exist?
raise UsageError, "#{path} doesn't exist on disk. " \
"Run #{Formatter.identifier("brew create --set-name #{path.basename} $URL")} " \
"to create a new formula!"
2020-10-20 20:48:43 +09:00
end.presence
2018-09-17 02:45:00 +02:00
# If no brews are listed, open the project root in an editor.
paths ||= [HOMEBREW_REPOSITORY]
exec_editor(*paths)
end
end