Skip to content

Commit

Permalink
✨ - Selectively run ppx's (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich authored Apr 16, 2024
1 parent a8ba799 commit b244400
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ pub fn get_compiler_args(path: &str, rescript_version: Option<String>) -> 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,
&relative_filename,
&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 {
Expand Down
34 changes: 24 additions & 10 deletions src/build/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pub fn parser_args(
version: &str,
workspace_root: &Option<String>,
root_path: &str,
contents: &str,
) -> (String, Vec<String>) {
let file = &filename.to_string();
let path = PathBuf::from(filename);
Expand All @@ -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();
Expand Down Expand Up @@ -263,6 +264,9 @@ fn generate_ast(
bsc_path: &str,
workspace_root: &Option<String>,
) -> Result<(String, Option<String>), 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,
Expand All @@ -271,6 +275,7 @@ fn generate_ast(
version,
workspace_root,
&root_package.path,
&contents,
);

/* Create .ast */
Expand Down Expand Up @@ -309,19 +314,28 @@ fn path_to_ast_extension(path: &Path) -> &str {
}
}

fn filter_ppx_flags(ppx_flags: &Option<Vec<OneOrMore<String>>>) -> Option<Vec<OneOrMore<String>>> {
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<Vec<OneOrMore<String>>>,
contents: &str,
) -> Option<Vec<OneOrMore<String>>> {
// 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::<Vec<OneOrMore<String>>>()
Expand Down
9 changes: 9 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -320,3 +322,10 @@ pub fn get_rescript_version(bsc_path: &str) -> String {
.replace('\n', "")
.replace("ReScript ", "")
}

pub fn read_file(path: &Path) -> Result<String, std::io::Error> {
let mut file = File::open(path).expect("file not found");
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}

0 comments on commit b244400

Please sign in to comment.