Skip to content

Commit 6aab920

Browse files
authored
Support rescript.json (#6382)
* add support rescript.json * migrate to rescript.json * add warning message and its test * add CHANGELOG
1 parent 167c586 commit 6aab920

File tree

55 files changed

+191
-93
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+191
-93
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions

jscomp/bsb/bsb_build_util.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
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. *)
@@ -101,7 +101,7 @@ let resolve_bsb_magic_file ~cwd ~desc p : result =
101101

102102
(** converting a file from Linux path format to Windows *)
103103

104-
(**
104+
(**
105105
{[
106106
mkp "a/b/c/d";;
107107
mkp "/a/b/c/d"
@@ -155,9 +155,8 @@ let extract_pinned_dependencies (map : Ext_json_types.t Map_string.t) : Set_stri
155155

156156
let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
157157
~(top : top) (dir : string) (queue : _ Queue.t) ~pinned_dependencies =
158-
let bsconfig_json = dir // Literals.bsconfig_json in
159-
match Ext_json_parse.parse_json_from_file bsconfig_json with
160-
| Obj { map; loc } ->
158+
match Bsb_config_load.load_json ~per_proj_dir:dir ~warn_legacy_config:false with
159+
| _, Obj { map; loc } ->
161160
let cur_package_name =
162161
match Map_string.find_opt map Bsb_build_schemas.name with
163162
| Some (Str { str; loc }) ->
@@ -183,7 +182,7 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
183182
else
184183
let explore_deps (deps : string) pinned_dependencies =
185184
map
186-
|? ( deps,
185+
|? ( deps,
187186
`Arr
188187
(fun (new_packages : Ext_json_types.t array) ->
189188
Ext_array.iter new_packages (fun js ->
@@ -205,8 +204,8 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
205204
| Expect_name n when Set_string.mem pinned_dependencies n -> true
206205
| _ -> false
207206
in
208-
let pinned_dependencies = match is_pinned with
209-
| true ->
207+
let pinned_dependencies = match is_pinned with
208+
| true ->
210209
let transitive_pinned_dependencies = extract_pinned_dependencies map
211210
in
212211
Set_string.union transitive_pinned_dependencies pinned_dependencies

jscomp/bsb/bsb_config_load.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let ( // ) = Ext_path.combine
2+
3+
let load_json ~(per_proj_dir : string) ~(warn_legacy_config : bool)
4+
: string * Ext_json_types.t =
5+
let filename, abs, in_chan =
6+
let filename = Literals.rescript_json in
7+
let abs = (per_proj_dir // filename) in
8+
match open_in abs
9+
with
10+
| in_chan -> (filename, abs, in_chan)
11+
| exception e ->
12+
let filename = Literals.bsconfig_json in
13+
let abs = (per_proj_dir // filename) in
14+
match open_in abs
15+
with
16+
| in_chan -> (filename, abs, in_chan)
17+
| exception _ -> raise e (* forward error from rescript.json *)
18+
in
19+
if warn_legacy_config && filename = Literals.bsconfig_json then
20+
print_endline "Warning: bsconfig.json is deprecated. Migrate it to rescript.json\n";
21+
match Ext_json_parse.parse_json_from_chan abs in_chan
22+
with
23+
| v -> close_in in_chan ; (filename, v)
24+
| exception e -> close_in in_chan ; raise e

jscomp/bsb/bsb_config_load.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val load_json :
2+
per_proj_dir:string -> warn_legacy_config:bool -> string * Ext_json_types.t

jscomp/bsb/bsb_config_parse.ml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ let extract_package_name_and_namespace (map : json_map) : string * string option
7070
- the running bsb need delete stale build artifacts
7171
(kinda check npm upgrade)
7272
73-
Note if the setup is correct:
73+
Note if the setup is correct:
7474
the running compiler and node_modules/rescript
75-
should be the same version,
76-
The exact check is that the running compiler should have a
75+
should be the same version,
76+
The exact check is that the running compiler should have a
7777
compatible runtime version installed, the location of the
7878
compiler is actually not relevant.
7979
We disable the check temporarily
@@ -235,9 +235,13 @@ let extract_js_post_build (map : json_map) cwd : string option =
235235
|> ignore;
236236
!js_post_build_cmd
237237

238-
(** ATT: make sure such function is re-entrant.
238+
(** ATT: make sure such function is re-entrant.
239239
With a given [cwd] it works anywhere*)
240-
let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
240+
let interpret_json
241+
~(filename : string)
242+
~(json : Ext_json_types.t)
243+
~(package_kind : Bsb_package_kind.t)
244+
~(per_proj_dir : string)
241245
: Bsb_config_types.t =
242246
(* we should not resolve it too early,
243247
since it is external configuration, no {!Bsb_build_util.convert_and_resolve_path}
@@ -253,8 +257,7 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
253257
1. if [build.ninja] does use [ninja] we need set a variable
254258
2. we need store it so that we can call ninja correctly
255259
*)
256-
match
257-
Ext_json_parse.parse_json_from_file (per_proj_dir // Literals.bsconfig_json)
260+
match json
258261
with
259262
| Obj { map } -> (
260263
let package_name, namespace = extract_package_name_and_namespace map in
@@ -349,17 +352,19 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
349352
(match package_kind with
350353
| Toplevel -> extract_uncurried map
351354
| Pinned_dependency x | Dependency x -> x.uncurried);
355+
filename;
352356
}
353357
| None ->
354-
Bsb_exception.invalid_spec "no sources specified in bsconfig.json")
355-
| _ -> Bsb_exception.invalid_spec "bsconfig.json expect a json object {}"
358+
Bsb_exception.invalid_spec ("no sources specified in " ^ filename))
359+
| _ -> Bsb_exception.invalid_spec (filename ^ " expect a json object {}")
356360

357361
let deps_from_bsconfig () =
358-
let json = Ext_json_parse.parse_json_from_file Literals.bsconfig_json in
359-
match json with
360-
| Obj { map } ->
361-
( Bsb_package_specs.from_map ~cwd:Bsb_global_paths.cwd map,
362+
let cwd = Bsb_global_paths.cwd in
363+
match Bsb_config_load.load_json ~per_proj_dir:cwd ~warn_legacy_config:false
364+
with
365+
| _, Obj { map } ->
366+
( Bsb_package_specs.from_map ~cwd map,
362367
Bsb_jsx.from_map map,
363368
extract_uncurried map,
364369
Bsb_build_util.extract_pinned_dependencies map )
365-
| _ -> assert false
370+
| _, _ -> assert false

jscomp/bsb/bsb_config_parse.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@
2525
val deps_from_bsconfig : unit -> Bsb_package_specs.t * Bsb_jsx.t * bool * Set_string.t
2626

2727
val interpret_json :
28-
package_kind:Bsb_package_kind.t -> per_proj_dir:string -> Bsb_config_types.t
28+
filename:string ->
29+
json:Ext_json_types.t ->
30+
package_kind:Bsb_package_kind.t ->
31+
per_proj_dir:string ->
32+
Bsb_config_types.t

jscomp/bsb/bsb_config_types.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ type t = {
6565
(* note when used as a dev mode, we will always ignore it *)
6666
gentype_config : gentype_config;
6767
uncurried: bool;
68+
69+
filename: string;
6870
}

jscomp/bsb/bsb_ninja_regen.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ let ( // ) = Ext_path.combine
3030
return None if we dont need regenerate
3131
otherwise return Some info
3232
*)
33-
let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
33+
let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir ~warn_legacy_config
3434
: Bsb_config_types.t option =
3535
let lib_artifacts_dir = Bsb_config.lib_bs in
3636
let lib_bs_dir = per_proj_dir // lib_artifacts_dir in
3737
let output_deps = lib_bs_dir // bsdeps in
3838
let check_result =
3939
Bsb_ninja_check.check ~package_kind ~per_proj_dir ~forced ~file:output_deps
4040
in
41+
let config_filename, config_json =
42+
Bsb_config_load.load_json ~per_proj_dir ~warn_legacy_config
43+
in
4144
match check_result with
4245
| Good -> None (* Fast path, no need regenerate ninja *)
4346
| Bsb_forced | Bsb_bsc_version_mismatch | Bsb_package_kind_inconsistent
@@ -52,7 +55,8 @@ let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
5255
Bsb_clean.clean_self per_proj_dir);
5356

5457
let config : Bsb_config_types.t =
55-
Bsb_config_parse.interpret_json ~package_kind ~per_proj_dir
58+
Bsb_config_parse.interpret_json
59+
~filename:config_filename ~json:config_json ~package_kind ~per_proj_dir
5660
in
5761
(* create directory, lib/bs, lib/js, lib/es6 etc *)
5862
Bsb_build_util.mkp lib_bs_dir;
@@ -75,5 +79,5 @@ let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
7579
since it may add files in the future *)
7680
Bsb_ninja_check.record ~package_kind ~per_proj_dir ~config
7781
~file:output_deps
78-
(Literals.bsconfig_json :: config.file_groups.globbed_dirs);
82+
(config.filename :: config.file_groups.globbed_dirs);
7983
Some config

jscomp/bsb/bsb_ninja_regen.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ val regenerate_ninja :
2626
package_kind:Bsb_package_kind.t ->
2727
forced:bool ->
2828
per_proj_dir:string ->
29+
warn_legacy_config:bool ->
2930
Bsb_config_types.t option
3031
(** Regenerate ninja file by need based on [.bsdeps]
3132
return None if we dont need regenerate

jscomp/bsb/bsb_world.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let make_world_deps cwd (config : Bsb_config_types.t option)
3030
let package_specs, jsx, uncurried, pinned_dependencies =
3131
match config with
3232
| None ->
33-
(* When this running bsb does not read bsconfig.json,
33+
(* When this running bsb does not read rescript.json,
3434
we will read such json file to know which [package-specs]
3535
it wants
3636
*)
@@ -70,6 +70,7 @@ let make_world_deps cwd (config : Bsb_config_types.t option)
7070
(if is_pinned then Pinned_dependency { package_specs; jsx; uncurried }
7171
else Dependency { package_specs; jsx; uncurried })
7272
~per_proj_dir:proj_dir ~forced:false
73+
~warn_legacy_config:false
7374
in
7475
let command =
7576
{ Bsb_unix.cmd = vendor_ninja; cwd = lib_bs_dir; args }

0 commit comments

Comments
 (0)