2023-05-21 23:52:00 +00:00
|
|
|
#![allow(clippy::redundant_clone)]
|
2023-05-14 19:54:17 +00:00
|
|
|
use std::{env, fs, path::Path};
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
2023-05-21 23:52:00 +00:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
const RELEASE_TYPE: &str = "Nightly";
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
const RELEASE_TYPE: &str = "Debug";
|
|
|
|
|
2023-05-14 19:54:17 +00:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
|
|
|
println!("cargo:rerun-if-env-changed=RELEASE_TAG_NAME");
|
|
|
|
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
|
|
|
|
|
2023-05-21 23:52:00 +00:00
|
|
|
let (tag, release, version, commit, full_version) = {
|
2023-05-14 19:54:17 +00:00
|
|
|
let tag = env::var("RELEASE_TAG_NAME").unwrap_or(String::from("nightly"));
|
|
|
|
|
2023-05-21 23:52:00 +00:00
|
|
|
if tag.starts_with('v') {
|
|
|
|
(
|
|
|
|
tag.clone(),
|
|
|
|
"Stable",
|
|
|
|
env::var("CARGO_PKG_VERSION")?,
|
|
|
|
git2::Oid::zero(),
|
|
|
|
tag.clone(),
|
|
|
|
)
|
2023-05-14 19:54:17 +00:00
|
|
|
} else {
|
2023-05-21 23:52:00 +00:00
|
|
|
let (commit, full_version) = match get_commit() {
|
|
|
|
Some(id) => (id, format!("{tag}-{} {id}", &id.to_string()[..7])),
|
|
|
|
None => (git2::Oid::zero(), tag.to_string()),
|
|
|
|
};
|
2023-05-14 19:54:17 +00:00
|
|
|
|
2023-05-21 23:52:00 +00:00
|
|
|
(tag.clone(), RELEASE_TYPE, tag.clone(), commit, full_version)
|
|
|
|
}
|
2023-05-14 19:54:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let meta_file = Path::new(&env::var("OUT_DIR")?).join("meta.rs");
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
let meta = format!(r#"
|
|
|
|
pub const TAG: &str = "{tag}";
|
2023-05-21 23:52:00 +00:00
|
|
|
pub const COMMIT: &str = "{commit}";
|
2023-05-14 19:54:17 +00:00
|
|
|
pub const VERSION: &str = "{version}";
|
2023-05-21 23:52:00 +00:00
|
|
|
pub const FULL_VERSION: &str = "{full_version}";
|
|
|
|
pub const NAME: &str = "Lapce-{release}";
|
2023-05-14 19:54:17 +00:00
|
|
|
pub const RELEASE: ReleaseType = ReleaseType::{release};
|
|
|
|
"#);
|
|
|
|
|
|
|
|
fs::write(meta_file, meta)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-21 23:52:00 +00:00
|
|
|
|
|
|
|
fn get_commit() -> Option<git2::Oid> {
|
|
|
|
if let Ok(repo) = git2::Repository::discover(".") {
|
|
|
|
if let Ok(head) = repo.find_reference("HEAD") {
|
|
|
|
let path = repo.path().to_path_buf();
|
|
|
|
|
|
|
|
if let Ok(resolved) = head.resolve() {
|
|
|
|
if let Some(name) = resolved.name() {
|
|
|
|
let path = path.join(name);
|
|
|
|
if path.exists() {
|
|
|
|
println!(
|
|
|
|
"cargo:rerun-if-changed={}",
|
|
|
|
path.canonicalize().unwrap().display()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Some(resolved.peel_to_commit().ok()?.id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|