Skip to content

Commit

Permalink
feat(build): add option to build specific dir (#8149)
Browse files Browse the repository at this point in the history
* feat(build): add option to build specific dir

* Changes after review:
- use source_files_iter helper, build single files and child dirs as well
- rename arg to paths, use 0.. pos arg

* Changes after review: reuse MultiCompilerLanguage::FILE_EXTENSIONS
  • Loading branch information
grandizzy authored Jun 14, 2024
1 parent 6a85dba commit c2e5297
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/cli/src/opts/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ pub struct CoreBuildArgs {
#[serde(skip)]
pub skip: Option<Vec<SkipBuildFilter>>,

/// Build source files from specified paths.
#[arg(long, short, num_args(0..))]
#[serde(skip)]
pub paths: Option<Vec<PathBuf>>,

#[command(flatten)]
#[serde(flatten)]
pub compiler: CompilerArgs,
Expand Down
15 changes: 14 additions & 1 deletion crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ 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::{
compilers::{multi::MultiCompilerLanguage, Language},
utils::source_files_iter,
Project, ProjectCompileOutput,
};
use foundry_config::{
figment::{
self,
Expand Down Expand Up @@ -80,7 +84,16 @@ impl BuildArgs {

let project = config.project()?;

// Collect sources to compile if build subdirectories specified.
let mut files = vec![];
if let Some(dirs) = self.args.paths {
for dir in dirs {
files.extend(source_files_iter(dir, MultiCompilerLanguage::FILE_EXTENSIONS));
}
}

let compiler = ProjectCompiler::new()
.files(files)
.print_names(self.names)
.print_sizes(self.sizes)
.quiet(self.format_json)
Expand Down
63 changes: 63 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,69 @@ function test_run() external {}
);
});

forgetest_init!(can_build_specific_paths, |prj, cmd| {
prj.wipe();
prj.add_source(
"Counter.sol",
r"
contract Counter {
function count() external {}
}",
)
.unwrap();
prj.add_test(
"Foo.sol",
r"
contract Foo {
function test_foo() external {}
}",
)
.unwrap();
prj.add_test(
"Bar.sol",
r"
contract Bar {
function test_bar() external {}
}",
)
.unwrap();

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

// Build single test file
prj.clear();
cmd.forge_fuse();
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_path_with_one_file.stdout"),
);
});

// checks that build --sizes includes all contracts even if unchanged
forgetest_init!(can_build_sizes_repeatedly, |prj, cmd| {
prj.clear_cache();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiling 1 files with 0.8.23
Solc 0.8.23 finished in 33.25ms
Compiler run successful!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiling 3 files with 0.8.23
Solc 0.8.23 finished in 33.25ms
Compiler run successful!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Compiling 2 files with 0.8.23
Solc 0.8.23 finished in 33.25ms
Compiler run successful!

0 comments on commit c2e5297

Please sign in to comment.