Skip to content

Commit

Permalink
♻️ - Use relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 21, 2023
1 parent de6b525 commit 37f150b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 132 deletions.
21 changes: 17 additions & 4 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::{fmt, fs};

#[derive(Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -27,10 +28,14 @@ pub struct PackageSource {
/// `to_qualified_without_children` takes a tree like structure of dependencies, coming in from
/// `bsconfig`, and turns it into a flat list. The main thing we extract here are the source
/// folders, and optional subdirs, where potentially, the subdirs recurse or not.
pub fn to_qualified_without_children(s: &Source) -> PackageSource {
pub fn to_qualified_without_children(s: &Source, sub_path: Option<PathBuf>) -> PackageSource {
match s {
Source::Shorthand(dir) => PackageSource {
dir: dir.to_owned(),
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: None,
type_: None,
},
Expand All @@ -39,12 +44,20 @@ pub fn to_qualified_without_children(s: &Source) -> PackageSource {
type_,
subdirs: Some(Subdirs::Recurse(should_recurse)),
}) => PackageSource {
dir: dir.to_owned(),
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: Some(Subdirs::Recurse(*should_recurse)),
type_: type_.to_owned(),
},
Source::Qualified(PackageSource { dir, type_, .. }) => PackageSource {
dir: dir.to_owned(),
dir: sub_path
.map(|p| p.join(Path::new(dir)))
.unwrap_or(Path::new(dir).to_path_buf())
.to_string_lossy()
.to_string(),
subdirs: None,
type_: type_.to_owned(),
},
Expand Down
14 changes: 6 additions & 8 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ fn generate_ast(
let uncurried_args = get_uncurried_args(version, &package, &root_package);
let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags);

let res_to_ast_args = |file: String| -> Vec<String> {
let res_to_ast_args = |file: &str| -> Vec<String> {
let file = "../../".to_string() + file;
vec![
vec!["-bs-v".to_string(), format!("{}", version)],
ppx_flags,
Expand All @@ -194,7 +195,7 @@ fn generate_ast(
};

/* Create .ast */
if let Some(res_to_ast) = helpers::canonicalize_string_path(file).map(|file| {
if let Some(res_to_ast) = Some(file).map(|file| {
Command::new(helpers::get_bsc(&root_path))
.current_dir(helpers::canonicalize_string_path(&build_path_abs).unwrap())
.args(res_to_ast_args(file))
Expand All @@ -206,12 +207,14 @@ fn generate_ast(
if res_to_ast.status.success() {
Ok((ast_path, Some(stderr.to_string())))
} else {
println!("err: {}", stderr.to_string());
Err(stderr.to_string())
}
} else {
Ok((ast_path, None))
}
} else {
println!("Parsing file {}...", file);
return Err(format!(
"Could not find canonicalize_string_path for file {} in package {}",
file, package.name
Expand Down Expand Up @@ -812,7 +815,6 @@ pub fn compile_file(
is_interface: bool,
) -> Result<Option<String>, String> {
let build_path_abs = helpers::get_build_path(root_path, &package.name);
let pkg_path_abs = helpers::get_package_path(root_path, &package.name);
let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags);

let normal_deps = package
Expand Down Expand Up @@ -933,8 +935,6 @@ pub fn compile_file(
format!(
"es6:{}:{}",
Path::new(implementation_file_path)
.strip_prefix(pkg_path_abs)
.unwrap()
.parent()
.unwrap()
.to_str()
Expand Down Expand Up @@ -965,7 +965,7 @@ pub fn compile_file(
// "-I".to_string(),
// abs_node_modules_path.to_string() + "/rescript/ocaml",
// ],
vec![helpers::canonicalize_string_path(&ast_path.to_owned()).unwrap()],
vec![ast_path.to_string()],
]
.concat();

Expand All @@ -987,8 +987,6 @@ pub fn compile_file(
.to_string();

let dir = std::path::Path::new(implementation_file_path)
.strip_prefix(helpers::get_package_path(root_path, &package.name))
.unwrap()
.parent()
.unwrap();

Expand Down
58 changes: 28 additions & 30 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::package_tree;
use ahash::{AHashMap, AHashSet};
use rayon::prelude::*;
use std::fs;
use std::path::PathBuf;
use std::time::SystemTime;

pub fn get_res_path_from_ast(ast_file: &str) -> Option<String> {
Expand Down Expand Up @@ -101,7 +102,14 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
.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(
PathBuf::from(&package.package_dir)
.join(source_file.implementation.path.to_owned())
.to_string_lossy()
.to_string(),
)
}
_ => None,
})
Expand All @@ -112,9 +120,13 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
.modules
.values()
.filter_map(|module| {
build::get_interface(module)
.as_ref()
.map(|interface| interface.path.to_string())
let package = build_state.packages.get(&module.package_name).unwrap();
build::get_interface(module).as_ref().map(|interface| {
PathBuf::from(&package.package_dir)
.join(interface.path.to_owned())
.to_string_lossy()
.to_string()
})
})
.collect::<AHashSet<String>>(),
);
Expand Down Expand Up @@ -181,51 +193,41 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
}
}

let canonicalized_rescript_file_locations = rescript_file_locations
.iter()
.filter_map(|rescript_file_location| {
helpers::canonicalize_parent_string_path(rescript_file_location)
})
.collect::<AHashSet<String>>();
// delete the .mjs file which appear in our previous compile assets
// but does not exists anymore
// delete the compiler assets for which modules we can't find a rescript file
// location of rescript file is in the AST
// delete the .mjs file for which we DO have a compiler asset, but don't have a
// rescript file anymore (path is found in the .ast file)
let diff = ast_rescript_file_locations
.difference(&canonicalized_rescript_file_locations)
.difference(&rescript_file_locations)
.collect::<Vec<&String>>();

let diff_len = diff.len();

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

remove_asts(
canonicalized_res_file_location,
package_name,
&build_state.project_root,
);
remove_asts(res_file_location, package_name, &build_state.project_root);
remove_compile_assets(
canonicalized_res_file_location,
res_file_location,
package_name,
package_namespace,
&build_state.project_root,
);
remove_mjs_file(&canonicalized_res_file_location)
remove_mjs_file(&res_file_location)
});

ast_rescript_file_locations
.intersection(&canonicalized_rescript_file_locations)
.intersection(&rescript_file_locations)
.into_iter()
.for_each(|canonicalized_res_file_location| {
.for_each(|res_file_location| {
let (module_name, _package_name, package_namespace, ast_last_modified, ast_file_path) =
ast_modules
.get(canonicalized_res_file_location)
.get(res_file_location)
.expect("Could not find module name for ast file");
let module = build_state
.modules
Expand Down Expand Up @@ -393,22 +395,18 @@ pub fn cleanup_after_build(build_state: &BuildState) {
match &module.source_type {
SourceType::SourceFile(source_file) => {
remove_compile_assets(
&helpers::canonicalize_parent_string_path(
&source_file.implementation.path,
)
.unwrap(),
&source_file.implementation.path,
&module.package_name,
&package.namespace,
&build_state.project_root,
);
}
SourceType::MlMap(_) => remove_compile_assets(
&helpers::canonicalize_string_path(&get_mlmap_path(
&get_mlmap_path(
&build_state.project_root,
&module.package_name,
&package.namespace.to_suffix().unwrap(),
))
.unwrap(),
),
&module.package_name,
&package_tree::Namespace::NoNamespace,
&build_state.project_root,
Expand Down
31 changes: 5 additions & 26 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ impl LexicalAbsolute for Path {
}
}

pub fn get_relative_package_path(package_name: &str) -> String {
format!("node_modules/{}", package_name)
}

pub fn get_package_path(root: &str, package_name: &str) -> String {
format!("{}/node_modules/{}", root, package_name)
}
Expand Down Expand Up @@ -197,24 +201,6 @@ pub fn canonicalize_string_path(path: &str) -> Option<String> {
.map(|path| path.to_str().expect("Could not canonicalize").to_string());
}

// sometimes we only want to canonicalize the parent path, if we potentially want to
// canonicalize file paths that might not exist anymore BUT the parent path does
pub fn canonicalize_parent_string_path(path: &str) -> Option<String> {
return Path::new(path)
.parent()
.unwrap()
.canonicalize()
.ok()
.map(|dir| {
let filename = Path::new(path)
.file_name()
.expect("There should always be a filename");
// add back file
let path = dir.join(filename);
return path.to_str().expect("Could not canonicalize").to_string();
});
}

pub fn get_bs_compiler_asset(
source_file: &str,
package_name: &str,
Expand All @@ -226,15 +212,8 @@ pub fn get_bs_compiler_asset(
"ast" | "iast" => &package_tree::Namespace::NoNamespace,
_ => namespace,
};
let canonicalized_source_file = source_file;
let canonicalized_path =
canonicalize_string_path(&get_package_path(root_path, &package_name)).unwrap();

let dir = std::path::Path::new(&canonicalized_source_file)
.strip_prefix(canonicalized_path)
.unwrap()
.parent()
.unwrap();
let dir = std::path::Path::new(&source_file).parent().unwrap();

std::path::Path::new(&get_bs_build_path(root_path, &package_name))
.join(dir)
Expand Down
Loading

0 comments on commit 37f150b

Please sign in to comment.