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

internal: refactor package filter #343

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 2 deletions crates/moon/src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ fn run_build_internal(
&moonbuild_opt,
)?;
moonc_opt.build_opt.warn_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.warn_list.clone()))
.collect();
moonc_opt.build_opt.alert_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.alert_list.clone()))
.collect();
Expand Down
4 changes: 2 additions & 2 deletions crates/moon/src/cli/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ fn run_bundle_internal(
&moonbuild_opt,
)?;
moonc_opt.build_opt.warn_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.warn_list.clone()))
.collect();
moonc_opt.build_opt.alert_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.alert_list.clone()))
.collect();
Expand Down
4 changes: 2 additions & 2 deletions crates/moon/src/cli/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ fn run_check_internal(
&moonbuild_opt,
)?;
moonc_opt.build_opt.warn_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.warn_list.clone()))
.collect();
moonc_opt.build_opt.alert_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.alert_list.clone()))
.collect();
Expand Down
9 changes: 2 additions & 7 deletions crates/moon/src/cli/generate_test_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,8 @@ pub fn generate_test_driver(
bail!("dry-run is not implemented for generate-test-driver");
}

for (pkgname, pkg) in module.packages.iter() {
if let Some(ref package) = filter_package {
if !package.contains(Path::new(pkgname)) {
continue;
}
}

let package_filter = moonbuild_opt.get_package_filter();
for (pkgname, pkg) in module.get_filtered_packages(package_filter) {
if pkg.is_third_party {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/moon/src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn run_info(cli: UniversalFlags, cmd: InfoSubcommand) -> anyhow::Result<i32>
None => source_dir.to_path_buf(),
Some(p) => source_dir.join(p),
};
for (name, pkg) in mdb.packages {
for (name, pkg) in mdb.get_all_packages() {
// Skip if pkg is not part of the module
if pkg.is_third_party {
continue;
Expand Down
4 changes: 2 additions & 2 deletions crates/moon/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ pub fn run_run_internal(cli: &UniversalFlags, cmd: RunSubcommand) -> anyhow::Res
&moonbuild_opt,
)?;
moonc_opt.build_opt.warn_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.warn_list.clone()))
.collect();
moonc_opt.build_opt.alert_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.alert_list.clone()))
.collect();
Expand Down
13 changes: 4 additions & 9 deletions crates/moon/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,8 @@ fn run_test_internal(
&moonbuild_opt,
)?;

