Skip to content

Commit

Permalink
✨ - Allow individual compilation of workspace member
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jfrolich committed Jan 12, 2024
1 parent 1647b2a commit 145cd38
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 114 deletions.
19 changes: 14 additions & 5 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::io::{stdout, Write};
use std::process::Command;
use std::time::Instant;

pub fn get_version(project_root: &str) -> String {
let version_cmd = Command::new(helpers::get_bsc(&project_root))
pub fn get_version(bsc_path: &str) -> String {
let version_cmd = Command::new(bsc_path)
.args(["-v"])
.output()
.expect("failed to find version");
Expand Down Expand Up @@ -47,8 +47,10 @@ fn is_dirty(module: &Module) -> bool {
pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Result<BuildState, ()> {
let timing_total = Instant::now();
let project_root = helpers::get_abs_path(path);
let rescript_version = get_version(&project_root);
let workspace_root = helpers::get_workspace_root(&project_root);
let bsc_path = helpers::get_bsc(&project_root, workspace_root.to_owned());
let root_config_name = packages::get_package_name(&project_root);
let rescript_version = get_version(&bsc_path);
let default_timing: Option<std::time::Duration> = if no_timing {
Some(std::time::Duration::new(0.0 as u64, 0.0 as u32))
} else {
Expand All @@ -62,7 +64,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
);
let _ = stdout().flush();
let timing_package_tree = Instant::now();
let packages = packages::make(&filter, &project_root);
let packages = packages::make(&filter, &project_root, workspace_root.to_owned());
let timing_package_tree_elapsed = timing_package_tree.elapsed();

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

let timing_ast = Instant::now();
let result_asts = parse::generate_asts(&rescript_version, &mut build_state, || pb.inc(1));
let result_asts = parse::generate_asts(
&rescript_version,
&mut build_state,
|| pb.inc(1),
&bsc_path,
workspace_root.to_owned(),
);
let timing_ast_elapsed = timing_ast.elapsed();

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

Expand Down
42 changes: 24 additions & 18 deletions src/build/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ use rayon::prelude::*;
use std::io::Write;
use std::time::Instant;

fn remove_ast(source_file: &str, package_name: &str, root_path: &str, is_root: bool) {
fn remove_ast(source_file: &str, package_path: &str, root_path: &str, is_root: bool) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
source_file,
package_name,
package_path,
&packages::Namespace::NoNamespace,
root_path,
"ast",
is_root,
));
}

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

fn remove_compile_asset(
source_file: &str,
package_name: &str,
package_path: &str,
namespace: &packages::Namespace,
root_path: &str,
is_root: bool,
extension: &str,
) {
let _ = std::fs::remove_file(helpers::get_compiler_asset(
source_file,
package_name,
package_path,
namespace,
root_path,
extension,
is_root,
));
let _ = std::fs::remove_file(helpers::get_bs_compiler_asset(
source_file,
package_name,
package_path,
namespace,
root_path,
extension,
Expand All @@ -67,7 +67,7 @@ fn remove_compile_asset(

pub fn remove_compile_assets(
source_file: &str,
package_name: &str,
package_path: &str,
namespace: &packages::Namespace,
root_path: &str,
is_root: bool,
Expand All @@ -77,7 +77,7 @@ pub fn remove_compile_assets(
for extension in &["cmj", "cmi", "cmt", "cmti"] {
remove_compile_asset(
source_file,
package_name,
package_path,
namespace,
root_path,
is_root,
Expand Down Expand Up @@ -154,9 +154,14 @@ pub fn cleanup_previous_build(
.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(
res_file_location,
package_name,
&package.package_dir,
package_namespace,
&build_state.project_root,
*is_root,
Expand All @@ -167,13 +172,13 @@ pub fn cleanup_previous_build(
);
remove_iast(
res_file_location,
package_name,
&package.package_dir,
&build_state.project_root,
*is_root,
);
remove_ast(
res_file_location,
package_name,
&package.package_dir,
&build_state.project_root,
*is_root,
);
Expand Down Expand Up @@ -356,13 +361,13 @@ pub fn cleanup_after_build(build_state: &BuildState) {
SourceType::SourceFile(source_file) => {
remove_iast(
&source_file.implementation.path,
&module.package_name,
&package.package_dir,
&build_state.project_root,
package.is_root,
);
remove_ast(
&source_file.implementation.path,
&module.package_name,
&package.package_dir,
&build_state.project_root,
package.is_root,
);
Expand All @@ -381,7 +386,7 @@ pub fn cleanup_after_build(build_state: &BuildState) {
// unecessary mark all the dependents as dirty, when there is no change in the interface
remove_compile_asset(
&source_file.implementation.path,
&module.package_name,
&package.package_dir,
&package.namespace,
&build_state.project_root,
package.is_root,
Expand All @@ -396,7 +401,8 @@ pub fn cleanup_after_build(build_state: &BuildState) {

pub fn clean(path: &str) {
let project_root = helpers::get_abs_path(path);
let packages = packages::make(&None, &project_root);
let workspace_root = helpers::get_workspace_root(&project_root);
let packages = packages::make(&None, &project_root, workspace_root);
let root_config_name = packages::get_package_name(&project_root);

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

let path_str = helpers::get_build_path(&project_root, &package.name, package.is_root);
let path_str = helpers::get_build_path(&project_root, &package.package_dir, package.is_root);
let path = std::path::Path::new(&path_str);
let _ = std::fs::remove_dir_all(path);

let path_str = helpers::get_bs_build_path(&project_root, &package.name, package.is_root);
let path_str = helpers::get_bs_build_path(&project_root, &package.package_dir, package.is_root);
let path = std::path::Path::new(&path_str);
let _ = std::fs::remove_dir_all(path);
});
Expand Down
42 changes: 29 additions & 13 deletions src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::logs;
use super::packages;
use crate::bsconfig;
use crate::helpers;
use ahash::AHashSet;
use ahash::{AHashMap, AHashSet};
use console::style;
use log::debug;
use log::{info, log_enabled, Level::Info};
Expand All @@ -19,6 +19,7 @@ pub fn compile(
rescript_version: &str,
inc: impl Fn() -> () + std::marker::Sync,
set_length: impl Fn(u64) -> (),
bsc_path: &str,
) -> (String, String, usize) {
let mut compiled_modules = AHashSet::<String>::new();

Expand Down Expand Up @@ -166,14 +167,16 @@ pub fn compile(
&root_package,
&helpers::get_iast_path(
&path,
&package.name,
&package.package_dir,
&build_state.project_root,
package.is_root,
),
module,
&build_state.project_root,
&rescript_version,
true,
bsc_path,
&build_state.packages,
);
Some(result)
}
Expand All @@ -184,14 +187,16 @@ pub fn compile(
&root_package,
&helpers::get_ast_path(
&source_file.implementation.path,
&package.name,
&package.package_dir,
&build_state.project_root,
package.is_root,
),
module,
&build_state.project_root,
&rescript_version,
false,
bsc_path,
&build_state.packages,
);
// if let Err(error) = result.to_owned() {
// println!("{}", error);
Expand Down Expand Up @@ -291,6 +296,7 @@ pub fn compile(
&build_state.project_root,
package.is_root,
&package.name,
&package.package_dir,
&err,
);
compile_warnings.push_str(&err);
Expand All @@ -302,6 +308,7 @@ pub fn compile(
&build_state.project_root,
package.is_root,
&package.name,
&package.package_dir,
&err,
);
compile_errors.push_str(&err);
Expand All @@ -315,6 +322,7 @@ pub fn compile(
&build_state.project_root,
package.is_root,
&package.name,
&package.package_dir,
&err,
);
compile_warnings.push_str(&err);
Expand All @@ -327,6 +335,7 @@ pub fn compile(
&build_state.project_root,
package.is_root,
&package.name,
&package.package_dir,
&err,
);
compile_errors.push_str(&err);
Expand Down Expand Up @@ -374,8 +383,10 @@ fn compile_file(
root_path: &str,
version: &str,
is_interface: bool,
bsc_path: &str,
packages: &AHashMap<String, packages::Package>,
) -> Result<Option<String>, String> {
let build_path_abs = helpers::get_build_path(root_path, &package.name, package.is_root);
let build_path_abs = helpers::get_build_path(root_path, &package.package_dir, package.is_root);
let bsc_flags = bsconfig::flatten_flags(&package.bsconfig.bsc_flags);

let normal_deps = package
Expand All @@ -398,10 +409,15 @@ fn compile_file(
.concat()
.into_iter()
.map(|x| {
let package = &packages.get(&x).expect("expect package");
vec![
"-I".to_string(),
helpers::canonicalize_string_path(&helpers::get_build_path(root_path, &x, package.is_root))
.unwrap(),
helpers::canonicalize_string_path(&helpers::get_build_path(
root_path,
&package.package_dir,
package.is_root,
))
.unwrap(),
]
})
.collect::<Vec<Vec<String>>>();
Expand Down Expand Up @@ -527,7 +543,7 @@ fn compile_file(
]
.concat();

let to_mjs = Command::new(helpers::get_bsc(&root_path))
let to_mjs = Command::new(bsc_path)
.current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap())
.args(to_mjs_args)
.output();
Expand All @@ -552,7 +568,7 @@ fn compile_file(
build_path_abs.to_string() + "/" + &module_name + ".cmi",
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
&package.package_dir,
package.is_root,
))
.join(dir)
Expand All @@ -565,7 +581,7 @@ fn compile_file(
build_path_abs.to_string() + "/" + &module_name + ".cmj",
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
&package.package_dir,
package.is_root,
))
.join(dir)
Expand All @@ -575,7 +591,7 @@ fn compile_file(
build_path_abs.to_string() + "/" + &module_name + ".cmt",
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
&package.package_dir,
package.is_root,
))
.join(dir)
Expand All @@ -589,7 +605,7 @@ fn compile_file(
build_path_abs.to_string() + "/" + &module_name + ".cmti",
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
&package.package_dir,
package.is_root,
))
.join(dir)
Expand All @@ -612,7 +628,7 @@ fn compile_file(
std::path::Path::new(&package.package_dir).join(path),
std::path::Path::new(&helpers::get_bs_build_path(
root_path,
&package.name,
&package.package_dir,
package.is_root,
))
.join(path),
Expand All @@ -623,7 +639,7 @@ fn compile_file(
std::path::Path::new(&package.package_dir).join(path),
std::path::Path::new(&helpers::get_build_path(
root_path,
&package.name,
&package.package_dir,
package.is_root,
))
.join(std::path::Path::new(path).file_name().unwrap()),
Expand Down
Loading

0 comments on commit 145cd38

Please sign in to comment.