From a6e9cebf92591fc1f44bcb30af335a4a0792a7e2 Mon Sep 17 00:00:00 2001 From: Liam Dawson Date: Thu, 14 Mar 2024 14:47:46 +1100 Subject: [PATCH] Update shell completions destination --- .gitignore | 1 + Cargo.toml | 12 ++++++------ build.rs | 40 +++++++++++++++++++--------------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 95e4ffb..ed66917 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /ref/ +/completions/ diff --git a/Cargo.toml b/Cargo.toml index 642a0cc..b5b934e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,12 +61,12 @@ targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux- pr-run-mode = "plan" include = [ - "target/release/_buildkite_waiter", - "target/release/_buildkite_waiter.ps1", - "target/release/buildkite_waiter.bash", - "target/release/buildkite_waiter.elv", - "target/release/buildkite_waiter.fish", - "target/release/buildkite_waiter.ts", + "completions/_buildkite_waiter", + "completions/_buildkite_waiter.ps1", + "completions/buildkite_waiter.bash", + "completions/buildkite_waiter.elv", + "completions/buildkite_waiter.fish", + "completions/buildkite_waiter.ts", ] [workspace.metadata.dist.dependencies.apt] diff --git a/build.rs b/build.rs index 7d4bcef..53e049b 100644 --- a/build.rs +++ b/build.rs @@ -5,33 +5,25 @@ use clap::CommandFactory; use clap_complete::Shell; use cli::Cli; use std::ffi::OsStr; -use std::fs::File; use std::{env, path::PathBuf}; fn main() { - let target_dir = match env::var_os("OUT_DIR") { - // if no OUT_DIR, no need to write man/completions - None => return, - Some(outdir) => PathBuf::from(outdir), - }; - - let mut completion_dirs = vec![target_dir.clone()]; + if env::var("PROFILE") != Ok("release".to_string()) { + // only generate completions on release builds + return; + } - if Ok("release".to_string()) == env::var("PROFILE") { - if let Some(manifest_dir) = env::var_os("CARGO_MANIFEST_DIR") { - completion_dirs.push(PathBuf::from(manifest_dir).join("target").join("release")); - } + let target_dir = match env::var_os("CARGO_MANIFEST_DIR") { + // if no CARGO_MANIFEST_DIR, don't completions + None => return, + Some(outdir) => PathBuf::from(outdir).join("completions"), }; - dbg!(&completion_dirs); - - for target_dir in completion_dirs { - generate_completions(target_dir.as_os_str()); + if !target_dir.exists() { + std::fs::create_dir_all(&target_dir).expect("should find or create completions directory"); } - let anchor = PathBuf::from(&target_dir).join("buildkite_waiter-stamp"); - - File::create(anchor).unwrap(); + generate_completions(target_dir.as_os_str()); } fn generate_completions(target_dir: &OsStr) { @@ -46,13 +38,19 @@ fn generate_completions(target_dir: &OsStr) { Shell::Zsh, ] { if let Err(err) = clap_complete::generate_to(*shell, &mut cmd, bin_name, target_dir) { - panic!("failed to generate completions for {:?} in {:?}: {:?}", shell, target_dir, err); + panic!( + "failed to generate completions for {:?} in {:?}: {:?}", + shell, target_dir, err + ); } } if let Err(err) = clap_complete::generate_to(clap_complete_fig::Fig, &mut cmd, bin_name, target_dir) { - panic!("failed to generate completions for fig in {:?}: {:?}", target_dir, err); + panic!( + "failed to generate completions for fig in {:?}: {:?}", + target_dir, err + ); } }