Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support specify alias for package in core #419

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
target_dir,
} = cli.source_tgt_dir.try_into_package_dirs()?;

let mod_desc = moonutil::common::read_module_desc_file_in_dir(&source_dir)?;

// Run moon install before build
let (resolved_env, dir_sync_result) = auto_sync(
&source_dir,
Expand Down Expand Up @@ -259,7 +261,7 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
bail!("{} is not a package", package_path);
}

let pkg = moonutil::common::read_package_desc_file_in_dir(&package)?;
let pkg = moonutil::common::read_package_desc_file_in_dir(&mod_desc.name, &package)?;
if !pkg.is_main {
bail!("`{}` is not a main package", package_path);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/moonutil/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ pub fn read_module_from_json(path: &Path) -> Result<MoonMod, MoonModJSONFormatEr
})
}

fn read_package_from_json(path: &Path) -> anyhow::Result<MoonPkg> {
fn read_package_from_json(module_name: &str, path: &Path) -> anyhow::Result<MoonPkg> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let j =
serde_json_lenient::from_reader(reader).context(format!("Failed to parse {:?}", path))?;
convert_pkg_json_to_package(j)
convert_pkg_json_to_package(module_name, j)
}

pub fn write_module_json_to_file(m: &MoonModJSON, source_dir: &Path) -> anyhow::Result<()> {
Expand All @@ -152,11 +152,11 @@ pub fn read_module_desc_file_in_dir(dir: &Path) -> anyhow::Result<MoonMod> {
Ok(read_module_from_json(&dir.join(MOON_MOD_JSON))?)
}

pub fn read_package_desc_file_in_dir(dir: &Path) -> anyhow::Result<MoonPkg> {
pub fn read_package_desc_file_in_dir(module_name: &str, dir: &Path) -> anyhow::Result<MoonPkg> {
if !dir.join(MOON_PKG_JSON).exists() {
bail!("`{:?}` does not exist", dir.join(MOON_PKG_JSON));
}
read_package_from_json(&dir.join(MOON_PKG_JSON))
read_package_from_json(module_name, &dir.join(MOON_PKG_JSON))
.context(format!("Failed to load {:?}", dir.join(MOON_PKG_JSON)))
}

Expand Down
55 changes: 50 additions & 5 deletions crates/moonutil/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
common::{FileName, GeneratedTestDriver},
common::{FileName, GeneratedTestDriver, MOONBITLANG_CORE},
cond_expr::{CompileCondition, CondExpr, RawTargets},
path::{ImportComponent, PathComponent},
};
Expand Down Expand Up @@ -333,8 +333,11 @@ pub struct MoonPkg {
pub is_main: bool,
pub need_link: bool,
pub imports: Vec<Import>,
pub core_imports: Vec<Import>,
pub wbtest_imports: Vec<Import>,
pub core_wbtest_imports: Vec<Import>,
pub test_imports: Vec<Import>,
pub core_test_imports: Vec<Import>,

pub link: Option<Link>,
pub warn_list: Option<String>,
Expand All @@ -361,7 +364,7 @@ impl Import {
}
}

pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result<MoonPkg> {
pub fn convert_pkg_json_to_package(module_name: &str, j: MoonPkgJSON) -> anyhow::Result<MoonPkg> {
let get_imports = |source: Option<PkgJSONImport>| -> Vec<Import> {
let mut imports = vec![];
if let Some(im) = source {
Expand Down Expand Up @@ -444,6 +447,19 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result<MoonPkg> {
alias_dedup.insert(alias.clone());
}
}
let mut imports_with_core = vec![];
let mut imports_without_core = vec![];
for im in imports {
let path = match &im {
Import::Simple(p) => p,
Import::Alias { path, alias: _ } => path,
};
if module_name != MOONBITLANG_CORE && path.starts_with(MOONBITLANG_CORE) {
imports_with_core.push(im);
} else {
imports_without_core.push(im);
}
}

// TODO: check on the fly
let mut alias_dedup: HashSet<String> = HashSet::new();
Expand All @@ -466,6 +482,19 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result<MoonPkg> {
alias_dedup.insert(alias.clone());
}
}
let mut wbtest_imports_with_core = vec![];
let mut wbtest_imports_without_core = vec![];
for im in wbtest_imports {
let path = match &im {
Import::Simple(p) => p,
Import::Alias { path, alias: _ } => path,
};
if module_name != MOONBITLANG_CORE && path.starts_with(MOONBITLANG_CORE) {
wbtest_imports_with_core.push(im);
} else {
wbtest_imports_without_core.push(im);
}
}

// TODO: check on the fly
let mut alias_dedup: HashSet<String> = HashSet::new();
Expand All @@ -488,14 +517,30 @@ pub fn convert_pkg_json_to_package(j: MoonPkgJSON) -> anyhow::Result<MoonPkg> {
alias_dedup.insert(alias.clone());
}
}
let mut test_imports_with_core = vec![];
let mut test_imports_without_core = vec![];
for im in test_imports {
let path = match &im {
Import::Simple(p) => p,
Import::Alias { path, alias: _ } => path,
};
if module_name != MOONBITLANG_CORE && path.starts_with(MOONBITLANG_CORE) {
test_imports_with_core.push(im);
} else {
test_imports_without_core.push(im);
}
}

let result = MoonPkg {
name: None,
is_main,
need_link,
imports,
wbtest_imports,
test_imports,
imports: imports_without_core,
core_imports: imports_with_core,
wbtest_imports: wbtest_imports_without_core,
core_wbtest_imports: wbtest_imports_with_core,
test_imports: test_imports_without_core,
core_test_imports: test_imports_with_core,
link: match j.link {
None => None,
Some(BoolOrLink::Bool(_)) => None,
Expand Down
2 changes: 1 addition & 1 deletion crates/moonutil/src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn scan_one_package(
Ok(imports)
};

let pkg = crate::common::read_package_desc_file_in_dir(pkg_path)?;
let pkg = crate::common::read_package_desc_file_in_dir(&mod_desc.name, pkg_path)?;
let rel = pkg_path.strip_prefix(module_source_dir)?;
let rel_path = PathComponent::from_path(rel)?;

Expand Down
Loading