From c12b30532ca705b8fc4cabfda4c399dbdd10a43b Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Thu, 11 Apr 2024 10:08:38 +0200 Subject: [PATCH] :sparkles: - Incremental compilation support --- lib/rewatch.lock | 1 + src/bsconfig.rs | 117 ++++++++++++++++++++++- src/build.rs | 36 +++---- src/build/compile.rs | 77 ++++++++------- src/build/packages.rs | 211 ++++++++++++++++-------------------------- src/build/parse.rs | 30 +++--- src/lock.rs | 2 +- src/main.rs | 5 +- 8 files changed, 278 insertions(+), 201 deletions(-) create mode 100644 lib/rewatch.lock diff --git a/lib/rewatch.lock b/lib/rewatch.lock new file mode 100644 index 0000000..fcac671 --- /dev/null +++ b/lib/rewatch.lock @@ -0,0 +1 @@ +81175 \ No newline at end of file diff --git a/src/bsconfig.rs b/src/bsconfig.rs index 38beb22..600de81 100644 --- a/src/bsconfig.rs +++ b/src/bsconfig.rs @@ -1,3 +1,5 @@ +use crate::build::packages; +use convert_case::{Case, Casing}; use serde::Deserialize; use std::fs; use std::path::{Path, PathBuf}; @@ -104,7 +106,7 @@ pub struct Reason { #[derive(Deserialize, Debug, Clone)] #[serde(untagged)] -pub enum Namespace { +pub enum NamespaceConfig { Bool(bool), String(String), } @@ -135,7 +137,7 @@ pub struct JsxSpecs { /// # bsconfig.json representation /// This is tricky, there is a lot of ambiguity. This is probably incomplete. #[derive(Deserialize, Debug, Clone)] -pub struct T { +pub struct Config { pub name: String, pub sources: OneOrMore, #[serde(rename = "package-specs")] @@ -153,7 +155,7 @@ pub struct T { #[serde(rename = "bsc-flags")] pub bsc_flags: Option>>, pub reason: Option, - pub namespace: Option, + pub namespace: Option, pub jsx: Option, pub uncurried: Option, // this is a new feature of rewatch, and it's not part of the bsconfig.json spec @@ -231,11 +233,116 @@ pub fn flatten_ppx_flags( } /// Try to convert a bsconfig from a certain path to a bsconfig struct -pub fn read(path: String) -> T { +pub fn read(path: String) -> Config { fs::read_to_string(path.clone()) .map_err(|e| format!("Could not read bsconfig. {path} - {e}")) .and_then(|x| { - serde_json::from_str::(&x).map_err(|e| format!("Could not parse bsconfig. {path} - {e}")) + serde_json::from_str::(&x).map_err(|e| format!("Could not parse bsconfig. {path} - {e}")) }) .expect("Errors reading bsconfig") } + +fn check_if_rescript11_or_higher(version: &str) -> bool { + version.split(".").nth(0).unwrap().parse::().unwrap() >= 11 +} + +fn namespace_from_package_name(package_name: &str) -> String { + package_name + .to_owned() + .replace("@", "") + .replace("/", "_") + .to_case(Case::Pascal) +} + +impl Config { + pub fn get_namespace(&self) -> packages::Namespace { + let namespace_from_package = namespace_from_package_name(&self.name); + match (self.namespace.as_ref(), self.namespace_entry.as_ref()) { + (Some(NamespaceConfig::Bool(false)), _) => packages::Namespace::NoNamespace, + (None, _) => packages::Namespace::NoNamespace, + (Some(NamespaceConfig::Bool(true)), None) => { + packages::Namespace::Namespace(namespace_from_package) + } + (Some(NamespaceConfig::Bool(true)), Some(entry)) => packages::Namespace::NamespaceWithEntry { + namespace: namespace_from_package, + entry: entry.to_string(), + }, + (Some(NamespaceConfig::String(str)), None) => match str.as_str() { + "true" => packages::Namespace::Namespace(namespace_from_package), + namespace if namespace.is_case(Case::UpperFlat) => { + packages::Namespace::Namespace(namespace.to_string()) + } + namespace => packages::Namespace::Namespace(namespace.to_string().to_case(Case::Pascal)), + }, + (Some(self::NamespaceConfig::String(str)), Some(entry)) => match str.as_str() { + "true" => packages::Namespace::NamespaceWithEntry { + namespace: namespace_from_package, + entry: entry.to_string(), + }, + namespace if namespace.is_case(Case::UpperFlat) => packages::Namespace::NamespaceWithEntry { + namespace: namespace.to_string(), + entry: entry.to_string(), + }, + namespace => packages::Namespace::NamespaceWithEntry { + namespace: namespace.to_string().to_case(Case::Pascal), + entry: entry.to_string(), + }, + }, + } + } + pub fn get_jsx_args(&self) -> Vec { + match (self.reason.to_owned(), self.jsx.to_owned()) { + (_, Some(jsx)) => match jsx.version { + Some(version) if version == 3 || version == 4 => { + vec!["-bs-jsx".to_string(), version.to_string()] + } + Some(_version) => panic!("Unsupported JSX version"), + None => vec![], + }, + (Some(reason), None) => { + vec!["-bs-jsx".to_string(), format!("{}", reason.react_jsx)] + } + _ => vec![], + } + } + + pub fn get_jsx_mode_args(&self) -> Vec { + match self.jsx.to_owned() { + Some(jsx) => match jsx.mode { + Some(JsxMode::Classic) => { + vec!["-bs-jsx-mode".to_string(), "classic".to_string()] + } + Some(JsxMode::Automatic) => { + vec!["-bs-jsx-mode".to_string(), "automatic".to_string()] + } + + None => vec![], + }, + _ => vec![], + } + } + + pub fn get_jsx_module_args(&self) -> Vec { + match self.jsx.to_owned() { + Some(jsx) => match jsx.module { + Some(JsxModule::React) => { + vec!["-bs-jsx-module".to_string(), "react".to_string()] + } + None => vec![], + }, + _ => vec![], + } + } + + pub fn get_uncurried_args(&self, version: &str) -> Vec { + if check_if_rescript11_or_higher(version) { + match self.uncurried.to_owned() { + // v11 is always uncurried except iff explicitly set to false in the root rescript.json + Some(false) => vec![], + _ => vec!["-uncurried".to_string()], + } + } else { + vec![] + } + } +} diff --git a/src/build.rs b/src/build.rs index 90ac962..0cd1eb5 100644 --- a/src/build.rs +++ b/src/build.rs @@ -50,36 +50,34 @@ pub struct CompilerArgs { pub parser_args: Vec, } -pub fn get_compiler_args(path: &str) -> String { +pub fn get_compiler_args(path: &str, rescript_version: Option) -> String { let filename = &helpers::get_abs_path(path); let package_root = helpers::get_abs_path( &helpers::get_nearest_bsconfig(&std::path::PathBuf::from(path)).expect("Couldn't find package root"), ); let workspace_root = get_workspace_root(&package_root).map(|p| helpers::get_abs_path(&p)); - let root_config_name = - packages::get_package_name(&workspace_root.to_owned().unwrap_or(package_root.to_owned())); - let package_name = packages::get_package_name(&package_root); - let bsc_path = helpers::get_bsc(&package_root, workspace_root.to_owned()); - let rescript_version = helpers::get_rescript_version(&bsc_path); - let packages = packages::make( - &None, - &workspace_root.to_owned().unwrap_or(package_root.to_owned()), - &workspace_root, - ); + let root_rescript_config = + packages::read_bsconfig(&workspace_root.to_owned().unwrap_or(package_root.to_owned())); + let rescript_config = packages::read_bsconfig(&package_root); + let rescript_version = if let Some(rescript_version) = rescript_version { + rescript_version + } else { + let bsc_path = helpers::get_bsc(&package_root, workspace_root.to_owned()); + helpers::get_rescript_version(&bsc_path) + }; // make PathBuf from package root and get the relative path for filename let relative_filename = PathBuf::from(&filename) .strip_prefix(PathBuf::from(&package_root).parent().unwrap()) .unwrap() .to_string_lossy() .to_string(); - let root_package = packages.get(&root_config_name).unwrap(); - let package = packages.get(&package_name).unwrap(); let (ast_path, parser_args) = parser_args( - package, - root_package, + &rescript_config, + &root_rescript_config, &relative_filename, &rescript_version, &workspace_root, + workspace_root.as_ref().unwrap_or(&package_root), ); let is_interface = filename.ends_with("i"); let has_interface = if is_interface { @@ -90,14 +88,16 @@ pub fn get_compiler_args(path: &str) -> String { PathBuf::from(&interface_filename).exists() }; let compiler_args = compiler_args( - package, - root_package, + &rescript_config, + &root_rescript_config, &ast_path, &rescript_version, &relative_filename, is_interface, has_interface, - &packages, + &package_root, + &workspace_root, + &None, ); serde_json::to_string_pretty(&CompilerArgs { compiler_args, diff --git a/src/build/compile.rs b/src/build/compile.rs index f32af76..d1f95c4 100644 --- a/src/build/compile.rs +++ b/src/build/compile.rs @@ -164,6 +164,8 @@ pub fn compile( true, &build_state.bsc_path, &build_state.packages, + &build_state.project_root, + &build_state.workspace_root, ); Some(result) } @@ -178,6 +180,8 @@ pub fn compile( false, &build_state.bsc_path, &build_state.packages, + &build_state.project_root, + &build_state.workspace_root, ); // if let Err(error) = result.to_owned() { // println!("{}", error); @@ -358,23 +362,22 @@ pub fn compile( } pub fn compiler_args( - package: &packages::Package, - root_package: &packages::Package, + config: &bsconfig::Config, + root_config: &bsconfig::Config, ast_path: &str, version: &str, file_path: &str, is_interface: bool, has_interface: bool, - packages: &AHashMap, + project_root: &str, + workspace_root: &Option, + // if packages are known, we pass a reference here + // this saves us a scan to find their paths + packages: &Option<&AHashMap>, ) -> Vec { - let normal_deps = package - .bsconfig - .bs_dependencies - .as_ref() - .unwrap_or(&vec![]) - .to_owned(); - - let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags); + let normal_deps = config.bs_dependencies.as_ref().unwrap_or(&vec![]).to_owned(); + + let bsc_flags = bsconfig::flatten_flags(&config.bsc_flags); // don't compile dev-deps yet // let dev_deps = source // .package @@ -386,24 +389,30 @@ pub fn compiler_args( let deps = vec![normal_deps] .concat() - .into_iter() - .map(|x| { - let package = &packages.get(&x).expect("expect package"); - vec![ - "-I".to_string(), - helpers::canonicalize_string_path(&package.get_build_path()).unwrap(), - ] + .par_iter() + .map(|package_name| { + let canonicalized_path = if let Some(packages) = packages { + packages + .get(package_name) + .expect("expect package") + .path + .to_string() + } else { + packages::read_dependency(package_name, project_root, project_root, workspace_root) + .expect("cannot find dep") + }; + vec!["-I".to_string(), packages::get_build_path(&canonicalized_path)] }) .collect::>>(); - let module_name = helpers::file_path_to_module_name(file_path, &package.namespace); + let module_name = helpers::file_path_to_module_name(file_path, &config.get_namespace()); - let namespace_args = match &package.namespace { + let namespace_args = match &config.get_namespace() { packages::Namespace::NamespaceWithEntry { namespace: _, entry } if &module_name == entry => { // if the module is the entry we just want to open the namespace vec![ "-open".to_string(), - package.namespace.to_suffix().unwrap().to_string(), + config.get_namespace().to_suffix().unwrap().to_string(), ] } packages::Namespace::Namespace(_) @@ -413,18 +422,18 @@ pub fn compiler_args( } => { vec![ "-bs-ns".to_string(), - package.namespace.to_suffix().unwrap().to_string(), + config.get_namespace().to_suffix().unwrap().to_string(), ] } packages::Namespace::NoNamespace => vec![], }; - let jsx_args = root_package.get_jsx_args(); - let jsx_module_args = root_package.get_jsx_module_args(); - let jsx_mode_args = root_package.get_jsx_mode_args(); - let uncurried_args = package.get_uncurried_args(version, &root_package); + let jsx_args = root_config.get_jsx_args(); + let jsx_module_args = root_config.get_jsx_module_args(); + let jsx_mode_args = root_config.get_jsx_mode_args(); + let uncurried_args = root_config.get_uncurried_args(version); - let warning_args: Vec = match package.bsconfig.warnings.to_owned() { + let warning_args: Vec = match config.warnings.to_owned() { None => vec![], Some(warnings) => { let warn_number = match warnings.number { @@ -466,14 +475,14 @@ pub fn compiler_args( debug!("Compiling file: {}", &module_name); // TODO: Also read suffix from package-spec. - let suffix = match root_package.bsconfig.suffix.to_owned() { + let suffix = match root_config.suffix.to_owned() { Some(suffix) => suffix, None => String::from(bsconfig::DEFAULT_SUFFIX), }; vec![ "-bs-package-name".to_string(), - package.bsconfig.name.to_owned(), + config.name.to_owned(), "-bs-package-output".to_string(), format!( "es6:{}:{}", @@ -520,6 +529,8 @@ fn compile_file( is_interface: bool, bsc_path: &str, packages: &AHashMap, + project_root: &str, + workspace_root: &Option, ) -> Result, String> { let build_path_abs = package.get_build_path(); let implementation_file_path = match module.source_type { @@ -529,14 +540,16 @@ fn compile_file( let module_name = helpers::file_path_to_module_name(implementation_file_path, &package.namespace); let has_interface = module.get_interface().is_some(); let to_mjs_args = compiler_args( - package, - root_package, + &package.bsconfig, + &root_package.bsconfig, ast_path, version, &implementation_file_path, is_interface, has_interface, - packages, + project_root, + workspace_root, + &Some(packages), ); let to_mjs = Command::new(bsc_path) diff --git a/src/build/packages.rs b/src/build/packages.rs index 52e8ed2..a12c349 100644 --- a/src/build/packages.rs +++ b/src/build/packages.rs @@ -6,7 +6,6 @@ use crate::helpers; use crate::helpers::emojis::*; use ahash::{AHashMap, AHashSet}; use console::style; -use convert_case::{Case, Casing}; use log::{debug, error}; use rayon::prelude::*; use std::error; @@ -40,7 +39,7 @@ impl Namespace { #[derive(Debug, Clone)] struct Dependency { name: String, - bsconfig: bsconfig::T, + bsconfig: bsconfig::Config, path: String, is_pinned: bool, dependencies: Vec, @@ -49,7 +48,7 @@ struct Dependency { #[derive(Debug, Clone)] pub struct Package { pub name: String, - pub bsconfig: bsconfig::T, + pub bsconfig: bsconfig::Config, pub source_folders: AHashSet, // these are the relative file paths (relative to the package root) pub source_files: Option>, @@ -62,13 +61,17 @@ pub struct Package { pub is_root: bool, } +pub fn get_build_path(canonical_path: &str) -> String { + format!("{}/lib/ocaml", canonical_path) +} + 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) + get_build_path(&self.path) } pub fn get_mlmap_path(&self) -> String { @@ -211,7 +214,7 @@ fn get_source_dirs(source: bsconfig::Source, sub_path: Option) -> AHash source_folders } -fn read_bsconfig(package_dir: &str) -> bsconfig::T { +pub fn read_bsconfig(package_dir: &str) -> bsconfig::Config { let prefix = if package_dir == "" { "".to_string() } else { @@ -228,6 +231,49 @@ fn read_bsconfig(package_dir: &str) -> bsconfig::T { } } +pub fn read_dependency( + package_name: &str, + parent_path: &str, + project_root: &str, + workspace_root: &Option, +) -> Result { + let path_from_parent = PathBuf::from(helpers::package_path(parent_path, package_name)); + let path_from_project_root = PathBuf::from(helpers::package_path(project_root, package_name)); + let maybe_path_from_workspace_root = workspace_root + .as_ref() + .map(|workspace_root| PathBuf::from(helpers::package_path(&workspace_root, package_name))); + + let path = match ( + path_from_parent, + path_from_project_root, + maybe_path_from_workspace_root, + ) { + (path_from_parent, _, _) if path_from_parent.exists() => Ok(path_from_parent), + (_, path_from_project_root, _) if path_from_project_root.exists() => Ok(path_from_project_root), + (_, _, Some(path_from_workspace_root)) if path_from_workspace_root.exists() => { + Ok(path_from_workspace_root) + } + _ => Err(format!( + "The package \"{}\" is not found (are node_modules up-to-date?)...", + package_name + )), + }?; + + let canonical_path = match path.canonicalize() { + Ok(canonical_path) => Ok(canonical_path.to_string_lossy().to_string()), + Err(e) => { + Err(format!( + "Failed canonicalizing the package \"{}\" path \"{}\" (are node_modules up-to-date?)...\nMore details: {}", + package_name, + path.to_string_lossy(), + e.to_string() + )) + } + }?; + + Ok(canonical_path) +} + /// # Make Package /// Given a bsconfig, reqursively finds all dependencies. @@ -238,7 +284,7 @@ fn read_bsconfig(package_dir: &str) -> bsconfig::T { /// recursively continues operation for their dependencies as well. fn read_dependencies<'a>( registered_dependencies_set: &'a mut AHashSet, - parent_bsconfig: &bsconfig::T, + parent_bsconfig: &bsconfig::Config, parent_path: &str, project_root: &str, workspace_root: Option, @@ -260,43 +306,19 @@ fn read_dependencies<'a>( // Read all bsconfig files in parallel instead of blocking .par_iter() .map(|package_name| { - let path_from_parent = PathBuf::from(helpers::package_path(parent_path, package_name)); - let path_from_project_root = PathBuf::from(helpers::package_path(project_root, package_name)); - let maybe_path_from_workspace_root = workspace_root.as_ref().map(|workspace_root| { - PathBuf::from(helpers::package_path(&workspace_root, package_name)) - }); - - let path = match (path_from_parent, path_from_project_root, maybe_path_from_workspace_root) { - (path_from_parent, _, _) if path_from_parent.exists() => path_from_parent, - (_, path_from_project_root, _) if path_from_project_root.exists() => path_from_project_root, - (_, _, Some(path_from_workspace_root)) if path_from_workspace_root.exists() => path_from_workspace_root, - _ => { - print!( - "{} {} Error building package tree. The package \"{}\" is not found (are node_modules up-to-date?)...", - style("[1/2]").bold().dim(), - CROSS, - package_name - ); - std::process::exit(2) - } - }; - - let canonical_path = match path.canonicalize() { - Ok(canonical_path) => canonical_path.to_string_lossy().to_string(), - Err(e) => { - print!( - "{} {} Error building package tree. Failed canonicalizing the package \"{}\" path \"{}\" (are node_modules up-to-date?)...\nMore details: {}", - style("[1/2]").bold().dim(), - CROSS, - package_name, - path.to_string_lossy(), - e.to_string() - ); - std::process::exit(2) - } - }; - - let bsconfig = read_bsconfig(&canonical_path); + let (bsconfig, canonical_path) = + match read_dependency(package_name, parent_path, project_root, &workspace_root) { + Err(error) => { + print!( + "{} {} Error building package tree. {}", + style("[1/2]").bold().dim(), + CROSS, + error + ); + std::process::exit(2) + } + Ok(canonical_path) => (read_bsconfig(&canonical_path), canonical_path), + }; let is_pinned = parent_bsconfig .pinned_dependencies .as_ref() @@ -332,7 +354,12 @@ fn flatten_dependencies(dependencies: Vec) -> Vec { flattened } -fn make_package(bsconfig: bsconfig::T, package_path: &str, is_pinned_dep: bool, is_root: bool) -> Package { +fn make_package( + bsconfig: bsconfig::Config, + 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) => { @@ -347,42 +374,12 @@ fn make_package(bsconfig: bsconfig::T, package_path: &str, is_pinned_dep: bool, } }; - let namespace_from_package = namespace_from_package_name(&bsconfig.name); Package { name: bsconfig.name.to_owned(), bsconfig: bsconfig.to_owned(), source_folders, source_files: None, - namespace: match (bsconfig.namespace, bsconfig.namespace_entry) { - (Some(bsconfig::Namespace::Bool(false)), _) => Namespace::NoNamespace, - (None, _) => Namespace::NoNamespace, - (Some(bsconfig::Namespace::Bool(true)), None) => Namespace::Namespace(namespace_from_package), - (Some(bsconfig::Namespace::Bool(true)), Some(entry)) => Namespace::NamespaceWithEntry { - namespace: namespace_from_package, - entry: entry, - }, - (Some(bsconfig::Namespace::String(str)), None) => match str.as_str() { - "true" => Namespace::Namespace(namespace_from_package), - namespace if namespace.is_case(Case::UpperFlat) => { - Namespace::Namespace(namespace.to_string()) - } - namespace => Namespace::Namespace(namespace.to_string().to_case(Case::Pascal)), - }, - (Some(bsconfig::Namespace::String(str)), Some(entry)) => match str.as_str() { - "true" => Namespace::NamespaceWithEntry { - namespace: namespace_from_package, - entry, - }, - namespace if namespace.is_case(Case::UpperFlat) => Namespace::NamespaceWithEntry { - namespace: namespace.to_string(), - entry: entry, - }, - namespace => Namespace::NamespaceWithEntry { - namespace: namespace.to_string().to_case(Case::Pascal), - entry, - }, - }, - }, + namespace: bsconfig.get_namespace(), modules: None, // we canonicalize the path name so it's always the same path: PathBuf::from(package_path) @@ -391,7 +388,7 @@ fn make_package(bsconfig: bsconfig::T, package_path: &str, is_pinned_dep: bool, .to_string_lossy() .to_string(), dirs: None, - is_pinned_dep: is_pinned_dep, + is_pinned_dep, is_root, } } @@ -468,14 +465,6 @@ pub fn get_source_files( map } -pub fn namespace_from_package_name(package_name: &str) -> String { - package_name - .to_owned() - .replace("@", "") - .replace("/", "_") - .to_case(Case::Pascal) -} - /// This takes the tree of packages, and finds all the source files for each, adding them to the /// respective packages. fn extend_with_children( @@ -736,65 +725,21 @@ pub fn parse_packages(build_state: &mut BuildState) { }); } -fn check_if_rescript11_or_higher(version: &str) -> bool { - version.split(".").nth(0).unwrap().parse::().unwrap() >= 11 -} - impl Package { pub fn get_jsx_args(&self) -> Vec { - match (self.bsconfig.reason.to_owned(), self.bsconfig.jsx.to_owned()) { - (_, Some(jsx)) => match jsx.version { - Some(version) if version == 3 || version == 4 => { - vec!["-bs-jsx".to_string(), version.to_string()] - } - Some(_version) => panic!("Unsupported JSX version"), - None => vec![], - }, - (Some(reason), None) => { - vec!["-bs-jsx".to_string(), format!("{}", reason.react_jsx)] - } - _ => vec![], - } + self.bsconfig.get_jsx_args() } pub fn get_jsx_mode_args(&self) -> Vec { - match self.bsconfig.jsx.to_owned() { - Some(jsx) => match jsx.mode { - Some(bsconfig::JsxMode::Classic) => { - vec!["-bs-jsx-mode".to_string(), "classic".to_string()] - } - Some(bsconfig::JsxMode::Automatic) => { - vec!["-bs-jsx-mode".to_string(), "automatic".to_string()] - } - - None => vec![], - }, - _ => vec![], - } + self.bsconfig.get_jsx_mode_args() } pub fn get_jsx_module_args(&self) -> Vec { - match self.bsconfig.jsx.to_owned() { - Some(jsx) => match jsx.module { - Some(bsconfig::JsxModule::React) => { - vec!["-bs-jsx-module".to_string(), "react".to_string()] - } - None => vec![], - }, - _ => vec![], - } + self.bsconfig.get_jsx_module_args() } pub fn get_uncurried_args(&self, version: &str, root_package: &packages::Package) -> Vec { - if check_if_rescript11_or_higher(version) { - match root_package.bsconfig.uncurried.to_owned() { - // v11 is always uncurried except iff explicitly set to false in the root rescript.json - Some(false) => vec![], - _ => vec!["-uncurried".to_string()], - } - } else { - vec![] - } + root_package.bsconfig.get_uncurried_args(version) } } @@ -908,7 +853,7 @@ mod test { ) -> Package { return Package { name: name.clone(), - bsconfig: crate::bsconfig::T { + bsconfig: crate::bsconfig::Config { name: name.clone(), sources: crate::bsconfig::OneOrMore::Single(Source::Shorthand(String::from("Source"))), package_specs: None, diff --git a/src/build/parse.rs b/src/build/parse.rs index ee22d12..c52bacd 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -234,11 +234,12 @@ pub fn generate_asts( } pub fn parser_args( - package: &packages::Package, - root_package: &packages::Package, + config: &bsconfig::Config, + root_config: &bsconfig::Config, filename: &str, version: &str, workspace_root: &Option, + root_path: &str, ) -> (String, Vec) { let file = &filename.to_string(); let path = PathBuf::from(filename); @@ -248,16 +249,16 @@ pub fn parser_args( &if let Some(workspace_root) = workspace_root { format!("{}/node_modules", &workspace_root) } else { - format!("{}/node_modules", &root_package.path) + format!("{}/node_modules", &root_path) }, - &filter_ppx_flags(&package.bsconfig.ppx_flags), - &package.name, + &filter_ppx_flags(&config.ppx_flags), + &config.name, ); - let jsx_args = root_package.get_jsx_args(); - let jsx_module_args = root_package.get_jsx_module_args(); - let jsx_mode_args = root_package.get_jsx_mode_args(); - let uncurried_args = root_package.get_uncurried_args(version, &root_package); - let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags); + let jsx_args = root_config.get_jsx_args(); + let jsx_module_args = root_config.get_jsx_module_args(); + let jsx_mode_args = root_config.get_jsx_mode_args(); + let uncurried_args = root_config.get_uncurried_args(version); + let bsc_flags = bsconfig::flatten_flags(&config.bsc_flags); let file = "../../".to_string() + file; ( @@ -291,7 +292,14 @@ fn generate_ast( workspace_root: &Option, ) -> Result<(String, Option), String> { let build_path_abs = package.get_build_path(); - let (ast_path, parser_args) = parser_args(&package, &root_package, filename, version, workspace_root); + let (ast_path, parser_args) = parser_args( + &package.bsconfig, + &root_package.bsconfig, + filename, + version, + workspace_root, + &root_package.path, + ); /* Create .ast */ if let Some(res_to_ast) = Some( diff --git a/src/lock.rs b/src/lock.rs index f9b00b8..e0217ce 100644 --- a/src/lock.rs +++ b/src/lock.rs @@ -1,6 +1,5 @@ use std::fs; use std::fs::File; -use std::io::prelude::*; use std::path::Path; use std::process; use sysinfo::{PidExt, System, SystemExt}; @@ -43,6 +42,7 @@ fn exists(to_check_pid: u32) -> bool { } fn create(lockfile_location: &Path, pid: u32) -> Lock { + use std::io::prelude::*; // Create /lib if not exists match lockfile_location .parent() diff --git a/src/main.rs b/src/main.rs index c6d470b..3d6f8e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,9 @@ struct Args { #[arg(long)] compiler_args: Option, + + #[arg(long)] + rescript_version: Option, } fn main() { @@ -63,7 +66,7 @@ fn main() { match args.compiler_args { None => (), Some(path) => { - println!("{}", build::get_compiler_args(&path)); + println!("{}", build::get_compiler_args(&path, args.rescript_version)); std::process::exit(0); } }