Skip to content

Commit

Permalink
formula: conditionally use executable name as completion name
Browse files Browse the repository at this point in the history
In most cases when running `generate_completions_from_executable` with
a formula's `bin`/`sbin` executable, the resulting completion is usually
intended for the executable itself.
  • Loading branch information
cho-m committed Dec 15, 2024
1 parent 7c942cc commit 7a4e2f0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2120,23 +2120,29 @@ def extract_macho_slice_from(file, arch = Hardware::CPU.arch)
# @param commands
# the path to the executable and any passed subcommand(s) to use for generating the completion scripts.
# @param base_name
# the base name of the generated completion script. Defaults to the formula name.
# the base name of the generated completion script. Defaults to the name of the executable if installed
# within formula's bin or sbin. Otherwise falls back to the formula name.
# @param shells
# the shells to generate completion scripts for. Defaults to `[:bash, :zsh, :fish]`.
# @param shell_parameter_format
# specify how `shells` should each be passed to the `executable`. Takes either a String representing a
# prefix, or one of `[:flag, :arg, :none, :click]`. Defaults to plainly passing the shell.
sig {
params(
commands: T.any(Pathname, String),
base_name: String, shells: T::Array[Symbol],
shell_parameter_format: T.nilable(T.any(Symbol, String))
commands: T.any(Pathname, String),
base_name: T.nilable(String),
shells: T::Array[Symbol],
shell_parameter_format: T.nilable(T.any(Symbol, String)),
).void
}
def generate_completions_from_executable(*commands,
base_name: name,
base_name: nil,
shells: [:bash, :zsh, :fish],
shell_parameter_format: nil)
executable = commands.first.to_s
base_name ||= File.basename(executable) if executable.start_with?(bin.to_s, sbin.to_s)
base_name ||= name

completion_script_path_map = {
bash: bash_completion/base_name,
zsh: zsh_completion/"_#{base_name}",
Expand All @@ -2155,7 +2161,7 @@ def generate_completions_from_executable(*commands,
elsif shell_parameter_format == :none
nil
elsif shell_parameter_format == :click
prog_name = File.basename(commands.first.to_s).upcase.tr("-", "_")
prog_name = File.basename(executable).upcase.tr("-", "_")
popen_read_env["_#{prog_name}_COMPLETE"] = "#{shell}_source"
nil
else
Expand Down

0 comments on commit 7a4e2f0

Please sign in to comment.