Skip to content

Commit 9a1c68b

Browse files
committed
continue refactoring
1 parent 62e43ec commit 9a1c68b

File tree

7 files changed

+124
-60
lines changed

7 files changed

+124
-60
lines changed

jscomp/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ BSB_SRCS= bsb_config \
307307
bsb_config_parse\
308308
bsb_unix\
309309
bsb_regex\
310+
bsb_clean\
310311
oCamlRes\
311312
bsb_templates\
312313
bsb_init

jscomp/all.depend

+9-5
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,8 @@ bsb/bsb_build_util.cmx : ext/string_map.cmx ext/string_hashtbl.cmx \
718718
ext/ext_filename.cmx ext/ext_array.cmx bsb/bsb_pkg.cmx \
719719
bsb/bsb_exception.cmx bsb/bsb_config.cmx bsb/bsb_build_schemas.cmx \
720720
bsb/bsb_build_util.cmi
721+
bsb/bsb_clean.cmx : ext/ext_filename.cmx bsb/bsb_unix.cmx bsb/bsb_config.cmx \
722+
bsb/bsb_build_util.cmx bsb/bsb_clean.cmi
721723
bsb/bsb_config.cmx : ext/string_set.cmx ext/literals.cmx ext/ext_string.cmx \
722724
ext/ext_filename.cmx bsb/bsb_config.cmi
723725
bsb/bsb_config_parse.cmx : ext/string_set.cmx ext/string_map.cmx \
@@ -752,13 +754,14 @@ bsb/bsb_helper_packer.cmx : ext/string_set.cmx ext/string_map.cmx \
752754
bsb/bsb_init.cmx : ext/string_hashtbl.cmx bsb/oCamlRes.cmx \
753755
ext/ext_string.cmx ext/ext_io.cmx bsb/bsb_templates.cmx bsb/bsb_regex.cmx \
754756
common/bs_version.cmx bsb/bsb_init.cmi
755-
bsb/bsb_log.cmx : bsb/bsb_log.cmi
757+
bsb/bsb_log.cmx : ext/ext_string.cmx ext/ext_color.cmx bsb/bsb_log.cmi
756758
bsb/bsb_main.cmx : ext/string_hash_set.cmx ext/literals.cmx ext/ext_sys.cmx \
757-
ext/ext_string.cmx ext/ext_filename.cmx ext/ext_color.cmx \
758-
ext/ext_array.cmx bsb/bsb_unix.cmx bsb/bsb_ninja_gen.cmx \
759-
bsb/bsb_merlin_gen.cmx bsb/bsb_init.cmx bsb/bsb_file.cmx \
759+
ext/ext_string.cmx ext/ext_filename.cmx ext/ext_array.cmx \
760+
bsb/bsb_unix.cmx bsb/bsb_ninja_gen.cmx bsb/bsb_merlin_gen.cmx \
761+
bsb/bsb_log.cmx bsb/bsb_init.cmx bsb/bsb_file.cmx \
760762
bsb/bsb_config_types.cmx bsb/bsb_config_parse.cmx bsb/bsb_config.cmx \
761-
bsb/bsb_build_util.cmx bsb/bsb_bsdeps.cmx bsb/bsb_main.cmi
763+
bsb/bsb_clean.cmx bsb/bsb_build_util.cmx bsb/bsb_bsdeps.cmx \
764+
bsb/bsb_main.cmi
762765
bsb/bsb_merlin_gen.cmx : ext/literals.cmx ext/ext_string.cmx \
763766
ext/ext_filename.cmx bsb/bsb_parse_sources.cmx bsb/bsb_config_types.cmx \
764767
bsb/bsb_config.cmx bsb/bsb_merlin_gen.cmi
@@ -792,6 +795,7 @@ bsb/bsb_watcher_gen.cmx : ext/ext_list.cmx ext/ext_json_noloc.cmx \
792795
bsb/oCamlRes.cmx :
793796
bsb/bsb_bsdeps.cmi :
794797
bsb/bsb_build_util.cmi : ext/ext_json_types.cmx
798+
bsb/bsb_clean.cmi :
795799
bsb/bsb_config.cmi : ext/string_set.cmi
796800
bsb/bsb_config_parse.cmi : bsb/bsb_config_types.cmx bsb/bsb_config.cmi
797801
bsb/bsb_default.cmi : ext/string_set.cmi bsb/bsb_config_types.cmx

