-
-
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.
- Loading branch information
1 parent
123e212
commit be27201
Showing
3 changed files
with
45 additions
and
176 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 |
---|---|---|
@@ -1,45 +1,59 @@ | ||
use chrono::prelude::*; | ||
use std::fs; | ||
use vergen_gitcl::{Emitter, GitclBuilder}; | ||
use std::process::Command; | ||
use std::str; | ||
|
||
fn main() { | ||
// Emit git information | ||
let git_instructions = GitclBuilder::default() | ||
.commit_timestamp(true) | ||
.sha(true) // Use shorter commit hash | ||
.dirty(true) // Count untracked files as dirty | ||
.build() | ||
.unwrap(); | ||
Emitter::default() | ||
.add_instructions(&git_instructions) | ||
.unwrap() | ||
.emit_and_set() | ||
.unwrap(); | ||
// Rebuild if this build.rs file changes | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
// Read the version file | ||
// 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" => { | ||
let commit_hash = std::env::var("VERGEN_GIT_SHA").unwrap(); | ||
let git_dirty_message = match std::env::var("VERGEN_GIT_DIRTY") { | ||
Ok(s) => match s.as_str() { | ||
"true" => " with additional changes", | ||
_ => "", | ||
}, | ||
_ => "", | ||
// 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") | ||
}; | ||
let git_commit_timestamp = std::env::var("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap(); | ||
format!( | ||
"built from commit {}{}, committed at {}", | ||
commit_hash, git_dirty_message, git_commit_timestamp | ||
) | ||
format!("built from commit {git_commit_hash}, committed at {git_commit_timestamp}") | ||
} | ||
_ => version_file_contents.trim().to_string(), | ||
}; | ||
// Emit the version to a build-time environment variable | ||
println!("cargo:rustc-env=ROC_VERSION={}", version); | ||
// Rebuild if version.txt changes | ||
println!("cargo:rerun-if-changed={}", version_file_path); | ||
} |