Skip to content

Commit

Permalink
Updated from_dir to check parent directories (FuelLabs#1232)
Browse files Browse the repository at this point in the history
* moved check project type to manifest

* added find_manifest_dir to from_dir

* find and remove duplicate find_manifest_dir

* added manifest_dir to fns that require it
  • Loading branch information
eureka-cpu authored Apr 14, 2022
1 parent 8333abe commit 030ac36
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 62 deletions.
24 changes: 20 additions & 4 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::pkg::parsing_failed;
use crate::pkg::{manifest_file_missing, parsing_failed, wrong_program_type};
use anyhow::{anyhow, bail, Result};
use forc_util::{println_yellow_err, validate_name};
use forc_util::{find_manifest_dir, println_yellow_err, validate_name};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -96,11 +96,13 @@ impl Manifest {
Ok(manifest)
}

/// Given a directory to a forc project containing a `Forc.toml`, read the manifest.
/// Read the manifest from the `Forc.toml` in the directory specified by the given `path` or any of its parent directories.
///
/// This is short for `Manifest::from_file`, but takes care of constructing the path to the
/// file.
pub fn from_dir(manifest_dir: &Path, sway_git_tag: &str) -> Result<Self> {
pub fn from_dir(dir: &Path, sway_git_tag: &str) -> Result<Self> {
let manifest_dir =
find_manifest_dir(dir).ok_or_else(|| manifest_file_missing(dir.to_path_buf()))?;
let file_path = manifest_dir.join(constants::MANIFEST_FILE_NAME);
Self::from_file(&file_path, sway_git_tag)
}
Expand Down Expand Up @@ -170,6 +172,20 @@ impl Manifest {
}
}

/// Given the current directory and expected program type, determines whether the correct program type is present.
pub fn check_program_type(&self, manifest_dir: PathBuf, expected_type: TreeType) -> Result<()> {
let parsed_type = self.program_type(manifest_dir)?;
if parsed_type != expected_type {
bail!(wrong_program_type(
&self.project.name,
expected_type,
parsed_type
));
} else {
Ok(())
}
}

/// Check for the `core` and `std` packages under `[dependencies]`. If both are missing, add
/// `std` implicitly.
///
Expand Down
18 changes: 0 additions & 18 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,21 +1221,3 @@ pub fn fuel_core_not_running(node_url: &str) -> anyhow::Error {
let message = format!("could not get a response from node at the URL {}. Start a node with `fuel-core`. See https://github.com/FuelLabs/fuel-core#running for more information", node_url);
Error::msg(message)
}

/// Given the current directory and expected program type, determines whether the correct program type is present.
pub fn check_program_type(
manifest: &Manifest,
manifest_dir: PathBuf,
expected_type: TreeType,
) -> Result<()> {
let parsed_type = manifest.program_type(manifest_dir)?;
if parsed_type != expected_type {
bail!(wrong_program_type(
&manifest.project.name,
expected_type,
parsed_type
));
} else {
Ok(())
}
}
9 changes: 4 additions & 5 deletions forc/src/ops/forc_abi_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
utils::SWAY_GIT_TAG,
};
use anyhow::Result;
use forc_pkg::{check_program_type, manifest_file_missing, Manifest};
use forc_pkg::Manifest;
use forc_util::find_manifest_dir;
use serde_json::{json, Value};
use std::fs::File;
Expand All @@ -16,10 +16,9 @@ pub fn build(command: JsonAbiCommand) -> Result<Value> {
} else {
std::env::current_dir()?
};
let manifest_dir =
find_manifest_dir(&curr_dir).ok_or_else(|| manifest_file_missing(curr_dir))?;
let manifest = Manifest::from_dir(&manifest_dir, SWAY_GIT_TAG)?;
check_program_type(&manifest, manifest_dir, TreeType::Contract)?;
let manifest = Manifest::from_dir(&curr_dir, SWAY_GIT_TAG)?;
let manifest_dir = find_manifest_dir(&curr_dir).unwrap();
manifest.check_program_type(manifest_dir, TreeType::Contract)?;

let build_command = BuildCommand {
path: command.path,
Expand Down
18 changes: 3 additions & 15 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{cli::BuildCommand, utils::SWAY_GIT_TAG};
use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, Result};
use forc_pkg::{self as pkg, lock, Lock, Manifest};
use forc_util::{default_output_directory, find_manifest_dir, lock_path};
use std::{
fs::{self, File},
path::PathBuf,
};
use sway_utils::MANIFEST_FILE_NAME;

pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
let BuildCommand {
Expand All @@ -31,24 +30,13 @@ pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
silent: silent_mode,
};

// find manifest directory, even if in subdirectory
let this_dir = if let Some(ref path) = path {
PathBuf::from(path)
} else {
std::env::current_dir()?
};

let manifest_dir = match find_manifest_dir(&this_dir) {
Some(dir) => dir,
None => {
bail!(
"could not find `{}` in `{}` or any parent directory",
MANIFEST_FILE_NAME,
this_dir.display(),
);
}
};
let manifest = Manifest::from_dir(&manifest_dir, SWAY_GIT_TAG)?;
let manifest = Manifest::from_dir(&this_dir, SWAY_GIT_TAG)?;
let manifest_dir = find_manifest_dir(&this_dir).unwrap();
let lock_path = lock_path(&manifest_dir);

// Load the build plan from the lock file.
Expand Down
9 changes: 4 additions & 5 deletions forc/src/ops/forc_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
utils::SWAY_GIT_TAG,
};
use anyhow::{bail, Result};
use forc_pkg::{check_program_type, manifest_file_missing, Manifest};
use forc_pkg::Manifest;
use forc_util::find_manifest_dir;
use fuel_gql_client::client::FuelClient;
use fuel_tx::{Output, Salt, Transaction};
Expand All @@ -19,10 +19,9 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
} else {
std::env::current_dir()?
};
let manifest_dir =
find_manifest_dir(&curr_dir).ok_or_else(|| manifest_file_missing(curr_dir))?;
let manifest = Manifest::from_dir(&manifest_dir, SWAY_GIT_TAG)?;
check_program_type(&manifest, manifest_dir, TreeType::Contract)?;
let manifest = Manifest::from_dir(&curr_dir, SWAY_GIT_TAG)?;
let manifest_dir = find_manifest_dir(&curr_dir).unwrap();
manifest.check_program_type(manifest_dir, TreeType::Contract)?;

let DeployCommand {
path,
Expand Down
9 changes: 4 additions & 5 deletions forc/src/ops/forc_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::ops::forc_build;
use crate::utils::parameters::TxParameters;
use crate::utils::SWAY_GIT_TAG;
use anyhow::{anyhow, bail, Result};
use forc_pkg::{check_program_type, fuel_core_not_running, manifest_file_missing, Manifest};
use forc_pkg::{fuel_core_not_running, Manifest};
use forc_util::find_manifest_dir;
use fuel_gql_client::client::FuelClient;
use fuel_tx::Transaction;
Expand All @@ -18,10 +18,9 @@ pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
} else {
std::env::current_dir().map_err(|e| anyhow!("{:?}", e))?
};
let manifest_dir =
find_manifest_dir(&path_dir).ok_or_else(|| manifest_file_missing(path_dir))?;
let manifest = Manifest::from_dir(&manifest_dir, SWAY_GIT_TAG)?;
check_program_type(&manifest, manifest_dir, TreeType::Script)?;
let manifest = Manifest::from_dir(&path_dir, SWAY_GIT_TAG)?;
let manifest_dir = find_manifest_dir(&path_dir).unwrap();
manifest.check_program_type(manifest_dir, TreeType::Script)?;

let input_data = &command.data.unwrap_or_else(|| "".into());
let data = format_hex_data(input_data);
Expand Down
12 changes: 2 additions & 10 deletions forc/src/ops/forc_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,9 @@ pub async fn update(command: UpdateCommand) -> Result<()> {
Some(path) => PathBuf::from(path),
None => std::env::current_dir()?,
};
let manifest_dir = match find_manifest_dir(&this_dir) {
Some(dir) => dir,
None => {
return Err(anyhow!(
"No manifest file found in this directory or any parent directories of it: {:?}",
this_dir
))
}
};

let manifest = Manifest::from_dir(&manifest_dir, SWAY_GIT_TAG)?;
let manifest = Manifest::from_dir(&this_dir, SWAY_GIT_TAG)?;
let manifest_dir = find_manifest_dir(&this_dir).unwrap();
let lock_path = lock_path(&manifest_dir);
let old_lock = Lock::from_path(&lock_path).ok().unwrap_or_default();
let offline = false;
Expand Down

0 comments on commit 030ac36

Please sign in to comment.