|
| 1 | +//! Build script: generates man pages and shell completions at build time. |
| 2 | +//! |
| 3 | +//! Man pages go to $OUT_DIR/man/ and completions to $OUT_DIR/completions/. |
| 4 | +//! Only runs in release builds or when ASH_GEN_ARTIFACTS=1 is set. |
| 5 | +//! |
| 6 | +//! The CLI definitions are included directly from src/cli.rs via include!() |
| 7 | +//! so this script has no dependency on the ash library itself. |
| 8 | +
|
| 9 | +include!("src/cli.rs"); |
| 10 | + |
| 11 | +fn main() { |
| 12 | + // Only generate in release builds or when explicitly requested |
| 13 | + let gen = std::env::var("ASH_GEN_ARTIFACTS").is_ok() |
| 14 | + || std::env::var("PROFILE").map(|p| p == "release").unwrap_or(false); |
| 15 | + |
| 16 | + if !gen { |
| 17 | + println!("cargo:rerun-if-changed=src/cli.rs"); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + let out_dir = std::env::var("OUT_DIR").unwrap(); |
| 22 | + let out_dir = std::path::Path::new(&out_dir); |
| 23 | + |
| 24 | + generate_man_pages(out_dir); |
| 25 | + generate_completions(out_dir); |
| 26 | +} |
| 27 | + |
| 28 | +fn generate_man_pages(out_dir: &std::path::Path) { |
| 29 | + use clap::CommandFactory; |
| 30 | + use clap_mangen::Man; |
| 31 | + |
| 32 | + let man_dir = out_dir.join("man"); |
| 33 | + std::fs::create_dir_all(&man_dir).unwrap(); |
| 34 | + |
| 35 | + let cmd = Cli::command(); |
| 36 | + |
| 37 | + // Top-level: ash(1) |
| 38 | + let man = Man::new(cmd.clone()); |
| 39 | + let mut buf = Vec::new(); |
| 40 | + man.render(&mut buf).unwrap(); |
| 41 | + std::fs::write(man_dir.join("ash.1"), buf).unwrap(); |
| 42 | + |
| 43 | + // Subcommands: ash-grep(1), ash-edit(1), etc. |
| 44 | + for sub in cmd.get_subcommands() { |
| 45 | + if sub.get_name() == "help" { |
| 46 | + continue; |
| 47 | + } |
| 48 | + let name = format!("ash-{}", sub.get_name()); |
| 49 | + let name_static: &'static str = name.clone().leak(); |
| 50 | + let sub_cmd = sub.clone().name(name_static); |
| 51 | + let man = Man::new(sub_cmd.clone()); |
| 52 | + let mut buf = Vec::new(); |
| 53 | + man.render(&mut buf).unwrap(); |
| 54 | + std::fs::write(man_dir.join(format!("{name}.1")), buf).unwrap(); |
| 55 | + |
| 56 | + // Nested: ash-edit-view(1), ash-terminal-start(1), etc. |
| 57 | + for nested in sub_cmd.get_subcommands() { |
| 58 | + if nested.get_name() == "help" { |
| 59 | + continue; |
| 60 | + } |
| 61 | + let nested_name = format!("{name}-{}", nested.get_name()); |
| 62 | + let nested_static: &'static str = nested_name.clone().leak(); |
| 63 | + let nested_cmd = nested.clone().name(nested_static); |
| 64 | + let man = Man::new(nested_cmd); |
| 65 | + let mut buf = Vec::new(); |
| 66 | + man.render(&mut buf).unwrap(); |
| 67 | + std::fs::write(man_dir.join(format!("{nested_name}.1")), buf).unwrap(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + println!("cargo:rerun-if-changed=src/cli.rs"); |
| 72 | +} |
| 73 | + |
| 74 | +fn generate_completions(out_dir: &std::path::Path) { |
| 75 | + use clap::CommandFactory; |
| 76 | + use clap_complete::{generate_to, Shell}; |
| 77 | + |
| 78 | + let comp_dir = out_dir.join("completions"); |
| 79 | + std::fs::create_dir_all(&comp_dir).unwrap(); |
| 80 | + |
| 81 | + let mut cmd = Cli::command(); |
| 82 | + for shell in [Shell::Bash, Shell::Zsh, Shell::Fish, Shell::Elvish, Shell::PowerShell] { |
| 83 | + let _ = generate_to(shell, &mut cmd, "ash", &comp_dir); |
| 84 | + } |
| 85 | + |
| 86 | + println!("cargo:rerun-if-changed=src/cli.rs"); |
| 87 | +} |
0 commit comments