From 1f8e72ca47743846e374be6aaf81ab08a578c9dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= Date: Tue, 15 Oct 2024 20:50:01 +0300 Subject: [PATCH] chore: add buildtime_env::CommonVariables, common to abi and wasm steps --- cargo-near-build/src/near/build/mod.rs | 46 ++++++----------- .../src/types/near/build/buildtime_env/mod.rs | 51 +++++++++++++++++++ .../src/types/near/build/input/mod.rs | 2 +- 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/cargo-near-build/src/near/build/mod.rs b/cargo-near-build/src/near/build/mod.rs index 8043b6d0..f8d1e3ac 100644 --- a/cargo-near-build/src/near/build/mod.rs +++ b/cargo-near-build/src/near/build/mod.rs @@ -24,12 +24,8 @@ use super::abi; /// builds a contract whose crate root is current workdir, or identified by [`Cargo.toml`/BuildOpts::manifest_path](crate::BuildOpts::manifest_path) location pub fn run(args: Opts) -> eyre::Result { - let nep330_build_cmd_env = buildtime_env::Nep330BuildCommand::compute(&args)?; - - let external_cargo_target_path_env = - buildtime_env::CargoTargetDir::maybe_new(args.override_cargo_target_dir); - let external_nep330_contract_path_env = - buildtime_env::Nep330ContractPath::maybe_new(args.override_nep330_contract_path); + let override_cargo_target_path_env = + buildtime_env::CargoTargetDir::maybe_new(args.override_cargo_target_dir.clone()); env_keys::nep330::print_env(); @@ -44,7 +40,7 @@ pub fn run(args: Opts) -> eyre::Result { })?; let crate_metadata = pretty_print::handle_step("Collecting cargo project metadata...", || { - let manifest_path: Utf8PathBuf = if let Some(manifest_path) = args.manifest_path { + let manifest_path: Utf8PathBuf = if let Some(manifest_path) = args.manifest_path.clone() { manifest_path } else { MANIFEST_FILE_NAME.into() @@ -53,11 +49,11 @@ pub fn run(args: Opts) -> eyre::Result { CrateMetadata::collect( manifest_path, args.no_locked, - external_cargo_target_path_env.as_ref(), + override_cargo_target_path_env.as_ref(), ) })?; - let out_dir = crate_metadata.resolve_output_dir(args.out_dir.map(Into::into))?; + let out_dir = crate_metadata.resolve_output_dir(args.out_dir.clone().map(Into::into))?; let mut cargo_args = vec!["--target", COMPILATION_TARGET]; let cargo_feature_args = { @@ -82,7 +78,14 @@ pub fn run(args: Opts) -> eyre::Result { let mut abi = None; let mut min_abi_path = None; let builder_version_info = VersionInfo::get_coerced_builder_version()?; - let builder_abi_versions_env = builder_version_info.compute_env_variables()?; + + let common_vars_env = buildtime_env::CommonVariables::new( + &args, + &builder_version_info, + &crate_metadata, + override_cargo_target_path_env, + )?; + if !args.no_abi { let mut contract_abi = { let mut abi_env = args @@ -90,17 +93,8 @@ pub fn run(args: Opts) -> eyre::Result { .iter() .map(|(key, value)| (key.as_ref(), value.as_ref())) .collect::>(); + common_vars_env.append_borrowed_to(&mut abi_env); - // required, otherwise `message: Build Details Extension field not provided or malformed: \ - // "`NEP330_BUILD_INFO_BUILD_COMMAND` is required, when \ - // `NEP330_BUILD_INFO_BUILD_ENVIRONMENT` is set, but it's either not set or empty!"` - // when generating abi in docker build - nep330_build_cmd_env.append_borrowed_to(&mut abi_env); - external_nep330_contract_path_env.append_borrowed_to(&mut abi_env); - builder_abi_versions_env.append_borrowed_to(&mut abi_env); - if let Some(ref target_dir) = external_cargo_target_path_env { - target_dir.append_borrowed_to(&mut abi_env); - } abi::generate::procedure( &crate_metadata, args.no_locked, @@ -143,8 +137,6 @@ pub fn run(args: Opts) -> eyre::Result { cargo_args.extend(&["--features", "near-sdk/__abi-embed"]); } - let nep330_version_env = buildtime_env::Nep330Version::new(&crate_metadata); - let nep330_link_env = buildtime_env::Nep330Link::new(&crate_metadata); let abi_path_env = buildtime_env::AbiPath::new(args.no_embed_abi, &min_abi_path); let build_env = { @@ -156,14 +148,8 @@ pub fn run(args: Opts) -> eyre::Result { ); abi_path_env.append_borrowed_to(&mut build_env); - nep330_version_env.append_borrowed_to(&mut build_env); - nep330_link_env.append_borrowed_to(&mut build_env); - nep330_build_cmd_env.append_borrowed_to(&mut build_env); - external_nep330_contract_path_env.append_borrowed_to(&mut build_env); - builder_abi_versions_env.append_borrowed_to(&mut build_env); - if let Some(ref target_dir) = external_cargo_target_path_env { - target_dir.append_borrowed_to(&mut build_env); - } + common_vars_env.append_borrowed_to(&mut build_env); + build_env }; pretty_print::step("Building contract"); diff --git a/cargo-near-build/src/types/near/build/buildtime_env/mod.rs b/cargo-near-build/src/types/near/build/buildtime_env/mod.rs index 44b31f4c..d53c8817 100644 --- a/cargo-near-build/src/types/near/build/buildtime_env/mod.rs +++ b/cargo-near-build/src/types/near/build/buildtime_env/mod.rs @@ -20,3 +20,54 @@ pub use abi_builder_version::BuilderAbiVersions; pub use external::cargo_target_dir::CargoTargetDir; pub use external::nep330_path::Nep330ContractPath; + +use crate::types::cargo::metadata::CrateMetadata; +use crate::BuildOpts; + +use super::output::version_info::VersionInfo; + +/// varibles, common for both steps of build, abi-gen and wasm build +pub struct CommonVariables { + pub nep330_version: Nep330Version, + pub nep330_link: Nep330Link, + pub nep330_build_cmd: Nep330BuildCommand, + pub builder_abi_versions: BuilderAbiVersions, + pub override_nep330_contract_path: Nep330ContractPath, + pub override_cargo_target_path: Option, +} + +impl CommonVariables { + pub fn new( + opts: &BuildOpts, + builder_version_info: &VersionInfo, + crate_metadata: &CrateMetadata, + override_cargo_target_path: Option, + ) -> eyre::Result { + let nep330_version = Nep330Version::new(crate_metadata); + let nep330_link = Nep330Link::new(crate_metadata); + + let nep330_build_cmd = Nep330BuildCommand::compute(opts)?; + let builder_abi_versions = builder_version_info.compute_env_variables()?; + let override_nep330_contract_path = + Nep330ContractPath::maybe_new(opts.override_nep330_contract_path.clone()); + let result = Self { + nep330_version, + nep330_link, + nep330_build_cmd, + builder_abi_versions, + override_nep330_contract_path, + override_cargo_target_path, + }; + Ok(result) + } + pub fn append_borrowed_to<'a>(&'a self, env: &mut Vec<(&str, &'a str)>) { + self.nep330_version.append_borrowed_to(env); + self.nep330_link.append_borrowed_to(env); + self.nep330_build_cmd.append_borrowed_to(env); + self.builder_abi_versions.append_borrowed_to(env); + self.override_nep330_contract_path.append_borrowed_to(env); + if let Some(ref target_dir) = self.override_cargo_target_path { + target_dir.append_borrowed_to(env); + } + } +} diff --git a/cargo-near-build/src/types/near/build/input/mod.rs b/cargo-near-build/src/types/near/build/input/mod.rs index 4fc32d9e..4911a61e 100644 --- a/cargo-near-build/src/types/near/build/input/mod.rs +++ b/cargo-near-build/src/types/near/build/input/mod.rs @@ -152,7 +152,7 @@ impl Opts { /// /// otherwise it's [ColorPreference::Always] if stderr is a terminal device, /// and [ColorPreference::Never] in the remaining cases -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub enum ColorPreference { Auto, Always,