Skip to content

Commit

Permalink
Merge pull request #18942 from Homebrew/completion-base_name
Browse files Browse the repository at this point in the history
formula: conditionally use executable name as completion name
  • Loading branch information
MikeMcQuaid authored Dec 16, 2024
2 parents 3001afe + 60b5e06 commit 6c14bcb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 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
6 changes: 3 additions & 3 deletions Library/Homebrew/test/formula_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1897,9 +1897,9 @@ def install

it "generates completion scripts" do
f.brew { f.install }
expect(f.bash_completion/"testball").to be_a_file
expect(f.zsh_completion/"_testball").to be_a_file
expect(f.fish_completion/"testball.fish").to be_a_file
expect(f.bash_completion/"foo").to be_a_file
expect(f.zsh_completion/"_foo").to be_a_file
expect(f.fish_completion/"foo.fish").to be_a_file
end
end

Expand Down

0 comments on commit 6c14bcb

Please sign in to comment.