From 63b999177cfc3d81dd2425178e6e971b4cc6efa5 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Fri, 12 Feb 2021 08:11:11 +0100 Subject: [PATCH 1/8] Generate metadata explicitly for the contract which is build --- src/cmd/metadata.rs | 14 ++++++++++++-- src/workspace/manifest.rs | 32 ++++++++++++++++---------------- src/workspace/metadata.rs | 19 ++++++++++++++++--- src/workspace/mod.rs | 30 +++++++++++++++++++++++++++--- 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 0197c2f1..22425293 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -29,7 +29,10 @@ use contract_metadata::{ SourceLanguage, SourceWasm, User, }; use semver::Version; -use std::{fs, path::PathBuf}; +use std::{ + fs, + path::{Path, PathBuf}, +}; use url::Url; const METADATA_FILE: &str = "metadata.json"; @@ -120,6 +123,13 @@ impl GenerateMetadataCommand { if self.unstable_options.original_manifest { generate_metadata(&self.crate_metadata.manifest_path)?; } else { + let package_name = self.crate_metadata.package_name.clone(); + let manifest_dir = match self.crate_metadata.manifest_path.directory() { + Some(dir) => dir, + None => Path::new("./"), + }; + let absolute_package_path = manifest_dir.canonicalize()?; + Workspace::new( &self.crate_metadata.cargo_meta, &self.crate_metadata.root_package.id, @@ -130,7 +140,7 @@ impl GenerateMetadataCommand { .with_profile_release_lto(false)?; Ok(()) })? - .with_metadata_gen_package()? + .with_metadata_gen_package(package_name, absolute_package_path)? .using_temp(generate_metadata)?; } diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index 40ef117c..23e0dab4 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -105,12 +105,21 @@ impl From for PathBuf { } } +/// Pointer to the package of a contract +pub struct ContractPackage { + /// Name of the contract + pub name: String, + /// Path to the contract directory + pub path: PathBuf, +} + /// Create, amend and save a copy of the specified `Cargo.toml`. pub struct Manifest { path: ManifestPath, toml: value::Table, - /// True if a metadata package should be generated for this manifest - metadata_package: bool, + /// Set to `Some(Contract)` if a metadata package should be generated for this manifest. + /// The `Contract` points to the package for which metadata should be generated. + metadata_package: Option, } impl Manifest { @@ -128,7 +137,7 @@ impl Manifest { Ok(Manifest { path: manifest_path, toml, - metadata_package: false, + metadata_package: None, }) } @@ -213,7 +222,7 @@ impl Manifest { } /// Adds a metadata package to the manifest workspace for generating metadata - pub fn with_metadata_package(&mut self) -> Result<&mut Self> { + pub fn with_metadata_package(&mut self, target_contract: ContractPackage) -> Result<&mut Self> { let workspace = self .toml .entry("workspace") @@ -242,7 +251,7 @@ impl Manifest { members.push(METADATA_PACKAGE_PATH.into()); } - self.metadata_package = true; + self.metadata_package = Some(target_contract); Ok(self) } @@ -359,7 +368,7 @@ impl Manifest { fs::create_dir_all(dir).context(format!("Creating directory '{}'", dir.display()))?; } - if self.metadata_package { + if let Some(metadata_package) = &self.metadata_package { let dir = if let Some(manifest_dir) = manifest_path.directory() { manifest_dir.join(METADATA_PACKAGE_PATH) } else { @@ -368,15 +377,6 @@ impl Manifest { fs::create_dir_all(&dir).context(format!("Creating directory '{}'", dir.display()))?; - let contract_package_name = self - .toml - .get("package") - .ok_or_else(|| anyhow::anyhow!("package section not found"))? - .get("name") - .ok_or_else(|| anyhow::anyhow!("[package] name field not found"))? - .as_str() - .ok_or_else(|| anyhow::anyhow!("[package] name should be a string"))?; - let ink_metadata = self .toml .get("dependencies") @@ -386,7 +386,7 @@ impl Manifest { .as_table() .ok_or_else(|| anyhow::anyhow!("ink_metadata dependency should be a table"))?; - metadata::generate_package(dir, contract_package_name, ink_metadata.clone())?; + metadata::generate_package(dir, &metadata_package, ink_metadata.clone())?; } let updated_toml = toml::to_string(&self.toml)?; diff --git a/src/workspace/metadata.rs b/src/workspace/metadata.rs index 76d356ec..319ba7ad 100644 --- a/src/workspace/metadata.rs +++ b/src/workspace/metadata.rs @@ -18,6 +18,8 @@ use anyhow::Result; use std::{fs, path::Path}; use toml::value; +use crate::workspace::manifest::ContractPackage; + /// Generates a cargo workspace package `metadata-gen` which will be invoked via `cargo run` to /// generate contract metadata. /// @@ -27,13 +29,13 @@ use toml::value; /// versions are utilized. pub(super) fn generate_package>( target_dir: P, - contract_package_name: &str, + target_package: &ContractPackage, mut ink_metadata_dependency: value::Table, ) -> Result<()> { let dir = target_dir.as_ref(); log::debug!( "Generating metadata package for {} in {}", - contract_package_name, + target_package.name, dir.display() ); @@ -53,7 +55,18 @@ pub(super) fn generate_package>( .expect("contract dependency specified in the template") .as_table_mut() .expect("contract dependency is a table specified in the template"); - contract.insert("package".into(), contract_package_name.into()); + + // the metadata data generation package is put under `.ink/metadata_gen` and we need to + // explicitly reference the (possibly sub-)contract which is build. + let path = target_package + .path + .to_str() + .expect("path must be convertible to str"); + contract.insert("path".into(), toml::Value::String(path.to_string())); + contract.insert( + "package".into(), + toml::Value::String(target_package.name.clone()), + ); // make ink_metadata dependency use default features ink_metadata_dependency.remove("default-features"); diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 0866d226..32b7d1b2 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -32,6 +32,8 @@ use std::{ path::{Path, PathBuf}, }; +use crate::workspace::manifest::ContractPackage; + /// Make a copy of a cargo workspace, maintaining only the directory structure and manifest /// files. Relative paths to source files and non-workspace dependencies are rewritten to absolute /// paths to the original locations. @@ -121,10 +123,32 @@ impl Workspace { Ok(self) } - /// Generates a package to invoke for generating contract metadata - pub(super) fn with_metadata_gen_package(&mut self) -> Result<&mut Self> { + /// Generates a package to invoke for generating contract metadata. + /// + /// The contract metadata will be generated for the supplied `package_name` + /// found at `package_path`. + pub(super) fn with_metadata_gen_package( + &mut self, + package_name: String, + package_path: PathBuf, + ) -> Result<&mut Self> { + // We strip the workspace root path from the path where the package is found. + // This way we have the relative path of the package in the workspace. + let stripped = package_path + .strip_prefix(self.workspace_root.clone()) + .expect("132"); + // We prepend this path since the metadata generation package will be under + // `.ink/metadata_gen/` and we need to traverse up from there. + let mut path = PathBuf::from("../../"); + path.push(stripped); + + let target_contract = ContractPackage { + name: package_name, + path, + }; + self.with_workspace_manifest(|manifest| { - manifest.with_metadata_package()?; + manifest.with_metadata_package(target_contract)?; Ok(()) }) } -- GitLab From f342f2364f1e7ba8859ca2f1f64bdcd25e203021 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Fri, 12 Feb 2021 22:25:25 +0100 Subject: [PATCH 2/8] Improve naming and comments --- src/workspace/manifest.rs | 11 ++++++----- src/workspace/mod.rs | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index 23e0dab4..94ff0d3a 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -105,7 +105,7 @@ impl From for PathBuf { } } -/// Pointer to the package of a contract +/// Pointer to the package of a contract. pub struct ContractPackage { /// Name of the contract pub name: String, @@ -117,8 +117,9 @@ pub struct ContractPackage { pub struct Manifest { path: ManifestPath, toml: value::Table, - /// Set to `Some(Contract)` if a metadata package should be generated for this manifest. - /// The `Contract` points to the package for which metadata should be generated. + /// Set to `Some(ContractPackage)` if a metadata package should be + /// generated for this manifest. The `ContractPackage` points to the + /// package for which metadata should be generated. metadata_package: Option, } @@ -368,7 +369,7 @@ impl Manifest { fs::create_dir_all(dir).context(format!("Creating directory '{}'", dir.display()))?; } - if let Some(metadata_package) = &self.metadata_package { + if let Some(metadata_target_package) = &self.metadata_package { let dir = if let Some(manifest_dir) = manifest_path.directory() { manifest_dir.join(METADATA_PACKAGE_PATH) } else { @@ -386,7 +387,7 @@ impl Manifest { .as_table() .ok_or_else(|| anyhow::anyhow!("ink_metadata dependency should be a table"))?; - metadata::generate_package(dir, &metadata_package, ink_metadata.clone())?; + metadata::generate_package(dir, &metadata_target_package, ink_metadata.clone())?; } let updated_toml = toml::to_string(&self.toml)?; diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 32b7d1b2..14694007 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -134,13 +134,13 @@ impl Workspace { ) -> Result<&mut Self> { // We strip the workspace root path from the path where the package is found. // This way we have the relative path of the package in the workspace. - let stripped = package_path + let relative_package_path = package_path .strip_prefix(self.workspace_root.clone()) - .expect("132"); + .expect("package path must be prefixed with workspace path"); // We prepend this path since the metadata generation package will be under // `.ink/metadata_gen/` and we need to traverse up from there. let mut path = PathBuf::from("../../"); - path.push(stripped); + path.push(relative_package_path); let target_contract = ContractPackage { name: package_name, -- GitLab From dfb8b6a59072f4f4cbb538541d48cdf06dd65815 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 16 Feb 2021 17:50:17 +0100 Subject: [PATCH 3/8] Revert me: Hotfix for funty issue --- Cargo.lock | 3 +++ Cargo.toml | 3 +++ 2 files changed, 6 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 3dac3dc7..3ff4004b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "Inflector" version = "0.11.4" @@ -575,6 +577,7 @@ dependencies = [ "colored", "contract-metadata", "env_logger", + "funty", "futures 0.3.12", "heck", "hex", diff --git a/Cargo.toml b/Cargo.toml index 6069dc04..0ad0122a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,9 @@ subxt = { version = "0.13.0", package = "substrate-subxt", optional = true } futures = { version = "0.3.12", optional = true } hex = { version = "0.4.2", optional = true } +# Should be removed once bitvecto-rs/bitvec#105 is resolved +funty = "=1.1.0" + [build-dependencies] anyhow = "1.0.38" zip = { version = "0.5.8", default-features = false } -- GitLab From 2c14e88a56506c404842ec9a88f77aff89f711a1 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 16 Feb 2021 17:49:48 +0100 Subject: [PATCH 4/8] Move path replacement logic --- src/workspace/manifest.rs | 6 ++++++ src/workspace/mod.rs | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index 94ff0d3a..5c66068b 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -147,6 +147,12 @@ impl Manifest { &self.path } + /// If existent the `ContractPackage` points to the package for which + /// metadata should be generated. + pub(super) fn metadata_package_mut(&mut self) -> Option<&mut ContractPackage> { + self.metadata_package.as_mut() + } + /// Get mutable reference to `[lib] crate-types = []` section fn get_crate_types_mut(&mut self) -> Result<&mut value::Array> { let lib = self diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 14694007..bbcc8919 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -132,19 +132,9 @@ impl Workspace { package_name: String, package_path: PathBuf, ) -> Result<&mut Self> { - // We strip the workspace root path from the path where the package is found. - // This way we have the relative path of the package in the workspace. - let relative_package_path = package_path - .strip_prefix(self.workspace_root.clone()) - .expect("package path must be prefixed with workspace path"); - // We prepend this path since the metadata generation package will be under - // `.ink/metadata_gen/` and we need to traverse up from there. - let mut path = PathBuf::from("../../"); - path.push(relative_package_path); - let target_contract = ContractPackage { name: package_name, - path, + path: package_path, }; self.with_workspace_manifest(|manifest| { @@ -173,7 +163,17 @@ impl Workspace { new_path.push(package.manifest_path.strip_prefix(&self.workspace_root)?); let new_manifest = ManifestPath::new(new_path)?; + // replace the original path to the package for which metadata should be + // generated with the absolute path to the package in the temporary directory. + if let Some(package) = manifest.metadata_package_mut() { + let absolute_package_path = package.path.canonicalize()?; + let mut new_path: PathBuf = target.as_ref().into(); + new_path.push(absolute_package_path.strip_prefix(&self.workspace_root)?); + package.path = new_path; + } + manifest.rewrite_relative_paths(&exclude_member_package_names)?; + manifest.write(&new_manifest)?; new_manifest_paths.push((package_id.clone(), new_manifest)); -- GitLab From bca9ee8aad48259a575028c80b1fb9ebc1a9ca55 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 16 Feb 2021 18:07:43 +0100 Subject: [PATCH 5/8] Revert new line change --- src/workspace/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index bbcc8919..d5153b5d 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -173,7 +173,6 @@ impl Workspace { } manifest.rewrite_relative_paths(&exclude_member_package_names)?; - manifest.write(&new_manifest)?; new_manifest_paths.push((package_id.clone(), new_manifest)); -- GitLab From d09fb2a6db6df94af2f594a75593bae3e6a3e002 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Wed, 17 Feb 2021 11:36:40 +0100 Subject: [PATCH 6/8] Simplify for which package metadata is generated --- src/cmd/metadata.rs | 3 +-- src/workspace/manifest.rs | 39 ++++++++++++++++----------------------- src/workspace/metadata.rs | 19 +++---------------- src/workspace/mod.rs | 31 +++++-------------------------- 4 files changed, 25 insertions(+), 67 deletions(-) diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 22425293..809e9ca1 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -123,7 +123,6 @@ impl GenerateMetadataCommand { if self.unstable_options.original_manifest { generate_metadata(&self.crate_metadata.manifest_path)?; } else { - let package_name = self.crate_metadata.package_name.clone(); let manifest_dir = match self.crate_metadata.manifest_path.directory() { Some(dir) => dir, None => Path::new("./"), @@ -140,7 +139,7 @@ impl GenerateMetadataCommand { .with_profile_release_lto(false)?; Ok(()) })? - .with_metadata_gen_package(package_name, absolute_package_path)? + .with_metadata_gen_package(absolute_package_path)? .using_temp(generate_metadata)?; } diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index 5c66068b..bb57b0ab 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -105,22 +105,12 @@ impl From for PathBuf { } } -/// Pointer to the package of a contract. -pub struct ContractPackage { - /// Name of the contract - pub name: String, - /// Path to the contract directory - pub path: PathBuf, -} - /// Create, amend and save a copy of the specified `Cargo.toml`. pub struct Manifest { path: ManifestPath, toml: value::Table, - /// Set to `Some(ContractPackage)` if a metadata package should be - /// generated for this manifest. The `ContractPackage` points to the - /// package for which metadata should be generated. - metadata_package: Option, + /// True if a metadata package should be generated for this manifest + metadata_package: bool, } impl Manifest { @@ -138,7 +128,7 @@ impl Manifest { Ok(Manifest { path: manifest_path, toml, - metadata_package: None, + metadata_package: false, }) } @@ -147,12 +137,6 @@ impl Manifest { &self.path } - /// If existent the `ContractPackage` points to the package for which - /// metadata should be generated. - pub(super) fn metadata_package_mut(&mut self) -> Option<&mut ContractPackage> { - self.metadata_package.as_mut() - } - /// Get mutable reference to `[lib] crate-types = []` section fn get_crate_types_mut(&mut self) -> Result<&mut value::Array> { let lib = self @@ -229,7 +213,7 @@ impl Manifest { } /// Adds a metadata package to the manifest workspace for generating metadata - pub fn with_metadata_package(&mut self, target_contract: ContractPackage) -> Result<&mut Self> { + pub fn with_metadata_package(&mut self) -> Result<&mut Self> { let workspace = self .toml .entry("workspace") @@ -258,7 +242,7 @@ impl Manifest { members.push(METADATA_PACKAGE_PATH.into()); } - self.metadata_package = Some(target_contract); + self.metadata_package = true; Ok(self) } @@ -375,13 +359,22 @@ impl Manifest { fs::create_dir_all(dir).context(format!("Creating directory '{}'", dir.display()))?; } - if let Some(metadata_target_package) = &self.metadata_package { + if self.metadata_package { let dir = if let Some(manifest_dir) = manifest_path.directory() { manifest_dir.join(METADATA_PACKAGE_PATH) } else { METADATA_PACKAGE_PATH.into() }; + let contract_package_name = self + .toml + .get("package") + .ok_or_else(|| anyhow::anyhow!("package section not found"))? + .get("name") + .ok_or_else(|| anyhow::anyhow!("[package] name field not found"))? + .as_str() + .ok_or_else(|| anyhow::anyhow!("[package] name should be a string"))?; + fs::create_dir_all(&dir).context(format!("Creating directory '{}'", dir.display()))?; let ink_metadata = self @@ -393,7 +386,7 @@ impl Manifest { .as_table() .ok_or_else(|| anyhow::anyhow!("ink_metadata dependency should be a table"))?; - metadata::generate_package(dir, &metadata_target_package, ink_metadata.clone())?; + metadata::generate_package(dir, contract_package_name, ink_metadata.clone())?; } let updated_toml = toml::to_string(&self.toml)?; diff --git a/src/workspace/metadata.rs b/src/workspace/metadata.rs index 319ba7ad..76d356ec 100644 --- a/src/workspace/metadata.rs +++ b/src/workspace/metadata.rs @@ -18,8 +18,6 @@ use anyhow::Result; use std::{fs, path::Path}; use toml::value; -use crate::workspace::manifest::ContractPackage; - /// Generates a cargo workspace package `metadata-gen` which will be invoked via `cargo run` to /// generate contract metadata. /// @@ -29,13 +27,13 @@ use crate::workspace::manifest::ContractPackage; /// versions are utilized. pub(super) fn generate_package>( target_dir: P, - target_package: &ContractPackage, + contract_package_name: &str, mut ink_metadata_dependency: value::Table, ) -> Result<()> { let dir = target_dir.as_ref(); log::debug!( "Generating metadata package for {} in {}", - target_package.name, + contract_package_name, dir.display() ); @@ -55,18 +53,7 @@ pub(super) fn generate_package>( .expect("contract dependency specified in the template") .as_table_mut() .expect("contract dependency is a table specified in the template"); - - // the metadata data generation package is put under `.ink/metadata_gen` and we need to - // explicitly reference the (possibly sub-)contract which is build. - let path = target_package - .path - .to_str() - .expect("path must be convertible to str"); - contract.insert("path".into(), toml::Value::String(path.to_string())); - contract.insert( - "package".into(), - toml::Value::String(target_package.name.clone()), - ); + contract.insert("package".into(), contract_package_name.into()); // make ink_metadata dependency use default features ink_metadata_dependency.remove("default-features"); diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index d5153b5d..11b39485 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -32,8 +32,6 @@ use std::{ path::{Path, PathBuf}, }; -use crate::workspace::manifest::ContractPackage; - /// Make a copy of a cargo workspace, maintaining only the directory structure and manifest /// files. Relative paths to source files and non-workspace dependencies are rewritten to absolute /// paths to the original locations. @@ -101,16 +99,15 @@ impl Workspace { } /// Amend the workspace manifest using the supplied function. - pub fn with_workspace_manifest(&mut self, f: F) -> Result<&mut Self> + pub fn with_contract_manifest(&mut self, path: &Path, f: F) -> Result<&mut Self> where F: FnOnce(&mut Manifest) -> Result<()>, { - let workspace_root = self.workspace_root.clone(); let workspace_manifest = self .members .iter_mut() .find_map(|(_, (_, manifest))| { - if manifest.path().directory() == Some(&workspace_root) { + if manifest.path().directory() == Some(path) { Some(manifest) } else { None @@ -127,18 +124,9 @@ impl Workspace { /// /// The contract metadata will be generated for the supplied `package_name` /// found at `package_path`. - pub(super) fn with_metadata_gen_package( - &mut self, - package_name: String, - package_path: PathBuf, - ) -> Result<&mut Self> { - let target_contract = ContractPackage { - name: package_name, - path: package_path, - }; - - self.with_workspace_manifest(|manifest| { - manifest.with_metadata_package(target_contract)?; + pub(super) fn with_metadata_gen_package(&mut self, package_path: PathBuf) -> Result<&mut Self> { + self.with_contract_manifest(&package_path, |manifest| { + manifest.with_metadata_package()?; Ok(()) }) } @@ -163,15 +151,6 @@ impl Workspace { new_path.push(package.manifest_path.strip_prefix(&self.workspace_root)?); let new_manifest = ManifestPath::new(new_path)?; - // replace the original path to the package for which metadata should be - // generated with the absolute path to the package in the temporary directory. - if let Some(package) = manifest.metadata_package_mut() { - let absolute_package_path = package.path.canonicalize()?; - let mut new_path: PathBuf = target.as_ref().into(); - new_path.push(absolute_package_path.strip_prefix(&self.workspace_root)?); - package.path = new_path; - } - manifest.rewrite_relative_paths(&exclude_member_package_names)?; manifest.write(&new_manifest)?; -- GitLab From c46b6a41eb4c461b678265a2dc82a6dfd458f096 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Wed, 17 Feb 2021 14:45:20 +0100 Subject: [PATCH 7/8] Change order back --- src/workspace/manifest.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index bb57b0ab..40ef117c 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -366,6 +366,8 @@ impl Manifest { METADATA_PACKAGE_PATH.into() }; + fs::create_dir_all(&dir).context(format!("Creating directory '{}'", dir.display()))?; + let contract_package_name = self .toml .get("package") @@ -375,8 +377,6 @@ impl Manifest { .as_str() .ok_or_else(|| anyhow::anyhow!("[package] name should be a string"))?; - fs::create_dir_all(&dir).context(format!("Creating directory '{}'", dir.display()))?; - let ink_metadata = self .toml .get("dependencies") -- GitLab From d7062c4c8cc5df8e3c57002a59cc581331822421 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Wed, 17 Feb 2021 14:47:11 +0100 Subject: [PATCH 8/8] Make code clearer --- src/workspace/mod.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 11b39485..bc461d0d 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -98,16 +98,16 @@ impl Workspace { Ok(self) } - /// Amend the workspace manifest using the supplied function. - pub fn with_contract_manifest(&mut self, path: &Path, f: F) -> Result<&mut Self> + /// Amend the manifest of the package at `package_path` using the supplied function. + pub fn with_contract_manifest(&mut self, package_path: &Path, f: F) -> Result<&mut Self> where F: FnOnce(&mut Manifest) -> Result<()>, { - let workspace_manifest = self + let manifest = self .members .iter_mut() .find_map(|(_, (_, manifest))| { - if manifest.path().directory() == Some(path) { + if manifest.path().directory() == Some(package_path) { Some(manifest) } else { None @@ -116,14 +116,13 @@ impl Workspace { .ok_or_else(|| { anyhow::anyhow!("The workspace root package should be a workspace member") })?; - f(workspace_manifest)?; + f(manifest)?; Ok(self) } /// Generates a package to invoke for generating contract metadata. /// - /// The contract metadata will be generated for the supplied `package_name` - /// found at `package_path`. + /// The contract metadata will be generated for the package found at `package_path`. pub(super) fn with_metadata_gen_package(&mut self, package_path: PathBuf) -> Result<&mut Self> { self.with_contract_manifest(&package_path, |manifest| { manifest.with_metadata_package()?; -- GitLab