brew/Library/Homebrew/cask/cmd/abstract_command.rb

100 lines
2.4 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
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
2021-01-25 09:18:10 +00:00
# Abstract superclass for all Cask implementations of commands.
2020-08-19 10:34:07 +02:00
#
# @api private
2017-05-20 19:08:03 +02:00
class AbstractCommand
2020-10-20 12:03:48 +02:00
extend T::Sig
extend T::Helpers
include Homebrew::Search
2017-05-21 00:15:56 +02:00
OPTIONS = [
[:switch, "--[no-]binaries", {
2020-11-12 10:40:48 -05:00
description: "Disable/enable linking of helper executables (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", {
2020-11-12 10:40:48 -05:00
description: "Disable/enable quarantining of downloads (default: enabled).",
env: :cask_opts_quarantine,
}],
].freeze
2020-08-01 02:30:46 +02:00
def self.parser(&block)
Cmd.parser do
2020-11-16 22:18:56 +01:00
instance_eval(&block) if block
2020-08-01 02:30:46 +02:00
OPTIONS.each do |option|
send(*option)
end
2020-08-01 02:30:46 +02:00
end
end
2017-05-21 00:15:56 +02:00
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def self.command_name
@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
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
2017-05-20 19:08:03 +02:00
def self.abstract?
name.split("::").last.match?(/^Abstract[^a-z]/)
2017-05-20 19:08:03 +02:00
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
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
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def self.help
2020-08-01 02:30:46 +02:00
parser.generate_help_text
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2020-08-01 02:30:46 +02:00
def self.short_description
2020-10-20 12:03:48 +02:00
description[/\A[^.]*\./]
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
private
def casks(alternative: -> { [] })
return @casks if defined?(@casks)
2018-09-17 02:45:00 +02:00
2020-09-29 23:46:30 +02:00
@casks = args.named.empty? ? alternative.call : args.named.to_casks
rescue CaskUnavailableError => e
reason = [e.reason, *suggestion_message(e.token)].join(" ")
raise e.class.new(e.token, reason)
end
def suggestion_message(cask_token)
matches = search_casks(cask_token)
if matches.one?
2021-01-26 15:21:24 -05:00
"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))}"
end
end
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
end