2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-20 13:45:55 -04:00
|
|
|
require "cask/cmd"
|
2021-01-05 22:44:39 -05:00
|
|
|
require "completions"
|
2020-07-20 13:18:09 -04:00
|
|
|
|
2020-08-14 03:12:03 +02:00
|
|
|
# Helper functions for commands.
|
|
|
|
#
|
|
|
|
# @api private
|
2016-09-07 20:09:08 +01:00
|
|
|
module Commands
|
2020-02-02 17:05:45 +01:00
|
|
|
module_function
|
|
|
|
|
|
|
|
HOMEBREW_CMD_PATH = (HOMEBREW_LIBRARY_PATH/"cmd").freeze
|
|
|
|
HOMEBREW_DEV_CMD_PATH = (HOMEBREW_LIBRARY_PATH/"dev-cmd").freeze
|
|
|
|
HOMEBREW_INTERNAL_COMMAND_ALIASES = {
|
|
|
|
"ls" => "list",
|
|
|
|
"homepage" => "home",
|
|
|
|
"-S" => "search",
|
|
|
|
"up" => "update",
|
|
|
|
"ln" => "link",
|
|
|
|
"instal" => "install", # gem does the same
|
|
|
|
"uninstal" => "uninstall",
|
|
|
|
"rm" => "uninstall",
|
|
|
|
"remove" => "uninstall",
|
|
|
|
"configure" => "diy",
|
|
|
|
"abv" => "info",
|
|
|
|
"dr" => "doctor",
|
|
|
|
"--repo" => "--repository",
|
|
|
|
"environment" => "--env",
|
|
|
|
"--config" => "config",
|
|
|
|
"-v" => "--version",
|
2020-09-23 20:28:07 +02:00
|
|
|
"tc" => "typecheck",
|
2020-02-02 17:05:45 +01:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def valid_internal_cmd?(cmd)
|
|
|
|
require?(HOMEBREW_CMD_PATH/cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_internal_dev_cmd?(cmd)
|
|
|
|
require?(HOMEBREW_DEV_CMD_PATH/cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_name(cmd)
|
|
|
|
cmd.to_s
|
|
|
|
.tr("-", "_")
|
|
|
|
.downcase
|
|
|
|
.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def args_method_name(cmd_path)
|
|
|
|
cmd_path_basename = basename_without_extension(cmd_path)
|
|
|
|
cmd_method_prefix = method_name(cmd_path_basename)
|
|
|
|
"#{cmd_method_prefix}_args".to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_cmd_path(cmd)
|
2017-11-05 15:37:57 +00:00
|
|
|
[
|
2020-02-02 17:05:45 +01:00
|
|
|
HOMEBREW_CMD_PATH/"#{cmd}.rb",
|
|
|
|
HOMEBREW_CMD_PATH/"#{cmd}.sh",
|
2017-11-05 15:37:57 +00:00
|
|
|
].find(&:exist?)
|
2016-09-07 20:09:08 +01:00
|
|
|
end
|
2020-02-02 17:05:45 +01:00
|
|
|
|
|
|
|
def internal_dev_cmd_path(cmd)
|
|
|
|
[
|
|
|
|
HOMEBREW_DEV_CMD_PATH/"#{cmd}.rb",
|
|
|
|
HOMEBREW_DEV_CMD_PATH/"#{cmd}.sh",
|
|
|
|
].find(&:exist?)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ruby commands which can be `require`d without being run.
|
|
|
|
def external_ruby_v2_cmd_path(cmd)
|
|
|
|
path = which("#{cmd}.rb", Tap.cmd_directories)
|
|
|
|
path if require?(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ruby commands which are run by being `require`d.
|
|
|
|
def external_ruby_cmd_path(cmd)
|
|
|
|
which("brew-#{cmd}.rb", PATH.new(ENV["PATH"]).append(Tap.cmd_directories))
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_cmd_path(cmd)
|
|
|
|
which("brew-#{cmd}", PATH.new(ENV["PATH"]).append(Tap.cmd_directories))
|
|
|
|
end
|
|
|
|
|
|
|
|
def path(cmd)
|
|
|
|
internal_cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
|
|
|
|
path ||= internal_cmd_path(internal_cmd)
|
|
|
|
path ||= internal_dev_cmd_path(internal_cmd)
|
|
|
|
path ||= external_ruby_v2_cmd_path(cmd)
|
|
|
|
path ||= external_ruby_cmd_path(cmd)
|
|
|
|
path ||= external_cmd_path(cmd)
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2021-01-05 22:44:39 -05:00
|
|
|
def commands(external: true, aliases: false)
|
2020-02-02 17:05:45 +01:00
|
|
|
cmds = internal_commands
|
|
|
|
cmds += internal_developer_commands
|
2021-01-05 22:44:39 -05:00
|
|
|
cmds += external_commands if external
|
2020-02-02 17:05:45 +01:00
|
|
|
cmds += internal_commands_aliases if aliases
|
2020-07-20 13:18:09 -04:00
|
|
|
cmds += cask_commands(aliases: aliases).map { |cmd| "cask #{cmd}" }
|
2020-02-02 17:05:45 +01:00
|
|
|
cmds.sort
|
|
|
|
end
|
|
|
|
|
2020-11-25 09:37:21 +00:00
|
|
|
def internal_commands_paths(cask: true)
|
|
|
|
cmds = find_commands HOMEBREW_CMD_PATH
|
|
|
|
# can be removed when cask commands are removed and no longer odeprecated/odisabled
|
|
|
|
cmds.delete(HOMEBREW_CMD_PATH/"cask.rb") unless cask
|
|
|
|
cmds
|
2020-02-02 17:05:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def internal_developer_commands_paths
|
|
|
|
find_commands HOMEBREW_DEV_CMD_PATH
|
|
|
|
end
|
|
|
|
|
2020-09-09 21:42:33 +02:00
|
|
|
def official_external_commands_paths(quiet:)
|
2020-05-30 14:19:56 +01:00
|
|
|
%w[bundle services test-bot].map do |cmd|
|
2020-03-11 12:17:25 +00:00
|
|
|
tap = Tap.fetch("Homebrew/#{cmd}")
|
2020-09-09 21:42:33 +02:00
|
|
|
tap.install(quiet: quiet) unless tap.installed?
|
2020-03-11 12:17:25 +00:00
|
|
|
external_ruby_v2_cmd_path(cmd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-02 17:05:45 +01:00
|
|
|
def internal_commands
|
|
|
|
find_internal_commands(HOMEBREW_CMD_PATH).map(&:to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_developer_commands
|
|
|
|
find_internal_commands(HOMEBREW_DEV_CMD_PATH).map(&:to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_commands_aliases
|
|
|
|
HOMEBREW_INTERNAL_COMMAND_ALIASES.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_internal_commands(path)
|
|
|
|
find_commands(path).map(&:basename)
|
|
|
|
.map(&method(:basename_without_extension))
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_commands
|
|
|
|
Tap.cmd_directories.flat_map do |path|
|
|
|
|
find_commands(path).select(&:executable?)
|
|
|
|
.map(&method(:basename_without_extension))
|
2020-07-20 13:18:09 -04:00
|
|
|
.map { |p| p.to_s.delete_prefix("brew-").strip }
|
2020-02-02 17:05:45 +01:00
|
|
|
end.map(&:to_s)
|
|
|
|
.sort
|
|
|
|
end
|
|
|
|
|
2020-07-20 13:18:09 -04:00
|
|
|
def cask_commands(aliases: false)
|
|
|
|
cmds = cask_internal_commands
|
|
|
|
cmds += cask_internal_command_aliases if aliases
|
|
|
|
cmds += cask_external_commands
|
|
|
|
cmds
|
|
|
|
end
|
|
|
|
|
|
|
|
def cask_internal_commands
|
2020-07-20 14:15:47 -04:00
|
|
|
Cask::Cmd.commands
|
2020-07-20 13:18:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def cask_internal_command_aliases
|
|
|
|
Cask::Cmd.aliases.keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def cask_external_commands
|
2020-07-20 13:45:55 -04:00
|
|
|
PATH.new(Tap.cmd_directories, ENV["HOMEBREW_PATH"]).flat_map do |search_path|
|
2020-07-20 13:18:09 -04:00
|
|
|
find_commands(search_path).map do |possible_command|
|
2020-07-22 22:45:47 -04:00
|
|
|
path = possible_command.to_path
|
|
|
|
command_name = path.match(/brewcask-(.*)\.rb/) { |data| data[1].delete_suffix(".rb") }
|
2020-07-20 13:18:09 -04:00
|
|
|
if command_name.blank? && possible_command.executable?
|
2020-07-22 22:45:47 -04:00
|
|
|
command_name = path.match(/brewcask-(.*)/) { |data| data[1] }
|
2020-07-20 13:18:09 -04:00
|
|
|
end
|
|
|
|
command_name
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-02 17:05:45 +01:00
|
|
|
def basename_without_extension(path)
|
|
|
|
path.basename(path.extname)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_commands(path)
|
|
|
|
Pathname.glob("#{path}/*")
|
|
|
|
.select(&:file?)
|
|
|
|
.sort
|
|
|
|
end
|
2020-06-17 18:03:40 -04:00
|
|
|
|
|
|
|
def rebuild_internal_commands_completion_list
|
|
|
|
cmds = internal_commands + internal_developer_commands + internal_commands_aliases
|
2021-01-05 22:44:39 -05:00
|
|
|
cmds.reject! { |cmd| Homebrew::Completions::COMPLETIONS_EXCLUSION_LIST.include? cmd }
|
2020-06-17 18:03:40 -04:00
|
|
|
|
|
|
|
file = HOMEBREW_REPOSITORY/"completions/internal_commands_list.txt"
|
2020-08-19 17:12:32 +01:00
|
|
|
file.atomic_write("#{cmds.sort.join("\n")}\n")
|
2020-06-17 18:03:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rebuild_commands_completion_list
|
|
|
|
# Ensure that the cache exists so we can build the commands list
|
|
|
|
HOMEBREW_CACHE.mkpath
|
|
|
|
|
2021-01-05 22:44:39 -05:00
|
|
|
cmds = commands(aliases: true).reject do |cmd|
|
2021-01-15 00:13:10 -05:00
|
|
|
# TODO: (2.8) remove the cask check when `brew cask` is removed
|
2021-01-05 22:44:39 -05:00
|
|
|
cmd.start_with?("cask ") || Homebrew::Completions::COMPLETIONS_EXCLUSION_LIST.include?(cmd)
|
|
|
|
end
|
|
|
|
|
2021-01-24 01:59:31 -05:00
|
|
|
all_commands_file = HOMEBREW_CACHE/"all_commands_list.txt"
|
|
|
|
external_commands_file = HOMEBREW_CACHE/"external_commands_list.txt"
|
|
|
|
all_commands_file.atomic_write("#{cmds.sort.join("\n")}\n")
|
|
|
|
external_commands_file.atomic_write("#{external_commands.sort.join("\n")}\n")
|
2021-01-05 22:44:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def command_options(command)
|
2021-01-15 00:13:10 -05:00
|
|
|
path = self.path(command)
|
|
|
|
return if path.blank?
|
2021-01-05 22:44:39 -05:00
|
|
|
|
|
|
|
if cmd_parser = Homebrew::CLI::Parser.from_cmd_path(path)
|
|
|
|
cmd_parser.processed_options.map do |short, long, _, desc|
|
|
|
|
[long || short, desc]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
options = []
|
|
|
|
comment_lines = path.read.lines.grep(/^#:/)
|
|
|
|
return options if comment_lines.empty?
|
|
|
|
|
|
|
|
# skip the comment's initial usage summary lines
|
|
|
|
comment_lines.slice(2..-1).each do |line|
|
|
|
|
if / (?<option>-[-\w]+) +(?<desc>.*)$/ =~ line
|
|
|
|
options << [option, desc]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-24 16:42:28 -05:00
|
|
|
def command_description(command, short: false)
|
2021-01-24 01:59:31 -05:00
|
|
|
path = self.path(command)
|
|
|
|
return if path.blank?
|
|
|
|
|
|
|
|
if cmd_parser = Homebrew::CLI::Parser.from_cmd_path(path)
|
2021-01-24 16:42:28 -05:00
|
|
|
if short
|
|
|
|
cmd_parser.description.split(".").first
|
|
|
|
else
|
|
|
|
cmd_parser.description
|
|
|
|
end
|
2021-01-24 01:59:31 -05:00
|
|
|
else
|
|
|
|
comment_lines = path.read.lines.grep(/^#:/)
|
|
|
|
|
|
|
|
# skip the comment's initial usage summary lines
|
|
|
|
comment_lines.slice(2..-1)&.each do |line|
|
|
|
|
if /^#: (?<desc>\w.*+)$/ =~ line
|
2021-01-24 16:42:28 -05:00
|
|
|
return desc.split(".").first if short
|
|
|
|
|
2021-01-24 01:59:31 -05:00
|
|
|
return desc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 22:44:39 -05:00
|
|
|
def named_args_type(command)
|
2021-01-15 00:13:10 -05:00
|
|
|
path = self.path(command)
|
|
|
|
return if path.blank?
|
2021-01-05 22:44:39 -05:00
|
|
|
|
|
|
|
cmd_parser = Homebrew::CLI::Parser.from_cmd_path(path)
|
|
|
|
return if cmd_parser.blank?
|
|
|
|
|
|
|
|
Array(cmd_parser.named_args_type)
|
2020-06-17 18:03:40 -04:00
|
|
|
end
|
2016-09-07 20:09:08 +01:00
|
|
|
end
|