Skip to content

Commit 80ee525

Browse files
committed
Remove global state for project root.
Move to config.
1 parent 7ccd648 commit 80ee525

File tree

6 files changed

+124
-131
lines changed

6 files changed

+124
-131
lines changed

jscomp/gentype/Config_.ml

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
module ModuleNameMap = Map.Make (ModuleName)
22

3-
let bsbProjectRoot = ref ""
4-
let projectRoot = ref ""
5-
63
type module_ = CommonJS | ES6
74
type bsVersion = int * int * int
85

96
type config = {
7+
mutable bsbProjectRoot : string;
108
bsDependencies : string list;
119
mutable emitImportCurry : bool;
1210
mutable emitImportReact : bool;
@@ -16,13 +14,15 @@ type config = {
1614
module_ : module_;
1715
namespace : string option;
1816
platformLib : string;
17+
mutable projectRoot : string;
1918
shimsMap : ModuleName.t ModuleNameMap.t;
2019
sources : Ext_json_types.t option;
2120
suffix : string;
2221
}
2322

2423
let default =
2524
{
25+
bsbProjectRoot = "";
2626
bsDependencies = [];
2727
emitImportCurry = false;
2828
emitImportReact = false;
@@ -32,6 +32,7 @@ let default =
3232
module_ = ES6;
3333
namespace = None;
3434
platformLib = "";
35+
projectRoot = "";
3536
shimsMap = ModuleNameMap.empty;
3637
sources = None;
3738
suffix = "";
@@ -86,7 +87,25 @@ let setDebug ~gtconf =
8687
| Some (Obj { map }) -> Map_string.iter map Debug.setItem
8788
| _ -> ()
8889

90+
let bsconfig = "bsconfig.json"
91+
92+
let rec findProjectRoot ~dir =
93+
if Sys.file_exists (Filename.concat dir bsconfig) then dir
94+
else
95+
let parent = dir |> Filename.dirname in
96+
if parent = dir then (
97+
prerr_endline
98+
("Error: cannot find project root containing " ^ bsconfig ^ ".");
99+
assert false)
100+
else findProjectRoot ~dir:parent
101+
89102
let readConfig ~bsVersion ~getBsConfigFile ~namespace =
103+
let projectRoot = findProjectRoot ~dir:(Sys.getcwd ()) in
104+
let bsbProjectRoot =
105+
match Sys.getenv_opt "BSB_PROJECT_ROOT" with
106+
| None -> projectRoot
107+
| Some s -> s
108+
in
90109
let parseConfig ~bsconf ~gtconf =
91110
let moduleString = gtconf |> getStringOption "module" in
92111
let exportInterfacesBool = gtconf |> getBool "exportInterfaces" in
@@ -150,9 +169,9 @@ let readConfig ~bsVersion ~getBsConfigFile ~namespace =
150169
| Some externalStdlib -> externalStdlib
151170
in
152171
if !Debug.config then (
153-
Log_.item "Project root: %s\n" !projectRoot;
154-
if !bsbProjectRoot <> !projectRoot then
155-
Log_.item "bsb project root: %s\n" !bsbProjectRoot;
172+
Log_.item "Project root: %s\n" projectRoot;
173+
if bsbProjectRoot <> projectRoot then
174+
Log_.item "bsb project root: %s\n" bsbProjectRoot;
156175
Log_.item "Config module:%s shims:%d entries bsVersion:%d.%d.%d\n"
157176
(match moduleString with None -> "" | Some s -> s)
158177
(shimsMap |> ModuleNameMap.cardinal)
@@ -186,6 +205,7 @@ let readConfig ~bsVersion ~getBsConfigFile ~namespace =
186205
| _ -> default.sources
187206
in
188207
{
208+
bsbProjectRoot;
189209
bsDependencies;
190210
suffix;
191211
emitImportCurry = false;
@@ -196,11 +216,12 @@ let readConfig ~bsVersion ~getBsConfigFile ~namespace =
196216
module_;
197217
namespace;
198218
platformLib;
219+
projectRoot;
199220
shimsMap;
200221
sources;
201222
}
202223
in
203-
match getBsConfigFile () with
224+
match getBsConfigFile ~projectRoot with
204225
| Some bsConfigFile -> (
205226
try
206227
let json = bsConfigFile |> Ext_json_parse.parse_json_from_file in

jscomp/gentype/EmitJs.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,12 @@ let rec emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
225225
let nameGen = EmitText.newNameGen () in
226226
let importPath =
227227
fileName
228-
|> ModuleResolver.resolveModule ~importExtension:config.suffix
228+
|> ModuleResolver.resolveModule ~config ~importExtension:config.suffix
229229
~outputFileRelative ~resolver ~useBsDependencies:false
230230
in
231231
let fileNameBs = fileName |> ModuleName.forBsFile in
232232
let envWithRequires =
233-
fileNameBs |> requireModule ~import:false ~env ~importPath
233+
fileNameBs |> requireModule ~import:false ~env ~importPath
234234
in
235235
let default = "default" in
236236
let make = "make" in

jscomp/gentype/GenType.ml

+53-58
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
open GenTypeCommon
2-
31
let version = Version.version
4-
52
let signFile s = s
63

74
type cliCommand = Add of string | Clean | NoOp | Rm of string list
@@ -30,7 +27,7 @@ let cli () =
3027
("-clean", Arg.Unit setClean, "clean all the generated files");
3128
("-cmt-add", Arg.String setAdd, "compile a .cmt[i] file");
3229
( "-cmt-rm",
33-
Arg.String (fun s -> setRm [s]),
30+
Arg.String (fun s -> setRm [ s ]),
3431
"remove one or more .cmt[i] files" );
3532
("-version", Arg.Unit versionAndExit, "show version information and exit");
3633
("--version", Arg.Unit versionAndExit, "show version information and exit");
@@ -39,63 +36,61 @@ let cli () =
3936
let executeCliCommand ~bsVersion cliCommand =
4037
match cliCommand with
4138
| Add s ->
42-
Log_.Color.forceColor := true;
43-
let splitColon = Str.split (Str.regexp ":") s in
44-
let cmt, mlast =
45-
match splitColon with
46-
| cmt :: rest ->
47-
let mlast = rest |> String.concat "" in
48-
(cmt, mlast)
49-
| _ -> assert false
50-
in
51-
let config =
52-
Paths.readConfig ~bsVersion ~namespace:(cmt |> Paths.findNameSpace)
53-
in
54-
if !Debug.basic then Log_.item "Add %s %s\n" cmt mlast;
55-
cmt |> GenTypeMain.processCmtFile ~signFile ~config;
56-
exit 0
39+
Log_.Color.forceColor := true;
40+
let splitColon = Str.split (Str.regexp ":") s in
41+
let cmt, mlast =
42+
match splitColon with
43+
| cmt :: rest ->
44+
let mlast = rest |> String.concat "" in
45+
(cmt, mlast)
46+
| _ -> assert false
47+
in
48+
let config =
49+
Paths.readConfig ~bsVersion ~namespace:(cmt |> Paths.findNameSpace)
50+
in
51+
if !Debug.basic then Log_.item "Add %s %s\n" cmt mlast;
52+
cmt |> GenTypeMain.processCmtFile ~signFile ~config;
53+
exit 0
5754
| Clean ->
58-
let config = Paths.readConfig ~bsVersion ~namespace:None in
59-
let sourceDirs =
60-
ModuleResolver.readSourceDirs ~configSources:config.sources
61-
in
62-
if !Debug.basic then
63-
Log_.item "Clean %d dirs\n" (sourceDirs.dirs |> List.length);
64-
let count = ref 0 in
65-
sourceDirs.dirs
66-
|> List.iter (fun dir ->
67-
let files = Sys.readdir dir in
68-
files
69-
|> Array.iter (fun file ->
70-
if
71-
Filename.check_suffix file ".re"
72-
|| Filename.check_suffix file ".res"
73-
then
74-
let extension = EmitType.outputFileSuffix ~config in
75-
let generated =
76-
Filename.concat dir
77-
((file |> Filename.chop_extension) ^ extension)
78-
in
79-
if Sys.file_exists generated then (
80-
Unix.unlink generated;
81-
incr count)));
82-
if !Debug.basic then Log_.item "Cleaned %d files\n" !count;
83-
exit 0
55+
let config = Paths.readConfig ~bsVersion ~namespace:None in
56+
let sourceDirs = ModuleResolver.readSourceDirs ~config in
57+
if !Debug.basic then
58+
Log_.item "Clean %d dirs\n" (sourceDirs.dirs |> List.length);
59+
let count = ref 0 in
60+
sourceDirs.dirs
61+
|> List.iter (fun dir ->
62+
let files = Sys.readdir dir in
63+
files
64+
|> Array.iter (fun file ->
65+
if
66+
Filename.check_suffix file ".re"
67+
|| Filename.check_suffix file ".res"
68+
then
69+
let extension = EmitType.outputFileSuffix ~config in
70+
let generated =
71+
Filename.concat dir
72+
((file |> Filename.chop_extension) ^ extension)
73+
in
74+
if Sys.file_exists generated then (
75+
Unix.unlink generated;
76+
incr count)));
77+
if !Debug.basic then Log_.item "Cleaned %d files\n" !count;
78+
exit 0
8479
| NoOp -> printUsageAndExit ()
8580
| Rm l ->
86-
let removeOne s =
87-
let cmtAbsolutePath = s in
88-
(* somehow the CMT hook is passing an absolute path here *)
89-
let cmt = cmtAbsolutePath |> Paths.relativePathFromBsLib in
90-
let config =
91-
Paths.readConfig ~bsVersion ~namespace:(cmt |> Paths.findNameSpace)
81+
let removeOne s =
82+
let cmtAbsolutePath = s in
83+
(* somehow the CMT hook is passing an absolute path here *)
84+
let cmt = cmtAbsolutePath |> Paths.relativePathFromBsLib in
85+
let config =
86+
Paths.readConfig ~bsVersion ~namespace:(cmt |> Paths.findNameSpace)
87+
in
88+
let outputFile = cmt |> Paths.getOutputFile ~config in
89+
if !Debug.basic then Log_.item "Remove %s\n" cmt;
90+
if Sys.file_exists outputFile then Unix.unlink outputFile
9291
in
93-
let outputFile = cmt |> Paths.getOutputFile ~config in
94-
if !Debug.basic then Log_.item "Remove %s\n" cmt;
95-
if Sys.file_exists outputFile then Unix.unlink outputFile
96-
in
97-
l |> List.rev |> List.iter removeOne;
98-
exit 0
92+
l |> List.rev |> List.iter removeOne;
93+
exit 0
9994
in
10095
let anonArg s =
10196
match !cliCommand with
@@ -104,6 +99,6 @@ let cli () =
10499
in
105100
Arg.parse speclist anonArg usage;
106101
executeCliCommand ~bsVersion:!bsVersion !cliCommand
107-
108102
;;
103+
109104
cli ()

jscomp/gentype/GenTypeCommon.ml

+20-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ let logNotImplemented x =
77
if !Debug.notImplemented then Log_.item "Not Implemented: %s\n" x
88

99
type optional = Mandatory | Optional
10-
1110
type mutable_ = Immutable | Mutable
1211

1312
type labelJS =
@@ -16,7 +15,7 @@ type labelJS =
1615
| IntLabel of string
1716
| StringLabel of string
1817

19-
type case = {label : string; labelJS : labelJS}
18+
type case = { label : string; labelJS : labelJS }
2019

2120
let isJSSafePropertyName name =
2221
let jsSafeRegex = {|^[A-z][A-z0-9]*$|} |> Str.regexp in
@@ -42,8 +41,8 @@ let labelJSToString ?(alwaysQuotes = false) case =
4241
| FloatLabel s -> s |> addQuotes
4342
| IntLabel i -> i |> addQuotes
4443
| StringLabel s ->
45-
if s = case.label && isNumber s then s |> addQuotes
46-
else s |> EmitText.quotes
44+
if s = case.label && isNumber s then s |> addQuotes
45+
else s |> EmitText.quotes
4746

4847
type closedFlag = Open | Closed
4948

@@ -63,8 +62,7 @@ type type_ =
6362
| Variant of variant
6463

6564
and fields = field list
66-
67-
and argType = {aName : string; aType : type_}
65+
and argType = { aName : string; aType : type_ }
6866

6967
and field = {
7068
mutable_ : mutable_;
@@ -82,7 +80,7 @@ and function_ = {
8280
uncurried : bool;
8381
}
8482

85-
and ident = {builtin : bool; name : string; typeArgs : type_ list}
83+
and ident = { builtin : bool; name : string; typeArgs : type_ list }
8684

8785
and variant = {
8886
bsStringOrInt : bool;
@@ -94,7 +92,7 @@ and variant = {
9492
unboxed : bool;
9593
}
9694

97-
and payload = {case : case; inlineRecord : bool; numArgs : int; t : type_}
95+
and payload = { case : case; inlineRecord : bool; numArgs : int; t : type_ }
9896

9997
let typeIsObject type_ =
10098
match type_ with
@@ -133,8 +131,8 @@ struct
133131
let ch = String.unsafe_get s off in
134132
match ch with
135133
| 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' ->
136-
add capital ch;
137-
aux false (off + 1) len
134+
add capital ch;
135+
aux false (off + 1) len
138136
| '/' | '-' -> aux true (off + 1) len
139137
| _ -> aux capital (off + 1) len
140138
in
@@ -159,7 +157,7 @@ struct
159157

160158
(** Common-DemoSomelibrary -> Common *)
161159
let removeGeneratedModule s =
162-
match s |> String.split_on_char '-' with [name; _scope] -> name | _ -> s
160+
match s |> String.split_on_char '-' with [ name; _scope ] -> name | _ -> s
163161
end
164162

165163
let rec depToString dep =
@@ -182,29 +180,30 @@ let createVariant ~bsStringOrInt ~inherits ~noPayloads ~payloads ~polymorphic =
182180
in
183181
let unboxed = payloads = [] in
184182
Variant
185-
{bsStringOrInt; hash; inherits; noPayloads; payloads; polymorphic; unboxed}
183+
{
184+
bsStringOrInt;
185+
hash;
186+
inherits;
187+
noPayloads;
188+
payloads;
189+
polymorphic;
190+
unboxed;
191+
}
186192

187193
let variantTable hash ~toJS =
188194
(match toJS with true -> "$$toJS" | false -> "$$toRE") ^ string_of_int hash
189195

190196
let ident ?(builtin = true) ?(typeArgs = []) name =
191-
Ident {builtin; name; typeArgs}
197+
Ident { builtin; name; typeArgs }
192198

193199
let sanitizeTypeName name = name |> Str.global_replace (Str.regexp "'") "_"
194-
195200
let unknown = ident "unknown"
196-
197201
let booleanT = ident "boolean"
198-
199202
let dateT = ident "Date"
200-
201203
let numberT = ident "number"
202-
203204
let stringT = ident "string"
204-
205205
let unitT = ident "void"
206-
207-
let int64T = Tuple [numberT; numberT]
206+
let int64T = Tuple [ numberT; numberT ]
208207

209208
module NodeFilename = struct
210209
include Filename
@@ -216,9 +215,7 @@ module NodeFilename = struct
216215
type t
217216

218217
val normalize : string -> t
219-
220218
val concat : t -> string -> t
221-
222219
val toString : t -> string
223220
end = struct
224221
type t = string
@@ -229,7 +226,6 @@ module NodeFilename = struct
229226
| _ -> path
230227

231228
let toString path = path
232-
233229
let length path = String.length path
234230

235231
let concat dirname filename =

0 commit comments

Comments
 (0)