From 167c0e57ad88f2e499c92b85ad911f7e137672a9 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sun, 25 Feb 2024 17:54:40 +0400 Subject: [PATCH 1/2] Use package path --- src/build.rs | 6 +- src/build/clean.rs | 103 +++++------------------- src/build/compile.rs | 135 ++++++++------------------------ src/build/deps.rs | 14 +--- src/build/logs.rs | 61 ++++----------- src/build/namespaces.rs | 7 +- src/build/packages.rs | 66 +++++++++++----- src/build/parse.rs | 56 ++----------- src/build/read_compile_state.rs | 11 +-- src/helpers.rs | 56 +------------ 10 files changed, 140 insertions(+), 375 deletions(-) diff --git a/src/build.rs b/src/build.rs index 584f90e..33a58a3 100644 --- a/src/build.rs +++ b/src/build.rs @@ -91,7 +91,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", @@ -158,7 +158,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, @@ -204,7 +204,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 8ee3249..1188935 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_path: &str, root_path: &str, is_root: bool) { +fn remove_ast(package: &packages::Package, source_file: &str) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_path, &packages::Namespace::NoNamespace, - root_path, "ast", - is_root, )); } -fn remove_iast(source_file: &str, package_path: &str, root_path: &str, is_root: bool) { +fn remove_iast(package: &packages::Package, source_file: &str) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_path, &packages::Namespace::NoNamespace, - root_path, "iast", - is_root, )); } @@ -39,50 +35,26 @@ fn remove_mjs_file(source_file: &str, suffix: &String) { )); } -fn remove_compile_asset( - source_file: &str, - package_path: &str, - namespace: &packages::Namespace, - root_path: &str, - is_root: bool, - extension: &str, -) { +fn remove_compile_asset(package: &packages::Package, source_file: &str, extension: &str) { let _ = std::fs::remove_file(helpers::get_compiler_asset( + package, source_file, - package_path, - namespace, - root_path, + &package.namespace, extension, - is_root, )); let _ = std::fs::remove_file(helpers::get_bs_compiler_asset( source_file, - package_path, - namespace, - root_path, + package, + &package.namespace, extension, - is_root, )); } -pub fn remove_compile_assets( - source_file: &str, - package_path: &str, - namespace: &packages::Namespace, - root_path: &str, - is_root: bool, -) { +pub fn remove_compile_assets(package: &packages::Package, source_file: &str) { // optimization // only issue cmti if htere is an interfacce file for extension in &["cmj", "cmi", "cmt", "cmti"] { - remove_compile_asset( - source_file, - package_path, - namespace, - root_path, - is_root, - extension, - ); + remove_compile_asset(package, source_file, extension); } } @@ -99,7 +71,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(), @@ -145,9 +117,7 @@ pub fn cleanup_previous_build( let AstModule { module_name, package_name, - namespace: package_namespace, ast_file_path, - is_root, suffix, .. } = compile_assets_state @@ -159,31 +129,15 @@ pub fn cleanup_previous_build( .packages .get(package_name) .expect("Could not find package"); - remove_compile_assets( - res_file_location, - &package.package_dir, - package_namespace, - &build_state.project_root, - *is_root, - ); + remove_compile_assets(package, res_file_location); remove_mjs_file( &res_file_location, &suffix .to_owned() .unwrap_or(String::from(bsconfig::DEFAULT_SUFFIX)), ); - remove_iast( - res_file_location, - &package.package_dir, - &build_state.project_root, - *is_root, - ); - remove_ast( - res_file_location, - &package.package_dir, - &build_state.project_root, - *is_root, - ); + remove_iast(package, res_file_location); + remove_ast(package, res_file_location); match helpers::get_extension(ast_file_path).as_str() { "iast" => Some(module_name.to_owned()), "ast" => None, @@ -361,18 +315,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, - &package.package_dir, - &build_state.project_root, - package.is_root, - ); - remove_ast( - &source_file.implementation.path, - &package.package_dir, - &build_state.project_root, - package.is_root, - ); + remove_iast(package, &source_file.implementation.path); + remove_ast(package, &source_file.implementation.path); } _ => (), } @@ -386,14 +330,7 @@ pub fn cleanup_after_build(build_state: &BuildState) { // we only clean the cmt (typed tree) here, this will cause the file to be recompiled // (and thus keep showing the warning), but it will keep the cmi file, so that we don't // unecessary mark all the dependents as dirty, when there is no change in the interface - remove_compile_asset( - &source_file.implementation.path, - &package.package_dir, - &package.namespace, - &build_state.project_root, - package.is_root, - "cmt", - ); + remove_compile_asset(package, &source_file.implementation.path, "cmt"); } SourceType::MlMap(_) => (), } @@ -424,11 +361,11 @@ pub fn clean(path: &str) { ); std::io::stdout().flush().unwrap(); - let path_str = helpers::get_build_path(&project_root, &package.package_dir, 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.package_dir, 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 64ab73d..62f1a31 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -143,12 +143,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); @@ -165,14 +163,8 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_iast_path( - &path, - &package.package_dir, - &build_state.project_root, - package.is_root, - ), + &package.get_iast_path(&path), module, - &build_state.project_root, &rescript_version, true, bsc_path, @@ -185,14 +177,8 @@ pub fn compile( let result = compile_file( &package, &root_package, - &helpers::get_ast_path( - &source_file.implementation.path, - &package.package_dir, - &build_state.project_root, - package.is_root, - ), + &package.get_ast_path(&source_file.implementation.path), module, - &build_state.project_root, &rescript_version, false, bsc_path, @@ -292,25 +278,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, - &package.package_dir, - &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, - &package.package_dir, - &err, - ); + logs::append(package, &err); compile_errors.push_str(&err); } }; @@ -318,26 +292,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, - &package.package_dir, - &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, - &package.package_dir, - &err, - ); + logs::append(package, &err); compile_errors.push_str(&err); } _ => (), @@ -380,13 +342,12 @@ fn compile_file( root_package: &packages::Package, ast_path: &str, module: &Module, - root_path: &str, version: &str, is_interface: bool, bsc_path: &str, packages: &AHashMap, ) -> Result, String> { - let build_path_abs = helpers::get_build_path(root_path, &package.package_dir, 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 @@ -412,12 +373,7 @@ fn compile_file( let package = &packages.get(&x).expect("expect package"); vec![ "-I".to_string(), - helpers::canonicalize_string_path(&helpers::get_build_path( - root_path, - &package.package_dir, - package.is_root, - )) - .unwrap(), + helpers::canonicalize_string_path(&package.get_build_path()).unwrap(), ] }) .collect::>>(); @@ -499,7 +455,7 @@ fn compile_file( // TODO: Also read suffix from package-spec. let suffix = match root_package.bsconfig.suffix.to_owned() { Some(suffix) => suffix, - None => String::from(bsconfig::DEFAULT_SUFFIX) + None => String::from(bsconfig::DEFAULT_SUFFIX), }; vec![ @@ -566,50 +522,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.package_dir, - 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.package_dir, - 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.package_dir, - 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.package_dir, - 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 { @@ -625,24 +565,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.package_dir, - 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.package_dir, - 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 151a0de..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, - &package.package_dir, - &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, - &package.package_dir, - &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 5c66018..7a2fa9c 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, package_path: &str, is_root: bool) -> String { +fn get_log_file_path(package: &packages::Package, subfolder: Location) -> String { let build_folder = match subfolder { - Location::Bs => helpers::get_bs_build_path(project_root, package_path, is_root), - Location::Ocaml => helpers::get_build_path(project_root, package_path, is_root), + Location::Bs => package.get_bs_build_path(), + Location::Ocaml => package.get_build_path(), }; build_folder.to_owned() + "/.compiler.log" @@ -44,61 +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, - &package.package_dir, - package.is_root, - )) - .map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time()))) - .expect( - &(format!( - "Cannot create compiler log for package {} in {}", - name, &package.package_dir - ) - .to_owned()), - ); + let _ = File::create(get_log_file_path(package, Location::Bs)) + .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, package_path: &str, str: &str) { +pub fn append(package: &packages::Package, str: &str) { File::options() .append(true) - .open(get_log_file_path( - project_root, - Location::Bs, - package_path, - 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, package_path, is_root)), - ); + .open(get_log_file_path(package, Location::Bs)) + .map(|file| write_to_log_file(file, &package.name, str)) + .expect(&("Cannot write compilerlog: ".to_owned() + &get_log_file_path(package, Location::Bs))); } -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, - &package.package_dir, - package.is_root, - )) + .open(get_log_file_path(package, Location::Bs)) .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, &package.package_dir, package.is_root), - get_log_file_path( - project_root, - Location::Ocaml, - &package.package_dir, - package.is_root, - ), + get_log_file_path(package, Location::Bs), + get_log_file_path(package, Location::Ocaml), ); }) } diff --git a/src/build/namespaces.rs b/src/build/namespaces.rs index 7f40035..394b582 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.package_dir, 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. @@ -50,8 +49,8 @@ pub fn gen_mlmap( path.to_string() } -pub fn compile_mlmap(package: &packages::Package, namespace: &str, root_path: &str, bsc_path: &str) { - let build_path_abs = helpers::get_build_path(root_path, &package.package_dir, package.is_root); +pub fn compile_mlmap(package: &packages::Package, namespace: &str, bsc_path: &str) { + 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 9eb7ea0..3f5f90b 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -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 @@ -275,7 +313,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) => { @@ -327,7 +365,7 @@ fn make_package(bsconfig: bsconfig::T, package_dir: &str, is_pinned_dep: bool, i }, }, modules: None, - package_dir: package_dir.to_string(), + path: package_path.to_string(), dirs: None, is_pinned_dep: is_pinned_dep, is_root, @@ -424,7 +462,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)); @@ -475,14 +513,7 @@ pub fn make( .into_iter() .for_each(|package| match &package.dirs { Some(dirs) => dirs.iter().for_each(|dir| { - let _ = std::fs::create_dir_all( - std::path::Path::new(&helpers::get_bs_build_path( - root_folder, - &package.package_dir, - package.is_root, - )) - .join(dir), - ); + let _ = std::fs::create_dir_all(std::path::Path::new(&package.get_bs_build_path()).join(dir)); }), None => (), }); @@ -505,10 +536,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.package_dir, package.is_root); - let bs_build_path = - helpers::get_bs_build_path(&build_state.project_root, &package.package_dir, 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); @@ -539,8 +568,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); @@ -885,7 +913,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 f995863..fe9db05 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -33,26 +33,10 @@ 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, - &package.package_dir, - &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, - &package.package_dir, - &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, bsc_path); + namespaces::compile_mlmap(&package, module_name, bsc_path); let mlmap_hash_after = helpers::compute_file_hash(&compile_path); let is_dirty = match (mlmap_hash, mlmap_hash_after) { @@ -143,13 +127,7 @@ pub fn generate_asts( } _ => (), } - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &package.package_dir, - &err, - ); + logs::append(package, &err); stderr.push_str(&err); } } @@ -161,13 +139,7 @@ pub fn generate_asts( } _ => (), } - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &package.package_dir, - &err, - ); + logs::append(package, &err); has_failure = true; stderr.push_str(&err); } @@ -186,13 +158,7 @@ pub fn generate_asts( } _ => (), } - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &package.package_dir, - &err, - ); + logs::append(package, &err); stderr.push_str(&err); } } @@ -208,13 +174,7 @@ pub fn generate_asts( } _ => (), } - logs::append( - &build_state.project_root, - package.is_root, - &package.name, - &package.package_dir, - &err, - ); + logs::append(package, &err); has_failure = true; stderr.push_str(&err); } @@ -239,7 +199,7 @@ fn generate_ast( workspace_root: Option, ) -> Result<(String, Option), String> { let file = &filename.to_string(); - let build_path_abs = helpers::get_build_path(root_path, &package.package_dir, 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 728e9aa..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.package_dir, - 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 e549415..f9680e9 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -50,20 +50,6 @@ pub fn package_path(root: &str, package_name: &str, is_root: bool) -> String { } } -pub fn get_build_path(root: &str, package_dir: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/lib/ocaml", root), - false => format!("{}/lib/ocaml", package_dir), - } -} - -pub fn get_bs_build_path(root: &str, package_path: &str, is_root: bool) -> String { - match is_root { - true => format!("{}/lib/bs", root), - false => format!("{}/lib/bs", package_path), - } -} - pub fn get_abs_path(path: &str) -> String { let abs_path_buf = PathBuf::from(path); @@ -184,14 +170,12 @@ pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool { } pub fn get_compiler_asset( + package: &packages::Package, source_file: &str, - package_path: &str, namespace: &packages::Namespace, - root_path: &str, extension: &str, - is_root: bool, ) -> String { - get_build_path(root_path, package_path, is_root) + package.get_build_path() + "/" + &file_path_to_compiler_asset_basename(source_file, namespace) + "." @@ -207,11 +191,9 @@ pub fn canonicalize_string_path(path: &str) -> Option { pub fn get_bs_compiler_asset( source_file: &str, - package_path: &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, @@ -220,7 +202,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_path, 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() @@ -238,36 +220,6 @@ pub fn is_interface_ast_file(file: &str) -> bool { file.ends_with(".iast") } -pub fn get_mlmap_path(root_path: &str, package_path: &str, namespace: &str, is_root: bool) -> String { - get_build_path(root_path, package_path, is_root) + "/" + namespace + ".mlmap" -} - -pub fn get_mlmap_compile_path(root_path: &str, package_path: &str, namespace: &str, is_root: bool) -> String { - get_build_path(root_path, package_path, is_root) + "/" + namespace + ".cmi" -} - -pub fn get_ast_path(source_file: &str, package_path: &str, root_path: &str, is_root: bool) -> String { - get_compiler_asset( - source_file, - package_path, - &packages::Namespace::NoNamespace, - root_path, - "ast", - is_root, - ) -} - -pub fn get_iast_path(source_file: &str, package_path: &str, root_path: &str, is_root: bool) -> String { - get_compiler_asset( - source_file, - package_path, - &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()) From 7827088323fc4c0b87623b0921e35e13ba890bfe Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sun, 25 Feb 2024 18:01:53 +0400 Subject: [PATCH 2/2] Change args order --- src/build/clean.rs | 8 ++++---- src/build/compile.rs | 2 +- src/build/packages.rs | 4 ++-- src/helpers.rs | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/build/clean.rs b/src/build/clean.rs index 1188935..5c15b0f 100644 --- a/src/build/clean.rs +++ b/src/build/clean.rs @@ -12,8 +12,8 @@ use std::time::Instant; fn remove_ast(package: &packages::Package, source_file: &str) { let _ = std::fs::remove_file(helpers::get_compiler_asset( package, - source_file, &packages::Namespace::NoNamespace, + source_file, "ast", )); } @@ -21,8 +21,8 @@ fn remove_ast(package: &packages::Package, source_file: &str) { fn remove_iast(package: &packages::Package, source_file: &str) { let _ = std::fs::remove_file(helpers::get_compiler_asset( package, - source_file, &packages::Namespace::NoNamespace, + source_file, "iast", )); } @@ -38,14 +38,14 @@ fn remove_mjs_file(source_file: &str, suffix: &String) { fn remove_compile_asset(package: &packages::Package, source_file: &str, extension: &str) { let _ = std::fs::remove_file(helpers::get_compiler_asset( package, - source_file, &package.namespace, + source_file, extension, )); let _ = std::fs::remove_file(helpers::get_bs_compiler_asset( - source_file, package, &package.namespace, + source_file, extension, )); } diff --git a/src/build/compile.rs b/src/build/compile.rs index 62f1a31..0cd4d1a 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -144,8 +144,8 @@ pub fn compile( SourceType::SourceFile(source_file) => { let cmi_path = helpers::get_compiler_asset( package, - &source_file.implementation.path, &package.namespace, + &source_file.implementation.path, "cmi", ); diff --git a/src/build/packages.rs b/src/build/packages.rs index 3f5f90b..99e052a 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -92,11 +92,11 @@ impl Package { } pub fn get_ast_path(&self, source_file: &str) -> String { - helpers::get_compiler_asset(self, source_file, &packages::Namespace::NoNamespace, "ast") + helpers::get_compiler_asset(self, &packages::Namespace::NoNamespace, source_file, "ast") } pub fn get_iast_path(&self, source_file: &str) -> String { - helpers::get_compiler_asset(self, source_file, &packages::Namespace::NoNamespace, "iast") + helpers::get_compiler_asset(self, &packages::Namespace::NoNamespace, source_file, "iast") } } diff --git a/src/helpers.rs b/src/helpers.rs index f9680e9..dab0e4f 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -171,8 +171,8 @@ pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool { pub fn get_compiler_asset( package: &packages::Package, - source_file: &str, namespace: &packages::Namespace, + source_file: &str, extension: &str, ) -> String { package.get_build_path() @@ -190,9 +190,9 @@ pub fn canonicalize_string_path(path: &str) -> Option { } pub fn get_bs_compiler_asset( - source_file: &str, package: &packages::Package, namespace: &packages::Namespace, + source_file: &str, extension: &str, ) -> String { let namespace = match extension {