From bd0db50637f5385b2672ec57d06887e5543ddded Mon Sep 17 00:00:00 2001 From: Aten Date: Sat, 20 Feb 2021 11:59:12 +0800 Subject: [PATCH 1/4] add git commit hash in cargo-contract version --- Cargo.lock | 17 +++++++++++++++ Cargo.toml | 2 ++ build.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 +++++- 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 658aff91..e496c2fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -499,6 +499,7 @@ dependencies = [ "log", "parity-scale-codec 2.0.0", "parity-wasm 0.42.1", + "platforms", "pretty_assertions", "pwasm-utils", "rustc_version 0.3.3", @@ -507,6 +508,7 @@ dependencies = [ "serde_json", "sp-core", "structopt", + "substrate-build-script-utils", "substrate-subxt", "tempfile", "toml", @@ -2254,6 +2256,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" @@ -3467,6 +3475,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" diff --git a/Cargo.toml b/Cargo.toml index b3dda572..6b29d21a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,8 @@ funty = "=1.1.0" anyhow = "1.0.38" zip = { version = "0.5.10", default-features = false } walkdir = "2.3.1" +substrate-build-script-utils = "3.0.0" +platforms = "1.1" [dev-dependencies] assert_matches = "1.5.0" diff --git a/build.rs b/build.rs index 232f0976..7bb2c14d 100644 --- a/build.rs +++ b/build.rs @@ -15,18 +15,23 @@ // along with cargo-contract. If not, see . use std::{ + borrow::Cow, env, ffi::OsStr, fs::File, io::{prelude::*, Write}, iter::Iterator, path::{Path, PathBuf}, + process::Command, }; use anyhow::Result; +use platforms::*; use walkdir::WalkDir; use zip::{write::FileOptions, CompressionMethod, ZipWriter}; +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,57 @@ fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result Ok(()) } + +// TODO This part is copy from `substrate-build-script-utils`, for the rustc-env is assigned `SUBSTRATE_CLI_IMPL_VERSION` +// if guys accept `SUBSTRATE_CLI_IMPL_VERSION`, I can use `generate_cargo_keys` in build.rs directly + +/// Generate the `cargo:` key output +pub 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(""), + ) +} diff --git a/src/main.rs b/src/main.rs index 445d4886..83c248bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -366,7 +366,11 @@ fn parse_code_hash(input: &str) -> Result { fn main() { env_logger::init(); - let Opts::Contract(args) = Opts::from_args(); + let app = Opts::clap(); + // TODO if guys accept `SUBSTRATE_CLI_IMPL_VERSION`, I can use `generate_cargo_keys` in build.rs directly + let full_version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"); + let app = app.version(full_version); + let Opts::Contract(args) = Opts::from_clap(&app.get_matches()); //Opts::from_args(); match exec(args.cmd) { Ok(maybe_msg) => { if let Some(msg) = maybe_msg { -- GitLab From b12862525ddec2c69fc971294360d1f2d414ce7b Mon Sep 17 00:00:00 2001 From: Aten Date: Thu, 1 Apr 2021 19:44:04 +0800 Subject: [PATCH 2/4] remove todo and add version for `cargo contract` command --- build.rs | 59 +---------------------------------------------------- src/main.rs | 8 +++----- 2 files changed, 4 insertions(+), 63 deletions(-) diff --git a/build.rs b/build.rs index 7bb2c14d..c30a3fd6 100644 --- a/build.rs +++ b/build.rs @@ -15,22 +15,19 @@ // along with cargo-contract. If not, see . use std::{ - borrow::Cow, env, ffi::OsStr, fs::File, io::{prelude::*, Write}, iter::Iterator, path::{Path, PathBuf}, - process::Command, }; use anyhow::Result; -use platforms::*; use walkdir::WalkDir; use zip::{write::FileOptions, CompressionMethod, ZipWriter}; -use substrate_build_script_utils::rerun_if_git_head_changed; +use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed}; const DEFAULT_UNIX_PERMISSIONS: u32 = 0o755; @@ -117,57 +114,3 @@ fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result Ok(()) } - -// TODO This part is copy from `substrate-build-script-utils`, for the rustc-env is assigned `SUBSTRATE_CLI_IMPL_VERSION` -// if guys accept `SUBSTRATE_CLI_IMPL_VERSION`, I can use `generate_cargo_keys` in build.rs directly - -/// Generate the `cargo:` key output -pub 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(""), - ) -} diff --git a/src/main.rs b/src/main.rs index 670319df..31bdd4ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,11 @@ use structopt::{clap, StructOpt}; #[derive(Debug, StructOpt)] #[structopt(bin_name = "cargo")] +#[structopt(version = env!("SUBSTRATE_CLI_IMPL_VERSION"))] pub(crate) enum Opts { /// Utilities to develop Wasm smart contracts. #[structopt(name = "contract")] + #[structopt(version = env!("SUBSTRATE_CLI_IMPL_VERSION"))] #[structopt(setting = clap::AppSettings::UnifiedHelpMessage)] #[structopt(setting = clap::AppSettings::DeriveDisplayOrder)] #[structopt(setting = clap::AppSettings::DontCollapseArgsInUsage)] @@ -456,11 +458,7 @@ fn parse_code_hash(input: &str) -> Result { fn main() { env_logger::init(); - let app = Opts::clap(); - // TODO if guys accept `SUBSTRATE_CLI_IMPL_VERSION`, I can use `generate_cargo_keys` in build.rs directly - let full_version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"); - let app = app.version(full_version); - let Opts::Contract(args) = Opts::from_clap(&app.get_matches()); //Opts::from_args(); + let Opts::Contract(args) = Opts::from_args(); match exec(args.cmd) { Ok(maybe_msg) => { if let Some(msg) = maybe_msg { -- GitLab From d3a801ff2bc0e8c97922f9521dc75530a1237efd Mon Sep 17 00:00:00 2001 From: Aten Date: Thu, 1 Apr 2021 19:55:14 +0800 Subject: [PATCH 3/4] It seems that I misunderstand the requirements. recover the old modify --- Cargo.lock | 1 + Cargo.toml | 1 + build.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 4 ++-- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ba9e2a02..d9ce4fe3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,6 +508,7 @@ dependencies = [ "log", "parity-scale-codec 2.0.1", "parity-wasm 0.42.2", + "platforms", "pretty_assertions", "pwasm-utils", "regex", diff --git a/Cargo.toml b/Cargo.toml index 17869cc0..b3bfd1d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ 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" diff --git a/build.rs b/build.rs index c30a3fd6..02639524 100644 --- a/build.rs +++ b/build.rs @@ -15,19 +15,22 @@ // along with cargo-contract. If not, see . 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 substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed}; +use platforms::{TARGET_ARCH, TARGET_ENV, TARGET_OS}; +use substrate_build_script_utils::rerun_if_git_head_changed; const DEFAULT_UNIX_PERMISSIONS: u32 = 0o755; @@ -114,3 +117,54 @@ fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result Ok(()) } + +/// Generate the `cargo:` key output +pub 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(""), + ) +} diff --git a/src/main.rs b/src/main.rs index 31bdd4ad..3ce2b55d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,11 +41,11 @@ use structopt::{clap, StructOpt}; #[derive(Debug, StructOpt)] #[structopt(bin_name = "cargo")] -#[structopt(version = env!("SUBSTRATE_CLI_IMPL_VERSION"))] +#[structopt(version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"))] pub(crate) enum Opts { /// Utilities to develop Wasm smart contracts. #[structopt(name = "contract")] - #[structopt(version = env!("SUBSTRATE_CLI_IMPL_VERSION"))] + #[structopt(version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"))] #[structopt(setting = clap::AppSettings::UnifiedHelpMessage)] #[structopt(setting = clap::AppSettings::DeriveDisplayOrder)] #[structopt(setting = clap::AppSettings::DontCollapseArgsInUsage)] -- GitLab From 4b898c3e303ab7d253e7ffc72d6bf9d6ce71498f Mon Sep 17 00:00:00 2001 From: Aten Date: Thu, 1 Apr 2021 19:56:04 +0800 Subject: [PATCH 4/4] remove `pub` --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 02639524..dc79aa3f 100644 --- a/build.rs +++ b/build.rs @@ -119,7 +119,7 @@ fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result } /// Generate the `cargo:` key output -pub fn generate_cargo_keys() { +fn generate_cargo_keys() { let output = Command::new("git") .args(&["rev-parse", "--short", "HEAD"]) .output(); -- GitLab