-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commit information to roc versions built from source
- Loading branch information
Showing
5 changed files
with
102 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,59 @@ | ||
use chrono::prelude::*; | ||
use std::fs; | ||
use std::process::Command; | ||
use std::str; | ||
|
||
fn main() { | ||
// Rebuild if this build.rs file changes | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
// The version file is located at the root of the repository | ||
let version_file_path = "../../version.txt"; | ||
|
||
// Rebuild if version file changes | ||
println!("cargo:rerun-if-changed={}", version_file_path); | ||
|
||
// Read the version file | ||
let version_file_contents = fs::read_to_string(version_file_path).unwrap(); | ||
|
||
// If the version is "built-from-source", replace it with the git commit information | ||
let version = match version_file_contents.trim() { | ||
"built-from-source" => { | ||
// Rebuild if a new Git commit is made | ||
println!("cargo:rerun-if-changed=.git/HEAD"); | ||
|
||
// Get the hash of the current commit | ||
let git_describe_output = Command::new("git") | ||
.arg("describe") | ||
.arg("--always") | ||
.arg("--dirty= with additional changes") // Add a suffix if the working directory is dirty | ||
.output() | ||
.expect("Failed to execute git describe command"); | ||
let git_commit_hash = str::from_utf8(&git_describe_output.stdout) | ||
.expect("Failed to parse git describe output") | ||
.trim(); | ||
|
||
// Get the datetime of the last commit | ||
let git_show_output = Command::new("git") | ||
.arg("show") | ||
.arg("--no-patch") | ||
.arg("--format=%ct") // Outputting a UNIX timestamp is the only way to always use UTC | ||
.output() | ||
.expect("Failed to execute git show command"); | ||
let git_commit_timestamp = { | ||
let timestamp = str::from_utf8(&git_show_output.stdout) | ||
.expect("Failed to parse git show output as a string") | ||
.trim() | ||
.parse::<i64>() | ||
.expect("Failed to parse timestamp as an integer"); | ||
DateTime::from_timestamp(timestamp, 0) | ||
.expect("Failed to parse timestamp") | ||
.format("%Y-%m-%d %H:%M:%S") | ||
}; | ||
format!("built from commit {git_commit_hash}, committed at {git_commit_timestamp} UTC") | ||
} | ||
_ => version_file_contents.trim().to_string(), | ||
}; | ||
// Emit the version to a build-time environment variable | ||
println!("cargo:rustc-env=ROC_VERSION={}", version); | ||
} |
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