Skip to content

Commit

Permalink
🎨 - Support non-monorepos
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 22, 2023
1 parent eb92714 commit 0fd6135
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 62 deletions.
101 changes: 74 additions & 27 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn generate_ast(
version: &str,
) -> Result<(String, Option<String>), String> {
let file = &filename.to_string();
let build_path_abs = helpers::get_build_path(root_path, &package.name);
let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root);
let path = PathBuf::from(filename);
let ast_extension = path_to_ast_extension(&path);

Expand Down Expand Up @@ -304,7 +304,7 @@ fn gen_mlmap(
depending_modules: &AHashSet<String>,
root_path: &str,
) -> String {
let build_path_abs = helpers::get_build_path(root_path, &package.name);
let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root);
// we don't really need to create a digest, because we track if we need to
// recompile in a different way but we need to put it in the file for it to
// be readable.
Expand Down Expand Up @@ -357,6 +357,7 @@ fn generate_asts(
.namespace
.to_suffix()
.expect("namespace should be set for mlmap module"),
package.is_root,
);
let compile_path = helpers::get_mlmap_compile_path(
&build_state.project_root,
Expand All @@ -365,6 +366,7 @@ fn generate_asts(
.namespace
.to_suffix()
.expect("namespace should be set for mlmap module"),
package.is_root,
);
let mlmap_hash = compute_file_hash(&compile_path);
compile_mlmap(&package, module_name, &build_state.project_root);
Expand Down Expand Up @@ -542,6 +544,7 @@ fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>) {
&source_file.implementation.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);

let mut deps = get_dep_modules(
Expand All @@ -557,6 +560,7 @@ fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>) {
&interface.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);

deps.extend(get_dep_modules(
Expand Down Expand Up @@ -610,8 +614,11 @@ pub fn parse_packages(build_state: &mut BuildState) {
Some(package_modules) => build_state.module_names.extend(package_modules),
None => (),
}
let build_path_abs =
helpers::get_build_path(&build_state.project_root, &package.bsconfig.name);
let build_path_abs = helpers::get_build_path(
&build_state.project_root,
&package.bsconfig.name,
package.is_root,
);
helpers::create_build_path(&build_path_abs);

package.namespace.to_suffix().iter().for_each(|namespace| {
Expand Down Expand Up @@ -786,7 +793,7 @@ 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 build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root);
let mlmap_name = format!("{}.mlmap", namespace);
let args = vec![
"-w",
Expand All @@ -813,7 +820,7 @@ pub fn compile_file(
version: &str,
is_interface: bool,
) -> Result<Option<String>, String> {
let build_path_abs = helpers::get_build_path(root_path, &package.name);
let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root);
let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags);

let normal_deps = package
Expand All @@ -838,7 +845,12 @@ pub fn compile_file(
.map(|x| {
vec![
"-I".to_string(),
helpers::canonicalize_string_path(&helpers::get_build_path(root_path, &x)).unwrap(),
helpers::canonicalize_string_path(&helpers::get_build_path(
root_path,
&x,
package.is_root,
))
.unwrap(),
]
})
.collect::<Vec<Vec<String>>>();
Expand Down Expand Up @@ -993,28 +1005,44 @@ pub fn compile_file(
if !is_interface {
let _ = std::fs::copy(
build_path_abs.to_string() + "/" + &module_name + ".cmi",
std::path::Path::new(&helpers::get_bs_build_path(root_path, &package.name))
.join(dir)
.join(module_name.to_owned() + ".cmi"),
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
package.is_root,
))
.join(dir)
.join(module_name.to_owned() + ".cmi"),
);
let _ = std::fs::copy(
build_path_abs.to_string() + "/" + &module_name + ".cmj",
std::path::Path::new(&helpers::get_bs_build_path(root_path, &package.name))
.join(dir)
.join(module_name.to_owned() + ".cmj"),
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
package.is_root,
))
.join(dir)
.join(module_name.to_owned() + ".cmj"),
);
let _ = std::fs::copy(
build_path_abs.to_string() + "/" + &module_name + ".cmt",
std::path::Path::new(&helpers::get_bs_build_path(root_path, &package.name))
.join(dir)
.join(module_name.to_owned() + ".cmt"),
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
package.is_root,
))
.join(dir)
.join(module_name.to_owned() + ".cmt"),
);
} else {
let _ = std::fs::copy(
build_path_abs.to_string() + "/" + &module_name + ".cmti",
std::path::Path::new(&helpers::get_bs_build_path(root_path, &package.name))
.join(dir)
.join(module_name.to_owned() + ".cmti"),
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
package.is_root,
))
.join(dir)
.join(module_name.to_owned() + ".cmti"),
);
}
match &module.source_type {
Expand All @@ -1030,18 +1058,34 @@ pub fn compile_file(
// editor tools expects the source file in lib/bs for finding the current package
// and in lib/ocaml when referencing modules in other packages
let _ = std::fs::copy(
std::path::Path::new(&helpers::get_package_path(root_path, &package.name))
.join(path),
std::path::Path::new(&helpers::get_bs_build_path(root_path, &package.name))
.join(path),
std::path::Path::new(&helpers::get_package_path(
root_path,
&package.name,
package.is_root,
))
.join(path),
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
package.is_root,
))
.join(path),
)
.expect("copying source file failed");

