-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathPackages.ml
253 lines (245 loc) · 9.63 KB
/
Packages.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
open SharedTypes
(* Creates the `pathsForModule` hashtbl, which maps a `moduleName` to it's `paths` (the ml/re, mli/rei, cmt, and cmti files) *)
let makePathsForModule ~projectFilesAndPaths ~dependenciesFilesAndPaths =
let pathsForModule = Hashtbl.create 30 in
dependenciesFilesAndPaths
|> List.iter (fun (modName, paths) ->
Hashtbl.replace pathsForModule modName paths);
projectFilesAndPaths
|> List.iter (fun (modName, paths) ->
Hashtbl.replace pathsForModule modName paths);
pathsForModule
let overrideRescriptVersion = ref None
let getReScriptVersion () =
match !overrideRescriptVersion with
| Some overrideRescriptVersion -> overrideRescriptVersion
| None -> (
(* TODO: Include patch stuff when needed *)
let defaultVersion = (11, 0) in
try
let value = Sys.getenv "RESCRIPT_VERSION" in
let version =
match value |> String.split_on_char '.' with
| major :: minor :: _rest -> (
match (int_of_string_opt major, int_of_string_opt minor) with
| Some major, Some minor -> (major, minor)
| _ -> defaultVersion)
| _ -> defaultVersion
in
version
with Not_found -> defaultVersion)
let newBsPackage ~rootPath =
let rescriptJson = Filename.concat rootPath "rescript.json" in
let bsconfigJson = Filename.concat rootPath "bsconfig.json" in
let parseRaw raw =
let libBs =
match !Cfg.isDocGenFromCompiler with
| true -> BuildSystem.getStdlib rootPath
| false -> BuildSystem.getLibBs rootPath
in
match Json.parse raw with
| Some config -> (
match FindFiles.findDependencyFiles rootPath config with
| None -> None
| Some (dependencyDirectories, dependenciesFilesAndPaths) -> (
match libBs with
| None -> None
| Some libBs ->
Some
(let namespace = FindFiles.getNamespace config in
let rescriptVersion = getReScriptVersion () in
let suffix =
match config |> Json.get "suffix" with
| Some (String suffix) -> suffix
| _ -> ".js"
in
let uncurried =
let ns = config |> Json.get "uncurried" in
match (rescriptVersion, ns) with
| (major, _), None when major >= 11 -> Some true
| _, ns -> Option.bind ns Json.bool
in
let genericJsxModule =
let jsxConfig = config |> Json.get "jsx" in
match jsxConfig with
| Some jsxConfig -> (
match jsxConfig |> Json.get "module" with
| Some (String m) when String.lowercase_ascii m <> "react" ->
Some m
| _ -> None)
| None -> None
in
let uncurried = uncurried = Some true in
let sourceDirectories =
FindFiles.getSourceDirectories ~includeDev:true ~baseDir:rootPath
config
in
let projectFilesAndPaths =
FindFiles.findProjectFiles
~public:(FindFiles.getPublic config)
~namespace ~path:rootPath ~sourceDirectories ~libBs
in
projectFilesAndPaths
|> List.iter (fun (_name, paths) -> Log.log (showPaths paths));
let pathsForModule =
makePathsForModule ~projectFilesAndPaths
~dependenciesFilesAndPaths
in
let opens_from_namespace =
match namespace with
| None -> []
| Some namespace ->
let cmt = Filename.concat libBs namespace ^ ".cmt" in
Log.log
("############ Namespaced as " ^ namespace ^ " at " ^ cmt);
Hashtbl.add pathsForModule namespace (Namespace {cmt});
let path = [FindFiles.nameSpaceToName namespace] in
[path]
in
Log.log
("Dependency dirs: "
^ String.concat " "
(dependencyDirectories |> List.map Utils.dumpPath));
let opens_from_bsc_flags =
let bind f x = Option.bind x f in
match Json.get "bsc-flags" config |> bind Json.array with
| Some l ->
List.fold_left
(fun opens item ->
match item |> Json.string with
| None -> opens
| Some s -> (
let parts = String.split_on_char ' ' s in
match parts with
| "-open" :: name :: _ ->
let path = name |> String.split_on_char '.' in
path :: opens
| _ -> opens))
[] l
| None -> []
in
let opens =
[
(if uncurried then "PervasivesU" else "Pervasives");
"JsxModules";
]
:: opens_from_namespace
|> List.rev_append opens_from_bsc_flags
|> List.map (fun path -> path @ ["place holder"])
in
Log.log
("Opens from ReScript config file: "
^ (opens |> List.map pathToString |> String.concat " "));
{
genericJsxModule;
suffix;
rescriptVersion;
rootPath;
projectFiles =
projectFilesAndPaths |> List.map fst |> FileSet.of_list;
dependenciesFiles =
dependenciesFilesAndPaths |> List.map fst |> FileSet.of_list;
pathsForModule;
opens;
namespace;
builtInCompletionModules =
(if
opens_from_bsc_flags
|> List.find_opt (fun opn ->
match opn with
| ["RescriptCore"] -> true
| _ -> false)
|> Option.is_some
then
{
arrayModulePath = ["Array"];
optionModulePath = ["Option"];
stringModulePath = ["String"];
intModulePath = ["Int"];
floatModulePath = ["Float"];
promiseModulePath = ["Promise"];
listModulePath = ["List"];
resultModulePath = ["Result"];
exnModulePath = ["Exn"];
regexpModulePath = ["RegExp"];
}
else if
opens_from_bsc_flags
|> List.find_opt (fun opn ->
match opn with
| ["Belt"] -> true
| _ -> false)
|> Option.is_some
then
{
arrayModulePath = ["Array"];
optionModulePath = ["Option"];
stringModulePath = ["Js"; "String2"];
intModulePath = ["Int"];
floatModulePath = ["Float"];
promiseModulePath = ["Js"; "Promise"];
listModulePath = ["List"];
resultModulePath = ["Result"];
exnModulePath = ["Js"; "Exn"];
regexpModulePath = ["Js"; "Re"];
}
else
{
arrayModulePath = ["Js"; "Array2"];
optionModulePath = ["Belt"; "Option"];
stringModulePath = ["Js"; "String2"];
intModulePath = ["Belt"; "Int"];
floatModulePath = ["Belt"; "Float"];
promiseModulePath = ["Js"; "Promise"];
listModulePath = ["Belt"; "List"];
resultModulePath = ["Belt"; "Result"];
exnModulePath = ["Js"; "Exn"];
regexpModulePath = ["Js"; "Re"];
});
uncurried;
})))
| None -> None
in
match Files.readFile rescriptJson with
| Some raw -> parseRaw raw
| None -> (
Log.log ("Unable to read " ^ rescriptJson);
match Files.readFile bsconfigJson with
| Some raw -> parseRaw raw
| None ->
Log.log ("Unable to read " ^ bsconfigJson);
None)
let findRoot ~uri packagesByRoot =
let path = Uri.toPath uri in
let rec loop path =
if path = "/" then None
else if Hashtbl.mem packagesByRoot path then Some (`Root path)
else if
Files.exists (Filename.concat path "rescript.json")
|| Files.exists (Filename.concat path "bsconfig.json")
then Some (`Bs path)
else
let parent = Filename.dirname path in
if parent = path then (* reached root *) None else loop parent
in
loop (Filename.dirname path)
let getPackage ~uri =
let open SharedTypes in
if Hashtbl.mem state.rootForUri uri then
Some (Hashtbl.find state.packagesByRoot (Hashtbl.find state.rootForUri uri))
else
match findRoot ~uri state.packagesByRoot with
| None ->
Log.log "No root directory found";
None
| Some (`Root rootPath) ->
Hashtbl.replace state.rootForUri uri rootPath;
Some
(Hashtbl.find state.packagesByRoot (Hashtbl.find state.rootForUri uri))
| Some (`Bs rootPath) -> (
match newBsPackage ~rootPath with
| None -> None
| Some package ->
Hashtbl.replace state.rootForUri uri package.rootPath;
Hashtbl.replace state.packagesByRoot package.rootPath package;
Some package)