-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic version constant feature
* Add custom build script to generate VERSION constant from Cargo.toml version field
- Loading branch information
ByteOtter
committed
Sep 6, 2024
1 parent
fe93bf8
commit 35ffbe1
Showing
3 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::Path; | ||
use toml::Value; | ||
|
||
fn main() { | ||
let manifest_path = Path::new("Cargo.toml"); | ||
|
||
let content = | ||
std::fs::read_to_string(manifest_path).expect("Unable to read manifest to string."); | ||
let parsed_toml: Value = content.parse().expect("Failed to parse toml!"); | ||
|
||
let version = parsed_toml["package"]["version"] | ||
.as_str() | ||
.unwrap_or("0.0.0"); | ||
|
||
let version_file_path = Path::new("src").join("version.rs"); | ||
let mut file = File::create(version_file_path).expect("Unable to create version.rs!"); | ||
writeln!( | ||
file, | ||
"#[allow(dead_code)]\npub const VERSION: &str = \"{}\";", | ||
version | ||
) | ||
.expect("Unable to write to file!"); | ||
} | ||
|