jscomp/bsb/bsb_clean.ml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
let (//) = Ext_filename.combine
27+
28+
let clean_bs_garbage cwd =
29+
Format.fprintf Format.std_formatter "@{<info>Cleaning:@} in %s@." cwd ;
30+
let aux x =
31+
let x = (cwd // x) in
32+
if Sys.file_exists x then
33+
Bsb_unix.remove_dir_recursive x in
34+
try
35+
List.iter aux Bsb_config.all_lib_artifacts
36+
with
37+
e ->
38+
Format.fprintf Format.err_formatter "@{<warning>Failed@} to clean due to %s" (Printexc.to_string e)
39+
40+
41+
let clean_bs_deps cwd =
42+
Bsb_build_util.walk_all_deps cwd (fun { cwd} ->
43+
(* whether top or not always do the cleaning *)
44+
clean_bs_garbage cwd
45+
)
46+
47+
let clean_self cwd = clean_bs_garbage cwd

jscomp/bsb/bsb_clean.mli

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(* Copyright (C) 2017 Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
27+
val clean_bs_deps : string -> unit
28+
29+
val clean_self : string -> unit

jscomp/bsb/bsb_log.ml

+27
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,30 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
let color_enabled = ref (Unix.isatty Unix.stdin)
26+
let set_color ppf =
27+
Format.pp_set_formatter_tag_functions ppf
28+
({ (Format.pp_get_formatter_tag_functions ppf () ) with
29+
mark_open_tag = (fun s -> if !color_enabled then Ext_color.ansi_of_tag s else Ext_string.empty) ;
30+
mark_close_tag = (fun _ -> if !color_enabled then Ext_color.reset_lit else Ext_string.empty);
31+
})
32+
33+
34+
let setup () =
35+
begin
36+
Format.pp_set_mark_tags Format.std_formatter true ;
37+
Format.pp_set_mark_tags Format.err_formatter true;
38+
Format.pp_set_mark_tags Format.str_formatter true;
39+
set_color Format.std_formatter ;
40+
set_color Format.err_formatter;
41+
set_color Format.str_formatter
42+
end
43+
44+
45+
46+
let print_string_args (args : string array) =
47+
for i = 0 to Array.length args - 1 do
48+
print_string (Array.unsafe_get args i) ;
49+
print_string Ext_string.single_space;
50+
done ;
51+
print_newline ()

jscomp/bsb/bsb_log.mli

+3
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
val color_enabled : bool ref
2526

27+
val setup : unit -> unit
2628

29+
val print_string_args : string array -> unit

jscomp/bsb/bsb_main.ml

+8-55
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,9 @@ let make_world = ref false
105105
let set_make_world () = make_world := true
106106

107107

108+
;; Bsb_log.setup ()
108109

109110

110-
let color_enabled = ref (Unix.isatty Unix.stdin)
111-
let set_color ppf =
112-
Format.pp_set_formatter_tag_functions ppf
113-
({ (Format.pp_get_formatter_tag_functions ppf () ) with
114-
mark_open_tag = (fun s -> if !color_enabled then Ext_color.ansi_of_tag s else Ext_string.empty) ;
115-
mark_close_tag = (fun _ -> if !color_enabled then Ext_color.reset_lit else Ext_string.empty);
116-
})
117-
118-
let () =
119-
begin
120-
Format.pp_set_mark_tags Format.std_formatter true ;
121-
Format.pp_set_mark_tags Format.err_formatter true;
122-
Format.pp_set_mark_tags Format.str_formatter true;
123-
set_color Format.std_formatter ;
124-
set_color Format.err_formatter;
125-
set_color Format.str_formatter
126-
end
127-
128-
129-
130-
let clean_bs_garbage cwd =
131-
Format.fprintf Format.std_formatter "@{<info>Cleaning:@} in %s@." cwd ;
132-
let aux x =
133-
let x = (cwd // x) in
134-
if Sys.file_exists x then
135-
Bsb_unix.remove_dir_recursive x in
136-
try
137-
List.iter aux Bsb_config.all_lib_artifacts
138-
with
139-
e ->
140-
Format.fprintf Format.err_formatter "@{<warning>Failed@} to clean due to %s" (Printexc.to_string e)
141-
142-
143-
let clean_bs_deps () =
144-
Bsb_build_util.walk_all_deps cwd (fun { cwd} ->
145-
(* whether top or not always do the cleaning *)
146-
clean_bs_garbage cwd
147-
)
148-
149-
let clean_self () = clean_bs_garbage cwd
150-
151111

152112
(** Regenerate ninja file if necessary
153113
return None if we dont need regenerate
@@ -177,7 +137,7 @@ let regenerate_ninja
177137
| Other _ ->
178138
if check_result = Bsb_bsc_version_mismatch then begin
179139
print_endline "Also clean current repo due to we have detected a different compiler";
180-
clean_self ();
140+
Bsb_clean.clean_self cwd;
181141
end ;
182142
Bsb_build_util.mkp (cwd // Bsb_config.lib_bs);
183143
let config =
@@ -206,19 +166,19 @@ let regenerate_ninja
206166

207167
let bsb_main_flags : (string * Arg.spec * string) list=
208168
[
209-
"-color", Arg.Set color_enabled,
169+
"-color", Arg.Set Bsb_log.color_enabled,
210170
" forced color output"
211171
;
212-
"-no-color", Arg.Clear color_enabled,
172+
"-no-color", Arg.Clear Bsb_log.color_enabled,
213173
" forced no color output";
214174
"-w", Arg.Set watch_mode,
215175
" Watch mode" ;
216176
regen, Arg.Set force_regenerate,
217177
" (internal) Always regenerate build.ninja no matter bsconfig.json is changed or not (for debugging purpose)"
218178
;
219-
"-clean-world", Arg.Unit clean_bs_deps,
179+
"-clean-world", Arg.Unit (fun _ -> Bsb_clean.clean_bs_deps cwd),
220180
" Clean all bs dependencies";
221-
"-clean", Arg.Unit clean_self,
181+
"-clean", Arg.Unit (fun _ -> Bsb_clean.clean_self cwd),
222182
" Clean only current project";
223183
"-make-world", Arg.Unit set_make_world,
224184
" Build all dependencies and itself ";
@@ -231,13 +191,6 @@ let bsb_main_flags : (string * Arg.spec * string) list=
231191
]
232192

233193

234-
let print_string_args (args : string array) =
235-
for i = 0 to Array.length args - 1 do
236-
print_string (Array.unsafe_get args i) ;
237-
print_string Ext_string.single_space;
238-
done ;
239-
print_newline ()
240-
241194

242195
(* Note that [keepdepfile] only makes sense when combined with [deps] for optimization
243196
It has to be the last command of [bsb]
@@ -257,7 +210,7 @@ let ninja_command_exit vendor_ninja ninja_args =
257210
(Filename.quote vendor_ninja) "-C" Bsb_config.lib_bs
258211
else
259212
let args = [|"ninja.exe"; "-C"; Bsb_config.lib_bs |] in
260-
print_string_args args ;
213+
Bsb_log.print_string_args args ;
261214
Unix.execvp vendor_ninja args
262215
else
263216
let fixed_args_length = 3 in
@@ -279,7 +232,7 @@ let ninja_command_exit vendor_ninja ninja_args =
279232
| 1 -> "-C"
280233
| 2 -> Bsb_config.lib_bs
281234
| _ -> Array.unsafe_get ninja_args (i - fixed_args_length) )) in
282-
print_string_args args ;
235+
Bsb_log.print_string_args args ;
283236
Unix.execvp vendor_ninja args
284237

285238

0 commit comments

Comments
 (0)