forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_filename.ml
362 lines (298 loc) · 9.79 KB
/
ext_filename.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(** Used when produce node compatible paths *)
let node_sep = "/"
let node_parent = ".."
let node_current = "."
type t =
[ `File of string
| `Dir of string ]
let cwd = lazy (Sys.getcwd ())
let (//) = Filename.concat
let combine path1 path2 =
if path1 = "" then
path2
else if path2 = "" then path1
else
if Filename.is_relative path2 then
path1// path2
else
path2
(* Note that [.//] is the same as [./] *)
let path_as_directory x =
if x = "" then x
else
if Ext_string.ends_with x Filename.dir_sep then
x
else
x ^ Filename.dir_sep
let absolute_path s =
let process s =
let s =
if Filename.is_relative s then
Lazy.force cwd // s
else s in
(* Now simplify . and .. components *)
let rec aux s =
let base,dir = Filename.basename s, Filename.dirname s in
if dir = s then dir
else if base = Filename.current_dir_name then aux dir
else if base = Filename.parent_dir_name then Filename.dirname (aux dir)
else aux dir // base
in aux s in
process s
let chop_extension ?(loc="") name =
try Filename.chop_extension name
with Invalid_argument _ ->
Ext_pervasives.invalid_argf
"Filename.chop_extension ( %s : %s )" loc name
let chop_extension_if_any fname =
try Filename.chop_extension fname with Invalid_argument _ -> fname
let os_path_separator_char = String.unsafe_get Filename.dir_sep 0
(** example
{[
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib/external/pervasives.cmj"
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib/ocaml_array.ml"
]}
The other way
{[
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib/ocaml_array.ml"
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib/external/pervasives.cmj"
]}
{[
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib//ocaml_array.ml"
]}
{[
/a/b
/c/d
]}
*)
let relative_path file_or_dir_1 file_or_dir_2 =
let sep_char = os_path_separator_char in
let relevant_dir1 =
(match file_or_dir_1 with
| `Dir x -> x
| `File file1 -> Filename.dirname file1) in
let relevant_dir2 =
(match file_or_dir_2 with
|`Dir x -> x
|`File file2 -> Filename.dirname file2 ) in
let dir1 = Ext_string.split relevant_dir1 sep_char in
let dir2 = Ext_string.split relevant_dir2 sep_char in
let rec go (dir1 : string list) (dir2 : string list) =
match dir1, dir2 with
| x::xs , y :: ys when x = y
-> go xs ys
| _, _
->
List.map (fun _ -> node_parent) dir2 @ dir1
in
match go dir1 dir2 with
| (x :: _ ) as ys when x = node_parent ->
String.concat node_sep ys
| ys ->
String.concat node_sep @@ node_current :: ys
(** path2: a/b
path1: a
result: ./b
TODO: [Filename.concat] with care
[file1] is currently compilation file
[file2] is the dependency
TODO: this is a hackish function: FIXME
*)
let node_relative_path node_modules_shorten (file1 : t)
(`File file2 as dep_file : [`File of string]) =
let v = Ext_string.find file2 ~sub:Literals.node_modules in
let len = String.length file2 in
if node_modules_shorten && v >= 0 then
let rec skip i =
if i >= len then
Ext_pervasives.failwithf ~loc:__LOC__ "invalid path: %s" file2
else
(* https://en.wikipedia.org/wiki/Path_(computing))
most path separator are a single char
*)
let curr_char = String.unsafe_get file2 i in
if curr_char = os_path_separator_char || curr_char = '.' then
skip (i + 1)
else i
(*
TODO: we need do more than this suppose user
input can be
{[
"xxxghsoghos/ghsoghso/node_modules/../buckle-stdlib/list.js"
]}
This seems weird though
*)
in
Ext_string.tail_from file2
(skip (v + Literals.node_modules_length))
else
relative_path
( match dep_file with
| `File x -> `File (absolute_path x)
| `Dir x -> `Dir (absolute_path x))
(match file1 with
| `File x -> `File (absolute_path x)
| `Dir x -> `Dir(absolute_path x))
^ node_sep ^
chop_extension_if_any (Filename.basename file2)
(* Input must be absolute directory *)
let rec find_root_filename ~cwd filename =
if Sys.file_exists (cwd // filename) then cwd
else
let cwd' = Filename.dirname cwd in
if String.length cwd' < String.length cwd then
find_root_filename ~cwd:cwd' filename
else
Ext_pervasives.failwithf
~loc:__LOC__
"%s not found from %s" filename cwd
let find_package_json_dir cwd =
find_root_filename ~cwd Literals.bsconfig_json
let package_dir = lazy (find_package_json_dir (Lazy.force cwd))
let module_name_of_file file =
String.capitalize
(Filename.chop_extension @@ Filename.basename file)
let module_name_of_file_if_any file =
String.capitalize
(chop_extension_if_any @@ Filename.basename file)
(** For win32 or case insensitve OS
[".cmj"] is the same as [".CMJ"]
*)
(* let has_exact_suffix_then_chop fname suf = *)
let combine p1 p2 =
if p1 = "" || p1 = Filename.current_dir_name then p2 else
if p2 = "" || p2 = Filename.current_dir_name then p1
else
if Filename.is_relative p2 then
Filename.concat p1 p2
else p2
(**
{[
split_aux "//ghosg//ghsogh/";;
- : string * string list = ("/", ["ghosg"; "ghsogh"])
]}
Note that
{[
Filename.dirname "/a/" = "/"
Filename.dirname "/a/b/" = Filename.dirname "/a/b" = "/a"
]}
Special case:
{[
basename "//" = "/"
basename "///" = "/"
]}
{[
basename "" = "."
basename "" = "."
dirname "" = "."
dirname "" = "."
]}
*)
let split_aux p =
let rec go p acc =
let dir = Filename.dirname p in
if dir = p then dir, acc
else
let new_path = Filename.basename p in
if Ext_string.equal new_path Filename.dir_sep then
go dir acc
(* We could do more path simplification here
leave to [rel_normalized_absolute_path]
*)
else
go dir (new_path :: acc)
in go p []
(**
TODO: optimization
if [from] and [to] resolve to the same path, a zero-length string is returned
*)
let rel_normalized_absolute_path from to_ =
let root1, paths1 = split_aux from in
let root2, paths2 = split_aux to_ in
if root1 <> root2 then root2
else
let rec go xss yss =
match xss, yss with
| x::xs, y::ys ->
if Ext_string.equal x y then go xs ys
else
let start =
List.fold_left (fun acc _ -> acc // Ext_string.parent_dir_lit )
Ext_string.parent_dir_lit xs in
List.fold_left (fun acc v -> acc // v) start yss
| [], [] -> Ext_string.empty
| [], y::ys -> List.fold_left (fun acc x -> acc // x) y ys
| x::xs, [] ->
List.fold_left (fun acc _ -> acc // Ext_string.parent_dir_lit )
Ext_string.parent_dir_lit xs in
go paths1 paths2
(*TODO: could be hgighly optimized later
{[
normalize_absolute_path "/gsho/./..";;
normalize_absolute_path "/a/b/../c../d/e/f";;
normalize_absolute_path "/gsho/./..";;
normalize_absolute_path "/gsho/./../..";;
normalize_absolute_path "/a/b/c/d";;
normalize_absolute_path "/a/b/c/d/";;
normalize_absolute_path "/a/";;
normalize_absolute_path "/a";;
]}
*)
(** See tests in {!Ounit_path_tests} *)
let normalize_absolute_path x =
let drop_if_exist xs =
match xs with
| [] -> []
| _ :: xs -> xs in
let rec normalize_list acc paths =
match paths with
| [] -> acc
| x :: xs ->
if Ext_string.equal x Ext_string.current_dir_lit then
normalize_list acc xs
else if Ext_string.equal x Ext_string.parent_dir_lit then
normalize_list (drop_if_exist acc ) xs
else
normalize_list (x::acc) xs
in
let root, paths = split_aux x in
let rev_paths = normalize_list [] paths in
let rec go acc rev_paths =
match rev_paths with
| [] -> Filename.concat root acc
| last::rest -> go (Filename.concat last acc ) rest in
match rev_paths with
| [] -> root
| last :: rest -> go last rest
let get_extension x =
let pos = Ext_string.rindex_neg x '.' in
if pos < 0 then ""
else Ext_string.tail_from x pos
let simple_convert_node_path_to_os_path =
if Sys.unix then fun x -> x
else if Sys.win32 || Sys.cygwin then
Ext_string.replace_slash_backward
else failwith ("Unknown OS : " ^ Sys.os_type)