Skip to content

Commit f75e929

Browse files
committed
fix
1 parent 47b0828 commit f75e929

File tree

5 files changed

+70
-63
lines changed

5 files changed

+70
-63
lines changed

rewatch/src/build/parse.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ fn generate_ast(
323323
);
324324

325325
// generate the dir of the ast_path (it mirrors the source file dir)
326-
helpers::create_path(&(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy()));
326+
let ast_parent_path = package.get_build_path().join(ast_path.parent().unwrap());
327+
helpers::create_path(&ast_parent_path);
327328

328329
/* Create .ast */
329330
let result = if let Some(res_to_ast) = Some(
@@ -344,11 +345,12 @@ fn generate_ast(
344345
Ok((ast_path, None))
345346
}
346347
} else {
347-
log::info!("Parsing file {}...", filename);
348+
log::info!("Parsing file {}...", filename.display());
348349

349350
Err(format!(
350351
"Could not find canonicalize_string_path for file {} in package {}",
351-
filename, package.name
352+
filename.display(),
353+
package.name
352354
))
353355
};
354356
if let Ok((ast_path, _)) = &result {

rewatch/src/build/read_compile_state.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use crate::helpers;
44
use ahash::{AHashMap, AHashSet};
55
use rayon::prelude::*;
66
use std::fs;
7-
use std::path::PathBuf;
7+
use std::path::{Path, PathBuf};
88
use std::time::SystemTime;
99

1010
pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
11-
let mut ast_modules: AHashMap<String, AstModule> = AHashMap::new();
11+
let mut ast_modules: AHashMap<PathBuf, AstModule> = AHashMap::new();
1212
let mut cmi_modules: AHashMap<String, SystemTime> = AHashMap::new();
1313
let mut cmt_modules: AHashMap<String, SystemTime> = AHashMap::new();
1414
let mut ast_rescript_file_locations = AHashSet::new();
@@ -20,31 +20,24 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
2020
SourceType::SourceFile(source_file) => {
2121
let package = build_state.packages.get(&module.package_name).unwrap();
2222

23-
Some(
24-
PathBuf::from(&package.path)
25-
.join(&source_file.implementation.path)
26-
.to_string_lossy()
27-
.to_string(),
28-
)
23+
Some(PathBuf::from(&package.path).join(&source_file.implementation.path))
2924
}
3025
_ => None,
3126
})
32-
.collect::<AHashSet<String>>();
27+
.collect::<AHashSet<PathBuf>>();
3328

3429
rescript_file_locations.extend(
3530
build_state
3631
.modules
3732
.values()
3833
.filter_map(|module| {
3934
let package = build_state.packages.get(&module.package_name).unwrap();
40-
module.get_interface().as_ref().map(|interface| {
41-
PathBuf::from(&package.path)
42-
.join(&interface.path)
43-
.to_string_lossy()
44-
.to_string()
45-
})
35+
module
36+
.get_interface()
37+
.as_ref()
38+
.map(|interface| PathBuf::from(&package.path).join(&interface.path))
4639
})
47-
.collect::<AHashSet<String>>(),
40+
.collect::<AHashSet<PathBuf>>(),
4841
);
4942

