Skip to content

Commit 254abe0

Browse files
committed
build.rs to update version in Cargo.toml for cargo bundle and cargo wix
1 parent 2367904 commit 254abe0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ authors = ["Linus Leo Stöckli"]
66
description = "Serial Monitor and Plotter written in rust."
77
license = "GPL-3.0"
88
homepage = "https://github.com/hacknus/serial-monitor-rust"
9-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
109

1110
[dependencies]
1211
csv = "1.3"
@@ -25,6 +24,9 @@ serde = { version = "1.0", features = ["derive"] }
2524
serialport = { version = "4.6.1", features = ["serde"] }
2625
log = "0.4.22"
2726

27+
[build-dependencies]
28+
regex = "1.11"
29+
2830
[package.metadata.bundle]
2931
name = "Serial Monitor"
3032
identifier = "com.hacknus.serial_monitor"

build.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
use std::fs;
12
fn main() {
23
println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.8");
4+
5+
// Get the package version from the environment variable
6+
let version = env!("CARGO_PKG_VERSION");
7+
8+
// Define the paths to `Cargo.toml`
9+
let cargo_toml_path = "Cargo.toml";
10+
11+
// Read `Cargo.toml`
12+
let content = fs::read_to_string(cargo_toml_path).expect("Failed to read Cargo.toml");
13+
14+
// Replace `version = ...` only within `[package.metadata.bundle]` and `[package.metadata.wix]`
15+
let updated_content = content
16+
.lines()
17+
.map(|line| {
18+
if line.trim_start().starts_with("version =") {
19+
// Check if we're inside the relevant sections
20+
if content.contains("[package.metadata.bundle]")
21+
|| content.contains("[package.metadata.wix]")
22+
{
23+
format!(r#"version = "{}""#, version)
24+
} else {
25+
line.to_string()
26+
}
27+
} else {
28+
line.to_string()
29+
}
30+
})
31+
.collect::<Vec<_>>()
32+
.join("\n");
33+
34+
// Write the updated content back to `Cargo.toml`
35+
fs::write(cargo_toml_path, updated_content).expect("Failed to write updated Cargo.toml");
36+
37+
println!("cargo:rerun-if-changed={}", cargo_toml_path);
338
}

0 commit comments

Comments
 (0)