Skip to content

Commit

Permalink
🎨 - Fix the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 26, 2023
1 parent c5ae426 commit 5bce515
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 312 deletions.
7 changes: 2 additions & 5 deletions benches/base_bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Criterion};
use rewatch::build;
use rewatch::package_tree;
use rewatch::helpers;
use rewatch::package_tree;

use std::fs::File;
use std::io::prelude::*;
Expand Down Expand Up @@ -37,10 +37,7 @@ fn criterion_benchmark(c: &mut Criterion) {
// Create initial build
let _ = build::build(folder);
// Update the file
let _ = writeln!(
file,
r#"let log2 = () => ["a", "b"]->forEach(Js.log);log2()"#
);
let _ = writeln!(file, r#"let log2 = () => ["a", "b"]->forEach(Js.log);log2()"#);
// Create another build
let _ = build::build(folder);

Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

max_width = 110
13 changes: 3 additions & 10 deletions src/bsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ pub enum Suffix {

impl fmt::Display for Suffix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
serde_json::to_value(self).unwrap().as_str().unwrap()
)
write!(f, "{}", serde_json::to_value(self).unwrap().as_str().unwrap())
}
}

Expand Down Expand Up @@ -233,9 +229,7 @@ pub fn flatten_ppx_flags(
OneOrMore::Multiple(ys) => {
let first_character = ys[0].chars().nth(0);
let ppx = match first_character {
Some('.') => {
node_modules_dir.to_owned() + "/" + package_name + "/" + &ys[0]
}
Some('.') => node_modules_dir.to_owned() + "/" + package_name + "/" + &ys[0],
_ => node_modules_dir.to_owned() + "/" + &ys[0],
};
vec![
Expand All @@ -258,8 +252,7 @@ pub fn read(path: String) -> T {
fs::read_to_string(path.clone())
.map_err(|e| format!("Could not read bsconfig. {path} - {e}"))
.and_then(|x| {
serde_json::from_str::<T>(&x)
.map_err(|e| format!("Could not parse bsconfig. {path} - {e}"))
serde_json::from_str::<T>(&x).map_err(|e| format!("Could not parse bsconfig. {path} - {e}"))
})
.expect("Errors reading bsconfig")
}
198 changes: 67 additions & 131 deletions src/build.rs

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions src/build_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ impl BuildState {
pub fn get_module(&self, module_name: &str) -> Option<&Module> {
self.modules.get(module_name)
}
pub fn new(
project_root: String,
root_config_name: String,
packages: AHashMap<String, Package>,
) -> Self {
pub fn new(project_root: String, root_config_name: String, packages: AHashMap<String, Package>) -> Self {
Self {
module_names: AHashSet::new(),
modules: AHashMap::new(),
Expand Down
113 changes: 49 additions & 64 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ pub fn clean_mjs_files(build_state: &BuildState, project_root: &str) {

rescript_file_locations
.par_iter()
.for_each(|(rescript_file_location, suffix)| {
remove_mjs_file(&rescript_file_location, &suffix)
});
.for_each(|(rescript_file_location, suffix)| remove_mjs_file(&rescript_file_location, &suffix));
}

pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AHashSet<String>) {
Expand Down Expand Up @@ -240,10 +238,8 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
// already includes a namespace
&package_tree::Namespace::NoNamespace,
);
cmi_modules.insert(
module_name,
entry.metadata().unwrap().modified().unwrap(),
);
cmi_modules
.insert(module_name, entry.metadata().unwrap().modified().unwrap());
}
_ => {
// println!("other extension: {:?}", other);
Expand Down Expand Up @@ -340,9 +336,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
let compile_dirty = cmi_modules.get(module_name);
if let Some(compile_dirty) = compile_dirty {
// println!("{} is not dirty", module_name);
let (implementation_last_modified, interface_last_modified) = match &module
.source_type
{
let (implementation_last_modified, interface_last_modified) = match &module.source_type {
SourceType::MlMap(_) => (None, None),
SourceType::SourceFile(source_file) => {
let implementation_last_modified = source_file.implementation.last_modified;
Expand All @@ -361,9 +355,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
Some(interface_last_modified)
}
}
(Some(implementation_last_modified), None) => {
Some(implementation_last_modified)
}
(Some(implementation_last_modified), None) => Some(implementation_last_modified),
_ => None,
};

Expand Down Expand Up @@ -428,11 +420,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
})
.collect::<AHashSet<String>>();

(
diff_len,
ast_rescript_file_locations.len(),
deleted_module_names,
)
(diff_len, ast_rescript_file_locations.len(), deleted_module_names)
}

fn failed_to_parse(module: &Module) -> bool {
Expand Down Expand Up @@ -480,57 +468,54 @@ fn failed_to_compile(module: &Module) -> bool {
}

pub fn cleanup_after_build(build_state: &BuildState) {
build_state
.modules
.par_iter()
.for_each(|(_module_name, module)| {
let package = build_state.get_package(&module.package_name).unwrap();
if failed_to_parse(module) {
match &module.source_type {
SourceType::SourceFile(source_file) => {
remove_iast(
&source_file.implementation.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);
remove_ast(
&source_file.implementation.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);
}
_ => (),
build_state.modules.par_iter().for_each(|(_module_name, module)| {
let package = build_state.get_package(&module.package_name).unwrap();
if failed_to_parse(module) {
match &module.source_type {
SourceType::SourceFile(source_file) => {
remove_iast(
&source_file.implementation.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);
remove_ast(
&source_file.implementation.path,
&module.package_name,
&build_state.project_root,
package.is_root,
);
}
_ => (),
}
if failed_to_compile(module) {
// only retain ast file if it compiled successfully, that's the only thing we check
// if we see a AST file, we assume it compiled successfully, so we also need to clean
// up the AST file if compile is not successful
match &module.source_type {
SourceType::SourceFile(source_file) => {
remove_compile_assets(
&source_file.implementation.path,
&module.package_name,
&package.namespace,
&build_state.project_root,
package.is_root,
);
}
SourceType::MlMap(_) => remove_compile_assets(
&get_mlmap_path(
&build_state.project_root,
&module.package_name,
&package.namespace.to_suffix().unwrap(),
package.is_root,
),
}
if failed_to_compile(module) {
// only retain ast file if it compiled successfully, that's the only thing we check
// if we see a AST file, we assume it compiled successfully, so we also need to clean
// up the AST file if compile is not successful
match &module.source_type {
SourceType::SourceFile(source_file) => {
remove_compile_assets(
&source_file.implementation.path,
&module.package_name,
&package_tree::Namespace::NoNamespace,
&package.namespace,
&build_state.project_root,
package.is_root,
),
);
}
SourceType::MlMap(_) => remove_compile_assets(
&get_mlmap_path(
&build_state.project_root,
&module.package_name,
&package.namespace.to_suffix().unwrap(),
package.is_root,
),
&module.package_name,
&package_tree::Namespace::NoNamespace,
&build_state.project_root,
package.is_root,
),
}
});
}
});
}
70 changes: 17 additions & 53 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,24 @@ fn capitalize(s: &str) -> String {

fn add_suffix(base: &str, namespace: &package_tree::Namespace) -> String {
match namespace {
package_tree::Namespace::NamespaceWithEntry {
namespace: _,
entry,
} if entry == base => base.to_string(),
package_tree::Namespace::NamespaceWithEntry { namespace: _, entry } if entry == base => {
base.to_string()
}
package_tree::Namespace::Namespace(namespace)
| package_tree::Namespace::NamespaceWithEntry {
namespace,
entry: _,
} => base.to_string() + "-" + &namespace,
| package_tree::Namespace::NamespaceWithEntry { namespace, entry: _ } => {
base.to_string() + "-" + &namespace
}
package_tree::Namespace::NoNamespace => base.to_string(),
}
}

pub fn module_name_with_namespace(
module_name: &str,
namespace: &package_tree::Namespace,
) -> String {
pub fn module_name_with_namespace(module_name: &str, namespace: &package_tree::Namespace) -> String {
capitalize(&add_suffix(module_name, namespace))
}

// this doesn't capitalize the module name! if the rescript name of the file is "foo.res" the
// compiler assets are foo-Namespace.cmt and foo-Namespace.cmj, but the module name is Foo
pub fn file_path_to_compiler_asset_basename(
path: &str,
namespace: &package_tree::Namespace,
) -> String {
pub fn file_path_to_compiler_asset_basename(path: &str, namespace: &package_tree::Namespace) -> String {
let base = get_basename(path);
add_suffix(&base, namespace)
}
Expand Down Expand Up @@ -186,13 +178,9 @@ pub fn get_bsc(root_path: &str) -> String {
}

pub fn string_ends_with_any(s: &PathBuf, suffixes: &[&str]) -> bool {
suffixes.iter().any(|&suffix| {
s.extension()
.unwrap_or(&OsString::new())
.to_str()
.unwrap_or("")
== suffix
})
suffixes
.iter()
.any(|&suffix| s.extension().unwrap_or(&OsString::new()).to_str().unwrap_or("") == suffix)
}

pub fn get_compiler_asset(
Expand Down Expand Up @@ -250,30 +238,15 @@ pub fn is_interface_ast_file(file: &str) -> bool {
file.ends_with(".iast")
}

pub fn get_mlmap_path(
root_path: &str,
package_name: &str,
namespace: &str,
is_root: bool,
) -> String {
pub fn get_mlmap_path(root_path: &str, package_name: &str, namespace: &str, is_root: bool) -> String {
get_build_path(root_path, package_name, is_root) + "/" + namespace + ".mlmap"
}

pub fn get_mlmap_compile_path(
root_path: &str,
package_name: &str,
namespace: &str,
is_root: bool,
) -> String {
pub fn get_mlmap_compile_path(root_path: &str, package_name: &str, namespace: &str, is_root: bool) -> String {
get_build_path(root_path, package_name, is_root) + "/" + namespace + ".cmi"
}

pub fn get_ast_path(
source_file: &str,
package_name: &str,
root_path: &str,
is_root: bool,
) -> String {
pub fn get_ast_path(source_file: &str, package_name: &str, root_path: &str, is_root: bool) -> String {
get_compiler_asset(
source_file,
package_name,
Expand All @@ -284,12 +257,7 @@ pub fn get_ast_path(
)
}

pub fn get_iast_path(
source_file: &str,
package_name: &str,
root_path: &str,
is_root: bool,
) -> String {
pub fn get_iast_path(source_file: &str, package_name: &str, root_path: &str, is_root: bool) -> String {
get_compiler_asset(
source_file,
package_name,
Expand All @@ -307,9 +275,7 @@ pub fn read_lines(filename: String) -> io::Result<io::Lines<io::BufReader<fs::Fi

pub fn get_system_time() -> u128 {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
since_the_epoch.as_millis()
}

Expand All @@ -333,9 +299,7 @@ pub fn is_source_file(extension: &str) -> bool {

pub fn is_non_exotic_module_name(module_name: &str) -> bool {
let mut chars = module_name.chars();
if chars.next().unwrap().is_ascii_uppercase()
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
{
if chars.next().unwrap().is_ascii_uppercase() && chars.all(|c| c.is_ascii_alphanumeric() || c == '_') {
return true;
}
return false;
Expand Down
5 changes: 2 additions & 3 deletions src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ fn get_log_file_path(subfolder: Location, name: &str) -> String {
}

fn escape_colours(str: &str) -> String {
let re =
Regex::new(r"[\u001b\u009b]\[[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]")
.expect("Could not create regex");
let re = Regex::new(r"[\u001b\u009b]\[[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]")
.expect("Could not create regex");
re.replace_all(str, "").to_string()
}

Expand Down
Loading

0 comments on commit 5bce515

Please sign in to comment.