Skip to content

Commit cfeaeb4

Browse files
committed
clean *.re.js when running bsb -clean, fix #2956
1 parent 40991b1 commit cfeaeb4

File tree

6 files changed

+1495
-1313
lines changed

6 files changed

+1495
-1313
lines changed

jscomp/all.depend

+9-7
Original file line numberDiff line numberDiff line change
@@ -874,8 +874,9 @@ bsb/bsb_build_util.cmx : ext/string_map.cmx ext/string_hashtbl.cmx \
874874
ext/ext_list.cmx ext/ext_json_types.cmx ext/ext_json_parse.cmx \
875875
ext/ext_json.cmx ext/ext_array.cmx bsb/bsb_pkg.cmx bsb/bsb_log.cmx \
876876
bsb/bsb_exception.cmx bsb/bsb_build_schemas.cmx bsb/bsb_build_util.cmi
877-
bsb/bsb_clean.cmx : ext/ext_path.cmx bsb/bsb_unix.cmx bsb/bsb_log.cmx \
878-
bsb/bsb_config.cmx bsb/bsb_build_util.cmx bsb/bsb_clean.cmi
877+
bsb/bsb_clean.cmx : ext/ext_path.cmx bsb/bsb_unix.cmx \
878+
bsb/bsb_parse_sources.cmx bsb/bsb_log.cmx bsb/bsb_config.cmx \
879+
bsb/bsb_build_util.cmx bsb/bsb_clean.cmi
879880
bsb/bsb_config.cmx : ext/ext_path.cmx bsb/bsb_config.cmi
880881
bsb/bsb_config_parse.cmx : ext/string_map.cmx ext/string_hash_set.cmx \
881882
ext/literals.cmx ext/ext_path.cmx ext/ext_namespace.cmx ext/ext_list.cmx \
@@ -957,11 +958,12 @@ bsb/bsb_package_specs.cmx : ext/string_map.cmx ext/literals.cmx \
957958
bsb/bsb_parse_sources.cmx : ext/string_vec.cmx ext/string_set.cmx \
958959
ext/string_map.cmx ext/literals.cmx ext/ext_string.cmx \
959960
ext/ext_position.cmx ext/ext_path.cmx ext/ext_namespace.cmx \
960-
ext/ext_json_types.cmx ext/ext_json.cmx ext/ext_filename.cmx \
961-
ext/ext_file_pp.cmx ext/ext_array.cmx bsb/bsb_log.cmx \
962-
bsb/bsb_file_groups.cmx bsb/bsb_exception.cmx bsb/bsb_dir_index.cmx \
963-
bsb/bsb_db.cmx bsb/bsb_config.cmx bsb/bsb_build_util.cmx \
964-
bsb/bsb_build_schemas.cmx bsb/bsb_parse_sources.cmi
961+
ext/ext_json_types.cmx ext/ext_json_parse.cmx ext/ext_json.cmx \
962+
ext/ext_filename.cmx ext/ext_file_pp.cmx ext/ext_array.cmx \
963+
bsb/bsb_log.cmx bsb/bsb_file_groups.cmx bsb/bsb_exception.cmx \
964+
bsb/bsb_dir_index.cmx bsb/bsb_db.cmx bsb/bsb_config.cmx \
965+
bsb/bsb_build_util.cmx bsb/bsb_build_schemas.cmx \
966+
bsb/bsb_parse_sources.cmi
965967
bsb/bsb_pkg.cmx : ext/string_hashtbl.cmx ext/literals.cmx bsb/bsb_log.cmx \
966968
bsb/bsb_exception.cmx bsb/bsb_pkg.cmi
967969
bsb/bsb_query.cmx : ext/string_map.cmx ext/ext_json_noloc.cmx \

jscomp/bsb/bsb_clean.ml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ let clean_bs_garbage bsc_dir proj_dir =
4545
if Sys.file_exists x then
4646
Bsb_unix.remove_dir_recursive x in
4747
try
48+
Bsb_parse_sources.clean_re_js proj_dir; (* clean re.js files*)
4849
ninja_clean bsc_dir proj_dir ;
4950
List.iter try_remove Bsb_config.all_lib_artifacts;
5051
with

jscomp/bsb/bsb_config_parse.ml

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ let interpret_json
293293
~not_dev
294294
~root: cwd
295295
~cut_generators: !cut_generators
296+
~clean_staled_bs_js:true (*TODO: IMPROVE if not suffix .bs.js *)
296297
~namespace
297298
x in
298299
if generate_watch_metadata then

jscomp/bsb/bsb_parse_sources.ml

+88-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type cxt = {
5151
cut_generators : bool;
5252
traverse : bool;
5353
namespace : string option;
54+
clean_staled_bs_js: bool;
5455
}
5556

