Unverified Commit cacf2d54 authored by Aten's avatar Aten Committed by GitHub

Add git commit hash in cargo-contract version (#189)

* 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`
parent e460d396
Pipeline #132816 passed with stages
in 5 minutes and 5 seconds
......@@ -508,6 +508,7 @@ dependencies = [
"log",
"parity-scale-codec 2.0.1",
"parity-wasm 0.42.2",
"platforms",
"pretty_assertions",
"pwasm-utils",
"regex",
......@@ -517,6 +518,7 @@ dependencies = [
"serde_json",
"sp-core",
"structopt",
"substrate-build-script-utils",
"substrate-subxt",
"tempfile",
"toml",
......@@ -2262,6 +2264,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "platforms"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "989d43012e2ca1c4a02507c67282691a0a3207f9dc67cec596b43fe925b3d325"
[[package]]
name = "polling"
version = "2.0.2"
......@@ -3474,6 +3482,15 @@ dependencies = [
"zeroize",
]
[[package]]
name = "substrate-build-script-utils"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd540ba72520174c2c73ce96bf507eeba3cc8a481f58be92525b69110e1fa645"
dependencies = [
"platforms",
]
[[package]]
name = "substrate-subxt"
version = "0.14.0"
......
......@@ -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"
......
......@@ -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() {
......@@ -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(_) => {
......@@ -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(""),
)
}
......@@ -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)]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment