diff --git a/src/build.rs b/src/build.rs index d9c2d63..345f403 100644 --- a/src/build.rs +++ b/src/build.rs @@ -72,6 +72,10 @@ pub fn get_compiler_args(path: &str, rescript_version: Option) -> String .unwrap() .to_string_lossy() .to_string(); + + let file_path = PathBuf::from(&package_root).join(filename); + let contents = helpers::read_file(&file_path).expect("Error reading file"); + let (ast_path, parser_args) = parser_args( &rescript_config, &root_rescript_config, @@ -79,6 +83,7 @@ pub fn get_compiler_args(path: &str, rescript_version: Option) -> String &rescript_version, &workspace_root, workspace_root.as_ref().unwrap_or(&package_root), + &contents, ); let is_interface = filename.ends_with('i'); let has_interface = if is_interface { diff --git a/src/build/parse.rs b/src/build/parse.rs index 03fb508..76b053a 100644 --- a/src/build/parse.rs +++ b/src/build/parse.rs @@ -212,6 +212,7 @@ pub fn parser_args( version: &str, workspace_root: &Option, root_path: &str, + contents: &str, ) -> (String, Vec) { let file = &filename.to_string(); let path = PathBuf::from(filename); @@ -223,7 +224,7 @@ pub fn parser_args( } else { format!("{}/node_modules", &root_path) }, - &filter_ppx_flags(&config.ppx_flags), + &filter_ppx_flags(&config.ppx_flags, contents), &config.name, ); let jsx_args = root_config.get_jsx_args(); @@ -263,6 +264,9 @@ fn generate_ast( bsc_path: &str, workspace_root: &Option, ) -> Result<(String, Option), String> { + let file_path = PathBuf::from(&package.path).join(filename); + let contents = helpers::read_file(&file_path).expect("Error reading file"); + let build_path_abs = package.get_build_path(); let (ast_path, parser_args) = parser_args( &package.bsconfig, @@ -271,6 +275,7 @@ fn generate_ast( version, workspace_root, &root_package.path, + &contents, ); /* Create .ast */ @@ -309,19 +314,28 @@ fn path_to_ast_extension(path: &Path) -> &str { } } -fn filter_ppx_flags(ppx_flags: &Option>>) -> Option>> { +fn include_ppx(flag: &str, contents: &str) -> bool { + if flag.contains("bisect") { + return std::env::var("BISECT_ENABLE").is_ok(); + } else if flag.contains("graphql-ppx") && !contents.contains("%graphql") { + return false; + } else if flag.contains("spice") && !contents.contains("@spice") { + return false; + } + return true; +} + +fn filter_ppx_flags( + ppx_flags: &Option>>, + contents: &str, +) -> Option>> { // get the environment variable "BISECT_ENABLE" if it exists set the filter to "bisect" - let filter = match std::env::var("BISECT_ENABLE") { - Ok(_) => None, - Err(_) => Some("bisect"), - }; ppx_flags.as_ref().map(|flags| { flags .iter() - .filter(|flag| match (flag, filter) { - (bsconfig::OneOrMore::Single(str), Some(filter)) => !str.contains(filter), - (bsconfig::OneOrMore::Multiple(str), Some(filter)) => !str.first().unwrap().contains(filter), - _ => true, + .filter(|flag| match flag { + bsconfig::OneOrMore::Single(str) => include_ppx(str, contents), + bsconfig::OneOrMore::Multiple(str) => include_ppx(str.first().unwrap(), contents), }) .map(|x| x.to_owned()) .collect::>>() diff --git a/src/helpers.rs b/src/helpers.rs index fe2434e..56765e8 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,6 +1,8 @@ use crate::build::packages; use std::ffi::OsString; use std::fs; +use std::fs::File; +use std::io::Read; use std::io::{self, BufRead}; use std::path::{Component, Path, PathBuf}; use std::process::Command; @@ -320,3 +322,10 @@ pub fn get_rescript_version(bsc_path: &str) -> String { .replace('\n', "") .replace("ReScript ", "") } + +pub fn read_file(path: &Path) -> Result { + let mut file = File::open(path).expect("file not found"); + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) +}