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