Skip to content

Commit

Permalink
feat: support check --patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Oct 24, 2024
1 parent eacda6b commit 2f4976e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 9 deletions.
12 changes: 11 additions & 1 deletion crates/moon/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ pub struct CheckSubcommand {
#[clap(long, short)]
pub watch: bool,

/// The package to check
/// The package(and it's deps) to check
pub package_path: Option<PathBuf>,

/// The patch file to check, Only valid when checking specified package.
#[clap(long, requires = "package_path")]
pub patch_file: Option<PathBuf>,

/// Whether to skip the mi generation, Only valid when checking specified package.
#[clap(long, requires = "package_path")]
pub no_mi: bool,
}

pub fn run_check(cli: &UniversalFlags, cmd: &CheckSubcommand) -> anyhow::Result<i32> {
Expand Down Expand Up @@ -157,6 +165,8 @@ fn run_check_internal(
build_graph: cli.build_graph,
check_opt: Some(CheckOpt {
package_path: cmd.package_path.clone(),
patch_file: cmd.patch_file.clone(),
no_mi: cmd.no_mi,
}),
test_opt: None,
fmt_opt: None,
Expand Down
65 changes: 65 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7351,3 +7351,68 @@ fn test_moon_check_filter_package() {
"#]],
);
}

#[test]
fn test_moon_check_package_with_patch() {
let dir = TestDir::new("test_check_filter.in");

// A has no deps
check(
get_stdout(
&dir,
[
"check",
"A",
"--patch-file",
"/path/to/patch.json",
"--dry-run",
"--sort-input",
],
),
expect![[r#"
moonc check -patch-file /path/to/patch.json ./A/hello.mbt ./A/test.mbt -o ./target/wasm-gc/release/check/A/A.mi -pkg username/hello/A -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources username/hello/A:./A -target wasm-gc
moonc check -patch-file /path/to/patch.json ./A/hello_test.mbt -o ./target/wasm-gc/release/check/A/A.blackbox_test.mi -pkg username/hello/A_blackbox_test -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/A/A.mi:A -pkg-sources username/hello/A_blackbox_test:./A -target wasm-gc
moonc check -patch-file /path/to/patch.json ./A/hello.mbt ./A/test.mbt ./A/hello_wbtest.mbt -o ./target/wasm-gc/release/check/A/A.whitebox_test.mi -pkg username/hello/A -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources username/hello/A:./A -target wasm-gc
"#]],
);

// lib has dep lib2
check(
get_stdout(
&dir,
[
"check",
"lib",
"--patch-file",
"/path/to/patch.json",
"--dry-run",
"--sort-input",
],
),
expect![[r#"
moonc check ./lib2/lib.mbt -o ./target/wasm-gc/release/check/lib2/lib2.mi -pkg username/hello/lib2 -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources username/hello/lib2:./lib2 -target wasm-gc
moonc check -patch-file /path/to/patch.json ./lib/hello.mbt -o ./target/wasm-gc/release/check/lib/lib.mi -pkg username/hello/lib -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib2/lib2.mi:lib2 -pkg-sources username/hello/lib:./lib -target wasm-gc
"#]],
);

// main has dep lib
check(
get_stdout(
&dir,
[
"check",
"main",
"--patch-file",
"/path/to/patch.json",
"--no-mi",
"--dry-run",
"--sort-input",
],
),
expect![[r#"
moonc check ./lib2/lib.mbt -o ./target/wasm-gc/release/check/lib2/lib2.mi -pkg username/hello/lib2 -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources username/hello/lib2:./lib2 -target wasm-gc
moonc check ./lib/hello.mbt -o ./target/wasm-gc/release/check/lib/lib.mi -pkg username/hello/lib -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib2/lib2.mi:lib2 -pkg-sources username/hello/lib:./lib -target wasm-gc
moonc check -patch-file /path/to/patch.json -no-mi ./main/main.mbt -o ./target/wasm-gc/release/check/main/main.mi -pkg username/hello/main -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib/lib.mi:lib -pkg-sources username/hello/main:./main -target wasm-gc
"#]],
);
}
58 changes: 52 additions & 6 deletions crates/moonbuild/src/gen/gen_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct CheckDepItem {
pub package_full_name: String,
pub package_source_dir: String,
pub is_main: bool,
pub patch_file: Option<PathBuf>,
pub no_mi: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -97,6 +99,8 @@ fn pkg_to_check_item(
package_full_name,
package_source_dir,
is_main: pkg.is_main,
patch_file: None,
no_mi: false,
})
}

Expand Down Expand Up @@ -161,6 +165,8 @@ fn pkg_with_wbtest_to_check_item(
package_full_name,
package_source_dir,
is_main: pkg.is_main,
patch_file: None,
no_mi: false,
})
}

Expand Down Expand Up @@ -246,6 +252,8 @@ fn pkg_with_test_to_check_item(
package_full_name,
package_source_dir,
is_main: pkg.is_main,
patch_file: None,
no_mi: false,
})
}

Expand All @@ -257,30 +265,63 @@ pub fn gen_check(
let _ = moonc_opt;
let _ = moonbuild_opt;
let mut dep_items = vec![];
let pkgs = match moonbuild_opt.check_opt.as_ref() {

// --patch and --no-mi only apply over the package which specified by path
let mut spec_pkg_to_check = None;
let mut patch_path = None;
let mut no_mi = false;

// if pkg is specified, check that pkg and it's deps; if no pkg specified, check all pkgs
let pkgs_to_check = match moonbuild_opt.check_opt.as_ref() {
Some(CheckOpt {
package_path: Some(pkg_path),
}) => &m.get_filtered_packages_and_its_deps(&moonbuild_opt.source_dir.join(pkg_path)),
patch_file: pp,
no_mi: nm,
}) => {
spec_pkg_to_check = m.get_package_by_path(&moonbuild_opt.source_dir.join(pkg_path));
patch_path = pp.clone();
no_mi = *nm;
&m.get_filtered_packages_and_its_deps(&moonbuild_opt.source_dir.join(pkg_path))
}
_ => m.get_all_packages(),
};
for (_, pkg) in pkgs {
let item = pkg_to_check_item(&pkg.root_path, m.get_all_packages(), pkg, moonc_opt)?;

for (_, pkg) in pkgs_to_check {
let (no_mi_for_cur_pkg, patch_path_for_cur_pkg) = spec_pkg_to_check
.map(|p| {
if p.root_path == pkg.root_path {
(no_mi, patch_path.clone())
} else {
(false, None)
}
})
.unwrap_or((false, None));

let mut item = pkg_to_check_item(&pkg.root_path, m.get_all_packages(), pkg, moonc_opt)?;
item.patch_file = patch_path_for_cur_pkg.clone();
item.no_mi = no_mi_for_cur_pkg;
dep_items.push(item);

if !pkg.wbtest_files.is_empty() {
let item = pkg_with_wbtest_to_check_item(
let mut item = pkg_with_wbtest_to_check_item(
&pkg.root_path,
m.get_all_packages(),
pkg,
moonc_opt,
)?;
item.patch_file = patch_path_for_cur_pkg.clone();
item.no_mi = no_mi_for_cur_pkg;
dep_items.push(item);
}
if !pkg.test_files.is_empty() {
let item =
let mut item =
pkg_with_test_to_check_item(&pkg.root_path, m.get_all_packages(), pkg, moonc_opt)?;
item.patch_file = patch_path_for_cur_pkg.clone();
item.no_mi = no_mi_for_cur_pkg;
dep_items.push(item);
}
}

Ok(N2CheckInput { dep_items })
}

Expand Down Expand Up @@ -336,6 +377,11 @@ pub fn gen_check_command(

let command = CommandBuilder::new("moonc")
.arg("check")
.arg_with_cond(item.patch_file.is_some(), "-patch-file")
.lazy_args_with_cond(item.patch_file.is_some(), || {
vec![item.patch_file.as_ref().unwrap().display().to_string()]
})
.arg_with_cond(item.no_mi, "-no-mi")
.args_with_cond(moonc_opt.render, vec!["-error-format", "json"])
.args_with_cond(
moonc_opt.build_opt.deny_warn,
Expand Down
2 changes: 2 additions & 0 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ impl MoonbuildOpt {
#[derive(Debug, Clone, Default)]
pub struct CheckOpt {
pub package_path: Option<PathBuf>,
pub patch_file: Option<PathBuf>,
pub no_mi: bool,
}

#[derive(Debug, Clone, Default)]
Expand Down
3 changes: 3 additions & 0 deletions crates/moonutil/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub struct Package {

pub targets: Option<IndexMap<FileName, CondExpr>>,
pub pre_build: Option<Vec<MoonPkgGenerate>>,

pub patch_file: Option<PathBuf>,
pub no_mi: bool,
}

impl Package {
Expand Down
5 changes: 5 additions & 0 deletions crates/moonutil/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ fn scan_one_package(
alert_list,
targets: cond_targets,
pre_build: pkg.pre_build,
patch_file: moonbuild_opt
.check_opt
.as_ref()
.and_then(|x| x.patch_file.clone()),
no_mi: moonbuild_opt.check_opt.as_ref().map_or(false, |x| x.no_mi),
};
if doc_mode {
// -o <folder>
Expand Down
4 changes: 3 additions & 1 deletion docs/manual-zh/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Check the current package, but don't build object files

###### **Arguments:**

* `<PACKAGE_PATH>` — The package to check
* `<PACKAGE_PATH>` — The package(and it's deps) to check

###### **Options:**

Expand All @@ -142,6 +142,8 @@ Check the current package, but don't build object files
* `--output-json` — Output in json format
* `--frozen` — Do not sync dependencies, assuming local dependencies are up-to-date
* `-w`, `--watch` — Monitor the file system and automatically check files
* `--patch-file <PATCH_FILE>` — The patch file to check, Only valid when checking specified package
* `--no-mi` — Whether to skip the mi generation, Only valid when checking specified package



Expand Down
4 changes: 3 additions & 1 deletion docs/manual/src/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Check the current package, but don't build object files

###### **Arguments:**

* `<PACKAGE_PATH>` — The package to check
* `<PACKAGE_PATH>` — The package(and it's deps) to check

###### **Options:**

Expand All @@ -142,6 +142,8 @@ Check the current package, but don't build object files
* `--output-json` — Output in json format
* `--frozen` — Do not sync dependencies, assuming local dependencies are up-to-date
* `-w`, `--watch` — Monitor the file system and automatically check files
* `--patch-file <PATCH_FILE>` — The patch file to check, Only valid when checking specified package
* `--no-mi` — Whether to skip the mi generation, Only valid when checking specified package



Expand Down

0 comments on commit 2f4976e

Please sign in to comment.