Skip to content

Commit

Permalink
Generate metadata explicitly for the contract which is build (#174)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Michael Müller authored Feb 18, 2021
1 parent 7182b43 commit 594b637
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/cmd/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand All @@ -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)?;
}

Expand Down
19 changes: 10 additions & 9 deletions src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,16 @@ impl Workspace {
Ok(self)
}

/// Amend the workspace manifest using the supplied function.
pub fn with_workspace_manifest<F>(&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<F>(&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
Expand All @@ -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(())
})
Expand Down

0 comments on commit 594b637

Please sign in to comment.