Skip to content

Commit

Permalink
Remove vergen dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Hannes-MoBagel committed Sep 12, 2024
1 parent 123e212 commit be27201
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 176 deletions.
149 changes: 2 additions & 147 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pretty_assertions.workspace = true
serial_test.workspace = true

[build-dependencies]
vergen-gitcl = { version = "1.0.0", features = ["emit_and_set"] }
chrono = "0.4.26"

[[bench]]
name = "time_bench"
Expand Down
70 changes: 42 additions & 28 deletions crates/cli/build.rs
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);
}

0 comments on commit be27201

Please sign in to comment.