|  | 
| 5 | 5 | 
 | 
| 6 | 6 | use std::path::PathBuf; | 
| 7 | 7 | 
 | 
|  | 8 | +use clap_complete::{Generator, shells}; | 
|  | 9 | + | 
| 8 | 10 | use crate::core::build_steps::dist::distdir; | 
| 9 | 11 | use crate::core::build_steps::test; | 
| 10 | 12 | use crate::core::build_steps::tool::{self, SourceType, Tool}; | 
| @@ -285,36 +287,35 @@ impl Step for GenerateWindowsSys { | 
| 285 | 287 |     } | 
| 286 | 288 | } | 
| 287 | 289 | 
 | 
|  | 290 | +/// Return tuples of (shell, file containing completions). | 
|  | 291 | +pub fn get_completion_paths(builder: &Builder<'_>) -> Vec<(&'static dyn Generator, PathBuf)> { | 
|  | 292 | +    vec![ | 
|  | 293 | +        (&shells::Bash as &'static dyn Generator, builder.src.join("src/etc/completions/x.py.sh")), | 
|  | 294 | +        (&shells::Zsh, builder.src.join("src/etc/completions/x.py.zsh")), | 
|  | 295 | +        (&shells::Fish, builder.src.join("src/etc/completions/x.py.fish")), | 
|  | 296 | +        (&shells::PowerShell, builder.src.join("src/etc/completions/x.py.ps1")), | 
|  | 297 | +        (&shells::Bash, builder.src.join("src/etc/completions/x.sh")), | 
|  | 298 | +        (&shells::Zsh, builder.src.join("src/etc/completions/x.zsh")), | 
|  | 299 | +        (&shells::Fish, builder.src.join("src/etc/completions/x.fish")), | 
|  | 300 | +        (&shells::PowerShell, builder.src.join("src/etc/completions/x.ps1")), | 
|  | 301 | +    ] | 
|  | 302 | +} | 
|  | 303 | + | 
| 288 | 304 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 
| 289 | 305 | pub struct GenerateCompletions; | 
| 290 | 306 | 
 | 
| 291 |  | -macro_rules! generate_completions { | 
| 292 |  | -    ( $( ( $shell:ident, $filename:expr ) ),* ) => { | 
| 293 |  | -        $( | 
| 294 |  | -            if let Some(comp) = get_completion($shell, &$filename) { | 
| 295 |  | -                std::fs::write(&$filename, comp).expect(&format!("writing {} completion", stringify!($shell))); | 
| 296 |  | -            } | 
| 297 |  | -        )* | 
| 298 |  | -    }; | 
| 299 |  | -} | 
| 300 |  | - | 
| 301 | 307 | impl Step for GenerateCompletions { | 
| 302 | 308 |     type Output = (); | 
| 303 | 309 | 
 | 
| 304 | 310 |     /// Uses `clap_complete` to generate shell completions. | 
| 305 | 311 |     fn run(self, builder: &Builder<'_>) { | 
| 306 |  | -        use clap_complete::shells::{Bash, Fish, PowerShell, Zsh}; | 
| 307 |  | - | 
| 308 |  | -        generate_completions!( | 
| 309 |  | -            (Bash, builder.src.join("src/etc/completions/x.py.sh")), | 
| 310 |  | -            (Zsh, builder.src.join("src/etc/completions/x.py.zsh")), | 
| 311 |  | -            (Fish, builder.src.join("src/etc/completions/x.py.fish")), | 
| 312 |  | -            (PowerShell, builder.src.join("src/etc/completions/x.py.ps1")), | 
| 313 |  | -            (Bash, builder.src.join("src/etc/completions/x.sh")), | 
| 314 |  | -            (Zsh, builder.src.join("src/etc/completions/x.zsh")), | 
| 315 |  | -            (Fish, builder.src.join("src/etc/completions/x.fish")), | 
| 316 |  | -            (PowerShell, builder.src.join("src/etc/completions/x.ps1")) | 
| 317 |  | -        ); | 
|  | 312 | +        for (shell, path) in get_completion_paths(builder) { | 
|  | 313 | +            if let Some(comp) = get_completion(shell, &path) { | 
|  | 314 | +                std::fs::write(&path, comp).unwrap_or_else(|e| { | 
|  | 315 | +                    panic!("writing completion into {} failed: {e:?}", path.display()) | 
|  | 316 | +                }); | 
|  | 317 | +            } | 
|  | 318 | +        } | 
| 318 | 319 |     } | 
| 319 | 320 | 
 | 
| 320 | 321 |     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { | 
|  | 
0 commit comments