Merge pull request #403 from ouch-org/dependabot/cargo/clap-4.2.3
[ouch.git] / build.rs
blobc7a1f91aa2f09119f6b5b687fe402148d29be65f
1 /// This build script checks for env vars to build ouch with shell completions and man pages.
2 ///
3 /// # How to generate shell completions and man pages:
4 ///
5 /// Set `OUCH_ARTIFACTS_FOLDER` to the name of the destination folder:
6 ///
7 /// ```sh
8 /// OUCH_ARTIFACTS_FOLDER=my-folder cargo build
9 /// ```
10 ///
11 /// All completion files will be generated inside of the folder "my-folder".
12 ///
13 /// If the folder does not exist, it will be created.
14 ///
15 /// We recommend you naming this folder "artifacts" for the sake of consistency.
16 ///
17 /// ```sh
18 /// OUCH_ARTIFACTS_FOLDER=artifacts cargo build
19 /// ```
20 use std::{
21     env,
22     fs::{create_dir_all, File},
23     path::Path,
26 use clap::{CommandFactory, ValueEnum};
27 use clap_complete::{generate_to, Shell};
28 use clap_mangen::Man;
30 include!("src/cli/args.rs");
32 fn main() {
33     println!("cargo:rerun-if-env-changed=OUCH_ARTIFACTS_FOLDER");
35     if let Some(dir) = env::var_os("OUCH_ARTIFACTS_FOLDER") {
36         let out = &Path::new(&dir);
37         create_dir_all(out).unwrap();
38         let cmd = &mut CliArgs::command();
40         Man::new(cmd.clone())
41             .render(&mut File::create(out.join("ouch.1")).unwrap())
42             .unwrap();
44         for subcmd in cmd.get_subcommands() {
45             let name = format!("ouch-{}", subcmd.get_name());
46             Man::new(subcmd.clone().name(&name))
47                 .render(&mut File::create(out.join(format!("{name}.1"))).unwrap())
48                 .unwrap();
49         }
51         for shell in Shell::value_variants() {
52             generate_to(*shell, cmd, "ouch", out).unwrap();
53         }
54     }