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: install missing deps on build #2617

Merged
merged 1 commit into from
Aug 4, 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
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