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

Extended info on --version #719

Closed
rinor opened this issue Aug 23, 2019 · 1 comment · Fixed by #730
Closed

Extended info on --version #719

rinor opened this issue Aug 23, 2019 · 1 comment · Fixed by #730
Labels
enhancement New feature or request

Comments

@rinor
Copy link
Contributor

rinor commented Aug 23, 2019

Is your feature request related to a problem/context ? Please describe if applicable.
This is a JIP and expands a bit over #645 and #606.
Right now the reported version from jormungandr --version compiled from master is the same as the one from the latest release so there is no way to tell if the binary is up to date or not. Furthermore since master is a fast moving branch, it would be nice to have more info reported form --version.

Describe the solution you'd like
The following is just an example and may be tweeked as needed.

jormungandr 0.3.3 (master-3326b9b+, debug build, linux [x86_64]) - [rustc 1.37.0 (eae3437df 2019-08-13)]
\_________/ \___/  \____/ \_____/|  \_________/  \___/  \____/      \_________________________________/
 |           |      |      |     |   |            |      |           |
 |           |      |      |     |   |            |      |           +- rustc --version
 |           |      |      |     |   |            |      +- std::env::consts::ARCH
 |           |      |      |     |   |            +- std::env::consts::OS
 |           |      |      |     |   +- Checks debug_assertions
 |           |      |      |     +- Adds a "+" if the working tree is not clean
 |           |      |      +- Commit hash
 |           |      +- Current branch name
 |           +- Package version from Cargo.toml
 +- Package name from Cargo.toml

Additional context
To produce the proposed output, tried to create a cargo build script build.rs that (ab)uses CARGO_PKG_VERSION, but due to zero rust experience wasn't able to properly complete it.
¯\_(ツ)_/¯
In the example the only dependency is the git binary and there is no need to change any other part of the code.
The following works, but needs proper error handling. The missing part is that if git binary is missing or there is an error, the build should continue and the reported version should the same as today jormungandr 0.3.3

use std::env::consts::{ARCH, OS};
use std::process::Command;

#[cfg(debug_assertions)]
const BUILD_TYPE: &'static str = "debug";
#[cfg(not(debug_assertions))]
const BUILD_TYPE: &'static str = "release";

fn main() {
    let version_string = format!(
        "{} ({}-{}{}, {}, {} [{}]) - [{}]",
        env!("CARGO_PKG_VERSION"),
        git_branch_name(),
        git_commit_hash(),
        if git_working_tree_isclean() { "" } else { "+" },
        BUILD_TYPE,
        OS,
        ARCH,
        compiler_version()
    );

    println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version_string);
}

fn compiler_version() -> String {
    let output = Command::new("rustc")
        .arg("--version")
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .output()
        .unwrap();

    assert!(output.status.success());

    String::from_utf8_lossy(&output.stdout)
        .trim_end()
        .to_string()
}

fn git_commit_hash() -> String {
    let output = Command::new("git")
        .arg("rev-parse")
        .arg("--short")
        .arg("HEAD")
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .output()
        .unwrap();

    assert!(output.status.success());

    String::from_utf8_lossy(&output.stdout)
        .trim_end()
        .to_string()
}

fn git_branch_name() -> String {
    let output = Command::new("git")
        .arg("rev-parse")
        .arg("--abbrev-ref")
        .arg("HEAD")
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .output()
        .unwrap();

    assert!(output.status.success());

    String::from_utf8_lossy(&output.stdout)
        .trim_end()
        .to_string()
}

fn git_working_tree_isclean() -> bool {
    let status = Command::new("git")
        .arg("diff")
        .arg("--quiet")
        .arg("--exit-code")
        .arg("HEAD")
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .status()
        .unwrap();

    status.code().unwrap() == 0
}

Note: https://vallentin.io/2019/06/06/versioning served as guide for the idea and the code.

@NicolasDP NicolasDP added the enhancement New feature or request label Aug 23, 2019
@NicolasDP
Copy link
Contributor

This is a great Idea. I'd love to see this in for jormungandr's binary and jcli's binary.

@rinor do you think you could make a PR for jcli's and jormungandr's build.rs for these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants