Skip to content

Commit b244400

Browse files
authored
✨ - Selectively run ppx's (#103)
1 parent a8ba799 commit b244400

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@ pub fn get_compiler_args(path: &str, rescript_version: Option<String>) -> String
7272
.unwrap()
7373
.to_string_lossy()
7474
.to_string();
75+
76+
let file_path = PathBuf::from(&package_root).join(filename);
77+
let contents = helpers::read_file(&file_path).expect("Error reading file");
78+
7579
let (ast_path, parser_args) = parser_args(
7680
&rescript_config,
7781
&root_rescript_config,
7882
&relative_filename,
7983
&rescript_version,
8084
&workspace_root,
8185
workspace_root.as_ref().unwrap_or(&package_root),
86+
&contents,
8287
);
8388
let is_interface = filename.ends_with('i');
8489
let has_interface = if is_interface {

src/build/parse.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub fn parser_args(
212212
version: &str,
213213
workspace_root: &Option<String>,
214214
root_path: &str,
215+
contents: &str,
215216
) -> (String, Vec<String>) {
216217
let file = &filename.to_string();
217218
let path = PathBuf::from(filename);
@@ -223,7 +224,7 @@ pub fn parser_args(
223224
} else {
224225
format!("{}/node_modules", &root_path)
225226
},
226-
&filter_ppx_flags(&config.ppx_flags),
227+
&filter_ppx_flags(&config.ppx_flags, contents),
227228
&config.name,
228229
);
229230
let jsx_args = root_config.get_jsx_args();
@@ -263,6 +264,9 @@ fn generate_ast(
263264
bsc_path: &str,
264265
workspace_root: &Option<String>,
265266
) -> Result<(String, Option<String>), String> {
267+
let file_path = PathBuf::from(&package.path).join(filename);
268+
let contents = helpers::read_file(&file_path).expect("Error reading file");
269+
266270
let build_path_abs = package.get_build_path();
267271
let (ast_path, parser_args) = parser_args(
268272
&package.bsconfig,
@@ -271,6 +275,7 @@ fn generate_ast(
271275
version,
272276
workspace_root,
273277
&root_package.path,
278+
&contents,
274279
);
275280

276281
/* Create .ast */
@@ -309,19 +314,28 @@ fn path_to_ast_extension(path: &Path) -> &str {
309314
}
310315
}
311316

312-
fn filter_ppx_flags(ppx_flags: &Option<Vec<OneOrMore<String>>>) -> Option<Vec<OneOrMore<String>>> {
317+
fn include_ppx(flag: &str, contents: &str) -> bool {
318+
if flag.contains("bisect") {
319+
return std::env::var("BISECT_ENABLE").is_ok();
320+
} else if flag.contains("graphql-ppx") && !contents.contains("%graphql") {
321+
return false;
322+
} else if flag.contains("spice") && !contents.contains("@spice") {
323+
return false;
324+
}
325+
return true;
326+
}
327+
328+
fn filter_ppx_flags(
329+
ppx_flags: &Option<Vec<OneOrMore<String>>>,
330+
contents: &str,
331+
) -> Option<Vec<OneOrMore<String>>> {
313332
// get the environment variable "BISECT_ENABLE" if it exists set the filter to "bisect"
314-
let filter = match std::env::var("BISECT_ENABLE") {
315-
Ok(_) => None,
316-
Err(_) => Some("bisect"),
317-
};
318333
ppx_flags.as_ref().map(|flags| {
319334
flags
320335
.iter()
321-
.filter(|flag| match (flag, filter) {
322-
(bsconfig::OneOrMore::Single(str), Some(filter)) => !str.contains(filter),
323-
(bsconfig::OneOrMore::Multiple(str), Some(filter)) => !str.first().unwrap().contains(filter),
324-
_ => true,
336+
.filter(|flag| match flag {
337+
bsconfig::OneOrMore::Single(str) => include_ppx(str, contents),
338+
bsconfig::OneOrMore::Multiple(str) => include_ppx(str.first().unwrap(), contents),
325339
})
326340
.map(|x| x.to_owned())
327341
.collect::<Vec<OneOrMore<String>>>()

src/helpers.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::build::packages;
22
use std::ffi::OsString;
33
use std::fs;
4+
use std::fs::File;
5+
use std::io::Read;
46
use std::io::{self, BufRead};
57
use std::path::{Component, Path, PathBuf};
68
use std::process::Command;
@@ -320,3 +322,10 @@ pub fn get_rescript_version(bsc_path: &str) -> String {
320322
.replace('\n', "")
321323
.replace("ReScript ", "")
322324
}
325+
326+
pub fn read_file(path: &Path) -> Result<String, std::io::Error> {
327+
let mut file = File::open(path).expect("file not found");
328+
let mut contents = String::new();
329+
file.read_to_string(&mut contents)?;
330+
Ok(contents)
331+
}

0 commit comments

Comments
 (0)