// Copyright 2018-2020 Parity Technologies (UK) Ltd.
// This file is part of cargo-contract.
//
// cargo-contract is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cargo-contract is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see .
use anyhow::{Context, Result};
use super::{metadata, Profile};
use std::convert::{TryFrom, TryInto};
use std::{
collections::HashSet,
fs,
path::{Path, PathBuf},
};
use toml::value;
const MANIFEST_FILE: &str = "Cargo.toml";
const LEGACY_METADATA_PACKAGE_PATH: &str = ".ink/abi_gen";
const METADATA_PACKAGE_PATH: &str = ".ink/metadata_gen";
/// Path to a Cargo.toml file
#[derive(Clone, Debug)]
pub struct ManifestPath {
path: PathBuf,
}
impl ManifestPath {
/// Create a new ManifestPath, errors if not path to `Cargo.toml`
pub fn new>(path: P) -> Result {
let manifest = path.as_ref();
if let Some(file_name) = manifest.file_name() {
if file_name != MANIFEST_FILE {
anyhow::bail!("Manifest file must be a Cargo.toml")
}
}
Ok(ManifestPath {
path: manifest.into(),
})
}
/// Create an arg `--manifest-path=` for `cargo` command
pub fn cargo_arg(&self) -> String {
format!("--manifest-path={}", self.path.to_string_lossy())
}
/// The directory path of the manifest path.
///
/// Returns `None` if the path is just the plain file name `Cargo.toml`
pub fn directory(&self) -> Option<&Path> {
let just_a_file_name =
self.path.iter().collect::>() == vec![Path::new(MANIFEST_FILE)];
if !just_a_file_name {
self.path.parent()
} else {
None
}
}
}
impl TryFrom<&PathBuf> for ManifestPath {
type Error = anyhow::Error;
fn try_from(value: &PathBuf) -> Result {
ManifestPath::new(value)
}
}
impl