5657
(** [public] has a list of modules, we do a sanity check to see if all the listed
@@ -217,6 +218,9 @@ let try_unlink s =
217218
Bsb_log.info "@{<info>Failed to remove %s}@." s
218219

219220

221+
(** This is the only place where we do some removal during scanning,
222+
configurabl
223+
*)
220224
let clean_staled_bs_js_files
221225
(context : cxt)
222226
(cur_sources : _ String_map.t )
@@ -369,7 +373,10 @@ let rec
369373
| Some s, _ -> parse_sources cxt s
370374
in
371375
(** Do some clean up *)
372-
clean_staled_bs_js_files cxt cur_sources (Lazy.force file_array );
376+
if cxt.clean_staled_bs_js then
377+
begin
378+
clean_staled_bs_js_files cxt cur_sources (Lazy.force file_array )
379+
end;
373380
Bsb_file_groups.merge {
374381
files = [ { dir ;
375382
sources = cur_sources;
@@ -396,7 +403,8 @@ and parsing_single_source ({not_dev; dir_index ; cwd} as cxt ) (x : Ext_json_typ
396403
| Obj {map} ->
397404
let current_dir_index =
398405
match String_map.find_opt Bsb_build_schemas.type_ map with
399-
| Some (Str {str="dev"}) -> Bsb_dir_index.get_dev_index ()
406+
| Some (Str {str="dev"}) ->
407+
Bsb_dir_index.get_dev_index ()
400408
| Some _ -> Bsb_exception.config_error x {|type field expect "dev" literal |}
401409
| None -> dir_index in
402410
if not_dev && not (Bsb_dir_index.is_lib_dir current_dir_index) then
@@ -430,13 +438,89 @@ and parse_sources ( cxt : cxt) (sources : Ext_json_types.t ) =
430438

431439

432440

433-
let scan ~not_dev ~root ~cut_generators ~namespace x =
441+
let scan ~not_dev ~root ~cut_generators ~namespace ~clean_staled_bs_js x =
434442
parse_sources {
435443
not_dev;
436444
dir_index = Bsb_dir_index.lib_dir_index;
437445
cwd = Filename.current_dir_name;
438446
root ;
439447
cut_generators;
440448
namespace;
449+
clean_staled_bs_js;
441450
traverse = false
442-
} x
451+
} x
452+
453+
454+
455+
type walk_cxt = {
456+
cwd : string ;
457+
root : string;
458+
traverse : bool;
459+
}
460+
let rec walk_sources (cxt : walk_cxt) (sources : Ext_json_types.t) =
461+
match sources with
462+
| Arr {content = file_groups} ->
463+
Array.iter (fun x -> walk_single_source cxt x) file_groups
464+
| x -> walk_single_source cxt x
465+
and walk_single_source cxt (x : Ext_json_types.t) =
466+
match x with
467+
| Str {str = dir}
468+
->
469+
walk_source_dir_map
470+
{cxt with
471+
cwd =
472+
Ext_path.concat cxt.cwd
473+
(Ext_filename.simple_convert_node_path_to_os_path dir)
474+
}
475+
String_map.empty
476+
| Obj {map} ->
477+
begin match String_map.find_opt Bsb_build_schemas.dir map with
478+
| Some (Str{str}) ->
479+
let dir = Ext_filename.simple_convert_node_path_to_os_path str in
480+
walk_source_dir_map
481+
{cxt with cwd = Ext_path.concat cxt.cwd dir} map
482+
| _ -> ()
483+
end
484+
| _ -> ()
485+
and walk_source_dir_map (cxt : walk_cxt) (input : Ext_json_types.t String_map.t) =
486+
let working_dir = Filename.concat cxt.root cxt.cwd in
487+
let file_array = Sys.readdir working_dir in
488+
file_array |> Array.iter begin fun file ->
489+
if Ext_string.ends_with file ".re.js" then
490+
Sys.remove file
491+
end;
492+
let sub_dirs_field =
493+
String_map.find_opt Bsb_build_schemas.subdirs input in
494+
let cxt_traverse = cxt.traverse in
495+
match sub_dirs_field, cxt_traverse with
496+
| None, true
497+
| Some(True _), _ ->
498+
file_array |> Array.iter begin fun f ->
499+
if Sys.is_directory (Filename.concat working_dir f ) then
500+
walk_source_dir_map
501+
{cxt with
502+
cwd =
503+
Ext_path.concat cxt.cwd
504+
(Ext_filename.simple_convert_node_path_to_os_path f);
505+
traverse = true
506+
} String_map.empty
507+
end
508+
| None, _
509+
| Some (False _), _ -> ()
510+
| Some s, _ -> walk_sources cxt s
511+
512+
let clean_re_js root =
513+
match Ext_json_parse.parse_json_from_file
514+
(Filename.concat root Literals.bsconfig_json) with
515+
| Obj {map ; loc} ->
516+
begin match String_map.find_opt Bsb_build_schemas.sources map with
517+
| Some config ->
518+
(try
519+
walk_sources
520+
{ root ; traverse = true; cwd = Filename.current_dir_name} config
521+
with _ -> ())
522+
| None -> ()
523+
end
524+
| _ -> ()
525+
| exception _ -> ()
526+

jscomp/bsb/bsb_parse_sources.mli

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ val scan :
3434
root: string ->
3535
cut_generators: bool ->
3636
namespace : string option ->
37+
clean_staled_bs_js:bool ->
3738
Ext_json_types.t ->
38-
Bsb_file_groups.t
39+
Bsb_file_groups.t
40+
41+
val clean_re_js:
42+
string -> unit

0 commit comments

Comments
 (0)