Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve output of --version #1367

Merged
merged 7 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/fj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ license.workspace = true
keywords.workspace = true
categories.workspace = true


[build-dependencies]
anyhow = "1.0.66"


[dependencies]
fj-proc.workspace = true

Expand Down
47 changes: 26 additions & 21 deletions crates/fj/build.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
use std::{
fmt::Write,
path::PathBuf,
process::{Command, Output, Stdio},
};

fn main() {
let version = Version::determine();
fn main() -> anyhow::Result<()> {
let version = Version::determine()?;

println!("cargo:rustc-env=FJ_VERSION_PKG={}", version.pkg_version);
println!("cargo:rustc-env=FJ_VERSION_FULL={}", version.full_string);
println!("cargo:rustc-env=FJ_VERSION_PKG={}", version.pkg);
println!("cargo:rustc-env=FJ_VERSION_FULL={}", version.full);

// Make sure the build script doesn't run too often.
println!("cargo:rerun-if-changed=Cargo.toml");

Ok(())
}

struct Version {
pkg_version: String,
full_string: String,
pkg: String,
full: String,
}
impl Version {
fn determine() -> Self {
let pkg_version = std::env::var("CARGO_PKG_VERSION").unwrap();
fn determine() -> anyhow::Result<Self> {
let pkg = std::env::var("CARGO_PKG_VERSION").unwrap();
let commit = git_description();

let official_release =
std::env::var("RELEASE_DETECTED").as_deref() == Ok("true");
println!("cargo:rerun-if-env-changed=RELEASE_DETECTED");

let full_string = match (commit, official_release) {
(Some(commit), true) => format!("{pkg_version} ({commit})"),
(Some(commit), false) => {
format!("{pkg_version} ({commit}, unreleased)")
}
(None, true) => pkg_version.clone(),
(None, false) => format!("{pkg_version} (unreleased)"),
};

Self {
pkg_version,
full_string,
let mut full = format!("{pkg} (");

if official_release {
write!(full, "official release binary")?;
} else {
write!(full, "development build")?;
}

if let Some(commit) = commit {
write!(full, "; {commit}")?;
}

writeln!(full, ")")?;

Ok(Self { pkg, full })
}
}

Expand All @@ -52,7 +57,7 @@ fn git_description() -> Option<String> {
let crate_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());

let mut cmd = Command::new("git");
cmd.args(["describe", "--always", "--dirty=-modified"])
cmd.args(["describe", "--always", "--tags"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.current_dir(&crate_dir);
Expand Down