Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formula: conditionally use executable name as completion name #18942

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading