Skip to content

Commit

Permalink
feat: install missing deps on build (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg committed Aug 4, 2022
1 parent 6e34042 commit 54191b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
28 changes: 25 additions & 3 deletions cli/src/cmd/forge/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
//! build command

//! Build command
use crate::{
cmd::{forge::watch::WatchArgs, Cmd},
cmd::{
forge::{
install::{self, DependencyInstallOpts},
watch::WatchArgs,
},
Cmd,
},
compile,
utils::p_println,
};
use clap::Parser;
use ethers::solc::{Project, ProjectCompileOutput};
use eyre::WrapErr;
use foundry_config::{
figment::{
self,
Expand Down Expand Up @@ -68,6 +75,21 @@ impl Cmd for BuildArgs {
fn run(self) -> eyre::Result<Self::Output> {
let project = self.project()?;

if install::has_missing_dependencies(&project.root()) {
// The extra newline is needed, otherwise the compiler output will overwrite the
// message
p_println!(!self.args.silent => "Missing dependencies found. Installing now.\n");
install::install(
&project.root(),
Vec::new(),
DependencyInstallOpts {
// TODO(onbjerg): We should settle on --quiet or --silent.
quiet: self.args.silent,
..Default::default()
},
).wrap_err("Your project has missing dependencies that could not be installed. Please ensure git is installed and available.")?
}

if self.args.silent {
compile::suppress_compile(&project)
} else {
Expand Down
19 changes: 17 additions & 2 deletions cli/src/cmd/forge/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,22 @@ pub(crate) fn install(
Ok(())
}

/// installs the dependency as an ordinary folder instead of a submodule
/// Checks if any submodules have not been initialized yet.
///
/// `git submodule status` will return a new line per submodule in the repository. If any line
/// starts with `-` then it has not been initialized yet.
pub fn has_missing_dependencies(root: impl AsRef<Path>) -> bool {
Command::new("git")
.args(&["submodule", "status"])
.current_dir(root)
.output()
.map(|output| {
String::from_utf8_lossy(&output.stdout).lines().any(|line| line.starts_with('-'))
})
.unwrap_or(false)
}

/// Installs the dependency as an ordinary folder instead of a submodule
fn install_as_folder(dep: &Dependency, libs: &Path, target_dir: &str) -> eyre::Result<()> {
// install the dep
git_clone(dep, libs, target_dir)?;
Expand All @@ -158,7 +173,7 @@ fn install_as_folder(dep: &Dependency, libs: &Path, target_dir: &str) -> eyre::R
Ok(())
}

/// installs the dependency as new submodule
/// Installs the dependency as new submodule
fn install_as_submodule(
dep: &Dependency,
libs: &Path,
Expand Down

0 comments on commit 54191b6

Please sign in to comment.