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

✨ - Add namespace entry feature #34

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
3 changes: 3 additions & 0 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ pub struct T {
pub namespace: Option<Namespace>,
pub jsx: Option<JsxSpecs>,
pub uncurried: Option<bool>,
// this is a new feature of rewatch, and it's not part of the bsconfig.json spec
#[serde(rename = "namespace-entry")]
pub namespace_entry: Option<String>,
}

/// This flattens string flags
Expand Down
100 changes: 78 additions & 22 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,15 @@ fn generate_asts(
&module.package_name,
&package
.namespace
.as_ref()
.to_suffix()
.expect("namespace should be set for mlmap module"),
);
let compile_path = helpers::get_mlmap_compile_path(
&build_state.project_root,
&module.package_name,
&package
.namespace
.as_ref()
.to_suffix()
.expect("namespace should be set for mlmap module"),
);
let mlmap_hash = compute_file_hash(&compile_path);
Expand Down Expand Up @@ -533,7 +533,7 @@ fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>) {

let mut deps = get_dep_modules(
&ast_path,
package.namespace.to_owned(),
package.namespace.to_suffix(),
&package.modules.as_ref().unwrap(),
all_mod,
);
Expand All @@ -548,13 +548,22 @@ fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>) {

deps.extend(get_dep_modules(
&iast_path,
package.namespace.to_owned(),
package.namespace.to_suffix(),
&package.modules.as_ref().unwrap(),
all_mod,
))
}
None => (),
}
match &package.namespace {
package_tree::Namespace::NamespaceWithEntry {
namespace: _,
entry,
} if entry == module_name => {
deps.insert(package.namespace.to_suffix().unwrap());
}
_ => (),
}
deps.remove(module_name);
(module_name.to_string(), deps)
}
Expand Down Expand Up @@ -592,7 +601,7 @@ pub fn parse_packages(build_state: &mut BuildState) {
helpers::get_build_path(&build_state.project_root, &package.bsconfig.name);
helpers::create_build_path(&build_path_abs);

package.namespace.iter().for_each(|namespace| {
package.namespace.to_suffix().iter().for_each(|namespace| {
// generate the mlmap "AST" file for modules that have a namespace configured
let source_files = match package.source_files.to_owned() {
Some(source_files) => source_files
Expand All @@ -601,10 +610,29 @@ pub fn parse_packages(build_state: &mut BuildState) {
.collect::<Vec<String>>(),
None => unreachable!(),
};
let entry = match &package.namespace {
package_tree::Namespace::NamespaceWithEntry {
entry,
namespace: _,
} => Some(entry),
_ => None,
};

let depending_modules = source_files
.iter()
.map(|path| helpers::file_path_to_module_name(&path, &None))
.map(|path| {
helpers::file_path_to_module_name(
&path,
&package_tree::Namespace::NoNamespace,
)
})
.filter(|module_name| {
if let Some(entry) = entry {
module_name != entry
} else {
true
}
})
.collect::<AHashSet<String>>();

let mlmap = gen_mlmap(
Expand All @@ -620,10 +648,20 @@ pub fn parse_packages(build_state: &mut BuildState) {
let deps = source_files
.iter()
.map(|path| helpers::file_path_to_module_name(&path, &package.namespace))
.filter(|module_name| {
if let Some(entry) = entry {
module_name != entry
} else {
true
}
})
.collect::<AHashSet<String>>();

build_state.insert_module(
&helpers::file_path_to_module_name(&mlmap.to_owned(), &None),
&helpers::file_path_to_module_name(
&mlmap.to_owned(),
&package_tree::Namespace::NoNamespace,
),
Module {
source_type: SourceType::MlMap(MlMap { dirty: false }),
deps: deps,
Expand Down Expand Up @@ -731,15 +769,14 @@ pub fn parse_packages(build_state: &mut BuildState) {
pub fn compile_mlmap(package: &package_tree::Package, namespace: &str, root_path: &str) {
let build_path_abs = helpers::get_build_path(root_path, &package.name);
let mlmap_name = format!("{}.mlmap", namespace);
let args = vec![vec![
let args = vec![
"-w",
"-49",
"-color",
"always",
"-no-alias-deps",
&mlmap_name,
]]
.concat();
];

let _ = Command::new(helpers::get_bsc(&root_path))
.current_dir(helpers::canonicalize_string_path(&build_path_abs).unwrap())
Expand Down Expand Up @@ -788,9 +825,36 @@ pub fn compile_file(
})
.collect::<Vec<Vec<String>>>();

let namespace_args = match package.namespace.to_owned() {
Some(namespace) => vec!["-bs-ns".to_string(), namespace],
None => vec![],
let implementation_file_path = match module.source_type {
SourceType::SourceFile(ref source_file) => &source_file.implementation.path,
_ => panic!("Not a source file"),
};

let module_name =
helpers::file_path_to_module_name(implementation_file_path, &package.namespace);

let namespace_args = match &package.namespace {
package_tree::Namespace::NamespaceWithEntry {
namespace: _,
entry,
} if &module_name == entry => {
// if the module is the entry we just want to open the namespace
vec![
"-open".to_string(),
package.namespace.to_suffix().unwrap().to_string(),
]
}
package_tree::Namespace::Namespace(_)
| package_tree::Namespace::NamespaceWithEntry {
namespace: _,
entry: _,
} => {
vec![
"-bs-ns".to_string(),
package.namespace.to_suffix().unwrap().to_string(),
]
}
package_tree::Namespace::NoNamespace => vec![],
};

let jsx_args = get_jsx_args(&root_package);
Expand Down Expand Up @@ -833,14 +897,6 @@ pub fn compile_file(
_ => vec![],
};

let implementation_file_path = match module.source_type {
SourceType::SourceFile(ref source_file) => &source_file.implementation.path,
_ => panic!("Not a source file"),
};

let module_name =
helpers::file_path_to_module_name(implementation_file_path, &package.namespace);

let implementation_args = if is_interface {
debug!("Compiling interface file: {}", &module_name);
vec![]
Expand Down Expand Up @@ -1289,7 +1345,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
// this is why mlmap is compiled in the AST generation stage
// compile_mlmap(&module.package, module_name, &project_root);
Some((
package.namespace.to_owned().unwrap(),
package.namespace.to_suffix().unwrap(),
Ok(None),
Some(Ok(None)),
false,
Expand Down
29 changes: 14 additions & 15 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::build;
use crate::build_types::*;
use crate::helpers;
use crate::helpers::get_mlmap_path;
use crate::package_tree;
use ahash::{AHashMap, AHashSet};
use rayon::prelude::*;
use std::fs;
Expand All @@ -23,18 +24,18 @@ pub fn get_res_path_from_ast(ast_file: &str) -> Option<String> {
return None;
}

fn remove_asts(source_file: &str, package_name: &str, namespace: &Option<String>, root_path: &str) {
fn remove_asts(source_file: &str, package_name: &str, root_path: &str) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
source_file,
package_name,
namespace,
&package_tree::Namespace::NoNamespace,
root_path,
"ast",
));
let _ = std::fs::remove_file(helpers::get_compiler_asset(
source_file,
package_name,
namespace,
&package_tree::Namespace::NoNamespace,
root_path,
"iast",
));
Expand All @@ -47,7 +48,7 @@ fn remove_mjs_file(source_file: &str) {
fn remove_compile_assets(
source_file: &str,
package_name: &str,
namespace: &Option<String>,
namespace: &package_tree::Namespace,
root_path: &str,
) {
// optimization
Expand Down Expand Up @@ -88,8 +89,10 @@ pub fn clean_mjs_files(all_modules: &AHashMap<String, Module>) {
}

pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AHashSet<String>) {
let mut ast_modules: AHashMap<String, (String, String, Option<String>, SystemTime, String)> =
AHashMap::new();
let mut ast_modules: AHashMap<
String,
(String, String, package_tree::Namespace, SystemTime, String),
> = AHashMap::new();
let mut cmi_modules: AHashMap<String, SystemTime> = AHashMap::new();
let mut ast_rescript_file_locations = AHashSet::new();

Expand Down Expand Up @@ -205,7 +208,6 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
remove_asts(
canonicalized_res_file_location,
package_name,
package_namespace,
&build_state.project_root,
);
remove_compile_assets(
Expand All @@ -229,11 +231,9 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
.modules
.get_mut(module_name)
.expect("Could not find module for ast file");
let full_module_name = module_name.to_string()
+ &match package_namespace {
Some(namespace) => "-".to_string() + namespace,
None => "".to_string(),
};
let full_module_name =
helpers::module_name_with_namespace(module_name, &package_namespace);

let compile_dirty = cmi_modules.get(&full_module_name);
if let Some(compile_dirty) = compile_dirty {
// println!("{} is not dirty", module_name);
Expand Down Expand Up @@ -389,7 +389,6 @@ pub fn cleanup_after_build(build_state: &BuildState, no_errors: bool) {
remove_asts(
&source_file.implementation.path,
&module.package_name,
&package.namespace,
&build_state.project_root,
);
}
Expand All @@ -416,11 +415,11 @@ pub fn cleanup_after_build(build_state: &BuildState, no_errors: bool) {
&helpers::canonicalize_string_path(&get_mlmap_path(
&build_state.project_root,
&module.package_name,
&package.namespace.as_ref().unwrap(),
&package.namespace.to_suffix().unwrap(),
))
.unwrap(),
&module.package_name,
&None,
&package_tree::Namespace::NoNamespace,
&build_state.project_root,
),
}
Expand Down
Loading
Loading