for (pkgname, pkg) in module.packages.iter_mut() {
if let Some(ref package) = filter_package {
if !package.contains(Path::new(pkgname)) {
continue;
}
}

let package_filter = moonbuild_opt.get_package_filter();
for (_, pkg) in module.get_filtered_packages_mut(package_filter) {
if pkg.is_third_party || pkg.is_main {
continue;
}
Expand Down Expand Up @@ -244,12 +239,12 @@ fn run_test_internal(
}

moonc_opt.build_opt.warn_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.warn_list.clone()))
.collect();
moonc_opt.build_opt.alert_lists = module
.packages
.get_all_packages()
.iter()
.map(|(name, pkg)| (name.clone(), pkg.alert_list.clone()))
.collect();
Expand Down
2 changes: 1 addition & 1 deletion crates/moonbuild/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ pub fn run_test(
artifacts_path: vec![],
};
for (pkgname, pkg) in module
.packages
.get_all_packages()
.iter()
.filter(|(_, p)| !(p.is_main || p.is_third_party))
{
Expand Down
2 changes: 1 addition & 1 deletion crates/moonbuild/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn gen_fmt(
moonbuild_opt: &MoonbuildOpt,
) -> anyhow::Result<N2FmtInput> {
let mut items = vec![];
for (_, pkg) in m.packages.iter() {
for (_, pkg) in m.get_all_packages().iter() {
if pkg.is_third_party {
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn gen_build_build_item(

for dep in pkg.imports.iter() {
let full_import_name = dep.path.make_full_path();
if !m.packages.contains_key(&full_import_name) {
if !m.contains_package(&full_import_name) {
bail!(
"{}: the imported package `{}` could not be located.",
m.source_dir
Expand All @@ -100,7 +100,7 @@ pub fn gen_build_build_item(
full_import_name,
);
}
let cur_pkg = &m.packages[&full_import_name];
let cur_pkg = m.get_package_by_name(&full_import_name);
let d = cur_pkg.artifact.with_extension("mi");
let alias = dep.alias.clone().unwrap_or(cur_pkg.last_name().into());
mi_deps.push(MiAlias {
Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn gen_build(
) -> anyhow::Result<N2BuildInput> {
let mut build_items = vec![];
let mut link_items = vec![];
for (i, (_, pkg)) in m.packages.iter().enumerate() {
for (i, (_, pkg)) in m.get_all_packages().iter().enumerate() {
let is_main = m.entries.contains(&i);

if is_main {
Expand Down
6 changes: 3 additions & 3 deletions crates/moonbuild/src/gen/gen_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ pub fn gen_bundle(
_moonbuild_opt: &MoonbuildOpt,
) -> anyhow::Result<N2BundleInput> {
let mut dep_items = vec![];
for (_, pkg) in m.packages.iter() {
let item = pkg_to_bundle_item(&m.source_dir, &m.packages, pkg, moonc_opt)?;
for (_, pkg) in m.get_all_packages().iter() {
let item = pkg_to_bundle_item(&m.source_dir, m.get_all_packages(), pkg, moonc_opt)?;
dep_items.push(item);
}

let nodes = super::util::toposort(m)?;
let mut order = vec![];
for node in nodes.iter() {
let p = &m.packages[node];
let p = &m.get_package_by_name(node);
order.push(
p.artifact
.with_extension("core")
Expand Down
14 changes: 10 additions & 4 deletions crates/moonbuild/src/gen/gen_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,21 @@ pub fn gen_check(
let _ = moonc_opt;
let _ = moonbuild_opt;
let mut dep_items = vec![];
for (_, pkg) in m.packages.iter() {
let item = pkg_to_check_item(&pkg.root_path, &m.packages, pkg, moonc_opt)?;
for (_, pkg) in m.get_all_packages().iter() {
let item = pkg_to_check_item(&pkg.root_path, m.get_all_packages(), pkg, moonc_opt)?;
dep_items.push(item);
if !pkg.wbtest_files.is_empty() {
let item = pkg_with_wbtest_to_check_item(&pkg.root_path, &m.packages, pkg, moonc_opt)?;
let item = pkg_with_wbtest_to_check_item(
&pkg.root_path,
m.get_all_packages(),
pkg,
moonc_opt,
)?;
dep_items.push(item);
}
if !pkg.test_files.is_empty() {
let item = pkg_with_test_to_check_item(&pkg.root_path, &m.packages, pkg, moonc_opt)?;
let item =
pkg_with_test_to_check_item(&pkg.root_path, m.get_all_packages(), pkg, moonc_opt)?;
dep_items.push(item);
}
}
Expand Down
20 changes: 10 additions & 10 deletions crates/moonbuild/src/gen/gen_runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn gen_package_core(
let mut mi_deps = vec![];
for dep in pkg.imports.iter() {
let full_import_name = dep.path.make_full_path();
if !m.packages.contains_key(&full_import_name) {
if !m.contains_package(&full_import_name) {
bail!(
"{}: the imported package `{}` could not be located.",
m.source_dir
Expand All @@ -159,7 +159,7 @@ pub fn gen_package_core(
full_import_name,
);
}
let cur_pkg = &m.packages[&full_import_name];
let cur_pkg = m.get_package_by_name(&full_import_name);
let d = cur_pkg.artifact.with_extension("mi");
let alias = dep.alias.clone().unwrap_or(cur_pkg.last_name().into());
mi_deps.push(MiAlias {
Expand Down Expand Up @@ -216,7 +216,7 @@ pub fn gen_package_internal_test(
let mut mi_deps = vec![];
for dep in pkg.imports.iter() {
let full_import_name = dep.path.make_full_path();
if !m.packages.contains_key(&full_import_name) {
if !m.contains_package(&full_import_name) {
bail!(
"{}: the imported package `{}` could not be located.",
m.source_dir
Expand All @@ -226,7 +226,7 @@ pub fn gen_package_internal_test(
full_import_name,
);
}
let cur_pkg = &m.packages[&full_import_name];
let cur_pkg = m.get_package_by_name(&full_import_name);
let d = cur_pkg.artifact.with_extension("mi");
let alias = dep.alias.clone().unwrap_or(cur_pkg.last_name().into());
mi_deps.push(MiAlias {
Expand Down Expand Up @@ -291,7 +291,7 @@ pub fn gen_package_whitebox_test(
let mut mi_deps = vec![];
for dep in pkg.imports.iter().chain(pkg.wbtest_imports.iter()) {
let full_import_name = dep.path.make_full_path();
if !m.packages.contains_key(&full_import_name) {
if !m.contains_package(&full_import_name) {
bail!(
"{}: the imported package `{}` could not be located.",
m.source_dir
Expand All @@ -301,7 +301,7 @@ pub fn gen_package_whitebox_test(
full_import_name,
);
}
let cur_pkg = &m.packages[&full_import_name];
let cur_pkg = m.get_package_by_name(&full_import_name);
let d = cur_pkg.artifact.with_extension("mi");
let alias = dep.alias.clone().unwrap_or(cur_pkg.last_name().into());
mi_deps.push(MiAlias {
Expand Down Expand Up @@ -386,7 +386,7 @@ pub fn gen_package_blackbox_test(

for dep in pkg.imports.iter().chain(pkg.test_imports.iter()) {
let full_import_name = dep.path.make_full_path();
if !m.packages.contains_key(&full_import_name) {
if !m.contains_package(&full_import_name) {
bail!(
"{}: the imported package `{}` could not be located.",
m.source_dir
Expand All @@ -396,7 +396,7 @@ pub fn gen_package_blackbox_test(
full_import_name,
);
}
let cur_pkg = &m.packages[&full_import_name];
let cur_pkg = m.get_package_by_name(&full_import_name);
let d = cur_pkg.artifact.with_extension("mi");
let alias = dep.alias.clone().unwrap_or(cur_pkg.last_name().into());
mi_deps.push(MiAlias {
Expand Down Expand Up @@ -443,7 +443,7 @@ fn get_pkg_topo_order<'a>(
return;
}
visited.insert(cur_pkg_full_name.clone());
let cur_pkg = &m.packages[cur_pkg_full_name];
let cur_pkg = m.get_package_by_name(cur_pkg_full_name);
let imports = cur_pkg
.imports
.iter()
Expand Down Expand Up @@ -665,7 +665,7 @@ pub fn gen_runtest(
.as_ref()
.and_then(|f| f.filter_package.as_ref());

for (pkgname, pkg) in m.packages.iter() {
for (pkgname, pkg) in m.get_all_packages().iter() {
if pkg.is_main {
continue;
}
Expand Down
18 changes: 9 additions & 9 deletions crates/moonbuild/src/gen/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn toposort(m: &ModuleDB) -> anyhow::Result<Vec<String>> {
let cycle = get_example_cycle(&m.graph, cycle.node_id());
let cycle = cycle
.into_iter()
.map(|n| m.packages[n.index()].full_name())
.map(|n| m.get_package_by_index(n.index()).full_name())
.collect::<Vec<_>>();
bail!("cyclic dependency detected: {:?}", cycle);
}
Expand All @@ -51,20 +51,20 @@ pub fn topo_from_node(m: &ModuleDB, pkg: &Package) -> anyhow::Result<Vec<String>

fn dfs(
m: &ModuleDB,
pkg_full_name: &String,
pkg_full_name: &str,
stk: &mut Vec<String>,
visited: &mut HashSet<String>,
) -> anyhow::Result<()> {
visited.insert(pkg_full_name.clone());
visited.insert(pkg_full_name.to_string());

for neighbor in m.packages[pkg_full_name].imports.iter() {
for neighbor in m.get_package_by_name(pkg_full_name).imports.iter() {
let neighbor_full_name = neighbor.path.make_full_path();
if !visited.contains(&neighbor_full_name) {
dfs(m, &neighbor_full_name, stk, visited)?;
}
}

stk.push(pkg_full_name.clone());
stk.push(pkg_full_name.to_string());
Ok(())
}

Expand All @@ -75,27 +75,27 @@ pub fn topo_from_node(m: &ModuleDB, pkg: &Package) -> anyhow::Result<Vec<String>
pub fn nodes_to_names(m: &ModuleDB, nodes: &[usize]) -> Vec<String> {
nodes
.iter()
.map(|index| m.packages[*index].full_name())
.map(|index| m.get_package_by_index(*index).full_name())
.collect::<Vec<_>>()
}

pub fn nodes_to_cores(m: &ModuleDB, nodes: &[String]) -> Vec<String> {
nodes
.iter()
.map(|index| m.packages[index].artifact.with_extension("core"))
.map(|name| m.get_package_by_name(name).artifact.with_extension("core"))
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
}

pub fn nodes_to_pkg_sources(m: &ModuleDB, nodes: &[String]) -> Vec<(String, String)> {
nodes
.iter()
.map(|index| {
.map(|name| {
let root_source_dir = match &m.source {
None => m.source_dir.clone(),
Some(x) => m.source_dir.join(x),
};
let pkg = &m.packages[index];
let pkg = &m.get_package_by_name(name);
let package_source_dir: String = if pkg.rel.components.is_empty() {
root_source_dir.display().to_string()
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/moonbuild/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn load_moon_generate(
let mut graph = n2graph::Graph::default();
let mut defaults: Vec<FileId> = vec![];

for (_, pkg) in module.packages.iter() {
for (_, pkg) in module.get_all_packages().iter() {
if pkg.is_third_party {
continue;
}
Expand Down
Loading