From e77930d2df9067dd3f915a31e1b7697cc3bf1e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sun, 5 Jun 2022 12:43:58 -0300 Subject: [PATCH 1/4] fix build script not detecting new env var changes --- build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build.rs b/build.rs index aa54539..ca0b8de 100644 --- a/build.rs +++ b/build.rs @@ -42,6 +42,7 @@ include!("src/opts.rs"); fn main() { println!("cargo:rerun-if-env-changed=GEN_COMPLETIONS"); + println!("cargo:rerun-if-env-changed=OUCH_COMPLETIONS_FOLDER"); if let Some(completions_output_directory) = detect_completions_output_directory() { create_dir_all(&completions_output_directory).expect("Could not create shell completions output folder."); From 2e165e57cb339c42622d154fe5430000f8b51078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sun, 5 Jun 2022 12:52:20 -0300 Subject: [PATCH 2/4] fix rustdoc lint warnings --- src/opts.rs | 4 ++++ src/utils/fs.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/opts.rs b/src/opts.rs index e33fbdc..b42b225 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -10,6 +10,9 @@ use clap::{Parser, ValueHint}; /// Repository: https://github.com/ouch-org/ouch #[derive(Parser, Debug)] #[clap(about, version)] +// Ignore bare urls in the documentation of this file because the doc comments +// are also being used by Clap's --help generation +#[allow(rustdoc::bare_urls)] pub struct Opts { /// Skip [Y/n] questions positively. #[clap(short, long, conflicts_with = "no", global = true)] @@ -48,6 +51,7 @@ pub struct Opts { // Clap commands: // - `help` #[derive(Parser, PartialEq, Eq, Debug)] +#[allow(rustdoc::bare_urls)] pub enum Subcommand { /// Compress one or more files into one output file. #[clap(alias = "c")] diff --git a/src/utils/fs.rs b/src/utils/fs.rs index ec321bd..96f5523 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -126,8 +126,8 @@ pub fn try_infer_extension(path: &Path) -> Option { } /// Returns true if a path is a symlink. -/// This is the same as the nightly https://doc.rust-lang.org/std/path/struct.Path.html#method.is_symlink -// Useful to detect broken symlinks when compressing. (So we can safely ignore them) +/// This is the same as the nightly +/// Useful to detect broken symlinks when compressing. (So we can safely ignore them) pub fn is_symlink(path: &Path) -> bool { fs::symlink_metadata(path) .map(|m| m.file_type().is_symlink()) From b05078489bfbb0b3a3016976ec42d9f7916ce9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sun, 5 Jun 2022 15:46:20 -0300 Subject: [PATCH 3/4] only generate completions for bash, zsh and fish --- build.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index ca0b8de..e830507 100644 --- a/build.rs +++ b/build.rs @@ -35,9 +35,11 @@ /// The _"195b34a8adca6ec3"_ part is a hash that might change between runs. use std::{env, fs::create_dir_all, path::Path}; -use clap::{ArgEnum, IntoApp}; +use clap::IntoApp; use clap_complete::{generate_to, Shell}; +const TARGET_SHELLS: &[Shell] = &[Shell::Bash, Shell::Zsh, Shell::Fish]; + include!("src/opts.rs"); fn main() { @@ -48,7 +50,7 @@ fn main() { create_dir_all(&completions_output_directory).expect("Could not create shell completions output folder."); let app = &mut Opts::command(); - for shell in Shell::value_variants() { + for shell in TARGET_SHELLS { generate_to(*shell, app, "ouch", &completions_output_directory) .unwrap_or_else(|err| panic!("Failed to generate shell completions for {}: {}.", shell, err)); } From 3552e21533689bae49309212d73c6cd0a9033413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sun, 5 Jun 2022 20:25:51 -0300 Subject: [PATCH 4/4] create a folder for each shell completion generated --- build.rs | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index e830507..fc7d954 100644 --- a/build.rs +++ b/build.rs @@ -33,7 +33,7 @@ /// - `target/debug/build/ouch-195b34a8adca6ec3/out/completions` /// /// The _"195b34a8adca6ec3"_ part is a hash that might change between runs. -use std::{env, fs::create_dir_all, path::Path}; +use std::{env, fs, path::Path}; use clap::IntoApp; use clap_complete::{generate_to, Shell}; @@ -46,14 +46,28 @@ fn main() { println!("cargo:rerun-if-env-changed=GEN_COMPLETIONS"); println!("cargo:rerun-if-env-changed=OUCH_COMPLETIONS_FOLDER"); - if let Some(completions_output_directory) = detect_completions_output_directory() { - create_dir_all(&completions_output_directory).expect("Could not create shell completions output folder."); - let app = &mut Opts::command(); + let completions_output_directory = match detect_completions_output_directory() { + Some(inner) => inner, + _ => return, + }; - for shell in TARGET_SHELLS { - generate_to(*shell, app, "ouch", &completions_output_directory) - .unwrap_or_else(|err| panic!("Failed to generate shell completions for {}: {}.", shell, err)); - } + fs::create_dir_all(&completions_output_directory).expect("Could not create shell completions output folder."); + + let app = &mut Opts::command(); + + for shell in TARGET_SHELLS { + let target_directory = if env::var_os("OUCH_COMPLETIONS_FOLDER").is_some() { + let shell_name = shell.to_string(); + + let shell_dir = completions_output_directory.join(&shell_name); + fs::create_dir(&shell_dir).expect("Failed to create directory for shell completions"); + + shell_dir + } else { + completions_output_directory.clone() + }; + generate_to(*shell, app, "ouch", &target_directory) + .unwrap_or_else(|err| panic!("Failed to generate shell completions for {}: {}.", shell, err)); } } @@ -66,6 +80,10 @@ fn detect_completions_output_directory() -> Option { return Some(dir.into()); }; + get_deprecated_completions_directory() +} + +fn get_deprecated_completions_directory() -> Option { // If set, directory goes inside of cargo's `target/` let gen_completions = env::var_os("GEN_COMPLETIONS").map(|var| &var == "1").unwrap_or(false); if gen_completions {