Skip to content

Commit

Permalink
refactor: use git describe for the dfx_version (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hans authored Dec 5, 2019
1 parent 7520664 commit 0e4630e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
21 changes: 20 additions & 1 deletion dfx/assets/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ fn add_assets(fn_name: &str, f: &mut File, path: &str) -> () {
.unwrap();
}

fn get_git_hash() -> Option<String> {
use std::process::Command;

let describe = Command::new("git").arg("describe").arg("--dirty").output();

if let Ok(output) = describe {
Some(String::from_utf8_lossy(&output.stdout).to_string())
} else {
None
}
}

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let loader_path = Path::new(&out_dir).join("load_assets.rs");
Expand All @@ -48,8 +60,15 @@ fn main() {
)
.unwrap();

let path = env::var("DFX_ASSETS").unwrap();
let path = env::var("DFX_ASSETS").expect("Cannot find DFX_ASSETS");
add_assets("binary_cache", &mut f, &path);
add_assets("language_bindings", &mut f, "assets/language_bindings");
add_assets("new_project_files", &mut f, "assets/new_project_files");

// Pass the git describe version at time of build.
if let Some(git) = get_git_hash() {
println!("cargo:rustc-env=CARGO_PKG_VERSION={}", git);
} else {
// Nothing to do here, as there is no GIT. We keep the CARGO_PKG_VERSION.
}
}
23 changes: 1 addition & 22 deletions dfx/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
pub mod cache;
pub mod dfinity;

static mut DFX_VERSION: Option<String> = None;
/// Returns the version of DFX that was built.
/// In debug, add a timestamp of the upstream compilation at the end of version to ensure all
/// debug runs are unique (and cached uniquely).
/// That timestamp is taken from the DFX_TIMESTAMP_DEBUG_MODE_ONLY env var that is set in
/// Nix.
pub fn dfx_version() -> &'static str {
unsafe {
match &DFX_VERSION {
Some(x) => x.as_str(),
None => {
let version = env!("CARGO_PKG_VERSION");
DFX_VERSION = Some(version.to_owned());

#[cfg(debug_assertions)]
{
DFX_VERSION = Some(format!(
"{}-{}",
version,
std::env::var("DFX_TIMESTAMP_DEBUG_MODE_ONLY")
.unwrap_or_else(|_| "local-debug".to_owned())
));
}

dfx_version()
}
}
}
env!("CARGO_PKG_VERSION")
}

0 comments on commit 0e4630e

Please sign in to comment.