-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathclean.rs
405 lines (369 loc) · 14.2 KB
/
clean.rs
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
use super::build_types::*;
use super::packages;
use crate::helpers;
use crate::helpers::emojis::*;
use ahash::AHashSet;
use anyhow::Result;
use console::style;
use rayon::prelude::*;
use std::io::Write;
use std::time::Instant;
fn remove_ast(package: &packages::Package, source_file: &str) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
package,
&packages::Namespace::NoNamespace,
source_file,
"ast",
));
}
fn remove_iast(package: &packages::Package, source_file: &str) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
package,
&packages::Namespace::NoNamespace,
source_file,
"iast",
));
}
fn remove_mjs_file(source_file: &str, suffix: &String) {
let _ = std::fs::remove_file(helpers::change_extension(
source_file,
// suffix.to_string includes the ., so we need to remove it
&suffix.to_string()[1..],
));
}
fn remove_compile_asset(package: &packages::Package, source_file: &str, extension: &str) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
package,
&package.namespace,
source_file,
extension,
));
let _ = std::fs::remove_file(helpers::get_bs_compiler_asset(
package,
&package.namespace,
source_file,
extension,
));
}
pub fn remove_compile_assets(package: &packages::Package, source_file: &str) {
// optimization
// only issue cmti if there is an interfacce file
for extension in &["cmj", "cmi", "cmt", "cmti"] {
remove_compile_asset(package, source_file, extension);
}
}
pub fn clean_mjs_files(build_state: &BuildState) {
// get all rescript file locations
let rescript_file_locations = build_state
.modules
.values()
.filter_map(|module| match &module.source_type {
SourceType::SourceFile(source_file) => {
let package = build_state.packages.get(&module.package_name).unwrap();
let root_package = build_state
.packages
.get(&build_state.root_config_name)
.expect("Could not find root package");
Some((
std::path::PathBuf::from(package.path.to_string())
.join(&source_file.implementation.path)
.to_string_lossy()
.to_string(),
root_package.config.get_suffix(),
))
}
_ => None,
})
.collect::<Vec<(String, String)>>();
rescript_file_locations
.par_iter()
.for_each(|(rescript_file_location, suffix)| remove_mjs_file(rescript_file_location, suffix));
}
// TODO: change to scan_previous_build => CompileAssetsState
// and then do cleanup on that state (for instance remove all .mjs files that are not in the state)
pub fn cleanup_previous_build(
build_state: &mut BuildState,
compile_assets_state: CompileAssetsState,
) -> (usize, usize) {
// delete the .mjs file which appear in our previous compile assets
// but does not exists anymore
// delete the compiler assets for which modules we can't find a rescript file
// location of rescript file is in the AST
// delete the .mjs file for which we DO have a compiler asset, but don't have a
// rescript file anymore (path is found in the .ast file)
let diff = compile_assets_state
.ast_rescript_file_locations
.difference(&compile_assets_state.rescript_file_locations)
.collect::<Vec<&String>>();
let diff_len = diff.len();
let deleted_interfaces = diff
.par_iter()
.map(|res_file_location| {
let AstModule {
module_name,
package_name,
ast_file_path,
suffix,
..
} = compile_assets_state
.ast_modules
.get(&res_file_location.to_string())
.expect("Could not find module name for ast file");
let package = build_state
.packages
.get(package_name)
.expect("Could not find package");
remove_compile_assets(package, res_file_location);
remove_mjs_file(res_file_location, suffix);
remove_iast(package, res_file_location);
remove_ast(package, res_file_location);
match helpers::get_extension(ast_file_path).as_str() {
"iast" => Some(module_name.to_owned()),
"ast" => None,
_ => None,
}
})
.collect::<Vec<Option<String>>>()
.iter()
.filter_map(|module_name| module_name.to_owned())
.collect::<AHashSet<String>>();
compile_assets_state
.ast_rescript_file_locations
.intersection(&compile_assets_state.rescript_file_locations)
.for_each(|res_file_location| {
let AstModule {
module_name,
last_modified: ast_last_modified,
ast_file_path,
..
} = compile_assets_state
.ast_modules
.get(res_file_location)
.expect("Could not find module name for ast file");
let module = build_state
.modules
.get_mut(module_name)
.expect("Could not find module for ast file");
let compile_dirty = compile_assets_state.cmi_modules.get(module_name);
if let Some(compile_dirty) = compile_dirty {
let last_modified = Some(ast_last_modified);
if let Some(last_modified) = last_modified {
if compile_dirty > last_modified && !deleted_interfaces.contains(module_name) {
module.compile_dirty = false;
}
}
}
match &mut module.source_type {
SourceType::MlMap(_) => unreachable!("MlMap is not matched with a ReScript file"),
SourceType::SourceFile(source_file) => {
if helpers::is_interface_ast_file(ast_file_path) {
let interface = source_file
.interface
.as_mut()
.expect("Could not find interface for module");
let source_last_modified = interface.last_modified;
if ast_last_modified > &source_last_modified {
interface.parse_dirty = false;
}
} else {
let implementation = &mut source_file.implementation;
let source_last_modified = implementation.last_modified;
if ast_last_modified > &source_last_modified
&& !deleted_interfaces.contains(module_name)
{
implementation.parse_dirty = false;
}
}
}
}
});
compile_assets_state
.cmi_modules
.iter()
.for_each(|(module_name, last_modified)| {
if let Some(module) = build_state.modules.get_mut(module_name) {
module.last_compiled_cmi = Some(*last_modified);
}
});
compile_assets_state
.cmt_modules
.iter()
.for_each(|(module_name, last_modified)| {
if let Some(module) = build_state.modules.get_mut(module_name) {
module.last_compiled_cmt = Some(*last_modified);
}
});
let ast_module_names = compile_assets_state
.ast_modules
.values()
.filter_map(
|AstModule {
module_name,
ast_file_path,
..
}| {
match helpers::get_extension(ast_file_path).as_str() {
"iast" => None,
"ast" => Some(module_name),
_ => None,
}
},
)
.collect::<AHashSet<&String>>();
let all_module_names = build_state.modules.keys().collect::<AHashSet<&String>>();
let deleted_module_names = ast_module_names
.difference(&all_module_names)
.flat_map(|module_name| {
// if the module is a namespace, we need to mark the whole namespace as dirty when a module has been deleted
if let Some(namespace) = helpers::get_namespace_from_module_name(module_name) {
return vec![namespace, module_name.to_string()];
}
vec![module_name.to_string()]
})
.collect::<AHashSet<String>>();
build_state.deleted_modules = deleted_module_names;
(diff_len, compile_assets_state.ast_rescript_file_locations.len())
}
fn has_parse_warnings(module: &Module) -> bool {
matches!(
&module.source_type,
SourceType::SourceFile(SourceFile {
implementation: Implementation {
parse_state: ParseState::Warning,
..
},
..
}) | SourceType::SourceFile(SourceFile {
interface: Some(Interface {
parse_state: ParseState::Warning,
..
}),
..
})
)
}
fn has_compile_warnings(module: &Module) -> bool {
matches!(
&module.source_type,
SourceType::SourceFile(SourceFile {
implementation: Implementation {
compile_state: CompileState::Warning,
..
},
..
}) | SourceType::SourceFile(SourceFile {
interface: Some(Interface {
compile_state: CompileState::Warning,
..
}),
..
})
)
}
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 has_parse_warnings(module) {
if let SourceType::SourceFile(source_file) = &module.source_type {
remove_iast(package, &source_file.implementation.path);
remove_ast(package, &source_file.implementation.path);
}
}
if has_compile_warnings(module) {
// only retain AST file if the compilation doesn't have warnings, we remove the AST in favor
// of the CMI/CMT/CMJ files because if we delete these, the editor tooling doesn't
// work anymore. If we remove the intermediate AST file, the editor tooling will
// work, and we have an indication that we need to recompile the file.
//
// Recompiling this takes a bit more time, because we have to parse again, but
// if we have warnings it's usually not a lot of files so the additional
// latency shouldn't be too bad
match &module.source_type {
SourceType::SourceFile(source_file) => {
// we only clean the ast here, this will cause the file to be recompiled
// (and thus keep showing the warning), but it will keep the cmi file, so that we don't
// unecessary mark all the dependents as dirty, when there is no change in the interface
remove_ast(package, &source_file.implementation.path);
remove_iast(package, &source_file.implementation.path);
}
SourceType::MlMap(_) => (),
}
}
});
}
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Result<()> {
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
let packages = packages::make(&None, &project_root, &workspace_root, show_progress)?;
let root_config_name = packages::get_package_name(&project_root)?;
let bsc_path = match bsc_path {
Some(bsc_path) => bsc_path,
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
};
let rescript_version = helpers::get_rescript_version(&bsc_path);
let timing_clean_compiler_assets = Instant::now();
if show_progress {
println!(
"{} {}Cleaning compiler assets...",
style("[1/2]").bold().dim(),
SWEEP
);
let _ = std::io::stdout().flush();
};
packages.iter().for_each(|(_, package)| {
if show_progress {
println!(
"{}{} {}Cleaning {}...",
LINE_CLEAR,
style("[1/2]").bold().dim(),
SWEEP,
package.name
);
let _ = std::io::stdout().flush();
}
let path_str = package.get_build_path();
let path = std::path::Path::new(&path_str);
let _ = std::fs::remove_dir_all(path);
let path_str = package.get_bs_build_path();
let path = std::path::Path::new(&path_str);
let _ = std::fs::remove_dir_all(path);
});
let timing_clean_compiler_assets_elapsed = timing_clean_compiler_assets.elapsed();
if show_progress {
println!(
"{}{} {}Cleaned compiler assets in {:.2}s",
LINE_CLEAR,
style("[1/2]").bold().dim(),
SWEEP,
timing_clean_compiler_assets_elapsed.as_secs_f64()
);
let _ = std::io::stdout().flush();
}
let timing_clean_mjs = Instant::now();
if show_progress {
println!("{} {}Cleaning mjs files...", style("[2/2]").bold().dim(), SWEEP);
let _ = std::io::stdout().flush();
}
let mut build_state = BuildState::new(
project_root.to_owned(),
root_config_name,
packages,
workspace_root,
rescript_version,
bsc_path,
);
packages::parse_packages(&mut build_state);
clean_mjs_files(&build_state);
let timing_clean_mjs_elapsed = timing_clean_mjs.elapsed();
if show_progress {
println!(
"{}{} {}Cleaned mjs files in {:.2}s",
LINE_CLEAR,
style("[2/2]").bold().dim(),
SWEEP,
timing_clean_mjs_elapsed.as_secs_f64()
);
let _ = std::io::stdout().flush();
}
Ok(())
}