Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forge): allow install in config folder #1858

Merged
merged 3 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions cli/src/cmd/forge/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ impl Cmd for InstallArgs {
let InstallArgs { root, .. } = self;
let root = root.unwrap_or_else(|| find_project_root_path().unwrap());
install(&root, self.dependencies, self.opts)?;
let mut config = Config::load_with_root(root);
let lib = PathBuf::from("lib");
if !config.libs.contains(&lib) {
config.libs.push(lib);
config.update_libs()?;
}
Ok(())
}
}
Expand All @@ -90,7 +84,10 @@ pub(crate) fn install(
opts: DependencyInstallOpts,
) -> eyre::Result<()> {
let root = root.as_ref();
let libs = root.join("lib");
let mut config = Config::load_with_root(root);

let install_lib_dir = config.install_lib_dir();
let libs = root.join(&install_lib_dir);

if dependencies.is_empty() {
Command::new("git")
Expand Down Expand Up @@ -123,6 +120,12 @@ pub(crate) fn install(

p_println!(!quiet => " {} {}", Paint::green("Installed"), dep.name);
}

// update `libs` in config if not included yet
if !config.libs.contains(&install_lib_dir) {
config.libs.push(install_lib_dir);
config.update_libs()?;
}
Ok(())
}

Expand Down
40 changes: 40 additions & 0 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ impl Config {
self.remappings.dedup();
}

/// Returns the directory in which dependencies should be installed
///
/// Returns the first dir from `libs` that is not `node_modules` or `lib` if `libs` is empty
pub fn install_lib_dir(&self) -> PathBuf {
self.libs
.iter()
.find(|p| !p.ends_with("node_modules"))
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("lib"))
}

/// Serves as the entrypoint for obtaining the project.
///
/// Returns the `Project` configured with all `solc` and path related values.
Expand Down Expand Up @@ -2014,6 +2025,35 @@ mod tests {
use std::{fs::File, io::Write};
use tempfile::tempdir;

#[test]
fn test_install_dir() {
figment::Jail::expect_with(|jail| {
let config = Config::load();
assert_eq!(config.install_lib_dir(), PathBuf::from("lib"));
jail.create_file(
"foundry.toml",
r#"
[default]
libs = ['node_modules', 'lib']
"#,
)?;
let config = Config::load();
assert_eq!(config.install_lib_dir(), PathBuf::from("lib"));

jail.create_file(
"foundry.toml",
r#"
[default]
libs = ['custom', 'node_modules', 'lib']
"#,
)?;
let config = Config::load();
assert_eq!(config.install_lib_dir(), PathBuf::from("custom"));

Ok(())
});
}

#[test]
fn test_figment_is_default() {
figment::Jail::expect_with(|_| {
Expand Down