Skip to content

Commit

Permalink
Merge pull request #306 from moonbitlang/robust-pre-build
Browse files Browse the repository at this point in the history
internal: robust pre build
  • Loading branch information
lijunchen authored Sep 14, 2024
2 parents 4f4c7c0 + 1e4db23 commit e22cb71
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 33 deletions.
52 changes: 52 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6669,3 +6669,55 @@ fn no_main_just_init() {
"#]],
);
}

#[test]
fn test_pre_build() {
let dir = TestDir::new("pre_build.in");
check(
&get_stderr_on_success_with_args_and_replace_dir(&dir, ["check"]),
expect![[r#"
Executed 3 pre-build tasks, now up to date
Finished. moon: ran 2 tasks, now up to date
"#]],
);
check(
&get_stderr_on_success_with_args_and_replace_dir(&dir, ["build"]),
expect![[r#"
Warning: [1002]
╭─[$ROOT/src/lib/a.mbt:3:5]
3 │ let resource : String =
│ ────┬───
│ ╰───── Warning: Unused toplevel variable 'resource'. Note if the body contains side effect, it will not happen. Use `fn init { .. }` to wrap the effect.
───╯
Finished. moon: ran 3 tasks, now up to date
"#]],
);

check(
&read(&dir.join("src/lib/a.mbt")),
expect![[r#"
// Generated by `moon tool embed --text`, do not edit.
let resource : String =
#|hello,
#|world
#|
"#]],
);
let content = read(&dir.join("src/lib/b.mbt"));
assert!(content.contains("// Generated by `moon tool embed --binary`, do not edit."));
assert!(content.contains(r#"let _b : Bytes = b"\x6d\x6f\x6f\x6e"#));
assert!(content.contains(r#"\x0a\x67\x65\x6e\x65\x72\x61\x74\x65"#));
check(
&read(&dir.join("src/lib/c.mbt")),
expect![[r#"
// Generated by `moon tool embed --text`, do not edit.
let _c : String =
#|hello,
#|world
#|
"#]],
);
}
26 changes: 0 additions & 26 deletions crates/moon/tests/test_cases/pre_build.in/moon.test.ignore

This file was deleted.

10 changes: 7 additions & 3 deletions crates/moonbuild/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,13 @@ fn vis_build_graph(state: &State, moonbuild_opt: &MoonbuildOpt) {

fn run_moon_generate(moonbuild_opt: &MoonbuildOpt, module: &ModuleDB) -> anyhow::Result<i32> {
let generate_state = crate::generate::load_moon_generate(moonbuild_opt, module)?;
let generate_result = n2_simple_run_interface(generate_state, moonbuild_opt)?;
render_generate_result(generate_result, moonbuild_opt.quiet)?;
Ok(0)
if let Some(generate_state) = generate_state {
let generate_result = n2_simple_run_interface(generate_state, moonbuild_opt)?;
render_generate_result(generate_result, moonbuild_opt.quiet)?;
Ok(0)
} else {
Ok(0)
}
}

fn render_generate_result(result: Option<usize>, quiet: bool) -> anyhow::Result<i32> {
Expand Down
15 changes: 11 additions & 4 deletions crates/moonbuild/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use n2::smallmap::SmallMap;
pub fn load_moon_generate(
moonbuild_opt: &MoonbuildOpt,
module: &ModuleDB,
) -> anyhow::Result<State> {
) -> anyhow::Result<Option<State>> {
let mut graph = n2graph::Graph::default();
let mut defaults: Vec<FileId> = vec![];

Expand Down Expand Up @@ -96,8 +96,11 @@ pub fn load_moon_generate(
};

let mut build = Build::new(loc, ins, outs);
let moon_bin = std::env::current_exe()?;
let command = if command.starts_with(":embed") {
command.replacen(":embed", "moon tool embed", 1).to_string()
command
.replacen(":embed", &format!("{} tool embed", moon_bin.display()), 1)
.to_string()
} else {
command.to_string()
};
Expand All @@ -110,6 +113,10 @@ pub fn load_moon_generate(
}
}

if defaults.is_empty() {
return Ok(None);
}

let mut hashed = n2graph::Hashes::default();
let common = moonbuild_opt.raw_target_dir.join("common");
if !common.exists() {
Expand All @@ -118,11 +125,11 @@ pub fn load_moon_generate(
let _lock = FileLock::lock(&common)?;
let n2_db_path = common.join("generate.db");
let db = n2::db::open(&n2_db_path, &mut graph, &mut hashed).unwrap();
Ok(State {
Ok(Some(State {
graph,
db,
hashes: hashed,
default: defaults,
pools: SmallMap::default(),
})
}))
}

0 comments on commit e22cb71

Please sign in to comment.