File: version.rs

package info (click to toggle)
rust-rust-decimal 1.36.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: makefile: 2
file content (72 lines) | stat: -rw-r--r-- 2,783 bytes parent folder | download | duplicates (2)
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
//! ```cargo
//! [dependencies]
//! glob = "*"
//! ```

use glob::glob;
use std::env;
use std::error::Error;
use std::fs;
use std::io::{self, BufRead};

fn main() -> Result<(), Box<dyn Error>> {
    let parent_dir = env::var("CARGO_MAKE_WORKING_DIRECTORY").expect("CARGO_MAKE_WORKING_DIRECTORY not specified");
    let version_type = env::var("RUST_DECIMAL_BUILD_VERSION").expect("RUST_DECIMAL_BUILD_VERSION not specified");

    // Load in the build number
    let build_number = format!("{parent_dir}/.buildnumber");
    let (major, minor, revision) = parse_build_number(&build_number, &version_type)?;
    println!("{major}.{minor}.{revision}");

    // Process all cargo files
    for entry in glob("./Cargo.toml").expect("Failed to read glob pattern") {
        let path = entry?;
        let toml = fs::read_to_string(&path)?;
        let mut updated_toml = String::new();
        let mut added = false;
        for line in toml.lines() {
            if !added && line.starts_with("version =") {
                added = true;
                updated_toml.push_str(&format!("version = \"{major}.{minor}.{revision}\"\n"));
            } else {
                updated_toml.push_str(line);
                updated_toml.push('\n');
            }
        }
        fs::write(&path, updated_toml)?;
    }

    // Also, update the readme with the build number
    let readme_path = format!("{parent_dir}/README.md");
    let readme = fs::read_to_string(&readme_path)?;
    let mut updated_readme = String::new();
    for line in readme.lines() {
        if line.starts_with("rust_decimal = \"") {
            updated_readme.push_str(&format!("rust_decimal = \"{major}.{minor}\"\n"));
        } else if line.starts_with("rust_decimal_macros = \"") {
            updated_readme.push_str(&format!("rust_decimal_macros = \"{major}.{minor}\"\n"));
        } else {
            updated_readme.push_str(line);
            updated_readme.push('\n');
        }
    }
    fs::write(&readme_path, updated_readme)?;

    // Finally, write the build number back
    fs::write(build_number, format!("{major}\n{minor}\n{revision}\n",))?;
    Ok(())
}

fn parse_build_number(path: &String, version_type: &String) -> Result<(u32, u32, u32), Box<dyn Error>> {
    let file = fs::File::open(path)?;
    let mut lines = io::BufReader::new(file).lines();
    let major: u32 = lines.next().expect("missing major version")?.parse()?;
    let minor: u32 = lines.next().expect("missing minor version")?.parse()?;
    let revision: u32 = lines.next().expect("missing revision version")?.parse()?;
    Ok(match &version_type[..] {
        "major" => (major + 1, 0, 0),
        "minor" => (major, minor + 1, 0),
        "revision" => (major, minor, revision + 1),
        _ => (major, minor, revision),
    })
}