Skip to content

Commit b543fdc

Browse files
committed
paths wip - windows support
1 parent 0e8c36f commit b543fdc

13 files changed

+247
-260
lines changed

rewatch/src/build.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use serde::Serialize;
2121
use std::fmt;
2222
use std::fs::File;
2323
use std::io::{stdout, Write};
24-
use std::path::PathBuf;
24+
use std::path::{Path, PathBuf};
2525
use std::time::{Duration, Instant};
2626

2727
use self::compile::compiler_args;
@@ -55,15 +55,14 @@ pub struct CompilerArgs {
5555
}
5656

5757
pub fn get_compiler_args(
58-
path: &str,
58+
path: &Path,
5959
rescript_version: Option<String>,
60-
bsc_path: Option<String>,
60+
bsc_path: &Option<PathBuf>,
6161
build_dev_deps: bool,
6262
) -> Result<String> {
6363
let filename = &helpers::get_abs_path(path);
64-
let package_root = helpers::get_abs_path(
65-
&helpers::get_nearest_config(&std::path::PathBuf::from(path)).expect("Couldn't find package root"),
66-
);
64+
let package_root =
65+
helpers::get_abs_path(&helpers::get_nearest_config(&path).expect("Couldn't find package root"));
6766
let workspace_root = get_workspace_root(&package_root).map(|p| helpers::get_abs_path(&p));
6867
let root_rescript_config =
6968
packages::read_config(&workspace_root.to_owned().unwrap_or(package_root.to_owned()))?;
@@ -73,17 +72,13 @@ pub fn get_compiler_args(
7372
} else {
7473
let bsc_path = match bsc_path {
7574
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
76-
None => helpers::get_bsc(&package_root, workspace_root.to_owned()),
75+
None => helpers::get_bsc(&package_root, &workspace_root),
7776
};
7877
helpers::get_rescript_version(&bsc_path)
7978
};
8079

8180
// make PathBuf from package root and get the relative path for filename
82-
let relative_filename = PathBuf::from(&filename)
83-
.strip_prefix(PathBuf::from(&package_root))
84-
.unwrap()
85-
.to_string_lossy()
86-
.to_string();
81+
let relative_filename = filename.strip_prefix(PathBuf::from(&package_root)).unwrap();
8782

8883
let file_path = PathBuf::from(&package_root).join(filename);
8984
let contents = helpers::read_file(&file_path).expect("Error reading file");
@@ -97,18 +92,18 @@ pub fn get_compiler_args(
9792
workspace_root.as_ref().unwrap_or(&package_root),
9893
&contents,
9994
);
100-
let is_interface = filename.ends_with('i');
95+
let is_interface = filename.to_string_lossy().ends_with('i');
10196
let has_interface = if is_interface {
10297
true
10398
} else {
104-
let mut interface_filename = filename.to_string();
99+
let mut interface_filename = filename.to_string_lossy().to_string();
105100
interface_filename.push('i');
106101
PathBuf::from(&interface_filename).exists()
107102
};
108103
let compiler_args = compiler_args(
109104
&rescript_config,
110105
&root_rescript_config,
111-
&ast_path.to_string_lossy(),
106+
&ast_path,
112107
&rescript_version,
113108
&relative_filename,
114109
is_interface,
@@ -131,15 +126,15 @@ pub fn initialize_build(
131126
default_timing: Option<Duration>,
132127
filter: &Option<regex::Regex>,
133128
show_progress: bool,
134-
path: &str,
135-
bsc_path: Option<String>,
129+
path: &Path,
130+
bsc_path: &Option<PathBuf>,
136131
build_dev_deps: bool,
137132
) -> Result<BuildState> {
138133
let project_root = helpers::get_abs_path(path);
139134
let workspace_root = helpers::get_workspace_root(&project_root);
140135
let bsc_path = match bsc_path {
141136
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
142-
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
137+
None => helpers::get_bsc(&project_root, &workspace_root),
143138
};
144139
let root_config_name = packages::read_package_name(&project_root)?;
145140
let rescript_version = helpers::get_rescript_version(&bsc_path);
@@ -453,11 +448,11 @@ pub fn write_build_ninja(build_state: &BuildState) {
453448

454449
pub fn build(
455450
filter: &Option<regex::Regex>,
456-
path: &str,
451+
path: &Path,
457452
show_progress: bool,
458453
no_timing: bool,
459454
create_sourcedirs: bool,
460-
bsc_path: Option<String>,
455+
bsc_path: &Option<PathBuf>,
461456
build_dev_deps: bool,
462457
) -> Result<BuildState> {
463458
let default_timing: Option<std::time::Duration> = if no_timing {

rewatch/src/build/build_types.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::build::packages::{Namespace, Package};
22
use ahash::{AHashMap, AHashSet};
3-
use std::{fmt::Display, time::SystemTime};
3+
use std::{fmt::Display, path::PathBuf, time::SystemTime};
44

55
#[derive(Debug, Clone, PartialEq)]
66
pub enum ParseState {
@@ -19,7 +19,7 @@ pub enum CompileState {
1919
}
2020
#[derive(Debug, Clone, PartialEq)]
2121
pub struct Interface {
22-
pub path: String,
22+
pub path: PathBuf,
2323
pub parse_state: ParseState,
2424
pub compile_state: CompileState,
2525
pub last_modified: SystemTime,
@@ -28,7 +28,7 @@ pub struct Interface {
2828

2929
#[derive(Debug, Clone, PartialEq)]
3030
pub struct Implementation {
31-
pub path: String,
31+
pub path: PathBuf,
3232
pub parse_state: ParseState,
3333
pub compile_state: CompileState,
3434
pub last_modified: SystemTime,
@@ -91,12 +91,12 @@ pub struct BuildState {
9191
pub modules: AHashMap<String, Module>,
9292
pub packages: AHashMap<String, Package>,
9393
pub module_names: AHashSet<String>,
94-
pub project_root: String,
94+
pub project_root: PathBuf,
9595
pub root_config_name: String,
9696
pub deleted_modules: AHashSet<String>,
9797
pub rescript_version: String,
98-
pub bsc_path: String,
99-
pub workspace_root: Option<String>,
98+
pub bsc_path: PathBuf,
99+
pub workspace_root: Option<PathBuf>,
100100
pub deps_initialized: bool,
101101
}
102102

@@ -109,12 +109,12 @@ impl BuildState {
109109
self.modules.get(module_name)
110110
}
111111
pub fn new(
112-
project_root: String,
112+
project_root: PathBuf,
113113
root_config_name: String,
114114
packages: AHashMap<String, Package>,
115-
workspace_root: Option<String>,
115+
workspace_root: Option<PathBuf>,
116116
rescript_version: String,
117-
bsc_path: String,
117+
bsc_path: PathBuf,
118118
) -> Self {
119119
Self {
120120
module_names: AHashSet::new(),
@@ -141,15 +141,15 @@ pub struct AstModule {
141141
pub package_name: String,
142142
pub namespace: Namespace,
143143
pub last_modified: SystemTime,
144-
pub ast_file_path: String,
144+
pub ast_file_path: PathBuf,
145145
pub is_root: bool,
146146
pub suffix: String,
147147
}
148148

149149
pub struct CompileAssetsState {
150-
pub ast_modules: AHashMap<String, AstModule>,
150+
pub ast_modules: AHashMap<PathBuf, AstModule>,
151151
pub cmi_modules: AHashMap<String, SystemTime>,
152152
pub cmt_modules: AHashMap<String, SystemTime>,
153-
pub ast_rescript_file_locations: AHashSet<String>,
154-
pub rescript_file_locations: AHashSet<String>,
153+
pub ast_rescript_file_locations: AHashSet<PathBuf>,
154+
pub rescript_file_locations: AHashSet<PathBuf>,
155155
}

rewatch/src/build/clean.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use anyhow::Result;
77
use console::style;
88
use rayon::prelude::*;
99
use std::io::Write;
10+
use std::path::{Path, PathBuf};
1011
use std::time::Instant;
1112

12-
fn remove_ast(package: &packages::Package, source_file: &str) {
13+
fn remove_ast(package: &packages::Package, source_file: &Path) {
1314
let _ = std::fs::remove_file(helpers::get_compiler_asset(
1415
package,
1516
&packages::Namespace::NoNamespace,
@@ -18,7 +19,7 @@ fn remove_ast(package: &packages::Package, source_file: &str) {
1819
));
1920
}
2021

21-
fn remove_iast(package: &packages::Package, source_file: &str) {
22+
fn remove_iast(package: &packages::Package, source_file: &Path) {
2223
let _ = std::fs::remove_file(helpers::get_compiler_asset(
2324
package,
2425
&packages::Namespace::NoNamespace,
@@ -27,15 +28,15 @@ fn remove_iast(package: &packages::Package, source_file: &str) {
2728
));
2829
}
2930

30-
fn remove_mjs_file(source_file: &str, suffix: &String) {
31+
fn remove_mjs_file(source_file: &Path, suffix: &String) {
3132
let _ = std::fs::remove_file(helpers::change_extension(
3233
source_file,
3334
// suffix.to_string includes the ., so we need to remove it
3435
&suffix.to_string()[1..],
3536
));
3637
}
3738

38-
fn remove_compile_asset(package: &packages::Package, source_file: &str, extension: &str) {
39+
fn remove_compile_asset(package: &packages::Package, source_file: &Path, extension: &str) {
3940
let _ = std::fs::remove_file(helpers::get_compiler_asset(
4041
package,
4142
&package.namespace,
@@ -50,7 +51,7 @@ fn remove_compile_asset(package: &packages::Package, source_file: &str, extensio
5051
));
5152
}
5253

53-
pub fn remove_compile_assets(package: &packages::Package, source_file: &str) {
54+
pub fn remove_compile_assets(package: &packages::Package, source_file: &Path) {
5455
// optimization
5556
// only issue cmti if there is an interfacce file
5657
for extension in &["cmj", "cmi", "cmt", "cmti"] {
@@ -79,23 +80,20 @@ pub fn clean_mjs_files(build_state: &BuildState) {
7980
.filter_map(|spec| {
8081
if spec.in_source {
8182
Some((
82-
std::path::PathBuf::from(package.path.to_string())
83-
.join(&source_file.implementation.path)
84-
.to_string_lossy()
85-
.to_string(),
83+
package.path.join(&source_file.implementation.path),
8684
root_package.config.get_suffix(spec),
8785
))
8886
} else {
8987
None
9088
}
9189
})
92-
.collect::<Vec<(String, String)>>(),
90+
.collect::<Vec<(PathBuf, String)>>(),
9391
)
9492
}
9593
_ => None,
9694
})
9795
.flatten()
98-
.collect::<Vec<(String, String)>>();
96+
.collect::<Vec<(PathBuf, String)>>();
9997

10098
rescript_file_locations
10199
.par_iter()
@@ -118,7 +116,7 @@ pub fn cleanup_previous_build(
118116
let diff = compile_assets_state
119117
.ast_rescript_file_locations
120118
.difference(&compile_assets_state.rescript_file_locations)
121-
.collect::<Vec<&String>>();
119+
.collect::<Vec<&PathBuf>>();
122120

123121
let diff_len = diff.len();
124122

@@ -133,7 +131,7 @@ pub fn cleanup_previous_build(
133131
..
134132
} = compile_assets_state
135133
.ast_modules
136-
.get(&res_file_location.to_string())
134+
.get(*res_file_location)
137135
.expect("Could not find module name for ast file");
138136

139137
let package = build_state
@@ -338,7 +336,12 @@ pub fn cleanup_after_build(build_state: &BuildState) {
338336
});
339337
}
340338

341-
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>, build_dev_deps: bool) -> Result<()> {
339+
pub fn clean(
340+
path: &Path,
341+
show_progress: bool,
342+
bsc_path: &Option<PathBuf>,
343+
build_dev_deps: bool,
344+
) -> Result<()> {
342345
let project_root = helpers::get_abs_path(path);
343346
let workspace_root = helpers::get_workspace_root(&project_root);
344347
let packages = packages::make(
@@ -352,7 +355,7 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>, build_de
352355
let root_config_name = packages::read_package_name(&project_root)?;
353356
let bsc_path = match bsc_path {
354357
Some(bsc_path) => helpers::get_abs_path(&bsc_path),
355-
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
358+
None => helpers::get_bsc(&project_root, &workspace_root),
356359
};
357360

358361
let rescript_version = helpers::get_rescript_version(&bsc_path);

0 commit comments

Comments
 (0)