From 594b63777d6a5a58baccdef0db32efbd00a055d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=BCller?= Date: Thu, 18 Feb 2021 19:45:25 +0100 Subject: [PATCH] Generate metadata explicitly for the contract which is build (#174) * Generate metadata explicitly for the contract which is build * Improve naming and comments * Revert me: Hotfix for funty issue * Move path replacement logic * Revert new line change * Simplify for which package metadata is generated * Change order back * Make code clearer --- src/cmd/metadata.rs | 13 +++++++++++-- src/workspace/mod.rs | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 4a20bb315..c301c3015 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,12 @@ impl GenerateMetadataCommand { if self.unstable_options.original_manifest { generate_metadata(&self.crate_metadata.manifest_path)?; } else { + 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 +139,7 @@ impl GenerateMetadataCommand { .with_profile_release_lto(false)?; Ok(()) })? - .with_metadata_gen_package()? + .with_metadata_gen_package(absolute_package_path)? .using_temp(generate_metadata)?; } diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index 0866d226d..bc461d0dc 100644 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -98,17 +98,16 @@ impl Workspace { Ok(self) } - /// Amend the workspace manifest using the supplied function. - pub fn with_workspace_manifest(&mut self, 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_root = self.workspace_root.clone(); - let workspace_manifest = self + let manifest = self .members .iter_mut() .find_map(|(_, (_, manifest))| { - if manifest.path().directory() == Some(&workspace_root) { + if manifest.path().directory() == Some(package_path) { Some(manifest) } else { None @@ -117,13 +116,15 @@ 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 - pub(super) fn with_metadata_gen_package(&mut self) -> Result<&mut Self> { - self.with_workspace_manifest(|manifest| { + /// Generates a package to invoke for generating contract metadata. + /// + /// 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()?; Ok(()) })