2020-10-18 14:52:13 +05:30
|
|
|
use std::process::Command;
|
2021-09-13 13:31:58 +05:30
|
|
|
use std::{env, io};
|
2020-10-18 14:52:13 +05:30
|
|
|
|
2021-08-18 21:55:25 +05:30
|
|
|
fn main() {
|
2021-10-17 12:11:19 +02:00
|
|
|
let pkg_version = env!("CARGO_PKG_VERSION");
|
2021-08-18 21:55:25 +05:30
|
|
|
let version = match env::var_os("PROFILE") {
|
2022-09-07 11:56:38 +05:30
|
|
|
Some(profile) if profile == "release" => format!("v{pkg_version}"),
|
|
|
|
_ => git_version().unwrap_or_else(|| format!("v{pkg_version}-unknown")),
|
2021-08-18 21:55:25 +05:30
|
|
|
};
|
2022-09-07 11:56:38 +05:30
|
|
|
println!("cargo:rustc-env=ZOXIDE_VERSION={version}");
|
2021-08-18 21:55:25 +05:30
|
|
|
|
2023-01-09 15:55:33 +05:30
|
|
|
// Since we are generating completions in the package directory, we need to set
|
|
|
|
// this so that Cargo doesn't rebuild every time.
|
2021-10-17 12:11:19 +02:00
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
2022-09-02 13:59:24 +05:30
|
|
|
println!("cargo:rerun-if-changed=src/");
|
|
|
|
println!("cargo:rerun-if-changed=templates/");
|
|
|
|
println!("cargo:rerun-if-changed=tests/");
|
2021-08-18 21:55:25 +05:30
|
|
|
|
|
|
|
generate_completions().unwrap();
|
|
|
|
}
|
|
|
|
|
2021-04-21 01:18:12 +05:30
|
|
|
fn git_version() -> Option<String> {
|
2021-10-17 12:11:19 +02:00
|
|
|
let dir = env!("CARGO_MANIFEST_DIR");
|
2021-04-21 01:18:12 +05:30
|
|
|
let mut git = Command::new("git");
|
2022-11-15 13:47:37 +05:30
|
|
|
git.args(["-C", dir, "describe", "--tags", "--match=v*.*.*", "--always", "--broken"]);
|
2021-04-21 01:18:12 +05:30
|
|
|
|
2021-05-03 02:46:37 +05:30
|
|
|
let output = git.output().ok()?;
|
2021-06-10 08:45:59 +05:30
|
|
|
if !output.status.success() || output.stdout.is_empty() || !output.stderr.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2021-05-03 02:46:37 +05:30
|
|
|
String::from_utf8(output.stdout).ok()
|
2021-04-21 01:18:12 +05:30
|
|
|
}
|
|
|
|
|
2021-08-15 17:26:56 +05:30
|
|
|
fn generate_completions() -> io::Result<()> {
|
2022-09-16 22:24:46 +05:30
|
|
|
#[path = "src/cmd/cmd.rs"]
|
2022-02-17 04:16:45 +05:30
|
|
|
mod cmd;
|
2021-05-04 02:42:43 +05:30
|
|
|
|
2022-02-17 04:16:45 +05:30
|
|
|
use clap::CommandFactory;
|
2022-01-03 05:52:55 +05:30
|
|
|
use clap_complete::generate_to;
|
2022-02-17 04:16:45 +05:30
|
|
|
use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh};
|
2022-01-03 05:52:55 +05:30
|
|
|
use clap_complete_fig::Fig;
|
2022-02-17 04:16:45 +05:30
|
|
|
use cmd::Cmd;
|
2021-05-04 02:42:43 +05:30
|
|
|
|
2022-02-17 04:16:45 +05:30
|
|
|
let cmd = &mut Cmd::command();
|
2021-10-17 12:11:19 +02:00
|
|
|
let bin_name = env!("CARGO_PKG_NAME");
|
2021-05-04 02:42:43 +05:30
|
|
|
let out_dir = "contrib/completions";
|
|
|
|
|
2022-02-17 04:16:45 +05:30
|
|
|
generate_to(Bash, cmd, bin_name, out_dir)?;
|
|
|
|
generate_to(Elvish, cmd, bin_name, out_dir)?;
|
|
|
|
generate_to(Fig, cmd, bin_name, out_dir)?;
|
|
|
|
generate_to(Fish, cmd, bin_name, out_dir)?;
|
|
|
|
generate_to(PowerShell, cmd, bin_name, out_dir)?;
|
|
|
|
generate_to(Zsh, cmd, bin_name, out_dir)?;
|
2021-08-15 17:26:56 +05:30
|
|
|
|
|
|
|
Ok(())
|
2021-05-04 02:42:43 +05:30
|
|
|
}
|