2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-09-25 21:13:26 +02:00
|
|
|
require "cask/config"
|
2018-06-09 15:40:44 +02:00
|
|
|
require "search"
|
2017-05-21 00:15:56 +02:00
|
|
|
|
2018-09-06 08:29:14 +02:00
|
|
|
module Cask
|
2018-09-04 08:45:48 +01:00
|
|
|
class Cmd
|
2020-08-19 10:34:07 +02:00
|
|
|
# Abstract superclass for all `brew cask` commands.
|
|
|
|
#
|
|
|
|
# @api private
|
2017-05-20 19:08:03 +02:00
|
|
|
class AbstractCommand
|
2018-06-09 15:40:44 +02:00
|
|
|
include Homebrew::Search
|
2017-05-21 00:15:56 +02:00
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
def self.min_named
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.max_named
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.banner_args
|
|
|
|
if min_named == :cask && max_named != 1
|
|
|
|
" <cask>"
|
|
|
|
elsif max_named&.zero?
|
|
|
|
""
|
|
|
|
else
|
|
|
|
" [<cask>]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.banner_headline
|
|
|
|
"`#{command_name}` [<options>]#{banner_args}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parser(&block)
|
|
|
|
banner = <<~EOS
|
|
|
|
`cask` #{banner_headline}
|
|
|
|
|
|
|
|
#{description}
|
|
|
|
EOS
|
|
|
|
|
|
|
|
min_n = min_named
|
|
|
|
max_n = max_named
|
|
|
|
|
|
|
|
Cmd.parser do
|
|
|
|
usage_banner banner
|
|
|
|
|
|
|
|
instance_eval(&block) if block_given?
|
|
|
|
|
|
|
|
switch "--[no-]binaries",
|
|
|
|
description: "Disable/enable linking of helper executables to `#{Config.global.binarydir}`. " \
|
|
|
|
"Default: enabled",
|
|
|
|
env: :cask_opts_binaries
|
|
|
|
|
|
|
|
switch "--require-sha",
|
|
|
|
description: "Require all casks to have a checksum.",
|
|
|
|
env: :cask_opts_require_sha
|
|
|
|
|
|
|
|
switch "--[no-]quarantine",
|
|
|
|
description: "Disable/enable quarantining of downloads. Default: enabled",
|
|
|
|
env: :cask_opts_quarantine
|
|
|
|
|
|
|
|
min_named min_n unless min_n.nil?
|
|
|
|
max_named max_n unless max_n.nil?
|
|
|
|
end
|
|
|
|
end
|
2017-05-21 00:15:56 +02:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.command_name
|
2016-10-14 20:03:34 +02:00
|
|
|
@command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-05-20 19:08:03 +02:00
|
|
|
def self.abstract?
|
2017-05-29 16:36:01 -07:00
|
|
|
name.split("::").last.match?(/^Abstract[^a-z]/)
|
2017-05-20 19:08:03 +02:00
|
|
|
end
|
|
|
|
|
2020-04-07 08:32:30 +02:00
|
|
|
def self.visible?
|
2016-09-24 13:52:43 +02:00
|
|
|
true
|
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2016-09-24 13:52:43 +02:00
|
|
|
def self.help
|
2020-08-01 02:30:46 +02:00
|
|
|
parser.generate_help_text
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.short_description
|
|
|
|
description.split(".").first
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
|
2017-05-20 19:08:03 +02:00
|
|
|
def self.run(*args)
|
|
|
|
new(*args).run
|
|
|
|
end
|
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
attr_reader :args
|
2017-05-21 00:15:56 +02:00
|
|
|
|
2017-05-19 21:13:08 +02:00
|
|
|
def initialize(*args)
|
2020-08-01 02:30:46 +02:00
|
|
|
@args = self.class.parser.parse(args)
|
2017-05-19 21:13:08 +02:00
|
|
|
end
|
2017-06-13 17:14:01 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def casks(alternative: -> { [] })
|
2017-09-11 08:37:15 +02:00
|
|
|
return @casks if defined?(@casks)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2020-08-01 02:30:46 +02:00
|
|
|
casks = args.named.empty? ? alternative.call : args.named
|
2017-09-11 08:37:15 +02:00
|
|
|
@casks = casks.map { |cask| CaskLoader.load(cask) }
|
|
|
|
rescue CaskUnavailableError => e
|
2018-06-02 02:47:23 +02:00
|
|
|
reason = [e.reason, *suggestion_message(e.token)].join(" ")
|
2017-09-11 08:37:15 +02:00
|
|
|
raise e.class.new(e.token, reason)
|
|
|
|
end
|
|
|
|
|
|
|
|
def suggestion_message(cask_token)
|
2018-06-09 15:40:44 +02:00
|
|
|
matches = search_casks(cask_token)
|
2017-06-13 17:14:01 +02:00
|
|
|
|
2018-06-02 02:47:23 +02:00
|
|
|
if matches.one?
|
|
|
|
"Did you mean “#{matches.first}”?"
|
|
|
|
elsif !matches.empty?
|
2019-04-20 14:07:29 +09:00
|
|
|
"Did you mean one of these?\n#{Formatter.columns(matches.take(20))}"
|
2017-09-11 08:37:15 +02:00
|
|
|
end
|
2017-06-13 17:14:01 +02:00
|
|
|
end
|
2016-09-24 13:52:43 +02:00
|
|
|
end
|
2016-08-18 22:11:42 +03:00
|
|
|
end
|
|
|
|
end
|