1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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};
use crate::MinverConfig;
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);
let config = MinverConfig::read_from_env().expect("Failed to parse configuration");
if let Err(e) = simple_logger::init_with_level(config.log_level) {
println!("Failed to initialize log: {}", e);
}
default_build_action_silent(&config);
}
pub fn default_build_action_silent(config: &MinverConfig) {
if env::var_os(UPDATE_VERSION_VAR).is_some() {
update_package_version(
&env::var_os("CARGO_MANIFEST_DIR").unwrap_or(OsString::from(".")),
config,
)
.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, config: &MinverConfig) -> 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, config)?;
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(())
}
}
}