Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/cmd/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ use contract_metadata::{
SourceLanguage, SourceWasm, User,
};
use semver::Version;
use std::{
fs,
path::{Path, PathBuf},
};
use std::{fs, path::PathBuf};
use url::Url;

const METADATA_FILE: &str = "metadata.json";
Expand Down Expand Up @@ -123,12 +120,6 @@ 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 @@ -139,7 +130,7 @@ impl GenerateMetadataCommand {
.with_profile_release_lto(false)?;
Ok(())
})?
.with_metadata_gen_package(absolute_package_path)?
.with_metadata_gen_package(util::absolute_path(&self.crate_metadata.manifest_path)?)?
.using_temp(generate_metadata)?;
}

Expand Down
9 changes: 8 additions & 1 deletion src/crate_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use crate::util;
use crate::ManifestPath;

use anyhow::{Context, Result};
use cargo_metadata::{Metadata as CargoMetadata, MetadataCommand, Package};
use semver::Version;
Expand Down Expand Up @@ -43,11 +45,16 @@ impl CrateMetadata {
/// Parses the contract manifest and returns relevant metadata.
pub fn collect(manifest_path: &ManifestPath) -> Result<Self> {
let (metadata, root_package) = get_cargo_metadata(manifest_path)?;
let target_directory = metadata.target_directory.as_path().join("ink");
let mut target_directory = metadata.target_directory.as_path().join("ink");

// Normalize the package name.
let package_name = root_package.name.replace("-", "_");

let absolute_manifest_path = util::absolute_path(&manifest_path)?;
if absolute_manifest_path != metadata.workspace_root {
target_directory = target_directory.join(package_name.clone());
}

// {target_dir}/wasm32-unknown-unknown/release/{package_name}.wasm
let mut original_wasm = target_directory.clone();
original_wasm.push("wasm32-unknown-unknown");
Expand Down
39 changes: 37 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use crate::workspace::ManifestPath;
use crate::Verbosity;

use anyhow::{Context, Result};
use rustc_version::Channel;
use std::{ffi::OsStr, path::Path, process::Command};
use std::{
ffi::OsStr,
path::{Path, PathBuf},
process::Command,
};

/// Check whether the current rust channel is valid: `nightly` is recommended.
pub fn assert_channel() -> Result<()> {
Expand Down Expand Up @@ -92,6 +98,15 @@ pub(crate) fn base_name(path: &Path) -> &str {
.expect("must be valid utf-8")
}

/// Returns the absolute path to the manifest directory.
pub(crate) fn absolute_path(manifest_path: &ManifestPath) -> Result<PathBuf, std::io::Error> {
let directory = match manifest_path.directory() {
Some(dir) => dir,
None => Path::new("./"),
};
directory.canonicalize()
}

/// Prints to stdout if `verbosity.is_verbose()` is `true`.
#[macro_export]
macro_rules! maybe_println {
Expand All @@ -104,7 +119,8 @@ macro_rules! maybe_println {

#[cfg(test)]
pub mod tests {
use std::path::Path;
use crate::workspace::ManifestPath;
use std::{fs, path::Path};

pub fn with_tmp_dir<F>(f: F)
where
Expand All @@ -118,4 +134,23 @@ pub mod tests {
// catch test panics in order to clean up temp dir which will be very large
f(tmp_dir.path()).expect("Error executing test with tmp dir")
}

#[test]
fn must_return_absolute_path_from_absolute_path() {
with_tmp_dir(|path| {
// given
let cargo_toml_path = path.join("Cargo.toml");
let _ = fs::File::create(&cargo_toml_path).expect("file creation failed");
let manifest_path =
ManifestPath::new(cargo_toml_path).expect("manifest path creation failed");

// when
let absolute_path =
super::absolute_path(&manifest_path).expect("absolute path extraction failed");

// then
assert_eq!(absolute_path.as_path(), path);
Ok(())
})
}
}