-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathhelpers.rs
More file actions
552 lines (493 loc) · 17.9 KB
/
helpers.rs
File metadata and controls
552 lines (493 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use crate::build::packages;
use crate::config::Config;
use crate::helpers;
use crate::project_context::ProjectContext;
use anyhow::anyhow;
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::time::{SystemTime, UNIX_EPOCH};
pub type StdErr = String;
pub mod deserialize;
pub mod emojis {
use console::Emoji;
pub static COMMAND: Emoji<'_, '_> = Emoji("🏃 ", "[run] ");
pub static SWEEP: Emoji<'_, '_> = Emoji("🧹 ", "[clean] ");
pub static PARSE: Emoji<'_, '_> = Emoji("🧱 ", "[parse] ");
pub static SWORDS: Emoji<'_, '_> = Emoji("🤺 ", "[build] ");
pub static CHECKMARK: Emoji<'_, '_> = Emoji("✅ ", "[ok] ");
pub static WARNING: Emoji<'_, '_> = Emoji("⚠️ ", "[warn] ");
pub static CROSS: Emoji<'_, '_> = Emoji("❌ ", "[error] ");
pub static LINE_CLEAR: &str = "\x1b[2K\r";
}
// Cached check: does the given directory contain a node_modules subfolder?
fn has_node_modules_cached(project_context: &ProjectContext, dir: &Path) -> bool {
match project_context.node_modules_exist_cache.read() {
Ok(cache) => {
if let Some(exists) = cache.get(dir) {
return *exists;
}
}
Err(poisoned) => {
log::warn!("node_modules_exist_cache read lock poisoned; recovering");
let cache = poisoned.into_inner();
if let Some(exists) = cache.get(dir) {
return *exists;
}
}
}
let exists = dir.join("node_modules").exists();
match project_context.node_modules_exist_cache.write() {
Ok(mut cache) => {
cache.insert(dir.to_path_buf(), exists);
}
Err(poisoned) => {
log::warn!("node_modules_exist_cache write lock poisoned; recovering");
let mut cache = poisoned.into_inner();
cache.insert(dir.to_path_buf(), exists);
}
}
exists
}
/// This trait is used to strip the verbatim prefix from a Windows path.
/// On non-Windows systems, it simply returns the original path.
/// This is needed until the rescript compiler can handle such paths.
pub trait StrippedVerbatimPath {
fn to_stripped_verbatim_path(self) -> PathBuf;
}
impl StrippedVerbatimPath for PathBuf {
fn to_stripped_verbatim_path(self) -> PathBuf {
if cfg!(not(target_os = "windows")) {
return self;
}
let mut stripped = PathBuf::new();
for component in self.components() {
match component {
Component::Prefix(prefix_component) => {
if prefix_component.kind().is_verbatim() {
stripped.push(
prefix_component
.as_os_str()
.to_string_lossy()
.strip_prefix("\\\\?\\")
.unwrap(),
);
} else {
stripped.push(prefix_component.as_os_str());
}
}
Component::RootDir => {
stripped.push(Component::RootDir);
}
Component::CurDir => {
stripped.push(Component::CurDir);
}
Component::ParentDir => {
stripped.push(Component::ParentDir);
}
Component::Normal(os_str) => {
stripped.push(Component::Normal(os_str));
}
}
}
stripped
}
}
pub trait LexicalAbsolute {
fn to_lexical_absolute(&self) -> std::io::Result<PathBuf>;
}
impl LexicalAbsolute for Path {
fn to_lexical_absolute(&self) -> std::io::Result<PathBuf> {
let mut absolute = if self.is_absolute() {
PathBuf::new()
} else {
std::env::current_dir()?
};
for component in self.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
absolute.pop();
}
component => absolute.push(component.as_os_str()),
}
}
Ok(absolute)
}
}
pub fn package_path(root: &Path, package_name: &str) -> PathBuf {
root.join("node_modules").join(package_name)
}
// Tap-style helper: cache and return the value (single clone for cache insert)
fn cache_package_tap(
project_context: &ProjectContext,
key: &(PathBuf, String),
value: PathBuf,
) -> anyhow::Result<PathBuf> {
match project_context.packages_cache.write() {
Ok(mut cache) => {
cache.insert(key.clone(), value.clone());
}
Err(poisoned) => {
log::warn!("packages_cache write lock poisoned; recovering");
let mut cache = poisoned.into_inner();
cache.insert(key.clone(), value.clone());
}
}
Ok(value)
}
/// Tries to find a path for input package_name.
/// The node_modules folder may be found at different levels in the case of a monorepo.
/// This helper tries a variety of paths.
pub fn try_package_path(
package_config: &Config,
project_context: &ProjectContext,
package_name: &str,
) -> anyhow::Result<PathBuf> {
// try cached result first, keyed by (package_dir, package_name)
let pkg_name = package_name.to_string();
let package_dir = package_config
.path
.parent()
.ok_or_else(|| {
anyhow!(
"Expected {} to have a parent folder",
package_config.path.to_string_lossy()
)
})?
.to_path_buf();
let cache_key = (package_dir.clone(), pkg_name.clone());
match project_context.packages_cache.read() {
Ok(cache) => {
if let Some(cached) = cache.get(&cache_key) {
return Ok(cached.clone());
}
}
Err(poisoned) => {
log::warn!("packages_cache read lock poisoned; recovering");
let cache = poisoned.into_inner();
if let Some(cached) = cache.get(&cache_key) {
return Ok(cached.clone());
}
}
}
// package folder + node_modules + package_name
// This can happen in the following scenario:
// The ProjectContext has a MonoRepoContext::MonorepoRoot.
// We are reading a dependency from the root package.
// And that local dependency has a hoisted dependency.
// Example, we need to find package_name `foo` in the following scenario:
// root/packages/a/node_modules/foo
let path_from_current_package = helpers::package_path(&package_dir, package_name);
// current folder + node_modules + package_name
let path_from_current_config = project_context
.current_config
.path
.parent()
.ok_or_else(|| {
anyhow!(
"Expected {} to have a parent folder",
project_context.current_config.path.to_string_lossy()
)
})
.map(|parent_path| package_path(parent_path, package_name))?;
// root folder + node_modules + package_name
let path_from_root = package_path(project_context.get_root_path(), package_name);
if path_from_current_package.exists() {
cache_package_tap(project_context, &cache_key, path_from_current_package)
} else if path_from_current_config.exists() {
cache_package_tap(project_context, &cache_key, path_from_current_config)
} else if path_from_root.exists() {
cache_package_tap(project_context, &cache_key, path_from_root)
} else {
// As a last resort, when we're in a Single project context, traverse upwards
// starting from the parent of the package root (package_config.path.parent().parent())
// and probe each ancestor's node_modules for the dependency. This covers hoisted
// workspace setups when building a package standalone.
if project_context.monorepo_context.is_none() {
match package_config.path.parent().and_then(|p| p.parent()) {
Some(start_dir) => {
return find_dep_in_upward_node_modules(project_context, start_dir, package_name)
.and_then(|p| cache_package_tap(project_context, &cache_key, p));
}
None => {
log::debug!(
"try_package_path: cannot compute start directory for upward traversal from '{}'",
package_config.path.to_string_lossy()
);
}
}
}
Err(anyhow!(
"The package \"{package_name}\" is not found (are node_modules up-to-date?)..."
))
}
}
fn find_dep_in_upward_node_modules(
project_context: &ProjectContext,
start_dir: &Path,
package_name: &str,
) -> anyhow::Result<PathBuf> {
log::debug!(
"try_package_path: falling back to upward traversal for '{}' starting at '{}'",
package_name,
start_dir.to_string_lossy()
);
let mut current = Some(start_dir);
while let Some(dir) = current {
if has_node_modules_cached(project_context, dir) {
let candidate = package_path(dir, package_name);
log::debug!("try_package_path: checking '{}'", candidate.to_string_lossy());
if candidate.exists() {
log::debug!(
"try_package_path: found '{}' at '{}' via upward traversal",
package_name,
candidate.to_string_lossy()
);
return Ok(candidate);
}
}
current = dir.parent();
}
log::debug!(
"try_package_path: no '{}' found during upward traversal from '{}'",
package_name,
start_dir.to_string_lossy()
);
Err(anyhow!(
"try_package_path: upward traversal did not find '{}' starting at '{}'",
package_name,
start_dir.to_string_lossy()
))
}
pub fn get_abs_path(path: &Path) -> PathBuf {
let abs_path_buf = PathBuf::from(path);
abs_path_buf
.to_lexical_absolute()
.expect("Could not canonicalize")
}
pub fn get_basename(path: &Path) -> String {
path.file_stem()
.expect("Could not get basename")
.to_str()
.expect("Could not get basename 2")
.to_string()
}
/// Capitalizes the first character in s.
fn capitalize(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
fn add_suffix(base: &str, namespace: &packages::Namespace) -> String {
match namespace {
packages::Namespace::NamespaceWithEntry { namespace: _, entry } if entry == base => base.to_string(),
packages::Namespace::Namespace(_)
| packages::Namespace::NamespaceWithEntry {
namespace: _,
entry: _,
} => base.to_string() + "-" + &namespace.to_suffix().unwrap(),
packages::Namespace::NoNamespace => base.to_string(),
}
}
pub fn module_name_with_namespace(module_name: &str, namespace: &packages::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: &Path, namespace: &packages::Namespace) -> String {
let base = get_basename(path);
add_suffix(&base, namespace)
}
pub fn file_path_to_module_name(path: &Path, namespace: &packages::Namespace) -> String {
capitalize(&file_path_to_compiler_asset_basename(path, namespace))
}
pub fn contains_ascii_characters(str: &str) -> bool {
for chr in str.chars() {
if chr.is_ascii_alphanumeric() {
return true;
}
}
false
}
pub fn create_path(path: &Path) {
fs::DirBuilder::new().recursive(true).create(path).unwrap();
}
pub fn create_path_for_path(path: &Path) {
fs::DirBuilder::new().recursive(true).create(path).unwrap();
}
pub fn get_bin_dir() -> PathBuf {
let current_exe_path = std::env::current_exe().expect("Could not get current executable path");
current_exe_path
.parent()
.expect("Could not get parent directory of current executable")
.to_path_buf()
}
pub fn get_bsc() -> PathBuf {
let bsc_path = match std::env::var("RESCRIPT_BSC_EXE") {
Ok(val) => PathBuf::from(val),
Err(_) => get_bin_dir().join("bsc.exe"),
};
bsc_path
.canonicalize()
.expect("Could not get bsc path, did you set environment variable RESCRIPT_BSC_EXE ?")
.to_stripped_verbatim_path()
}
pub fn string_ends_with_any(s: &Path, suffixes: &[&str]) -> bool {
suffixes
.iter()
.any(|&suffix| s.extension().unwrap_or(&OsString::new()).to_str().unwrap_or("") == suffix)
}
fn path_to_ast_extension(path: &Path) -> &str {
let extension = path.extension().unwrap().to_str().unwrap();
if extension.ends_with("i") { ".iast" } else { ".ast" }
}
pub fn get_ast_path(source_file: &Path) -> PathBuf {
let source_path = source_file;
let basename = file_path_to_compiler_asset_basename(source_file, &packages::Namespace::NoNamespace);
let extension = path_to_ast_extension(source_path);
source_path
.parent()
.unwrap()
.join(format!("{basename}{extension}"))
}
pub fn get_compiler_asset(
package: &packages::Package,
namespace: &packages::Namespace,
source_file: &Path,
extension: &str,
) -> PathBuf {
let namespace = match extension {
"ast" | "iast" => &packages::Namespace::NoNamespace,
_ => namespace,
};
let basename = file_path_to_compiler_asset_basename(source_file, namespace);
package
.get_ocaml_build_path()
.join(format!("{basename}.{extension}"))
}
pub fn canonicalize_string_path(path: &str) -> Option<PathBuf> {
Path::new(path)
.canonicalize()
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
.ok()
}
pub fn get_bs_compiler_asset(
package: &packages::Package,
namespace: &packages::Namespace,
source_file: &Path,
extension: &str,
) -> String {
let namespace = match extension {
"ast" | "iast" => &packages::Namespace::NoNamespace,
_ => namespace,
};
let dir = source_file.parent().unwrap();
let basename = file_path_to_compiler_asset_basename(source_file, namespace);
package
.get_build_path()
.join(dir)
.join(format!("{basename}{extension}"))
.to_str()
.unwrap()
.to_owned()
}
pub fn get_namespace_from_module_name(module_name: &str) -> Option<String> {
let mut split = module_name.split('-');
let _ = split.next();
split.next().map(|s| s.to_string())
}
pub fn is_interface_ast_file(file: &Path) -> bool {
file.extension()
.map(|extension| extension.eq_ignore_ascii_case("iast"))
.unwrap_or(false)
}
pub fn read_lines(filename: &Path) -> io::Result<io::Lines<io::BufReader<fs::File>>> {
let file = fs::File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
pub fn get_system_time() -> u128 {
let start = SystemTime::now();
let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards");
since_the_epoch.as_millis()
}
pub fn is_interface_file(extension: &str) -> bool {
extension == "resi"
}
pub fn is_implementation_file(extension: &str) -> bool {
extension == "res"
}
pub fn is_source_file(extension: &str) -> bool {
is_interface_file(extension) || is_implementation_file(extension)
}
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 == '_') {
return true;
}
false
}
pub fn get_extension(path: &Path) -> String {
path.extension()
.expect("Could not get extension")
.to_str()
.expect("Could not get extension 2")
.to_string()
}
pub fn format_namespaced_module_name(module_name: &str) -> String {
// from ModuleName-Namespace to Namespace.ModuleName
// also format ModuleName-@Namespace to Namespace.ModuleName
let mut split = module_name.split('-');
let module_name = split.next().unwrap();
let namespace = split.next();
let namespace = namespace.map(|ns| ns.trim_start_matches('@'));
match namespace {
None => module_name.to_string(),
Some(ns) => ns.to_string() + "." + module_name,
}
}
pub fn compute_file_hash(path: &Path) -> Option<blake3::Hash> {
match fs::read(path) {
Ok(str) => Some(blake3::hash(&str)),
Err(_) => None,
}
}
fn has_rescript_config(path: &Path) -> bool {
path.join("rescript.json").exists()
}
// traverse up the directory tree until we find a config.json, if not return None
pub fn get_nearest_config(path_buf: &Path) -> Option<PathBuf> {
let mut current_dir = path_buf.to_owned();
loop {
if has_rescript_config(¤t_dir) {
return Some(current_dir);
}
match current_dir.parent() {
None => return None,
Some(parent) => current_dir = parent.to_path_buf(),
}
}
}
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)
}
pub fn get_source_file_from_rescript_file(path: &Path, suffix: &str) -> PathBuf {
path.with_extension(
// suffix.to_string includes the ., so we need to remove it
&suffix.to_string()[1..],
)
}
pub fn is_local_package(workspace_path: &Path, canonical_package_path: &Path) -> bool {
canonical_package_path.starts_with(workspace_path)
&& !canonical_package_path
.components()
.any(|c| c.as_os_str() == "node_modules")
}