2022-03-11 14:45:05 +03:00
|
|
|
use std::{
|
2023-01-02 23:18:05 +03:00
|
|
|
env, fs, io,
|
2022-03-11 14:45:05 +03:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
2023-09-12 10:36:45 +03:00
|
|
|
use clap_complete::{generate_to, shells::Shell, Generator};
|
|
|
|
use clap_complete_fig::Fig;
|
|
|
|
use clap_complete_nushell::Nushell;
|
2022-11-10 09:22:19 +03:00
|
|
|
|
2023-06-19 08:47:57 +03:00
|
|
|
include!("src/args.rs");
|
2022-03-11 14:45:05 +03:00
|
|
|
|
2023-01-02 23:18:05 +03:00
|
|
|
fn create_dir(dir: &Path) -> io::Result<()> {
|
2022-11-04 11:17:22 +03:00
|
|
|
let res = fs::create_dir_all(dir);
|
2022-03-11 14:45:05 +03:00
|
|
|
match &res {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(err) => {
|
2023-06-20 08:52:54 +03:00
|
|
|
eprintln!("Failed to create a directory at location {dir:?}, encountered error {err:?}. Aborting...",);
|
2022-03-11 14:45:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2023-09-12 10:36:45 +03:00
|
|
|
fn generate_completions<G>(to_generate: G, cmd: &mut Command, out_dir: &Path) -> io::Result<PathBuf>
|
|
|
|
where
|
|
|
|
G: Generator,
|
|
|
|
{
|
|
|
|
generate_to(to_generate, cmd, "btm", out_dir)
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:18:05 +03:00
|
|
|
fn btm_generate() -> io::Result<()> {
|
|
|
|
const ENV_KEY: &str = "BTM_GENERATE";
|
2022-08-31 01:25:16 +03:00
|
|
|
|
2023-01-02 23:18:05 +03:00
|
|
|
match env::var_os(ENV_KEY) {
|
2022-08-31 01:25:16 +03:00
|
|
|
Some(var) if !var.is_empty() => {
|
2023-01-02 23:18:05 +03:00
|
|
|
const COMPLETION_DIR: &str = "./target/tmp/bottom/completion/";
|
|
|
|
const MANPAGE_DIR: &str = "./target/tmp/bottom/manpage/";
|
|
|
|
|
2022-08-31 01:25:16 +03:00
|
|
|
let completion_out_dir = PathBuf::from(COMPLETION_DIR);
|
|
|
|
let manpage_out_dir = PathBuf::from(MANPAGE_DIR);
|
|
|
|
|
|
|
|
create_dir(&completion_out_dir)?;
|
|
|
|
create_dir(&manpage_out_dir)?;
|
|
|
|
|
|
|
|
// Generate completions
|
|
|
|
let mut app = build_app();
|
2023-09-12 10:36:45 +03:00
|
|
|
generate_completions(Shell::Bash, &mut app, &completion_out_dir)?;
|
|
|
|
generate_completions(Shell::Zsh, &mut app, &completion_out_dir)?;
|
|
|
|
generate_completions(Shell::Fish, &mut app, &completion_out_dir)?;
|
|
|
|
generate_completions(Shell::PowerShell, &mut app, &completion_out_dir)?;
|
|
|
|
generate_completions(Shell::Elvish, &mut app, &completion_out_dir)?;
|
|
|
|
generate_completions(Fig, &mut app, &completion_out_dir)?;
|
|
|
|
generate_completions(Nushell, &mut app, &completion_out_dir)?;
|
2022-08-31 01:25:16 +03:00
|
|
|
|
|
|
|
// Generate manpage
|
|
|
|
let app = app.name("btm");
|
|
|
|
let man = clap_mangen::Man::new(app);
|
|
|
|
let mut buffer: Vec<u8> = Default::default();
|
|
|
|
man.render(&mut buffer)?;
|
2023-10-23 05:29:03 +03:00
|
|
|
fs::write(manpage_out_dir.join("btm.1"), buffer)?;
|
2022-08-31 01:25:16 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
2022-03-11 14:45:05 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 23:18:05 +03:00
|
|
|
println!("cargo:rerun-if-env-changed={ENV_KEY}");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:52:54 +03:00
|
|
|
fn extract_sha(sha: Option<&str>) -> Option<&str> {
|
|
|
|
sha.and_then(|sha: &str| sha.get(0..8))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn output_nightly_version(version: &str, git_hash: &str) {
|
|
|
|
println!("cargo:rustc-env=NIGHTLY_VERSION={version}-nightly-{git_hash}");
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:18:05 +03:00
|
|
|
fn nightly_version() {
|
|
|
|
const ENV_KEY: &str = "BTM_BUILD_RELEASE_CALLER";
|
|
|
|
|
|
|
|
match env::var_os(ENV_KEY) {
|
|
|
|
Some(var) if !var.is_empty() && var == "nightly" => {
|
|
|
|
let version = env!("CARGO_PKG_VERSION");
|
|
|
|
|
2023-06-20 08:52:54 +03:00
|
|
|
if let Some(hash) = extract_sha(option_env!("CIRRUS_CHANGE_IN_REPO")) {
|
|
|
|
// May be set if we're building with Cirrus CI.
|
|
|
|
output_nightly_version(version, hash);
|
|
|
|
} else if let Some(hash) = extract_sha(option_env!("GITHUB_SHA")) {
|
|
|
|
// May be set if we're building with GHA.
|
|
|
|
output_nightly_version(version, hash);
|
2023-01-02 23:18:05 +03:00
|
|
|
} else if let Ok(output) = std::process::Command::new("git")
|
2023-01-12 11:41:15 +03:00
|
|
|
.args(["rev-parse", "--short=8", "HEAD"])
|
2023-01-02 23:18:05 +03:00
|
|
|
.output()
|
|
|
|
{
|
2023-06-20 08:52:54 +03:00
|
|
|
// If we're not building in either, we do the lazy thing and fall back to
|
|
|
|
// manually grabbing info using git as a command.
|
|
|
|
let hash = String::from_utf8(output.stdout).unwrap();
|
|
|
|
output_nightly_version(version, &hash);
|
2023-01-02 23:18:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-env-changed={ENV_KEY}");
|
|
|
|
println!("cargo:rerun-if-env-changed=CIRRUS_CHANGE_IN_REPO");
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:07:34 +03:00
|
|
|
fn main() -> io::Result<()> {
|
2023-01-02 23:18:05 +03:00
|
|
|
btm_generate()?;
|
|
|
|
nightly_version();
|
2022-03-11 14:45:05 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|