Skip to content

Commit

Permalink
Merge pull request #504 from moonbitlang/tweak_install_bin
Browse files Browse the repository at this point in the history
internal: tweak install bin
  • Loading branch information
Young-Flash authored Dec 3, 2024
2 parents 28bfce6 + 45b6408 commit 68c0ee8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 76 deletions.
54 changes: 31 additions & 23 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8030,39 +8030,47 @@ fn test_moon_query() {
}

#[test]
#[allow(clippy::just_underscores_and_digits)]
fn test_moon_install_bin() {
let dir = TestDir::new("moon_install_bin.in");
let dir = dir.join("user.in");
let top_dir = TestDir::new("moon_install_bin.in");
let dir = top_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);
let _1 = top_dir.join("author2.in").join("author2-native.exe");
let _2 = top_dir.join("author2.in").join("author2-js.js");
let _3 = top_dir.join("author2.in").join("author2-wasm.wasm");
let _4 = top_dir.join("author1.in").join("this-is-wasm.wasm");
let _5 = top_dir.join("author1.in").join("main-js.js");

// 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!(_1.exists());
assert!(_2.exists());
assert!(_3.exists());
assert!(_4.exists());
assert!(_5.exists());

assert!(!bin_dir.exists());
{
// delete all bin files
std::fs::remove_file(&_1).unwrap();
std::fs::remove_file(&_2).unwrap();
std::fs::remove_file(&_3).unwrap();
std::fs::remove_file(&_4).unwrap();
std::fs::remove_file(&_5).unwrap();
assert!(!_1.exists());
assert!(!_2.exists());
assert!(!_3.exists());
assert!(!_4.exists());
assert!(!_5.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());
assert!(_1.exists());
assert!(_2.exists());
assert!(_3.exists());
assert!(_4.exists());
assert!(_5.exists());

check(
get_stderr(&dir, ["build"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"input": [],
"output": ["1.txt"],
"command": "node $mooncake_bin/main-js.js"
"command": "node ../author1.in/main-js.js"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
"path": "../author1.in",
"bin_pkg": [
"main-js",
{
"name": "main-wasm",
"alias": "m-wasm"
}
"main-wasm"
]
},
"author2/flash": {
Expand Down
39 changes: 3 additions & 36 deletions crates/mooncake/src/pkg/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ pub(crate) fn install_impl(
}

if let Some(ref bin_deps) = m.bin_deps {
let install_path = source_dir
.join(moonutil::common::DEP_PATH)
.join(moonutil::common::MOON_BIN_DIR);

let moon_path = std::env::current_exe()
.map_or_else(|_| "moon".into(), |x| x.to_string_lossy().into_owned());

Expand All @@ -86,14 +82,7 @@ pub(crate) fn install_impl(
let module_db = get_module_db(&bin_mod_path, &res, &dep_dir)?;

if let Some(ref bin_pkg) = info.bin_pkg {
for bin_pkg_to_install in bin_pkg {
let (pkg_name, bin_alias) = match bin_pkg_to_install {
moonutil::dependency::BinPkgItem::Simple(pkg_name) => (pkg_name, None),
moonutil::dependency::BinPkgItem::Detailed { name, alias } => {
(name, alias.as_ref())
}
};

for pkg_name in bin_pkg {
let full_pkg_name = format!("{bin_mod_to_install}/{pkg_name}");

let pkg = module_db.get_package_by_name_safe(&full_pkg_name);
Expand All @@ -103,9 +92,8 @@ pub(crate) fn install_impl(
&moon_path,
&bin_mod_path,
&full_pkg_name,
&install_path,
&bin_mod_path,
pkg.bin_target.to_backend_ext(),
bin_alias,
verbose,
)?;
}
Expand All @@ -122,28 +110,13 @@ pub(crate) fn install_impl(
&moon_path,
&bin_mod_path,
full_pkg_name,
&install_path,
&bin_mod_path,
pkg.bin_target.to_backend_ext(),
None,
verbose,
)?;
}
}
}

// remove all files except .exe, .wasm, .js
if install_path.exists() {
for entry in std::fs::read_dir(&install_path)? {
let entry = entry?;
let path = entry.path();
if path.is_file() {
let ext = path.extension().and_then(|s| s.to_str());
if !matches!(ext, Some("exe" | "wasm" | "js")) {
std::fs::remove_file(path)?;
}
}
}
}
}

Ok((res, dep_dir))
Expand All @@ -155,7 +128,6 @@ fn build_and_install_bin_package(
full_pkg_name: &str,
install_path: &Path,
bin_target: impl AsRef<str>,
bin_alias: Option<&String>,
verbose: bool,
) -> anyhow::Result<()> {
let mut build_args = vec![
Expand All @@ -170,11 +142,6 @@ fn build_and_install_bin_package(
full_pkg_name.to_string(),
];

if let Some(bin_alias) = bin_alias {
build_args.push("--bin-alias".to_string());
build_args.push(bin_alias.to_string());
}

if !verbose {
build_args.push("--quiet".to_string());
}
Expand Down
13 changes: 1 addition & 12 deletions crates/moonutil/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,7 @@ pub struct BinaryDependencyInfo {

/// Binary packages to compile.
#[serde(skip_serializing_if = "Option::is_none", alias = "bin-pkg")]
pub bin_pkg: Option<Vec<BinPkgItem>>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum BinPkgItem {
Simple(String),
Detailed {
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
alias: Option<String>,
},
pub bin_pkg: Option<Vec<String>>,
}

impl BinaryDependencyInfo {
Expand Down

0 comments on commit 68c0ee8

Please sign in to comment.