formula: conditionally use executable name as completion name

When running `generate_completions_from_executable` with a formula's
`bin`/`sbin` executable, the resulting completion is usually intended
for the executable itself.
This commit is contained in:
Michael Cho 2024-12-15 11:09:05 -05:00
parent 7c942ccaef
commit 60b5e063e3
No known key found for this signature in database
GPG Key ID: 55E85E28A7CD1E85
2 changed files with 15 additions and 9 deletions

View File

@ -2120,7 +2120,8 @@ class Formula
# @param commands # @param commands
# the path to the executable and any passed subcommand(s) to use for generating the completion scripts. # the path to the executable and any passed subcommand(s) to use for generating the completion scripts.
# @param base_name # @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 # @param shells
# the shells to generate completion scripts for. Defaults to `[:bash, :zsh, :fish]`. # the shells to generate completion scripts for. Defaults to `[:bash, :zsh, :fish]`.
# @param shell_parameter_format # @param shell_parameter_format
@ -2128,15 +2129,20 @@ class Formula
# prefix, or one of `[:flag, :arg, :none, :click]`. Defaults to plainly passing the shell. # prefix, or one of `[:flag, :arg, :none, :click]`. Defaults to plainly passing the shell.
sig { sig {
params( params(
commands: T.any(Pathname, String), commands: T.any(Pathname, String),
base_name: String, shells: T::Array[Symbol], base_name: T.nilable(String),
shell_parameter_format: T.nilable(T.any(Symbol, String)) shells: T::Array[Symbol],
shell_parameter_format: T.nilable(T.any(Symbol, String)),
).void ).void
} }
def generate_completions_from_executable(*commands, def generate_completions_from_executable(*commands,
base_name: name, base_name: nil,
shells: [:bash, :zsh, :fish], shells: [:bash, :zsh, :fish],
shell_parameter_format: nil) 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 = { completion_script_path_map = {
bash: bash_completion/base_name, bash: bash_completion/base_name,
zsh: zsh_completion/"_#{base_name}", zsh: zsh_completion/"_#{base_name}",
@ -2155,7 +2161,7 @@ class Formula
elsif shell_parameter_format == :none elsif shell_parameter_format == :none
nil nil
elsif shell_parameter_format == :click 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" popen_read_env["_#{prog_name}_COMPLETE"] = "#{shell}_source"
nil nil
else else

View File

@ -1897,9 +1897,9 @@ RSpec.describe Formula do
it "generates completion scripts" do it "generates completion scripts" do
f.brew { f.install } f.brew { f.install }
expect(f.bash_completion/"testball").to be_a_file expect(f.bash_completion/"foo").to be_a_file
expect(f.zsh_completion/"_testball").to be_a_file expect(f.zsh_completion/"_foo").to be_a_file
expect(f.fish_completion/"testball.fish").to be_a_file expect(f.fish_completion/"foo.fish").to be_a_file
end end
end end