Skip to content

Commit

Permalink
Changes after review:
Browse files Browse the repository at this point in the history
- use source_files_iter helper, build single files and child dirs as well
- rename arg to paths, use 0.. pos arg
  • Loading branch information
grandizzy committed Jun 13, 2024
1 parent d67bbb7 commit 6da9a55
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
8 changes: 3 additions & 5 deletions crates/cli/src/opts/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,10 @@ pub struct CoreBuildArgs {
#[serde(skip)]
pub skip: Option<Vec<SkipBuildFilter>>,

/// Build files within specific directories.
///
/// Child directories are not accounted and should be explicitly added.
#[arg(long, num_args(1..))]
/// Build source files from specified paths.
#[arg(long, short, num_args(0..))]
#[serde(skip)]
pub dirs: Option<Vec<PathBuf>>,
pub paths: Option<Vec<PathBuf>>,

#[command(flatten)]
#[serde(flatten)]
Expand Down
16 changes: 6 additions & 10 deletions crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use eyre::Result;
use foundry_cli::{opts::CoreBuildArgs, utils::LoadConfig};
use foundry_common::compile::ProjectCompiler;
use foundry_compilers::{Project, ProjectCompileOutput};
use foundry_compilers::{utils::source_files_iter, Project, ProjectCompileOutput};
use foundry_config::{
figment::{
self,
Expand All @@ -14,11 +14,13 @@ use foundry_config::{
Config,
};
use serde::Serialize;
use std::fs;
use watchexec::config::{InitConfig, RuntimeConfig};

foundry_config::merge_impl_figment_convert!(BuildArgs, args);

// Extensions accepted by `forge build`
const BUILD_EXTENSIONS: &[&str] = &["sol", "yul", "vy", "vyi"];

/// CLI arguments for `forge build`.
///
/// CLI arguments take the highest precedence in the Config/Figment hierarchy.
Expand Down Expand Up @@ -83,15 +85,9 @@ impl BuildArgs {

// Collect sources to compile if build subdirectories specified.
let mut files = vec![];
if let Some(dirs) = self.args.dirs {
if let Some(dirs) = self.args.paths {
for dir in dirs {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
files.push(entry.path());
}
}
files.extend(source_files_iter(dir, BUILD_EXTENSIONS));
}
}

Expand Down
31 changes: 21 additions & 10 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@ function test_run() external {}
);
});

forgetest_init!(can_build_specific_dir, |prj, cmd| {
forgetest_init!(can_build_specific_paths, |prj, cmd| {
prj.wipe();
prj.add_source(
"Counter.sol",
Expand All @@ -1694,28 +1694,39 @@ function test_bar() external {}
)
.unwrap();

// Build only files within test dir
// Build 2 files within test dir
prj.clear();
cmd.args(["build", "--dirs", "test", "--force"]);
cmd.args(["build", "--paths", "test", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/can_build_test_dir.stdout"),
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_two_files.stdout"),
);

// Build one file within src dir
prj.clear();
cmd.forge_fuse();
cmd.args(["build", "--paths", "src", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_one_file.stdout"),
);

// Build only files within src dir
// Build 3 files from test and src dirs
prj.clear();
cmd.forge_fuse();
cmd.args(["build", "--dirs", "src", "--force"]);
cmd.args(["build", "--paths", "src", "test", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/can_build_src_dir.stdout"),
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_path_with_three_files.stdout"),
);

// Build multiple dirs
// Build single test file
prj.clear();
cmd.forge_fuse();
cmd.args(["build", "--dirs", "src", "--dirs", "test", "--force"]);
cmd.args(["build", "--paths", "test/Bar.sol", "--force"]);
cmd.unchecked_output().stdout_matches_path(
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/can_build_multiple_dirs.stdout"),
.join("tests/fixtures/can_build_path_with_one_file.stdout"),
);
});

Expand Down

0 comments on commit 6da9a55

Please sign in to comment.