Skip to content

Commit 229a98f

Browse files
committed
copy compiler files
1 parent c227502 commit 229a98f

File tree

4 files changed

+75
-39
lines changed

4 files changed

+75
-39
lines changed

src/build.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::bsconfig;
22
use crate::helpers;
3+
use crate::helpers::get_bs_build_path;
4+
use crate::helpers::get_package_path;
35
use crate::package_tree;
46
use ahash::{AHashMap, AHashSet};
57
use log::{debug, error};
@@ -421,16 +423,16 @@ pub fn compile_mlmap(package: &package_tree::Package, namespace: &str, root_path
421423
pub fn compile_file(
422424
package_name: &str,
423425
ast_path: &str,
424-
source: &Module,
426+
module: &Module,
425427
root_path: &str,
426428
is_interface: bool,
427-
) -> Option<String> {
429+
) -> Result<(), String> {
428430
let build_path_abs = helpers::get_build_path(root_path, package_name);
429431
let pkg_path_abs = helpers::get_package_path(root_path, package_name);
430432
let abs_node_modules_path = helpers::get_node_modules_path(root_path);
431-
let bsc_flags = bsconfig::flatten_flags(&source.package.bsconfig.bsc_flags);
433+
let bsc_flags = bsconfig::flatten_flags(&module.package.bsconfig.bsc_flags);
432434

433-
let normal_deps = source
435+
let normal_deps = module
434436
.package
435437
.bsconfig
436438
.bs_dependencies
@@ -453,12 +455,12 @@ pub fn compile_file(
453455
.map(|x| vec!["-I".to_string(), helpers::get_build_path(root_path, &x)])
454456
.collect::<Vec<Vec<String>>>();
455457

456-
let namespace_args = match source.namespace.to_owned() {
458+
let namespace_args = match module.namespace.to_owned() {
457459
Some(namespace) => vec!["-bs-ns".to_string(), namespace],
458460
None => vec![],
459461
};
460462

461-
let read_cmi_args = match source.asti_path {
463+
let read_cmi_args = match module.asti_path {
462464
Some(_) => {
463465
if is_interface {
464466
vec![]
@@ -469,27 +471,21 @@ pub fn compile_file(
469471
_ => vec![],
470472
};
471473

474+
let module_name =
475+
helpers::file_path_to_module_name(&module.file_path, module.namespace.to_owned());
476+
472477
let implementation_args = if is_interface {
473-
debug!(
474-
"Compiling interface file: {}",
475-
&helpers::file_path_to_module_name(
476-
&source.interface_file_path.as_ref().unwrap(),
477-
source.namespace.to_owned()
478-
)
479-
);
478+
debug!("Compiling interface file: {}", &module_name);
480479
vec![]
481480
} else {
482-
debug!(
483-
"Compiling file: {}",
484-
&helpers::file_path_to_module_name(&source.file_path, source.namespace.to_owned())
485-
);
481+
debug!("Compiling file: {}", &module_name);
486482
vec![
487483
"-bs-package-name".to_string(),
488-
source.package.bsconfig.name.to_owned(),
484+
module.package.bsconfig.name.to_owned(),
489485
"-bs-package-output".to_string(),
490486
format!(
491487
"es6:{}:.mjs",
492-
Path::new(&source.file_path)
488+
Path::new(&module.file_path)
493489
.strip_prefix(pkg_path_abs)
494490
.unwrap()
495491
.parent()
@@ -528,8 +524,36 @@ pub fn compile_file(
528524
.output();
529525

530526
match to_mjs {
531-
Ok(x) if !x.status.success() => Some(std::str::from_utf8(&x.stderr).expect("").to_string()),
532-
Err(e) => Some(format!("ERROR, {}, {:?}", e, ast_path)),
533-
Ok(_) => None,
527+
Ok(x) if !x.status.success() => Err(std::str::from_utf8(&x.stderr).expect("").to_string()),
528+
Err(e) => Err(format!("ERROR, {}, {:?}", e, ast_path)),
529+
Ok(_) => {
530+
if !is_interface {
531+
let dir = std::path::Path::new(&module.file_path)
532+
.strip_prefix(get_package_path(root_path, &module.package.name))
533+
.unwrap()
534+
.parent()
535+
.unwrap();
536+
537+
let _ = std::fs::copy(
538+
build_path_abs.to_string() + "/" + &module_name + ".cmi",
539+
std::path::Path::new(&get_bs_build_path(root_path, &module.package.name))
540+
.join(dir)
541+
.join(module_name.to_owned() + ".cmi"),
542+
);
543+
let _ = std::fs::copy(
544+
build_path_abs.to_string() + "/" + &module_name + ".cmj",
545+
std::path::Path::new(&get_bs_build_path(root_path, &module.package.name))
546+
.join(dir)
547+
.join(module_name.to_owned() + ".cmj"),
548+
);
549+
let _ = std::fs::copy(
550+
build_path_abs.to_string() + "/" + &module_name + ".cmt",
551+
std::path::Path::new(&get_bs_build_path(root_path, &module.package.name))
552+
.join(dir)
553+
.join(module_name.to_owned() + ".cmt"),
554+
);
555+
}
556+
Ok(())
557+
}
534558
}
535559
}

src/helpers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ pub fn get_package_path(root: &str, package_name: &str) -> String {
3232
}
3333

3434
pub fn get_build_path(root: &str, package_name: &str) -> String {
35+
format!("{}/node_modules/{}/lib/ocaml", root, package_name)
36+
}
37+
38+
pub fn get_bs_build_path(root: &str, package_name: &str) -> String {
3539
format!("{}/node_modules/{}/lib/bs", root, package_name)
3640
}
3741

src/main.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ pub mod helpers;
55
pub mod package_tree;
66
pub mod structure_hashmap;
77
pub mod watcher;
8-
use crate::grouplike::*;
98
use ahash::AHashSet;
109
use console::{style, Emoji};
1110
use indicatif::ProgressBar;
@@ -23,6 +22,10 @@ fn clean() {
2322

2423
packages.iter().for_each(|(_, package)| {
2524
println!("Cleaning {}...", package.name);
25+
let path = std::path::Path::new(&package.package_dir)
26+
.join("lib")
27+
.join("ocaml");
28+
let _ = std::fs::remove_dir_all(path);
2629
let path = std::path::Path::new(&package.package_dir)
2730
.join("lib")
2831
.join("bs");
@@ -133,44 +136,52 @@ fn build() {
133136
modules
134137
.par_iter()
135138
.map(|(module_name, module)| {
136-
let mut stderr = None;
139+
let mut stderr = "".to_string();
137140
if module.deps.is_subset(&compiled_modules)
138141
&& !compiled_modules.contains(module_name)
139142
{
140143
match module.source_type.to_owned() {
141-
build::SourceType::MlMap => (Some(module_name.to_owned()), None),
144+
build::SourceType::MlMap => (Some(module_name.to_owned()), stderr),
142145
build::SourceType::SourceFile => {
143146
// compile interface first
144147
match module.asti_path.to_owned() {
145148
Some(asti_path) => {
146-
let asti_err = build::compile_file(
149+
let result = build::compile_file(
147150
&module.package.name,
148151
&asti_path,
149152
module,
150153
&project_root,
151154
true,
152155
);
153-
stderr = stderr.mappend(asti_err);
156+
match result {
157+
Err(err) => stderr.push_str(&err),
158+
Ok(()) => (),
159+
}
154160
}
155161
_ => (),
156162
}
157163

158-
let ast_err = build::compile_file(
164+
let result = build::compile_file(
159165
&module.package.name,
160166
&module.ast_path.to_owned().unwrap(),
161167
module,
162168
&project_root,
163169
false,
164170
);
165171

166-
(Some(module_name.to_owned()), stderr.mappend(ast_err))
172+
match result {
173+
Err(err) => stderr.push_str(&err),
174+
Ok(()) => (),
175+
}
176+
177+
(Some(module_name.to_owned()), stderr)
167178
}
168179
}
169180
} else {
170-
(None, None)
181+
(None, stderr)
171182
}
172183
})
173-
.collect::<Vec<(Option<String>, Option<String>)>>()
184+
.collect::<Vec<(Option<String>, String)>>()
174185
.iter()
175186
.for_each(|(module_name, stderr)| {
176187
module_name.iter().for_each(|name| {
@@ -181,10 +192,8 @@ fn build() {
181192
compiled_modules.insert(name.to_string());
182193
});
183194

184-
stderr.iter().for_each(|err| {
185-
compile_errors.push_str(err);
186-
// error!("Some error were generated compiling this round: \n {}", err);
187-
})
195+
compile_errors.push_str(&stderr);
196+
// error!("Some error were generated compiling this round: \n {}", err);
188197
});
189198

190199
files_total_count += files_current_loop_count;

src/package_tree.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::bsconfig;
22
use crate::bsconfig::*;
33
use crate::helpers;
4-
use crate::helpers::get_build_path;
5-
use crate::helpers::get_package_path;
64
use crate::structure_hashmap;
75
use ahash::{AHashMap, AHashSet};
86
use convert_case::{Case, Casing};
@@ -92,7 +90,7 @@ fn get_package_dir(package_name: &str, is_root: bool, project_root: &str) -> Str
9290
if is_root {
9391
project_root.to_owned()
9492
} else {
95-
get_package_path(project_root, package_name)
93+
helpers::get_package_path(project_root, package_name)
9694
}
9795
}
9896

@@ -285,7 +283,8 @@ pub fn make(root_folder: &str) -> AHashMap<String, Package> {
285283
.for_each(|package| match &package.dirs {
286284
Some(dirs) => dirs.iter().for_each(|dir| {
287285
let _ = std::fs::create_dir_all(
288-
std::path::Path::new(&get_build_path(root_folder, &package.name)).join(dir),
286+
std::path::Path::new(&helpers::get_bs_build_path(root_folder, &package.name))
287+
.join(dir),
289288
);
290289
}),
291290
None => (),

0 commit comments

Comments
 (0)