Skip to content

Commit 0c3740d

Browse files
committed
✨ - Allow individual compilation of workspace member
It's now possible to just compile a single package in a workspace, this will decrease compilation time for that purpose (no need to compile the whole workspace)
1 parent 1647b2a commit 0c3740d

File tree

10 files changed

+274
-148
lines changed

10 files changed

+274
-148
lines changed

src/build.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use std::io::{stdout, Write};
1717
use std::process::Command;
1818
use std::time::Instant;
1919

20-
pub fn get_version(project_root: &str) -> String {
21-
let version_cmd = Command::new(helpers::get_bsc(&project_root))
20+
pub fn get_version(bsc_path: &str) -> String {
21+
let version_cmd = Command::new(bsc_path)
2222
.args(["-v"])
2323
.output()
2424
.expect("failed to find version");
@@ -47,8 +47,10 @@ fn is_dirty(module: &Module) -> bool {
4747
pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Result<BuildState, ()> {
4848
let timing_total = Instant::now();
4949
let project_root = helpers::get_abs_path(path);
50-
let rescript_version = get_version(&project_root);
50+
let workspace_root = helpers::get_workspace_root(&project_root);
51+
let bsc_path = helpers::get_bsc(&project_root, workspace_root.to_owned());
5152
let root_config_name = packages::get_package_name(&project_root);
53+
let rescript_version = get_version(&bsc_path);
5254
let default_timing: Option<std::time::Duration> = if no_timing {
5355
Some(std::time::Duration::new(0.0 as u64, 0.0 as u32))
5456
} else {
@@ -62,7 +64,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
6264
);
6365
let _ = stdout().flush();
6466
let timing_package_tree = Instant::now();
65-
let packages = packages::make(&filter, &project_root);
67+
let packages = packages::make(&filter, &project_root, workspace_root.to_owned());
6668
let timing_package_tree_elapsed = timing_package_tree.elapsed();
6769

6870
println!(
@@ -134,7 +136,13 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
134136
);
135137

136138
let timing_ast = Instant::now();
137-
let result_asts = parse::generate_asts(&rescript_version, &mut build_state, || pb.inc(1));
139+
let result_asts = parse::generate_asts(
140+
&rescript_version,
141+
&mut build_state,
142+
|| pb.inc(1),
143+
&bsc_path,
144+
workspace_root.to_owned(),
145+
);
138146
let timing_ast_elapsed = timing_ast.elapsed();
139147

140148
match result_asts {
@@ -192,6 +200,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
192200
&rescript_version,
193201
|| pb.inc(1),
194202
|size| pb.set_length(size),
203+
&bsc_path,
195204
);
196205
let compile_duration = start_compiling.elapsed();
197206

src/build/clean.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ use rayon::prelude::*;
99
use std::io::Write;
1010
use std::time::Instant;
1111

12-
fn remove_ast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) {
12+
fn remove_ast(source_file: &str, package_path: &str, root_path: &str, is_root: bool) {
1313
let _ = std::fs::remove_file(helpers::get_compiler_asset(
1414
source_file,
15-
package_name,
15+
package_path,
1616
&packages::Namespace::NoNamespace,
1717
root_path,
1818
"ast",
1919
is_root,
2020
));
2121
}
2222

23-
fn remove_iast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) {
23+
fn remove_iast(source_file: &str, package_path: &str, root_path: &str, is_root: bool) {
2424
let _ = std::fs::remove_file(helpers::get_compiler_asset(
2525
source_file,
26-
package_name,
26+
package_path,
2727
&packages::Namespace::NoNamespace,
2828
root_path,
2929
"iast",
@@ -41,23 +41,23 @@ fn remove_mjs_file(source_file: &str, suffix: &bsconfig::Suffix) {
4141

4242
fn remove_compile_asset(
4343
source_file: &str,
44-
package_name: &str,
44+
package_path: &str,
4545
namespace: &packages::Namespace,
4646
root_path: &str,
4747
is_root: bool,
4848
extension: &str,
4949
) {
5050
let _ = std::fs::remove_file(helpers::get_compiler_asset(
5151
source_file,
52-
package_name,
52+
package_path,
5353
namespace,
5454
root_path,
5555
extension,
5656
is_root,
5757
));
5858
let _ = std::fs::remove_file(helpers::get_bs_compiler_asset(
5959
source_file,
60-
package_name,
60+
package_path,
6161
namespace,
6262
root_path,
6363
extension,
@@ -67,7 +67,7 @@ fn remove_compile_asset(
6767

6868
pub fn remove_compile_assets(
6969
source_file: &str,
70-
package_name: &str,
70+
package_path: &str,
7171
namespace: &packages::Namespace,
7272
root_path: &str,
7373
is_root: bool,
@@ -77,7 +77,7 @@ pub fn remove_compile_assets(
7777
for extension in &["cmj", "cmi", "cmt", "cmti"] {
7878
remove_compile_asset(
7979
source_file,
80-
package_name,
80+
package_path,
8181
namespace,
8282
root_path,
8383
is_root,
@@ -154,9 +154,14 @@ pub fn cleanup_previous_build(
154154
.ast_modules
155155
.get(&res_file_location.to_string())
156156
.expect("Could not find module name for ast file");
157+
158+
let package = build_state
159+
.packages
160+
.get(package_name)
161+
.expect("Could not find package");
157162
remove_compile_assets(
158163
res_file_location,
159-
package_name,
164+
&package.package_dir,
160165
package_namespace,
161166
&build_state.project_root,
162167
*is_root,
@@ -167,13 +172,13 @@ pub fn cleanup_previous_build(
167172
);
168173
remove_iast(
169174
res_file_location,
170-
package_name,
175+
&package.package_dir,
171176
&build_state.project_root,
172177
*is_root,
173178
);
174179
remove_ast(
175180
res_file_location,
176-
package_name,
181+
&package.package_dir,
177182
&build_state.project_root,
178183
*is_root,
179184
);
@@ -356,13 +361,13 @@ pub fn cleanup_after_build(build_state: &BuildState) {
356361
SourceType::SourceFile(source_file) => {
357362
remove_iast(
358363
&source_file.implementation.path,
359-
&module.package_name,
364+
&package.package_dir,
360365
&build_state.project_root,
361366
package.is_root,
362367
);
363368
remove_ast(
364369
&source_file.implementation.path,
365-
&module.package_name,
370+
&package.package_dir,
366371
&build_state.project_root,
367372
package.is_root,
368373
);
@@ -381,7 +386,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
381386
// unecessary mark all the dependents as dirty, when there is no change in the interface
382387
remove_compile_asset(
383388
&source_file.implementation.path,
384-
&module.package_name,
389+
&package.package_dir,
385390
&package.namespace,
386391
&build_state.project_root,
387392
package.is_root,
@@ -396,7 +401,8 @@ pub fn cleanup_after_build(build_state: &BuildState) {
396401

397402
pub fn clean(path: &str) {
398403
let project_root = helpers::get_abs_path(path);
399-
let packages = packages::make(&None, &project_root);
404+
let workspace_root = helpers::get_workspace_root(&project_root);
405+
let packages = packages::make(&None, &project_root, workspace_root);
400406
let root_config_name = packages::get_package_name(&project_root);
401407

402408
let timing_clean_compiler_assets = Instant::now();
@@ -416,11 +422,11 @@ pub fn clean(path: &str) {
416422
);
417423
std::io::stdout().flush().unwrap();
418424

419-
let path_str = helpers::get_build_path(&project_root, &package.name, package.is_root);
425+
let path_str = helpers::get_build_path(&project_root, &package.package_dir, package.is_root);
420426
let path = std::path::Path::new(&path_str);
421427
let _ = std::fs::remove_dir_all(path);
422428

423-
let path_str = helpers::get_bs_build_path(&project_root, &package.name, package.is_root);
429+
let path_str = helpers::get_bs_build_path(&project_root, &package.package_dir, package.is_root);
424430
let path = std::path::Path::new(&path_str);
425431
let _ = std::fs::remove_dir_all(path);
426432
});

src/build/compile.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::logs;
55
use super::packages;
66
use crate::bsconfig;
77
use crate::helpers;
8-
use ahash::AHashSet;
8+
use ahash::{AHashMap, AHashSet};
99
use console::style;
1010
use log::debug;
1111
use log::{info, log_enabled, Level::Info};
@@ -19,6 +19,7 @@ pub fn compile(
1919
rescript_version: &str,
2020
inc: impl Fn() -> () + std::marker::Sync,
2121
set_length: impl Fn(u64) -> (),
22+
bsc_path: &str,
2223
) -> (String, String, usize) {
2324
let mut compiled_modules = AHashSet::<String>::new();
2425

@@ -166,14 +167,16 @@ pub fn compile(
166167
&root_package,
167168
&helpers::get_iast_path(
168169
&path,
169-
&package.name,
170+
&package.package_dir,
170171
&build_state.project_root,
171172
package.is_root,
172173
),
173174
module,
174175
&build_state.project_root,
175176
&rescript_version,
176177
true,
178+
bsc_path,
179+
&build_state.packages,
177180
);
178181
Some(result)
179182
}
@@ -184,14 +187,16 @@ pub fn compile(
184187
&root_package,
185188
&helpers::get_ast_path(
186189
&source_file.implementation.path,
187-
&package.name,
190+
&package.package_dir,
188191
&build_state.project_root,
189192
package.is_root,
190193
),
191194
module,
192195
&build_state.project_root,
193196
&rescript_version,
194197
false,
198+
bsc_path,
199+
&build_state.packages,
195200
);
196201
// if let Err(error) = result.to_owned() {
197202
// println!("{}", error);
@@ -291,6 +296,7 @@ pub fn compile(
291296
&build_state.project_root,
292297
package.is_root,
293298
&package.name,
299+
&package.package_dir,
294300
&err,
295301
);
296302
compile_warnings.push_str(&err);
@@ -302,6 +308,7 @@ pub fn compile(
302308
&build_state.project_root,
303309
package.is_root,
304310
&package.name,
311+
&package.package_dir,
305312
&err,
306313
);
307314
compile_errors.push_str(&err);
@@ -315,6 +322,7 @@ pub fn compile(
315322
&build_state.project_root,
316323
package.is_root,
317324
&package.name,
325+
&package.package_dir,
318326
&err,
319327
);
320328
compile_warnings.push_str(&err);
@@ -327,6 +335,7 @@ pub fn compile(
327335
&build_state.project_root,
328336
package.is_root,
329337
&package.name,
338+
&package.package_dir,
330339
&err,
331340
);
332341
compile_errors.push_str(&err);
@@ -374,8 +383,10 @@ fn compile_file(
374383
root_path: &str,
375384
version: &str,
376385
is_interface: bool,
386+
bsc_path: &str,
387+
packages: &AHashMap<String, packages::Package>,
377388
) -> Result<Option<String>, String> {
378-
let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root);
389+
let build_path_abs = helpers::get_build_path(root_path, &package.package_dir, package.is_root);
379390
let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags);
380391

381392
let normal_deps = package
@@ -398,10 +409,15 @@ fn compile_file(
398409
.concat()
399410
.into_iter()
400411
.map(|x| {
412+
let package = &packages.get(&x).expect("expect package");
401413
vec![
402414
"-I".to_string(),
403-
helpers::canonicalize_string_path(&helpers::get_build_path(root_path, &x, package.is_root))
404-
.unwrap(),
415+
helpers::canonicalize_string_path(&helpers::get_build_path(
416+
root_path,
417+
&package.package_dir,
418+
package.is_root,
419+
))
420+
.unwrap(),
405421
]
406422
})
407423
.collect::<Vec<Vec<String>>>();
@@ -527,7 +543,7 @@ fn compile_file(
527543
]
528544
.concat();
529545

530-
let to_mjs = Command::new(helpers::get_bsc(&root_path))
546+
let to_mjs = Command::new(bsc_path)
531547
.current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap())
532548
.args(to_mjs_args)
533549
.output();
@@ -552,7 +568,7 @@ fn compile_file(
552568
build_path_abs.to_string() + "/" + &module_name + ".cmi",
553569
std::path::Path::new(&helpers::get_bs_build_path(
554570
root_path,
555-
&package.name,
571+
&package.package_dir,
556572
package.is_root,
557573
))
558574
.join(dir)
@@ -565,7 +581,7 @@ fn compile_file(
565581
build_path_abs.to_string() + "/" + &module_name + ".cmj",
566582
std::path::Path::new(&helpers::get_bs_build_path(
567583
root_path,
568-
&package.name,
584+
&package.package_dir,
569585
package.is_root,
570586
))
571587
.join(dir)
@@ -575,7 +591,7 @@ fn compile_file(
575591
build_path_abs.to_string() + "/" + &module_name + ".cmt",
576592
std::path::Path::new(&helpers::get_bs_build_path(
577593
root_path,
578-
&package.name,
594+
&package.package_dir,
579595
package.is_root,
580596
))
581597
.join(dir)
@@ -589,7 +605,7 @@ fn compile_file(
589605
build_path_abs.to_string() + "/" + &module_name + ".cmti",
590606
std::path::Path::new(&helpers::get_bs_build_path(
591607
root_path,
592-
&package.name,
608+
&package.package_dir,
593609
package.is_root,
594610
))
595611
.join(dir)
@@ -612,7 +628,7 @@ fn compile_file(
612628
std::path::Path::new(&package.package_dir).join(path),
613629
std::path::Path::new(&helpers::get_bs_build_path(
614630
root_path,
615-
&package.name,
631+
&package.package_dir,
616632
package.is_root,
617633
))
618634
.join(path),
@@ -623,7 +639,7 @@ fn compile_file(
623639
std::path::Path::new(&package.package_dir).join(path),
624640
std::path::Path::new(&helpers::get_build_path(
625641
root_path,
626-
&package.name,
642+
&package.package_dir,
627643
package.is_root,
628644
))
629645
.join(std::path::Path::new(path).file_name().unwrap()),

0 commit comments

Comments
 (0)