Skip to content

Commit

Permalink
🐛 - Fix clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 24, 2023
1 parent dc671ec commit fe27f4b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
15 changes: 7 additions & 8 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,12 @@ pub fn clean(path: &str) {
);
std::io::stdout().flush().unwrap();

let path = std::path::Path::new(&package.package_dir)
.join("lib")
.join("ocaml");
let path_str = helpers::get_build_path(&project_root, &package.name, package.is_root);
let path = std::path::Path::new(&path_str);
let _ = std::fs::remove_dir_all(path);
let path = std::path::Path::new(&package.package_dir)
.join("lib")
.join("bs");

let path_str = helpers::get_bs_build_path(&project_root, &package.name, package.is_root);
let path = std::path::Path::new(&path_str);
let _ = std::fs::remove_dir_all(path);
});
let timing_clean_compiler_assets_elapsed = timing_clean_compiler_assets.elapsed();
Expand All @@ -1155,9 +1154,9 @@ pub fn clean(path: &str) {
SWEEP
);
std::io::stdout().flush().unwrap();
let mut build_state = BuildState::new(project_root, root_config_name, packages);
let mut build_state = BuildState::new(project_root.to_owned(), root_config_name, packages);
parse_packages(&mut build_state);
clean_mjs_files(&build_state.modules);
clean_mjs_files(&build_state, &project_root);
let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed();
println!(
"{}\r{} {}Cleaned mjs files in {:.2}s",
Expand Down
44 changes: 35 additions & 9 deletions src/clean.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::bsconfig;
use crate::build;
use crate::build_types::*;
use crate::helpers;
Expand Down Expand Up @@ -44,8 +45,8 @@ fn remove_asts(source_file: &str, package_name: &str, root_path: &str, is_root:
));
}

fn remove_mjs_file(source_file: &str) {
let _ = std::fs::remove_file(helpers::change_extension(source_file, "mjs"));
fn remove_mjs_file(source_file: &str, suffix: &bsconfig::Suffix) {
let _ = std::fs::remove_file(helpers::change_extension(source_file, &suffix.to_string()));
}

fn remove_compile_assets(
Expand Down Expand Up @@ -77,21 +78,39 @@ fn remove_compile_assets(
}
}

pub fn clean_mjs_files(all_modules: &AHashMap<String, Module>) {
pub fn clean_mjs_files(build_state: &BuildState, project_root: &str) {
// get all rescript file locations
let rescript_file_locations = all_modules
let rescript_file_locations = build_state
.modules
.values()
.filter_map(|module| match &module.source_type {
SourceType::SourceFile(source_file) => {
Some(source_file.implementation.path.to_string())
let package = build_state.packages.get(&module.package_name).unwrap();
Some((
std::path::PathBuf::from(helpers::get_package_path(
&project_root,
&module.package_name,
package.is_root,
))
.join(source_file.implementation.path.to_string())
.to_string_lossy()
.to_string(),
package
.bsconfig
.suffix
.to_owned()
.unwrap_or(bsconfig::Suffix::Mjs),
))
}
_ => None,
})
.collect::<AHashSet<String>>();
.collect::<Vec<(String, bsconfig::Suffix)>>();

rescript_file_locations
.par_iter()
.for_each(|rescript_file_location| remove_mjs_file(&rescript_file_location));
.for_each(|(rescript_file_location, suffix)| {
remove_mjs_file(&rescript_file_location, &suffix)
});
}

pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AHashSet<String>) {
Expand All @@ -104,6 +123,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
SystemTime,
String,
bool,
Option<bsconfig::Suffix>,
),
> = AHashMap::new();
let mut cmi_modules: AHashMap<String, SystemTime> = AHashMap::new();
Expand Down Expand Up @@ -178,6 +198,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
entry.metadata().unwrap().modified().unwrap(),
ast_file_path,
package.is_root,
package.bsconfig.suffix.to_owned(),
),
);
let _ = ast_rescript_file_locations.insert(res_file_path);
Expand Down Expand Up @@ -227,6 +248,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
_last_modified,
_ast_file_path,
is_root,
suffix,
) = ast_modules
.get(&res_file_location.to_string())
.expect("Could not find module name for ast file");
Expand All @@ -244,7 +266,10 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
&build_state.project_root,
*is_root,
);
remove_mjs_file(&res_file_location)
remove_mjs_file(
&res_file_location,
&suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs),
);
});

ast_rescript_file_locations
Expand All @@ -258,6 +283,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
ast_last_modified,
ast_file_path,
_is_root,
_suffix,
) = ast_modules
.get(res_file_location)
.expect("Could not find module name for ast file");
Expand Down Expand Up @@ -331,7 +357,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

0 comments on commit fe27f4b

Please sign in to comment.