diff --git a/src/build.rs b/src/build.rs index b26ec0a..7252625 100644 --- a/src/build.rs +++ b/src/build.rs @@ -89,7 +89,7 @@ pub fn build(filter: &Option, path: &str, no_timing: bool) -> Resu let _ = stdout().flush(); let mut build_state = BuildState::new(project_root, root_config_name, packages); packages::parse_packages(&mut build_state); - logs::initialize(&build_state.project_root, &build_state.packages); + logs::initialize(&build_state.packages); let timing_source_files_elapsed = timing_source_files.elapsed(); println!( "{}\r{} {}Found source files in {:.2}s", @@ -150,7 +150,7 @@ pub fn build(filter: &Option, path: &str, no_timing: bool) -> Resu print!("{}", &err); } Err(err) => { - logs::finalize(&build_state.project_root, &build_state.packages); + logs::finalize(&build_state.packages); println!( "{}\r{} {}Error parsing source files in {:.2}s", LINE_CLEAR, @@ -195,7 +195,7 @@ pub fn build(filter: &Option, path: &str, no_timing: bool) -> Resu ); let compile_duration = start_compiling.elapsed(); - logs::finalize(&build_state.project_root, &build_state.packages); + logs::finalize(&build_state.packages); pb.finish(); clean::cleanup_after_build(&build_state); if compile_errors.len() > 0 { diff --git a/src/build/clean.rs b/src/build/clean.rs index 2c1df97..8c5ca88 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -9,25 +9,21 @@ use rayon::prelude::*; use std::io::Write; use std::time::Instant; -fn remove_ast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) { +fn remove_ast(source_file: &str, package: &packages::Package) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_name, &packages::Namespace::NoNamespace, - root_path, "ast", - is_root, )); } -fn remove_iast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) { +fn remove_iast(source_file: &str, package: &packages::Package) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_name, &packages::Namespace::NoNamespace, - root_path, "iast", - is_root, )); } @@ -41,48 +37,33 @@ fn remove_mjs_file(source_file: &str, suffix: &bsconfig::Suffix) { fn remove_compile_asset( source_file: &str, - package_name: &str, + package: &packages::Package, namespace: &packages::Namespace, - root_path: &str, - is_root: bool, extension: &str, ) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_name, namespace, - root_path, extension, - is_root, )); let _ = std::fs::remove_file(helpers::get_bs_compiler_asset( source_file, - package_name, + package, namespace, - root_path, extension, - is_root, )); } pub fn remove_compile_assets( source_file: &str, - package_name: &str, + package: &packages::Package, namespace: &packages::Namespace, - root_path: &str, - is_root: bool, ) { // optimization // only issue cmti if htere is an interfacce file for extension in &["cmj", "cmi", "cmt", "cmti"] { - remove_compile_asset( - source_file, - package_name, - namespace, - root_path, - is_root, - extension, - ); + remove_compile_asset(source_file, package, namespace, extension); } } @@ -99,7 +80,7 @@ pub fn clean_mjs_files(build_state: &BuildState) { .get(&build_state.root_config_name) .expect("Could not find root package"); Some(( - std::path::PathBuf::from(package.package_dir.to_string()) + std::path::PathBuf::from(package.path.to_string()) .join(source_file.implementation.path.to_string()) .to_string_lossy() .to_string(), @@ -147,36 +128,20 @@ pub fn cleanup_previous_build( package_name, namespace: package_namespace, ast_file_path, - is_root, suffix, .. } = compile_assets_state .ast_modules .get(&res_file_location.to_string()) .expect("Could not find module name for ast file"); - remove_compile_assets( - res_file_location, - package_name, - package_namespace, - &build_state.project_root, - *is_root, - ); + let package = build_state.get_package(package_name).unwrap(); + remove_compile_assets(res_file_location, package, package_namespace); remove_mjs_file( &res_file_location, &suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs), ); - remove_iast( - res_file_location, - package_name, - &build_state.project_root, - *is_root, - ); - remove_ast( - res_file_location, - package_name, - &build_state.project_root, - *is_root, - ); + remove_iast(res_file_location, package); + remove_ast(res_file_location, package); match helpers::get_extension(ast_file_path).as_str() { "iast" => Some(module_name.to_owned()), "ast" => None, @@ -354,18 +319,8 @@ pub fn cleanup_after_build(build_state: &BuildState) { if failed_to_parse(module) { match &module.source_type { SourceType::SourceFile(source_file) => { - remove_iast( - &source_file.implementation.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); - remove_ast( - &source_file.implementation.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); + remove_iast(&source_file.implementation.path, package); + remove_ast(&source_file.implementation.path, package); } _ => (), } @@ -381,10 +336,8 @@ pub fn cleanup_after_build(build_state: &BuildState) { // unecessary mark all the dependents as dirty, when there is no change in the interface remove_compile_asset( &source_file.implementation.path, - &module.package_name, + package, &package.namespace, - &build_state.project_root, - package.is_root, "cmt", ); } @@ -416,11 +369,11 @@ pub fn clean(path: &str) { ); std::io::stdout().flush().unwrap(); - let path_str = helpers::get_build_path(&project_root, &package.name, package.is_root); + let path_str = package.get_build_path(); let path = std::path::Path::new(&path_str); let _ = std::fs::remove_dir_all(path); - let path_str = helpers::get_bs_build_path(&project_root, &package.name, package.is_root); + let path_str = package.get_bs_build_path(); let path = std::path::Path::new(&path_str); let _ = std::fs::remove_dir_all(path); }); diff --git a/src/build/compile.rs b/src/build/compile.rs index 9cde3ec..86dafaf 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -142,12 +142,10 @@ pub fn compile( } SourceType::SourceFile(source_file) => { let cmi_path = helpers::get_compiler_asset( + package, &source_file.implementation.path, - &module.package_name, &package.namespace, - &build_state.project_root, "cmi", - package.is_root, ); let cmi_digest = helpers::compute_file_hash(&cmi_path); @@ -164,12 +162,7 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_iast_path( - &path, - &package.name, - &build_state.project_root, - package.is_root, - ), + &package.get_iast_path(&path), module, &build_state.project_root, &rescript_version, @@ -182,12 +175,7 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_ast_path( - &source_file.implementation.path, - &package.name, - &build_state.project_root, - package.is_root, - ), + &package.get_ast_path(&source_file.implementation.path), module, &build_state.project_root, &rescript_version, @@ -287,23 +275,13 @@ pub fn compile( match result { Ok(Some(err)) => { source_file.implementation.compile_state = CompileState::Warning; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(package, &err); compile_warnings.push_str(&err); } Ok(None) => (), Err(err) => { source_file.implementation.compile_state = CompileState::Error; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(package, &err); compile_errors.push_str(&err); } }; @@ -311,24 +289,14 @@ pub fn compile( Some(Ok(Some(err))) => { source_file.interface.as_mut().unwrap().compile_state = CompileState::Warning; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(package, &err); compile_warnings.push_str(&err); } Some(Ok(None)) => (), Some(Err(err)) => { source_file.interface.as_mut().unwrap().compile_state = CompileState::Error; - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &err, - ); + logs::append(package, &err); compile_errors.push_str(&err); } _ => (), @@ -375,7 +343,7 @@ fn compile_file( version: &str, is_interface: bool, ) -> Result, String> { - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags); let normal_deps = package @@ -398,10 +366,11 @@ fn compile_file( .concat() .into_iter() .map(|x| { + // TODO: Add support for non-hoisted set up + let hoisted_path = format!("{}/node_modules/{}/lib/ocaml", root_path, &x); vec![ "-I".to_string(), - helpers::canonicalize_string_path(&helpers::get_build_path(root_path, &x, package.is_root)) - .unwrap(), + helpers::canonicalize_string_path(&hoisted_path).unwrap(), ] }) .collect::>>(); @@ -550,50 +519,34 @@ 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, - package.is_root, - )) - .join(dir) - // because editor tooling doesn't support namespace entries yet - // we just remove the @ for now. This makes sure the editor support - // doesn't break - .join(module_name.to_owned().replace("@", "") + ".cmi"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + // because editor tooling doesn't support namespace entries yet + // we just remove the @ for now. This makes sure the editor support + // doesn't break + .join(module_name.to_owned().replace("@", "") + ".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, - package.is_root, - )) - .join(dir) - .join(module_name.to_owned() + ".cmj"), + std::path::Path::new(&package.get_bs_build_path()) + .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, - package.is_root, - )) - .join(dir) - // because editor tooling doesn't support namespace entries yet - // we just remove the @ for now. This makes sure the editor support - // doesn't break - .join(module_name.to_owned().replace("@", "") + ".cmt"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + // because editor tooling doesn't support namespace entries yet + // we just remove the @ for now. This makes sure the editor support + // doesn't break + .join(module_name.to_owned().replace("@", "") + ".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, - package.is_root, - )) - .join(dir) - .join(module_name.to_owned() + ".cmti"), + std::path::Path::new(&package.get_bs_build_path()) + .join(dir) + .join(module_name.to_owned() + ".cmti"), ); } match &module.source_type { @@ -609,24 +562,15 @@ 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(&package.package_dir).join(path), - std::path::Path::new(&helpers::get_bs_build_path( - root_path, - &package.name, - package.is_root, - )) - .join(path), + std::path::Path::new(&package.path).join(path), + std::path::Path::new(&package.get_bs_build_path()).join(path), ) .expect("copying source file failed"); let _ = std::fs::copy( - std::path::Path::new(&package.package_dir).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()), + std::path::Path::new(&package.path).join(path), + std::path::Path::new(&package.get_build_path()) + .join(std::path::Path::new(path).file_name().unwrap()), ) .expect("copying source file failed"); } diff --git a/src/build/deps.rs b/src/build/deps.rs index 1be9be8..39589c7 100644 --- a/src/build/deps.rs +++ b/src/build/deps.rs @@ -78,12 +78,7 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet let package = build_state .get_package(&module.package_name) .expect("Package not found"); - let ast_path = helpers::get_ast_path( - &source_file.implementation.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); + let ast_path = package.get_ast_path(&source_file.implementation.path); let mut deps = get_dep_modules( &ast_path, @@ -94,12 +89,7 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet match &source_file.interface { Some(interface) => { - let iast_path = helpers::get_iast_path( - &interface.path, - &module.package_name, - &build_state.project_root, - package.is_root, - ); + let iast_path = package.get_iast_path(&interface.path); deps.extend(get_dep_modules( &iast_path, diff --git a/src/build/logs.rs b/src/build/logs.rs index 2c71e98..d20cf46 100644 --- a/src/build/logs.rs +++ b/src/build/logs.rs @@ -7,15 +7,17 @@ use regex::Regex; use std::fs::File; use std::io::prelude::*; +use super::packages; + enum Location { Bs, Ocaml, } -fn get_log_file_path(project_root: &str, subfolder: Location, name: &str, is_root: bool) -> String { +fn get_log_file_path(subfolder: Location, package: &packages::Package) -> String { let build_folder = match subfolder { - Location::Bs => helpers::get_bs_build_path(project_root, name, is_root), - Location::Ocaml => helpers::get_build_path(project_root, name, is_root), + Location::Bs => package.get_bs_build_path(), + Location::Ocaml => package.get_build_path(), }; build_folder.to_owned() + "/.compiler.log" @@ -44,45 +46,32 @@ fn write_to_log_file(mut file: File, package_name: &str, content: &str) { } } -pub fn initialize(project_root: &str, packages: &AHashMap) { +pub fn initialize(packages: &AHashMap) { packages.par_iter().for_each(|(name, package)| { - let _ = File::create(get_log_file_path( - project_root, - Location::Bs, - name, - package.is_root, - )) - .map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time()))) - .expect(&("Cannot create compiler log for package ".to_owned() + name)); + let _ = File::create(get_log_file_path(Location::Bs, package)) + .map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time()))) + .expect(&("Cannot create compiler log for package ".to_owned() + name)); }) } -pub fn append(project_root: &str, is_root: bool, name: &str, str: &str) { +pub fn append(package: &packages::Package, str: &str) { File::options() .append(true) - .open(get_log_file_path(project_root, Location::Bs, name, is_root)) - .map(|file| write_to_log_file(file, &name, str)) - .expect( - &("Cannot write compilerlog: ".to_owned() - + &get_log_file_path(project_root, Location::Bs, name, is_root)), - ); + .open(get_log_file_path(Location::Bs, package)) + .map(|file| write_to_log_file(file, &package.name, str)) + .expect(&("Cannot write compilerlog: ".to_owned() + &get_log_file_path(Location::Bs, package))); } -pub fn finalize(project_root: &str, packages: &AHashMap) { +pub fn finalize(packages: &AHashMap) { packages.par_iter().for_each(|(name, package)| { let _ = File::options() .append(true) - .open(get_log_file_path( - project_root, - Location::Bs, - name, - package.is_root, - )) + .open(get_log_file_path(Location::Bs, package)) .map(|file| write_to_log_file(file, &name, &format!("#Done({})\n", helpers::get_system_time()))); let _ = std::fs::copy( - get_log_file_path(project_root, Location::Bs, name, package.is_root), - get_log_file_path(project_root, Location::Ocaml, name, package.is_root), + get_log_file_path(Location::Bs, package), + get_log_file_path(Location::Ocaml, package), ); }) } diff --git a/src/build/namespaces.rs b/src/build/namespaces.rs index b72b3d7..10a0fd7 100644 --- a/src/build/namespaces.rs +++ b/src/build/namespaces.rs @@ -23,9 +23,8 @@ pub fn gen_mlmap( package: &packages::Package, namespace: &str, depending_modules: &AHashSet, - root_path: &str, ) -> String { - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); // 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. @@ -51,7 +50,7 @@ pub fn gen_mlmap( } pub fn compile_mlmap(package: &packages::Package, namespace: &str, root_path: &str) { - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); let mlmap_name = format!("{}.mlmap", namespace); let args = vec!["-w", "-49", "-color", "always", "-no-alias-deps", &mlmap_name]; diff --git a/src/build/packages.rs b/src/build/packages.rs index 419390c..43c3804 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -43,7 +43,7 @@ struct Dependency { bsconfig: bsconfig::T, path: String, is_pinned: bool, - dependencies: Vec + dependencies: Vec, } #[derive(Debug, Clone)] @@ -56,12 +56,50 @@ pub struct Package { pub namespace: Namespace, pub modules: Option>, // canonicalized dir of the package - pub package_dir: String, + pub path: String, pub dirs: Option>, pub is_pinned_dep: bool, pub is_root: bool, } +impl Package { + pub fn get_bs_build_path(&self) -> String { + format!("{}/lib/bs", self.path) + } + + pub fn get_build_path(&self) -> String { + format!("{}/lib/ocaml", self.path) + } + + pub fn get_mlmap_path(&self) -> String { + self.get_build_path() + + "/" + + &self + .namespace + .to_suffix() + .expect("namespace should be set for mlmap module") + + ".mlmap" + } + + pub fn get_mlmap_compile_path(&self) -> String { + self.get_build_path() + + "/" + + &self + .namespace + .to_suffix() + .expect("namespace should be set for mlmap module") + + ".cmi" + } + + pub fn get_ast_path(&self, source_file: &str) -> String { + helpers::get_compiler_asset(self, source_file, &packages::Namespace::NoNamespace, "ast") + } + + pub fn get_iast_path(&self, source_file: &str) -> String { + helpers::get_compiler_asset(self, source_file, &packages::Namespace::NoNamespace, "iast") + } +} + impl PartialEq for Package { fn eq(&self, other: &Self) -> bool { self.name == other.name @@ -201,6 +239,8 @@ fn read_bsconfig(package_dir: &str) -> bsconfig::T { fn read_dependencies<'a>( registered_dependencies_set: &'a mut AHashSet, parent_bsconfig: bsconfig::T, + parent_path: &str, + project_root: &str, ) -> Vec { return parent_bsconfig .bs_dependencies @@ -219,19 +259,26 @@ fn read_dependencies<'a>( // Read all bsconfig files in parallel instead of blocking .par_iter() .map(|package_name| { - let path = - match PathBuf::from(helpers::get_relative_package_path(package_name, false)).canonicalize() { - Ok(dir) => dir.to_string_lossy().to_string(), - Err(e) => { - print!( - "{} {} Error building package tree (are node_modules up-to-date?)... \n More details: {}", - style("[1/2]").bold().dim(), - CROSS, - e.to_string() - ); - std::process::exit(2) + let path_buf = + match PathBuf::from(format!("{}/node_modules/{}", parent_path, package_name)).canonicalize() { + Ok(p) => p, + Err(e1) => { + match PathBuf::from(format!("{}/node_modules/{}", project_root, package_name)).canonicalize() { + Ok(p) => p, + Err(e2) => { + print!( + "{} {} Error building package tree (are node_modules up-to-date?)... \n More details: {}\n{}", + style("[1/2]").bold().dim(), + CROSS, + e1.to_string(), + e2.to_string() + ); + std::process::exit(2) + } + } } }; + let path = path_buf.to_string_lossy().to_string(); let bsconfig = read_bsconfig(&path); let is_pinned = parent_bsconfig @@ -240,7 +287,7 @@ fn read_dependencies<'a>( .map(|p| p.contains(&bsconfig.name)) .unwrap_or(false); - let dependencies = read_dependencies(&mut registered_dependencies_set.clone(),bsconfig.to_owned()); + let dependencies = read_dependencies(&mut registered_dependencies_set.clone(),bsconfig.to_owned(), &path, &project_root); Dependency { name: package_name.to_owned(), @@ -249,8 +296,7 @@ fn read_dependencies<'a>( is_pinned, dependencies, } - }) - .collect::>(); + }).collect::>(); } fn flatten_dependencies(dependencies: Vec) -> Vec { @@ -263,12 +309,7 @@ fn flatten_dependencies(dependencies: Vec) -> Vec { flattened } -fn make_package ( - bsconfig: bsconfig::T, - package_dir: &str, - is_pinned_dep: bool, - is_root: bool -) -> Package { +fn make_package(bsconfig: bsconfig::T, package_path: &str, is_pinned_dep: bool, is_root: bool) -> Package { let source_folders = match bsconfig.sources.to_owned() { bsconfig::OneOrMore::Single(source) => get_source_dirs(source, None), bsconfig::OneOrMore::Multiple(sources) => { @@ -320,7 +361,7 @@ fn make_package ( }, }, modules: None, - package_dir: package_dir.to_string(), + path: package_path.to_string(), dirs: None, is_pinned_dep: is_pinned_dep, is_root, @@ -332,14 +373,25 @@ fn read_packages(project_root: &str) -> AHashMap { // Store all packages and completely deduplicate them let mut map: AHashMap = AHashMap::new(); - map.insert(root_bsconfig.name.to_owned(), make_package(root_bsconfig.to_owned(), project_root, false, true)); + map.insert( + root_bsconfig.name.to_owned(), + make_package(root_bsconfig.to_owned(), project_root, false, true), + ); let mut registered_dependencies_set: AHashSet = AHashSet::new(); - let dependencies = flatten_dependencies(read_dependencies(&mut registered_dependencies_set, root_bsconfig.to_owned())); + let dependencies = flatten_dependencies(read_dependencies( + &mut registered_dependencies_set, + root_bsconfig.to_owned(), + project_root, + project_root, + )); dependencies.iter().for_each(|d| { - if !map.contains_key(&d.name) { - map.insert(d.name.to_owned(), make_package(d.bsconfig.to_owned(), &d.path, d.is_pinned, false)); - } + if !map.contains_key(&d.name) { + map.insert( + d.name.to_owned(), + make_package(d.bsconfig.to_owned(), &d.path, d.is_pinned, false), + ); + } }); return map; @@ -406,7 +458,7 @@ fn extend_with_children( value .source_folders .par_iter() - .map(|source| get_source_files(Path::new(&value.package_dir), &filter, source)) + .map(|source| get_source_files(Path::new(&value.path), &filter, source)) .collect::>>() .into_iter() .for_each(|source| map.extend(source)); @@ -453,14 +505,7 @@ pub fn make(filter: &Option, root_folder: &str) -> AHashMap dirs.iter().for_each(|dir| { - let _ = std::fs::create_dir_all( - std::path::Path::new(&helpers::get_bs_build_path( - root_folder, - &package.name, - package.is_root, - )) - .join(dir), - ); + let _ = std::fs::create_dir_all(std::path::Path::new(&package.get_bs_build_path()).join(dir)); }), None => (), }); @@ -483,13 +528,8 @@ 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, package.is_root); - let bs_build_path = helpers::get_bs_build_path( - &build_state.project_root, - &package.bsconfig.name, - package.is_root, - ); + let build_path_abs = package.get_build_path(); + let bs_build_path = package.get_bs_build_path(); helpers::create_build_path(&build_path_abs); helpers::create_build_path(&bs_build_path); @@ -520,8 +560,7 @@ pub fn parse_packages(build_state: &mut BuildState) { .filter(|module_name| helpers::is_non_exotic_module_name(module_name)) .collect::>(); - let mlmap = - namespaces::gen_mlmap(&package, namespace, &depending_modules, &build_state.project_root); + let mlmap = namespaces::gen_mlmap(&package, namespace, &depending_modules); // mlmap will be compiled in the AST generation step // compile_mlmap(&package, namespace, &project_root); @@ -866,7 +905,7 @@ mod test { source_files: None, namespace: Namespace::Namespace(String::from("Package1")), modules: None, - package_dir: String::from("./something"), + path: String::from("./something"), dirs: None, is_pinned_dep: false, is_root: false, diff --git a/src/build/parse.rs b/src/build/parse.rs index bd704d4..dc54a4a 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -31,24 +31,8 @@ pub fn generate_asts( SourceType::MlMap(_) => { // probably better to do this in a different function // specific to compiling mlmaps - let path = helpers::get_mlmap_path( - &build_state.project_root, - &module.package_name, - &package - .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, - &module.package_name, - &package - .namespace - .to_suffix() - .expect("namespace should be set for mlmap module"), - package.is_root, - ); + let path = package.get_mlmap_path(); + let compile_path = package.get_mlmap_compile_path(); let mlmap_hash = helpers::compute_file_hash(&compile_path); namespaces::compile_mlmap(&package, module_name, &build_state.project_root); let mlmap_hash_after = helpers::compute_file_hash(&compile_path); @@ -137,7 +121,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(package, &err); stderr.push_str(&err); } } @@ -149,7 +133,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(package, &err); has_failure = true; stderr.push_str(&err); } @@ -168,7 +152,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(package, &err); stderr.push_str(&err); } } @@ -184,7 +168,7 @@ pub fn generate_asts( } _ => (), } - logs::append(&build_state.project_root, package.is_root, &package.name, &err); + logs::append(package, &err); has_failure = true; stderr.push_str(&err); } @@ -207,7 +191,7 @@ fn generate_ast( version: &str, ) -> Result<(String, Option), String> { let file = &filename.to_string(); - let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root); + let build_path_abs = package.get_build_path(); let path = PathBuf::from(filename); let ast_extension = path_to_ast_extension(&path); diff --git a/src/build/read_compile_state.rs b/src/build/read_compile_state.rs index 6b13777..0f4f8f2 100644 --- a/src/build/read_compile_state.rs +++ b/src/build/read_compile_state.rs @@ -20,7 +20,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { let package = build_state.packages.get(&module.package_name).unwrap(); Some( - PathBuf::from(&package.package_dir) + PathBuf::from(&package.path) .canonicalize() .expect("Could not canonicalize") .join(source_file.implementation.path.to_owned()) @@ -39,7 +39,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { .filter_map(|module| { let package = build_state.packages.get(&module.package_name).unwrap(); module.get_interface().as_ref().map(|interface| { - PathBuf::from(&package.package_dir) + PathBuf::from(&package.path) .canonicalize() .expect("Could not canonicalize") .join(interface.path.to_owned()) @@ -52,12 +52,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState { // scan all ast files in all packages for package in build_state.packages.values() { - let read_dir = fs::read_dir(std::path::Path::new(&helpers::get_build_path( - &build_state.project_root, - &package.name, - package.is_root, - ))) - .unwrap(); + let read_dir = fs::read_dir(std::path::Path::new(&package.get_build_path())).unwrap(); for entry in read_dir { match entry { diff --git a/src/helpers.rs b/src/helpers.rs index 8eeebc2..6c39436 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -43,34 +43,6 @@ impl LexicalAbsolute for Path { } } -pub fn get_relative_package_path(package_name: &str, is_root: bool) -> String { - match is_root { - true => "".to_string(), - false => format!("node_modules/{}", package_name), - } -} - -pub fn get_build_path(root: &str, package_name: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/lib/ocaml", root), - false => format!("{}/node_modules/{}/lib/ocaml", root, package_name), - } -} - -pub fn get_bs_build_path(root: &str, package_name: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/lib/bs", root), - false => format!("{}/node_modules/{}/lib/bs", root, package_name), - } -} - -pub fn get_path(root: &str, package_name: &str, file: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/{}", root, file), - false => format!("{}/node_modules/{}/{}", root, package_name, file), - } -} - pub fn get_node_modules_path(root: &str) -> String { format!("{}/node_modules", root) } @@ -176,14 +148,12 @@ pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool { } pub fn get_compiler_asset( + package: &packages::Package, source_file: &str, - package_name: &str, namespace: &packages::Namespace, - root_path: &str, extension: &str, - is_root: bool, ) -> String { - get_build_path(root_path, package_name, is_root) + package.get_build_path() + "/" + &file_path_to_compiler_asset_basename(source_file, namespace) + "." @@ -199,11 +169,9 @@ pub fn canonicalize_string_path(path: &str) -> Option { pub fn get_bs_compiler_asset( source_file: &str, - package_name: &str, + package: &packages::Package, namespace: &packages::Namespace, - root_path: &str, extension: &str, - is_root: bool, ) -> String { let namespace = match extension { "ast" | "iast" => &packages::Namespace::NoNamespace, @@ -212,7 +180,7 @@ pub fn get_bs_compiler_asset( let dir = std::path::Path::new(&source_file).parent().unwrap(); - std::path::Path::new(&get_bs_build_path(root_path, &package_name, is_root)) + std::path::Path::new(&package.get_bs_build_path()) .join(dir) .join(file_path_to_compiler_asset_basename(source_file, namespace) + extension) .to_str() @@ -230,36 +198,6 @@ pub fn is_interface_ast_file(file: &str) -> bool { file.ends_with(".iast") } -pub fn get_mlmap_path(root_path: &str, package_name: &str, namespace: &str, is_root: bool) -> String { - get_build_path(root_path, package_name, is_root) + "/" + namespace + ".mlmap" -} - -pub fn get_mlmap_compile_path(root_path: &str, package_name: &str, namespace: &str, is_root: bool) -> String { - get_build_path(root_path, package_name, is_root) + "/" + namespace + ".cmi" -} - -pub fn get_ast_path(source_file: &str, package_name: &str, root_path: &str, is_root: bool) -> String { - get_compiler_asset( - source_file, - package_name, - &packages::Namespace::NoNamespace, - root_path, - "ast", - is_root, - ) -} - -pub fn get_iast_path(source_file: &str, package_name: &str, root_path: &str, is_root: bool) -> String { - get_compiler_asset( - source_file, - package_name, - &packages::Namespace::NoNamespace, - root_path, - "iast", - is_root, - ) -} - pub fn read_lines(filename: String) -> io::Result>> { let file = fs::File::open(filename)?; Ok(io::BufReader::new(file).lines())