@@ -22,7 +22,14 @@ pub struct Options {
2222}
2323
2424fn completions_for ( shell : Shell , manager : & ' static str , cmd : Command ) -> Vec < u8 > {
25- let tauri = cmd. name ( "tauri" ) ;
25+ let tauri_bin_name = match manager {
26+ "npm" | "bun" => format ! ( "{manager} run tauri" ) ,
27+ "deno" => format ! ( "{manager} task tauri" ) ,
28+ _ => format ! ( "{manager} tauri" ) ,
29+ } ;
30+ // override the bin name inherited from how the CLI was invoked (e.g. `cargo-tauri`),
31+ // clap_complete resolves subcommands from the words of the bin name
32+ let tauri = cmd. name ( "tauri" ) . bin_name ( tauri_bin_name) ;
2633 let mut command = if manager == "npm" || manager == "bun" {
2734 Command :: new ( manager)
2835 . bin_name ( manager)
@@ -40,25 +47,25 @@ fn completions_for(shell: Shell, manager: &'static str, cmd: Command) -> Vec<u8>
4047 buf
4148}
4249
50+ // Bash completions for the commands that invoke the Tauri CLI directly.
51+ //
52+ // Bash completion functions are registered per command name, so registering one for the package
53+ // managers (`cargo`, `npm`...) would replace their own completions; those are not registered.
54+ fn bash_completions ( cmd : Command ) -> String {
55+ let mut command = cmd. name ( "tauri" ) . bin_name ( "tauri" ) ;
56+ let mut buf = Vec :: new ( ) ;
57+ generate ( Shell :: Bash , & mut command, "tauri" , & mut buf) ;
58+ // use a function name that does not clash with other completions (clap names it `_tauri`)
59+ // and also register it for the `cargo-tauri` binary
60+ String :: from_utf8_lossy ( & buf)
61+ . replace ( "_tauri() {" , "_tauri_cli() {" )
62+ . replace ( "complete -F _tauri " , "complete -F _tauri_cli " )
63+ . replace ( " -o default tauri\n " , " -o default tauri cargo-tauri\n " )
64+ }
65+
4366fn get_completions ( shell : Shell , cmd : Command ) -> Result < String > {
4467 let completions = if shell == Shell :: Bash {
45- let mut completions =
46- String :: from_utf8_lossy ( & completions_for ( shell, "cargo" , cmd) ) . into_owned ( ) ;
47- for & manager in PKG_MANAGERS {
48- completions. push_str ( & format ! (
49- "complete -F _cargo -o bashdefault -o default {} tauri\n " ,
50- if manager == "npm" {
51- "npm run"
52- } else if manager == "bun" {
53- "bun run"
54- } else if manager == "deno" {
55- "deno task"
56- } else {
57- manager
58- }
59- ) ) ;
60- }
61- completions
68+ bash_completions ( cmd)
6269 } else {
6370 let mut buffer = String :: new ( ) ;
6471
@@ -101,3 +108,47 @@ pub fn command(options: Options, cmd: Command) -> Result<()> {
101108
102109 Ok ( ( ) )
103110}
111+
112+ #[ cfg( test) ]
113+ mod tests {
114+ use super :: * ;
115+ use clap:: CommandFactory ;
116+
117+ #[ test]
118+ fn bash_completions_do_not_hijack_other_commands ( ) {
119+ for bin_name in [ "cargo-tauri" , "cargo tauri" , "tauri" ] {
120+ let completions =
121+ get_completions ( Shell :: Bash , crate :: Cli :: command ( ) . bin_name ( bin_name) ) . unwrap ( ) ;
122+ assert ! ( completions. contains( "_tauri_cli() {" ) ) ;
123+ assert ! ( !completions. contains( "_tauri() {" ) ) ;
124+ assert ! ( !completions. contains( "_cargo" ) ) ;
125+
126+ let registrations: Vec < _ > = completions
127+ . lines ( )
128+ . filter ( |line| line. trim_start ( ) . starts_with ( "complete " ) )
129+ . collect ( ) ;
130+ assert ! ( !registrations. is_empty( ) ) ;
131+ for registration in registrations {
132+ assert ! (
133+ registration
134+ . trim_start( )
135+ . starts_with( "complete -F _tauri_cli " )
136+ ) ;
137+ assert ! ( registration. ends_with( " -o default tauri cargo-tauri" ) ) ;
138+ }
139+ }
140+ }
141+
142+ #[ test]
143+ fn completions_generate_for_every_shell ( ) {
144+ for shell in [ Shell :: Zsh , Shell :: Fish , Shell :: PowerShell , Shell :: Elvish ] {
145+ for bin_name in [ "cargo-tauri" , "cargo tauri" , "tauri" ] {
146+ assert ! (
147+ !get_completions( shell, crate :: Cli :: command( ) . bin_name( bin_name) )
148+ . unwrap( )
149+ . is_empty( )
150+ ) ;
151+ }
152+ }
153+ }
154+ }
0 commit comments