| 1 |
//! General build script used by bottom to generate completion files and set |
| 2 |
//! binary version. |
| 3 |
|
| 4 |
#![expect(clippy::unwrap_used)] |
| 5 |
|
| 6 |
#[expect(dead_code)] |
| 7 |
#[path = "src/options/args.rs"] |
| 8 |
mod args; |
| 9 |
|
| 10 |
use std::{ |
| 11 |
env, fs, io, |
| 12 |
path::{Path, PathBuf}, |
| 13 |
}; |
| 14 |
|
| 15 |
use clap::{Command, CommandFactory}; |
| 16 |
use clap_complete::{Generator, generate_to, shells::Shell}; |
| 17 |
use clap_complete_nushell::Nushell; |
| 18 |
|
| 19 |
use crate::args::BottomArgs; |
| 20 |
|
| 21 |
fn create_dir(dir: &Path) -> io::Result<()> { |
| 22 |
fs::create_dir_all(dir).inspect_err(|err| { |
| 23 |
eprintln!( |
| 24 |
"Couldn't create a directory at {} ({:?}). Aborting.", |
| 25 |
dir.display(), |
| 26 |
err |
| 27 |
) |
| 28 |
}) |
| 29 |
} |
| 30 |
|
| 31 |
fn generate_completions<G>(to_generate: G, cmd: &mut Command, out_dir: &Path) -> io::Result<PathBuf> |
| 32 |
where |
| 33 |
G: Generator, |
| 34 |
{ |
| 35 |
generate_to(to_generate, cmd, "btm", out_dir) |
| 36 |
} |
| 37 |
|
| 38 |
fn btm_generate() -> io::Result<()> { |
| 39 |
const ENV_KEY: &str = "BTM_GENERATE"; |
| 40 |
|
| 41 |
match env::var_os(ENV_KEY) { |
| 42 |
Some(var) if !var.is_empty() => { |
| 43 |
let completion_dir = |
| 44 |
option_env!("COMPLETION_DIR").unwrap_or("./target/tmp/bottom/completion/"); |
| 45 |
let manpage_dir = option_env!("MANPAGE_DIR").unwrap_or("./target/tmp/bottom/manpage/"); |
| 46 |
|
| 47 |
let completion_out_dir = PathBuf::from(completion_dir); |
| 48 |
let manpage_out_dir = PathBuf::from(manpage_dir); |
| 49 |
|
| 50 |
create_dir(&completion_out_dir)?; |
| 51 |
create_dir(&manpage_out_dir)?; |
| 52 |
|
| 53 |
// Generate completions |
| 54 |
let mut app = BottomArgs::command(); |
| 55 |
generate_completions(Shell::Bash, &mut app, &completion_out_dir)?; |
| 56 |
generate_completions(Shell::Zsh, &mut app, &completion_out_dir)?; |
| 57 |
generate_completions(Shell::Fish, &mut app, &completion_out_dir)?; |
| 58 |
generate_completions(Shell::PowerShell, &mut app, &completion_out_dir)?; |
| 59 |
generate_completions(Shell::Elvish, &mut app, &completion_out_dir)?; |
| 60 |
generate_completions(Nushell, &mut app, &completion_out_dir)?; |
| 61 |
|
| 62 |
// Generate manpage |
| 63 |
let app = app.name("btm"); |
| 64 |
let man = clap_mangen::Man::new(app); |
| 65 |
let mut buffer: Vec<u8> = Default::default(); |
| 66 |
man.render(&mut buffer)?; |
| 67 |
fs::write(manpage_out_dir.join("btm.1"), buffer)?; |
| 68 |
} |
| 69 |
_ => {} |
| 70 |
} |
| 71 |
|
| 72 |
println!("cargo:rerun-if-env-changed={ENV_KEY}"); |
| 73 |
|
| 74 |
Ok(()) |
| 75 |
} |
| 76 |
|
| 77 |
fn extract_sha(sha: Option<&str>) -> Option<&str> { |
| 78 |
sha.and_then(|sha: &str| sha.get(0..8)) |
| 79 |
} |
| 80 |
|
| 81 |
fn output_nightly_version(version: &str, git_hash: &str) { |
| 82 |
println!("cargo:rustc-env=NIGHTLY_VERSION={version}-nightly-{git_hash}"); |
| 83 |
} |
| 84 |
|
| 85 |
fn nightly_version() { |
| 86 |
const ENV_KEY: &str = "BTM_BUILD_RELEASE_CALLER"; |
| 87 |
|
| 88 |
match env::var_os(ENV_KEY) { |
| 89 |
Some(var) if !var.is_empty() && var == "ci" => { |
| 90 |
let version = env!("CARGO_PKG_VERSION"); |
| 91 |
|
| 92 |
if let Some(hash) = extract_sha(option_env!("GITHUB_SHA")) { |
| 93 |
// May be set if we're building with GHA. |
| 94 |
output_nightly_version(version, hash); |
| 95 |
} else if let Ok(output) = std::process::Command::new("git") |
| 96 |
.args(["rev-parse", "--short=8", "HEAD"]) |
| 97 |
.output() |
| 98 |
{ |
| 99 |
// If we're not building in either, we do the lazy thing and fall back to |
| 100 |
// manually grabbing info using git as a command. |
| 101 |
let hash = String::from_utf8(output.stdout).unwrap(); |
| 102 |
output_nightly_version(version, &hash); |
| 103 |
} |
| 104 |
} |
| 105 |
_ => {} |
| 106 |
} |
| 107 |
|
| 108 |
println!("cargo:rerun-if-env-changed={ENV_KEY}"); |
| 109 |
} |
| 110 |
|
| 111 |
fn main() -> io::Result<()> { |
| 112 |
btm_generate()?; |
| 113 |
nightly_version(); |
| 114 |
|
| 115 |
Ok(()) |
| 116 |
} |