Skip to content

Commit

Permalink
Merge pull request #490 from moonbitlang/install_bin
Browse files Browse the repository at this point in the history
feat: support moon install --bin
  • Loading branch information
Young-Flash authored Nov 28, 2024
2 parents 3cf7509 + 9ba99a7 commit b242ba1
Show file tree
Hide file tree
Showing 68 changed files with 902 additions and 81 deletions.
3 changes: 3 additions & 0 deletions crates/moon/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub use fmt::*;
pub use generate_test_driver::*;
pub use info::*;
use moonbuild::upgrade::UpgradeSubcommand;
use mooncake::pkg::{
add::AddSubcommand, install::InstallSubcommand, remove::RemoveSubcommand, tree::TreeSubcommand,
};
pub use new::*;
pub use query::*;
pub use run::*;
Expand Down
30 changes: 30 additions & 0 deletions crates/moon/src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use moonbuild::entry;
use moonbuild::watch::watching;
use mooncake::pkg::sync::auto_sync;
use moonutil::common::lower_surface_targets;
use moonutil::common::BuildOpt;
use moonutil::common::FileLock;
use moonutil::common::MoonbuildOpt;
use moonutil::common::RunMode;
Expand All @@ -31,6 +32,7 @@ use moonutil::mooncakes::sync::AutoSyncFlags;
use moonutil::mooncakes::RegistryConfig;
use n2::trace;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::thread;

Expand All @@ -50,8 +52,19 @@ pub struct BuildSubcommand {
#[clap(long, short)]
pub watch: bool,

#[clap(long, hide = true)]
pub install_path: Option<PathBuf>,

#[clap(long, hide = true)]
pub show_artifacts: bool,

// package name (username/hello/lib)
#[clap(long, hide = true)]
pub package: Option<String>,

// when package is specified, specify the alias of the binary package artifact to install
#[clap(long, hide = true, requires("package"))]
pub bin_alias: Option<String>,
}

