Skip to content

Commit

Permalink
internal: move output artifact into ./target dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Jul 26, 2024
1 parent a929999 commit 222c96e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
44 changes: 35 additions & 9 deletions crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use moonutil::common::FileLock;
use moonutil::common::RunMode;
use moonutil::common::SurfaceTarget;
use moonutil::common::TargetBackend;
use moonutil::common::MOONBITLANG_CORE;
use moonutil::common::MOON_PKG_JSON;
use moonutil::common::{MoonbuildOpt, OutputFormat};
use moonutil::dirs::check_moon_pkg_exist;
use moonutil::dirs::mk_arch_mode_dir;
use moonutil::dirs::PackageDirs;
use moonutil::moon_dir::core_core;
use moonutil::mooncakes::sync::AutoSyncFlags;
use moonutil::mooncakes::RegistryConfig;
use n2::trace;
Expand All @@ -51,6 +51,9 @@ pub struct RunSubcommand {
pub auto_sync_flags: AutoSyncFlags,

pub args: Vec<String>,

#[clap(long)]
pub build_only: bool,
}

pub fn run_run(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Result<i32> {
Expand Down Expand Up @@ -80,6 +83,7 @@ pub fn run_run(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Result<i32>
fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Result<i32> {
let current_dir = std::env::current_dir()?;
let mbt_file_path = dunce::canonicalize(current_dir.join(cmd.package_or_mbt_file))?;
let mbt_file_parent_path = mbt_file_path.parent().unwrap();

if !mbt_file_path.is_file() {
bail!("{} is not exist or not a file", mbt_file_path.display());
Expand All @@ -92,7 +96,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
.map_or(TargetBackend::default(), |it| *it);
let core_bundle_path = moonutil::moon_dir::core_bundle(target_backend);

let output_artifact_path = std::env::temp_dir().join("moon_run_artifact");
let output_artifact_path = mbt_file_parent_path.join("target");

let output_core_path = &(output_artifact_path
.join(format!("{}.core", file_name))
Expand All @@ -103,6 +107,7 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
.display()
.to_string());

let pkg_name = "moon/run/single";
let build_package_command = [
"build-package",
&mbt_file_path.display().to_string(),
Expand All @@ -111,31 +116,48 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
"-std-path",
core_bundle_path.to_str().unwrap(),
"-is-main",
"-pkg",
pkg_name,
"-g",
"-source-map",
"-target",
target_backend.to_flag(),
];
let link_core_command = [
"link-core",
&core_core(target_backend).display().to_string(),
&moonutil::moon_dir::core_core(target_backend)
.display()
.to_string(),
&(output_artifact_path
.join(format!("{}.core", file_name))
.display()
.to_string()),
"-o",
output_wasm_or_js_path,
"-pkg-sources",
&format!("{}:{}", pkg_name, mbt_file_parent_path.display()),
"-pkg-sources",
&format!(
"{}:{}",
MOONBITLANG_CORE,
moonutil::moon_dir::core().display()
),
"-g",
"-source-map",
"-target",
target_backend.to_flag(),
];

if cli.dry_run {
println!("moonc {}", build_package_command.join(" "));
println!("moonc {}", link_core_command.join(" "));
let runner = match target_backend {
TargetBackend::Wasm | TargetBackend::WasmGC => "moonrun",
TargetBackend::Js => "node",
};
println!("{runner} {output_wasm_or_js_path}");

if !cmd.build_only {
let runner = match target_backend {
TargetBackend::Wasm | TargetBackend::WasmGC => "moonrun",
TargetBackend::Js => "node",
};
println!("{runner} {output_wasm_or_js_path}");
}
return Ok(0);
}

Expand All @@ -161,6 +183,10 @@ fn run_single_mbt_file(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Resu
bail!("failed to run: moonc {}", link_core_command.join(" "))
}

if cmd.build_only {
return Ok(0);
}

trace::scope("run", || match target_backend {
TargetBackend::Wasm | TargetBackend::WasmGC => {
moonbuild::build::run_wat(&PathBuf::from(output_wasm_or_js_path), &cmd.args)
Expand Down
55 changes: 29 additions & 26 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4933,43 +4933,46 @@ fn test_many_targets_expect_failed() {
fn test_moon_run_single_mbt_file() {
let dir = TestDir::new("run_single_mbt_file.in");

fn replace_tmp(s: String) -> String {
s.split(' ')
.map(|s| {
if let Some(pos) = s.find("moon_run_artifact") {
format!("{}/{}", "$tmp", &s[pos..])
} else {
s.to_string()
}
})
.collect::<Vec<String>>()
.join(" ")
}

let output = replace_tmp(get_stdout_with_args_and_replace_dir(
let output = get_stdout_with_args_and_replace_dir(
&dir,
["run", "a/b/single.mbt", "--dry-run"],
));
[
"run",
"a/b/single.mbt",
"--target",
"js",
"--build-only",
"--dry-run",
],
);
check(
&output,
expect![[r#"
moonc build-package $ROOT/a/b/single.mbt -o $tmp/moon_run_artifact/single.core -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -is-main -target wasm-gc
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core $tmp/moon_run_artifact/single.core -o $tmp/moon_run_artifact/single.wasm -target wasm-gc
moonrun $tmp/moon_run_artifact/single.wasm
"#]],
moonc build-package $ROOT/a/b/single.mbt -o $ROOT/a/b/target/single.core -std-path $MOON_HOME/lib/core/target/js/release/bundle -is-main -pkg moon/run/single -g -source-map -target js
moonc link-core $MOON_HOME/lib/core/target/js/release/bundle/core.core $ROOT/a/b/target/single.core -o $ROOT/a/b/target/single.js -pkg-sources moon/run/single:$ROOT/a/b -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -g -source-map -target js
"#]],
);

let output = replace_tmp(get_stdout_with_args_and_replace_dir(
let output = get_stdout_with_args_and_replace_dir(&dir, ["run", "a/b/single.mbt", "--dry-run"]);
check(
&output,
expect![[r#"
moonc build-package $ROOT/a/b/single.mbt -o $ROOT/a/b/target/single.core -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -is-main -pkg moon/run/single -g -source-map -target wasm-gc
moonc link-core $MOON_HOME/lib/core/target/wasm-gc/release/bundle/core.core $ROOT/a/b/target/single.core -o $ROOT/a/b/target/single.wasm -pkg-sources moon/run/single:$ROOT/a/b -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -g -source-map -target wasm-gc
moonrun $ROOT/a/b/target/single.wasm
"#]],
);

let output = get_stdout_with_args_and_replace_dir(
&dir,
["run", "a/b/single.mbt", "--target", "js", "--dry-run"],
));
);
check(
&output,
expect![[r#"
moonc build-package $ROOT/a/b/single.mbt -o $tmp/moon_run_artifact/single.core -std-path $MOON_HOME/lib/core/target/js/release/bundle -is-main -target js
moonc link-core $MOON_HOME/lib/core/target/js/release/bundle/core.core $tmp/moon_run_artifact/single.core -o $tmp/moon_run_artifact/single.js -target js
node $tmp/moon_run_artifact/single.js
"#]],
moonc build-package $ROOT/a/b/single.mbt -o $ROOT/a/b/target/single.core -std-path $MOON_HOME/lib/core/target/js/release/bundle -is-main -pkg moon/run/single -g -source-map -target js
moonc link-core $MOON_HOME/lib/core/target/js/release/bundle/core.core $ROOT/a/b/target/single.core -o $ROOT/a/b/target/single.js -pkg-sources moon/run/single:$ROOT/a/b -pkg-sources moonbitlang/core:$MOON_HOME/lib/core -g -source-map -target js
node $ROOT/a/b/target/single.js
"#]],
);

let output = get_stdout_with_args_and_replace_dir(&dir, ["run", "a/b/single.mbt"]);
Expand Down

0 comments on commit 222c96e

Please sign in to comment.