Skip to content

Commit

Permalink
Add git commit hash in cargo-contract version (use-ink#189)
Browse files Browse the repository at this point in the history
* add git commit hash in cargo-contract version

* remove todo and add version for `cargo contract` command

* It seems that I misunderstand the requirements. recover the old modify

* remove `pub`
  • Loading branch information
atenjin authored Apr 1, 2021
1 parent e460d39 commit cacf2d5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ funty = "=1.1.0"
anyhow = "1.0.40"
zip = { version = "0.5.11", default-features = false }
walkdir = "2.3.2"
substrate-build-script-utils = "3.0.0"
platforms = "1.1"

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
59 changes: 59 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use std::{
borrow::Cow,
env,
ffi::OsStr,
fs::File,
io::{prelude::*, Write},
iter::Iterator,
path::{Path, PathBuf},
process::Command,
};

use anyhow::Result;
use walkdir::WalkDir;
use zip::{write::FileOptions, CompressionMethod, ZipWriter};

use platforms::{TARGET_ARCH, TARGET_ENV, TARGET_OS};
use substrate_build_script_utils::rerun_if_git_head_changed;

const DEFAULT_UNIX_PERMISSIONS: u32 = 0o755;

fn main() {
Expand All @@ -46,6 +51,9 @@ fn main() {
dst_file.display()
);

generate_cargo_keys();
rerun_if_git_head_changed();

std::process::exit(
match zip_dir(&template_dir, &dst_file, CompressionMethod::Stored) {
Ok(_) => {
Expand Down Expand Up @@ -109,3 +117,54 @@ fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result

Ok(())
}

/// Generate the `cargo:` key output
fn generate_cargo_keys() {
let output = Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output();

let commit = match output {
Ok(o) if o.status.success() => {
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
Cow::from(sha)
}
Ok(o) => {
println!("cargo:warning=Git command failed with status: {}", o.status);
Cow::from("unknown")
}
Err(err) => {
println!("cargo:warning=Failed to execute git command: {}", err);
Cow::from("unknown")
}
};

println!(
"cargo:rustc-env=CARGO_CONTRACT_CLI_IMPL_VERSION={}",
get_version(&commit)
)
}

fn get_version(impl_commit: &str) -> String {
let commit_dash = if impl_commit.is_empty() { "" } else { "-" };

format!(
"{}{}{}-{}",
std::env::var("CARGO_PKG_VERSION").unwrap_or_default(),
commit_dash,
impl_commit,
get_platform(),
)
}

fn get_platform() -> String {
let env_dash = if TARGET_ENV.is_some() { "-" } else { "" };

format!(
"{}-{}{}{}",
TARGET_ARCH.as_str(),
TARGET_OS.as_str(),
env_dash,
TARGET_ENV.map(|x| x.as_str()).unwrap_or(""),
)
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ use structopt::{clap, StructOpt};

#[derive(Debug, StructOpt)]
#[structopt(bin_name = "cargo")]
#[structopt(version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"))]
pub(crate) enum Opts {
/// Utilities to develop Wasm smart contracts.
#[structopt(name = "contract")]
#[structopt(version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"))]
#[structopt(setting = clap::AppSettings::UnifiedHelpMessage)]
#[structopt(setting = clap::AppSettings::DeriveDisplayOrder)]
#[structopt(setting = clap::AppSettings::DontCollapseArgsInUsage)]
Expand Down

0 comments on commit cacf2d5

Please sign in to comment.