Skip to content

Commit 86d7d40

Browse files
committed
optimize GenerateCompletions::run
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 02c3f4d commit 86d7d40

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/bootstrap/run.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::path::PathBuf;
22
use std::process::Command;
33

4-
use clap_complete::shells;
5-
64
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
75
use crate::config::TargetSelection;
86
use crate::dist::distdir;
@@ -268,26 +266,29 @@ impl Step for GenerateWindowsSys {
268266
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
269267
pub struct GenerateCompletions;
270268

269+
macro_rules! generate_completions {
270+
( $( ( $shell:ident, $filename:expr ) ),* ) => {
271+
$(
272+
if let Some(comp) = get_completion($shell, &$filename) {
273+
std::fs::write(&$filename, comp).expect(&format!("writing {} completion", stringify!($shell)));
274+
}
275+
)*
276+
};
277+
}
278+
271279
impl Step for GenerateCompletions {
272280
type Output = ();
273281

274282
/// Uses `clap_complete` to generate shell completions.
275283
fn run(self, builder: &Builder<'_>) {
276-
let [bash, zsh, fish, powershell] = ["x.py.sh", "x.py.zsh", "x.py.fish", "x.py.ps1"]
277-
.map(|filename| builder.src.join("src/etc/completions").join(filename));
278-
279-
if let Some(comp) = get_completion(shells::Bash, &bash) {
280-
std::fs::write(&bash, comp).expect("writing bash completion");
281-
}
282-
if let Some(comp) = get_completion(shells::Zsh, &zsh) {
283-
std::fs::write(&zsh, comp).expect("writing bash completion");
284-
}
285-
if let Some(comp) = get_completion(shells::Fish, &fish) {
286-
std::fs::write(&fish, comp).expect("writing fish completion");
287-
}
288-
if let Some(comp) = get_completion(shells::PowerShell, &powershell) {
289-
std::fs::write(&powershell, comp).expect("writing powershell completion");
290-
}
284+
use clap_complete::shells::{Bash, Fish, PowerShell, Zsh};
285+
286+
generate_completions!(
287+
(Bash, builder.src.join("src/etc/completions/x.py.sh")),
288+
(Zsh, builder.src.join("src/etc/completions/x.py.zsh")),
289+
(Fish, builder.src.join("src/etc/completions/x.py.fish")),
290+
(PowerShell, builder.src.join("src/etc/completions/x.py.ps1"))
291+
);
291292
}
292293

293294
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {

0 commit comments

Comments
 (0)