5043
// scan all ast files in all packages
@@ -84,18 +77,18 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
8477
|(path, last_modified, extension, package_name, package_namespace, package_is_root)| {
8578
match extension.as_str() {
8679
"iast" | "ast" => {
87-
let module_name =
88-
helpers::file_path_to_module_name(path.to_str().unwrap(), package_namespace);
80+
let module_name = helpers::file_path_to_module_name(path, package_namespace);
8981

90-
let ast_file_path = path.to_str().unwrap().to_owned();
91-
let res_file_path = get_res_path_from_ast(&ast_file_path);
82+
let ast_file_path = path.to_path_buf();
83+
let res_file_path = get_res_path_from_ast(&path);
9284
let root_package = build_state
9385
.packages
9486
.get(&build_state.root_config_name)
9587
.expect("Could not find root package");
9688
if let Some(res_file_path) = res_file_path {
89+
let res_file_path_buf = PathBuf::from(res_file_path);
9790
let _ = ast_modules.insert(
98-
res_file_path.to_owned(),
91+
res_file_path_buf.clone(),
9992
AstModule {
10093
module_name,
10194
package_name: package_name.to_owned(),
@@ -108,12 +101,12 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
108101
.get_suffix(root_package.config.get_package_specs().first().unwrap()),
109102
},
110103
);
111-
let _ = ast_rescript_file_locations.insert(res_file_path);
104+
let _ = ast_rescript_file_locations.insert(res_file_path_buf);
112105
}
113106
}
114107
"cmi" => {
115108
let module_name = helpers::file_path_to_module_name(
116-
path.to_str().unwrap(),
109+
path,
117110
// we don't want to include a namespace here because the CMI file
118111
// already includes a namespace
119112
&packages::Namespace::NoNamespace,
@@ -122,7 +115,7 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
122115
}
123116
"cmt" => {
124117
let module_name = helpers::file_path_to_module_name(
125-
path.to_str().unwrap(),
118+
path,
126119
// we don't want to include a namespace here because the CMI file
127120
// already includes a namespace
128121
&packages::Namespace::NoNamespace,
@@ -145,8 +138,8 @@ pub fn read(build_state: &mut BuildState) -> CompileAssetsState {
145138
}
146139
}
147140

148-
fn get_res_path_from_ast(ast_file: &str) -> Option<String> {
149-
if let Ok(lines) = helpers::read_lines(ast_file.to_string()) {
141+
fn get_res_path_from_ast(ast_file: &Path) -> Option<String> {
142+
if let Ok(lines) = helpers::read_lines(ast_file) {
150143
// we skip the first line with is some null characters
151144
// the following lines in the AST are the dependency modules
152145
// we stop when we hit a line that starts with a "/", this is the path of the file.

rewatch/src/config.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn flatten_ppx_flags(
252252
) -> Vec<String> {
253253
match flags {
254254
None => vec![],
255-
Some(xs) => xs
255+
Some(flags) => flags
256256
.iter()
257257
.flat_map(|x| match x {
258258
OneOrMore::Single(y) => {
@@ -261,18 +261,29 @@ pub fn flatten_ppx_flags(
261261
Some('.') => {
262262
vec![
263263
"-ppx".to_string(),
264-
node_modules_dir.to_owned() + "/" + package_name + "/" + y,
264+
node_modules_dir
265+
.join(package_name)
266+
.join(y)
267+
.to_string_lossy()
268+
.to_string(),
265269
]
266270
}
267-
_ => vec!["-ppx".to_string(), node_modules_dir.to_owned() + "/" + y],
271+
_ => vec![
272+
"-ppx".to_string(),
273+
node_modules_dir.join(y).to_string_lossy().to_string(),
274+
],
268275
}
269276
}
270277
OneOrMore::Multiple(ys) if ys.is_empty() => vec![],
271278
OneOrMore::Multiple(ys) => {
272279
let first_character = ys[0].chars().next();
273280
let ppx = match first_character {
274-
Some('.') => node_modules_dir.to_owned() + "/" + package_name + "/" + &ys[0],
275-
_ => node_modules_dir.to_owned() + "/" + &ys[0],
281+
Some('.') => node_modules_dir
282+
.join(package_name)
283+
.join(&ys[0])
284+
.to_string_lossy()
285+
.to_string(),
286+
_ => node_modules_dir.join(&ys[0]).to_string_lossy().to_string(),
276287
};
277288
vec![
278289
"-ppx".to_string(),

rewatch/src/main.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clap_verbosity_flag::InfoLevel;
44
use log::LevelFilter;
55
use regex::Regex;
66
use std::io::Write;
7+
use std::path::{Path, PathBuf};
78

89
use rewatch::{build, cmd, lock, watcher};
910

@@ -102,7 +103,12 @@ fn main() -> Result<()> {
102103
Some(path) => {
103104
println!(
104105
"{}",
105-
build::get_compiler_args(&path, args.rescript_version, args.bsc_path, args.dev)?
106+
build::get_compiler_args(
107+
Path::new(&path),
108+
args.rescript_version,
109+
&args.bsc_path.map(PathBuf::from),
110+
args.dev
111+
)?
106112
);
107113
std::process::exit(0);
108114
}
@@ -118,15 +124,20 @@ fn main() -> Result<()> {
118124
std::process::exit(1)
119125
}
120126
lock::Lock::Aquired(_) => match command {
121-
Command::Clean => build::clean::clean(&folder, show_progress, args.bsc_path, args.dev),
127+
Command::Clean => build::clean::clean(
128+
Path::new(&folder),
129+
show_progress,
130+
&args.bsc_path.map(PathBuf::from),
131+
args.dev,
132+
),
122133
Command::Build => {
123134
match build::build(
124135
&filter,
125-
&folder,
136+
Path::new(&folder),
126137
show_progress,
127138
args.no_timing,
128139
args.create_sourcedirs,
129-
args.bsc_path,
140+
&args.bsc_path.map(PathBuf::from),
130141
args.dev,
131142
) {
132143
Err(e) => {

rewatch/src/watcher.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::queue::*;
99
use futures_timer::Delay;
1010
use notify::event::ModifyKind;
1111
use notify::{Config, Error, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
12-
use std::path::Path;
12+
use std::path::{Path, PathBuf};
1313
use std::sync::Arc;
1414
use std::sync::Mutex;
1515
use std::time::{Duration, Instant};
@@ -48,23 +48,17 @@ fn matches_filter(path_buf: &Path, filter: &Option<regex::Regex>) -> bool {
4848

4949
async fn async_watch(
5050
q: Arc<FifoQueue<Result<Event, Error>>>,
51-
path: &str,
51+
path: &Path,
5252
show_progress: bool,
5353
filter: &Option<regex::Regex>,
5454
after_build: Option<String>,
5555
create_sourcedirs: bool,
5656
build_dev_deps: bool,
57-
bsc_path: Option<String>,
57+
bsc_path: Option<PathBuf>,
5858
) -> notify::Result<()> {
59-
let mut build_state = build::initialize_build(
60-
None,
61-
filter,
62-
show_progress,
63-
path,
64-
bsc_path.clone(),
65-
build_dev_deps,
66-
)
67-
.expect("Can't initialize build");
59+
let mut build_state =
60+
build::initialize_build(None, filter, show_progress, path, &bsc_path, build_dev_deps)
61+
.expect("Can't initialize build");
6862
let mut needs_compile_type = CompileType::Incremental;
6963
// create a mutex to capture if ctrl-c was pressed
7064
let ctrlc_pressed = Arc::new(Mutex::new(false));
@@ -167,8 +161,7 @@ async fn async_watch(
167161
// mark the interface file dirty
168162
if let Some(ref mut interface) = source_file.interface {
169163
let canonicalized_interface_file =
170-
std::path::PathBuf::from(package.path.to_string())
171-
.join(&interface.path);
164+
package.path.join(&interface.path);
172165
if canonicalized_path_buf == canonicalized_interface_file {
173166
if let Ok(modified) = canonicalized_path_buf
174167
.metadata()
@@ -235,15 +228,9 @@ async fn async_watch(
235228
}
236229
CompileType::Full => {
237230
let timing_total = Instant::now();
238-
build_state = build::initialize_build(
239-
None,
240-
filter,
241-
show_progress,
242-
path,
243-
bsc_path.clone(),
244-
build_dev_deps,
245-
)
246-
.expect("Can't initialize build");
231+
build_state =
232+
build::initialize_build(None, filter, show_progress, path, &bsc_path, build_dev_deps)
233+
.expect("Can't initialize build");
247234
let _ = build::incremental_build(
248235
&mut build_state,
249236
None,
@@ -300,15 +287,18 @@ pub fn start(
300287
.watch(folder.as_ref(), RecursiveMode::Recursive)
301288
.expect("Could not start watcher");
302289

290+
let path = Path::new(folder);
291+
let bsc_path_buf = bsc_path.map(PathBuf::from);
292+
303293
if let Err(e) = async_watch(
304294
consumer,
305-
folder,
295+
path,
306296
show_progress,
307297
filter,
308298
after_build,
309299
create_sourcedirs,
310300
build_dev_deps,
311-
bsc_path,
301+
bsc_path_buf,
312302
)
313303
.await
314304
{

0 commit comments

Comments
 (0)