pub fn run_build(cli: &UniversalFlags, cmd: &BuildSubcommand) -> anyhow::Result<i32> {
Expand Down Expand Up @@ -138,6 +151,10 @@ fn run_build_internal(
build_graph: cli.build_graph,
test_opt: None,
check_opt: None,
build_opt: Some(BuildOpt {
install_path: cmd.install_path.clone(),
filter_package: cmd.package.clone(),
}),
fmt_opt: None,
args: vec![],
output_json: false,
Expand All @@ -152,6 +169,19 @@ fn run_build_internal(
&dir_sync_result,
)?;

if let Some(bin_alias) = cmd.bin_alias.clone() {
let pkg = module.get_package_by_name_mut_safe(cmd.package.as_ref().unwrap());
match pkg {
Some(pkg) => {
pkg.bin_name = Some(bin_alias);
}
_ => anyhow::bail!(format!(
"package `{}` not found",
cmd.package.as_ref().unwrap()
)),
}
}

moonutil::common::set_native_backend_link_flags(
run_mode,
cmd.build_flags.release,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn run_bundle_internal(
run_mode,
test_opt: None,
check_opt: None,
build_opt: None,
fmt_opt: None,
args: vec![],
verbose: cli.verbose,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fn run_check_internal(
no_mi: cmd.no_mi,
}),
test_opt: None,
build_opt: None,
fmt_opt: None,
args: vec![],
no_parallelize: false,
Expand Down
44 changes: 19 additions & 25 deletions crates/moon/src/cli/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,29 @@
// For inquiries, you can contact us via e-mail at [email protected].

use anyhow::bail;
use mooncake::pkg::{
add::AddSubcommand, install::InstallSubcommand, remove::RemoveSubcommand, tree::TreeSubcommand,
};
use moonutil::{
dirs::PackageDirs,
mooncakes::{ModuleName, RegistryConfig},
};

use super::UniversalFlags;

/// Install dependencies
#[derive(Debug, clap::Parser)]
pub struct InstallSubcommand {}

/// Remove a dependency
#[derive(Debug, clap::Parser)]
pub struct RemoveSubcommand {
/// The package path to remove
pub package_path: String,
}

/// Add a dependency
#[derive(Debug, clap::Parser)]
pub struct AddSubcommand {
/// The package path to add
pub package_path: String,
}

/// Display the dependency tree
#[derive(Debug, clap::Parser)]
pub struct TreeSubcommand {}

pub fn install_cli(cli: UniversalFlags, _cmd: InstallSubcommand) -> anyhow::Result<i32> {
let PackageDirs {
source_dir,
target_dir,
} = cli.source_tgt_dir.try_into_package_dirs()?;
let registry_config = RegistryConfig::load();
mooncake::pkg::install::install(&source_dir, &target_dir, &registry_config, false)
mooncake::pkg::install::install(
&source_dir,
&target_dir,
&registry_config,
cli.quiet,
cli.verbose,
)
}

pub fn remove_cli(cli: UniversalFlags, cmd: RemoveSubcommand) -> anyhow::Result<i32> {
Expand Down Expand Up @@ -100,9 +87,16 @@ pub fn add_cli(cli: UniversalFlags, cmd: AddSubcommand) -> anyhow::Result<i32> {
if parts.len() == 2 {
let version: &str = parts[1];
let version = version.parse()?;
mooncake::pkg::add::add(&source_dir, &target_dir, &pkg_name, &version, false)
mooncake::pkg::add::add(
&source_dir,
&target_dir,
&pkg_name,
cmd.bin,
&version,
false,
)
} else {
mooncake::pkg::add::add_latest(&source_dir, &target_dir, &pkg_name, false)
mooncake::pkg::add::add_latest(&source_dir, &target_dir, &pkg_name, cmd.bin, false)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result<i32> {
run_mode,
test_opt: None,
check_opt: None,
build_opt: None,
fmt_opt: None,
args: vec![],
verbose: cli.verbose,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn run_fmt(cli: &UniversalFlags, cmd: FmtSubcommand) -> anyhow::Result<i32>
build_graph: cli.build_graph,
test_opt: None,
check_opt: None,
build_opt: None,
args: vec![],
verbose: cli.verbose,
quiet: cli.quiet,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/generate_test_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub fn generate_test_driver(
patch_file: cmd.patch_file.clone(),
}),
check_opt: None,
build_opt: None,
fmt_opt: None,
sort_input,
run_mode,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub fn run_info(cli: UniversalFlags, cmd: InfoSubcommand) -> anyhow::Result<i32>
run_mode: RunMode::Check,
test_opt: None,
check_opt: None,
build_opt: None,
fmt_opt: None,
args: vec![],
verbose: cli.verbose,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
build_graph: cli.build_graph,
test_opt: None,
check_opt: None,
build_opt: None,
fmt_opt: None,
output_json: false,
no_parallelize: false,
Expand Down
1 change: 1 addition & 0 deletions crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ fn run_test_internal(
patch_file,
}),
check_opt: None,
build_opt: None,
sort_input,
run_mode,
quiet: true,
Expand Down
47 changes: 47 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8020,3 +8020,50 @@ fn test_moon_query() {

get_stdout(&dir, ["query", "moonbitlang/x"]);
}

#[test]
fn test_moon_install_bin() {
let dir = TestDir::new("moon_install_bin.in");
let dir = dir.join("user.in");

let mooncakes_path = dir.join(moonutil::common::DEP_PATH);
if mooncakes_path.exists() {
std::fs::remove_dir_all(&mooncakes_path).unwrap();
}

let bin_dir = mooncakes_path.join(moonutil::common::MOON_BIN_DIR);

// moon check should auto install bin deps
get_stdout(&dir, ["check"]);
assert!(bin_dir.exists());
assert!(bin_dir.join("author2-native.exe").exists());
assert!(bin_dir.join("author2-js.js").exists());
assert!(bin_dir.join("author2-wasm.wasm").exists());
assert!(bin_dir.join("m-wasm.wasm").exists());
assert!(bin_dir.join("main-js.js").exists());

std::fs::remove_dir_all(&mooncakes_path).unwrap();

assert!(!bin_dir.exists());

// moon install should install bin deps
get_stdout(&dir, ["install"]);

assert!(bin_dir.exists());
assert!(bin_dir.join("author2-native.exe").exists());
assert!(bin_dir.join("author2-js.js").exists());
assert!(bin_dir.join("author2-wasm.wasm").exists());
assert!(bin_dir.join("m-wasm.wasm").exists());
assert!(bin_dir.join("main-js.js").exists());

check(
get_stderr(&dir, ["build"]),
expect![[r#"
main-js
lib Hello, world!
()
Executed 1 pre-build task, now up to date
Finished. moon: ran 17 tasks, now up to date
"#]],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.mooncakes/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# username/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() -> Unit {
println("lib Hello, world!")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
println("main-js")
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"is_main": true,
"import": [
"username/flash/lib"
],
"bin-target": "js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
println("main-native")
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"is_main": true,
"import": [
"username/flash/lib"
],
"bin-target": "native",
"bin-name": "this-is-native"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
println("main-wasm")
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"is_main": true,
"import": [
"username/flash/lib"
],
"bin-target": "wasm",
"bin-name": "this-is-wasm"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "username/flash",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"license": "Apache-2.0",
"keywords": [],
"description": ""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.mooncakes/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# username/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() -> Unit {
println("lib Hello, world!")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
println("main-js")
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"is_main": true,
"import": [
"author2/flash/lib"
],
"bin-target": "js",
"bin-name": "author2-js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
println("main-native")
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"is_main": true,
"import": [
"author2/flash/lib"
],
"bin-target": "native",
"bin-name": "author2-native"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main {
println("main-wasm")
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"is_main": true,
"import": [
"author2/flash/lib"
],
"bin-target": "wasm",
"bin-name": "author2-wasm"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "author2/flash",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"license": "Apache-2.0",
"keywords": [],
"description": ""
}
Loading

0 comments on commit b242ba1

Please sign in to comment.