use anyhow::Result;
use git2::Repository;
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use toml_edit::{value, Document};
pub const UPDATE_VERSION_VAR: &str = "MINVER_UPDATE_VERSION";
pub fn default_build_action() {
println!("cargo:rerun-if-changed=.git/refs/tags/");
println!("cargo:rerun-if-env-changed={}", UPDATE_VERSION_VAR);
if env!("CARGO_PKG_NAME") != env!("CARGO_CRATE_NAME") {
default_build_action_silent();
}
}
pub fn default_build_action_silent() {
if env::var_os(UPDATE_VERSION_VAR).is_some() {
update_package_version(&env::var_os("CARGO_MANIFEST_DIR").unwrap_or(OsString::from(".")))
.unwrap()
} else {
log::info!(
"Environment variable {} is not set, no action will be taken",
UPDATE_VERSION_VAR
);
}
}
pub fn update_package_version(manifest_dir: &OsString) -> Result<()> {
let manifest_path = Path::new(manifest_dir).join("Cargo.toml");
log::debug!("Will update manifest file at {:?}", manifest_path);
let mut document: Document = fs::read_to_string(&manifest_path)?.parse::<Document>()?;
log::debug!("Successfully read manifest file");
match Repository::open(manifest_dir) {
Ok(repo) => {
let version = crate::get_version(&repo)?;
document["package"]["version"] = value(version.to_string());
log::debug!("Updated version to {}", version);
Ok(fs::write(
&manifest_path,
document.to_string_in_original_order(),
)?)
}
Err(_) => {
log::info!("Build util run outside of repository, manifest file will not be updated");
Ok(())
}
}
}