let _ = std::fs::copy(
std::path::Path::new(&helpers::get_package_path(root_path, &package.name))
.join(path),
std::path::Path::new(&helpers::get_build_path(root_path, &package.name))
.join(std::path::Path::new(path).file_name().unwrap()),
std::path::Path::new(&helpers::get_package_path(
root_path,
&package.name,
package.is_root,
))
.join(path),
std::path::Path::new(&helpers::get_build_path(
root_path,
&package.name,
package.is_root,
))
.join(std::path::Path::new(path).file_name().unwrap()),
)
.expect("copying source file failed");
}
Expand Down Expand Up @@ -1403,6 +1447,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
&package.namespace,
&build_state.project_root,
"cmi",
package.is_root,
);

let cmi_digest = compute_file_hash(&cmi_path);
Expand All @@ -1424,6 +1469,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
&path,
&package.name,
&build_state.project_root,
package.is_root,
),
module,
&build_state.project_root,
Expand All @@ -1441,6 +1487,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
&source_file.implementation.path,
&package.name,
&build_state.project_root,
package.is_root,
),
module,
&build_state.project_root,
Expand Down
60 changes: 48 additions & 12 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ pub fn get_res_path_from_ast(ast_file: &str) -> Option<String> {
return None;
}

fn remove_asts(source_file: &str, package_name: &str, root_path: &str) {
fn remove_asts(source_file: &str, package_name: &str, root_path: &str, is_root: bool) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
source_file,
package_name,
&package_tree::Namespace::NoNamespace,
root_path,
"ast",
is_root,
));
let _ = std::fs::remove_file(helpers::get_compiler_asset(
source_file,
package_name,
&package_tree::Namespace::NoNamespace,
root_path,
"iast",
is_root,
));
}

Expand All @@ -51,6 +53,7 @@ fn remove_compile_assets(
package_name: &str,
namespace: &package_tree::Namespace,
root_path: &str,
is_root: bool,
) {
// optimization
// only issue cmti if htere is an interfacce file
Expand All @@ -61,13 +64,15 @@ fn remove_compile_assets(
namespace,
root_path,
extension,
is_root,
));
let _ = std::fs::remove_file(helpers::get_bs_compiler_asset(
source_file,
package_name,
namespace,
root_path,
extension,
is_root,
));
}
}
Expand All @@ -92,7 +97,14 @@ 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, package_tree::Namespace, SystemTime, String),
(
String,
String,
package_tree::Namespace,
SystemTime,
String,
bool,
),
> = AHashMap::new();
let mut cmi_modules: AHashMap<String, SystemTime> = AHashMap::new();
let mut ast_rescript_file_locations = AHashSet::new();
Expand Down Expand Up @@ -136,6 +148,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
let read_dir = fs::read_dir(std::path::Path::new(&helpers::get_build_path(
&build_state.project_root,
&package.name,
package.is_root,
)))
.unwrap();

Expand Down Expand Up @@ -164,6 +177,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
package.namespace.to_owned(),
entry.metadata().unwrap().modified().unwrap(),
ast_file_path,
package.is_root,
),
);
let _ = ast_rescript_file_locations.insert(res_file_path);
Expand Down Expand Up @@ -206,17 +220,29 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
let diff_len = diff.len();

diff.par_iter().for_each(|res_file_location| {
let (_module_name, package_name, package_namespace, _last_modified, _ast_file_path) =
ast_modules
.get(&res_file_location.to_string())
.expect("Could not find module name for ast file");
let (
_module_name,
package_name,
package_namespace,
_last_modified,
_ast_file_path,
is_root,
) = ast_modules
.get(&res_file_location.to_string())
.expect("Could not find module name for ast file");

remove_asts(res_file_location, package_name, &build_state.project_root);
remove_asts(
res_file_location,
package_name,
&build_state.project_root,
*is_root,
);
remove_compile_assets(
res_file_location,
package_name,
package_namespace,
&build_state.project_root,
*is_root,
);
remove_mjs_file(&res_file_location)
});
Expand All @@ -225,10 +251,16 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
.intersection(&rescript_file_locations)
.into_iter()
.for_each(|res_file_location| {
let (module_name, _package_name, package_namespace, ast_last_modified, ast_file_path) =
ast_modules
.get(res_file_location)
.expect("Could not find module name for ast file");
let (
module_name,
_package_name,
package_namespace,
ast_last_modified,
ast_file_path,
_is_root,
) = ast_modules
.get(res_file_location)
.expect("Could not find module name for ast file");
let module = build_state
.modules
.get_mut(module_name)
Expand Down Expand Up @@ -299,7 +331,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH

let ast_module_names = ast_modules
.values()
.map(|(module_name, _, _, _, _)| module_name)
.map(|(module_name, _, _, _, _, _)| module_name)
.collect::<AHashSet<&String>>();

let all_module_names = build_state
Expand Down Expand Up @@ -383,6 +415,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
&source_file.implementation.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);
}
_ => (),
Expand All @@ -399,17 +432,20 @@ pub fn cleanup_after_build(build_state: &BuildState) {
&module.package_name,
&package.namespace,
&build_state.project_root,
package.is_root,
);
}
SourceType::MlMap(_) => remove_compile_assets(
&get_mlmap_path(
&build_state.project_root,
&module.package_name,
&package.namespace.to_suffix().unwrap(),
package.is_root,
),
&module.package_name,
&package_tree::Namespace::NoNamespace,
&build_state.project_root,
package.is_root,
),
}
}
Expand Down
Loading

0 comments on commit 0fd6135

Please sign in to comment.