Debian Codegraph @

make/scripts/version.rs

raw symbols SCIP
1 //! ```cargo
2 //! [dependencies]
3 //! glob = "*"
4 //! ```
5
6 use glob::glob;
7 use std::env;
8 use std::error::Error;
9 use std::fs;
10 use std::io::{self, BufRead};
11
12 fn main() -> Result<(), Box<dyn Error>> {
13 let parent_dir = env::var("CARGO_MAKE_WORKING_DIRECTORY").expect("CARGO_MAKE_WORKING_DIRECTORY not specified");
14 let version_type = env::var("RUST_DECIMAL_BUILD_VERSION").expect("RUST_DECIMAL_BUILD_VERSION not specified");
15
16 // Load in the build number
17 let build_number = format!("{parent_dir}/.buildnumber");
18 let (major, minor, revision) = parse_build_number(&build_number, &version_type)?;
19 println!("{major}.{minor}.{revision}");
20
21 // Process all cargo files
22 for entry in glob("./Cargo.toml").expect("Failed to read glob pattern") {
23 let path = entry?;
24 let toml = fs::read_to_string(&path)?;
25 let mut updated_toml = String::new();
26 let mut added = false;
27 for line in toml.lines() {
28 if !added && line.starts_with("version =") {
29 added = true;
30 updated_toml.push_str(&format!("version = \"{major}.{minor}.{revision}\"\n"));
31 } else {
32 updated_toml.push_str(line);
33 updated_toml.push('\n');
34 }
35 }
36 fs::write(&path, updated_toml)?;
37 }
38
39 // Also, update the readme with the build number
40 let readme_path = format!("{parent_dir}/README.md");
41 let readme = fs::read_to_string(&readme_path)?;
42 let mut updated_readme = String::new();
43 for line in readme.lines() {
44 if line.starts_with("rust_decimal = \"") {
45 updated_readme.push_str(&format!("rust_decimal = \"{major}.{minor}\"\n"));
46 } else if line.starts_with("rust_decimal_macros = \"") {
47 updated_readme.push_str(&format!("rust_decimal_macros = \"{major}.{minor}\"\n"));
48 } else {
49 updated_readme.push_str(line);
50 updated_readme.push('\n');
51 }
52 }
53 fs::write(&readme_path, updated_readme)?;
54
55 // Finally, write the build number back
56 fs::write(build_number, format!("{major}\n{minor}\n{revision}\n",))?;
57 Ok(())
58 }
59
60 fn parse_build_number(path: &String, version_type: &String) -> Result<(u32, u32, u32), Box<dyn Error>> {
61 let file = fs::File::open(path)?;
62 let mut lines = io::BufReader::new(file).lines();
63 let major: u32 = lines.next().expect("missing major version")?.parse()?;
64 let minor: u32 = lines.next().expect("missing minor version")?.parse()?;
65 let revision: u32 = lines.next().expect("missing revision version")?.parse()?;
66 Ok(match &version_type[..] {
67 "major" => (major + 1, 0, 0),
68 "minor" => (major, minor + 1, 0),
69 "revision" => (major, minor, revision + 1),
70 _ => (major, minor, revision),
71 })
72 }