-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathext_filename.ml
236 lines (201 loc) · 6.61 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
(* 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
match s with
| `File x -> `File (process x )
| `Dir x -> `Dir (process x)
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 try_chop_extension s = try Filename.chop_extension s with _ -> s
(** 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 = Filename.dir_sep.[0] 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
let node_modules = "node_modules"
let node_modules_length = String.length "node_modules"
let package_json = "package.json"
(** path2: a/b
path1: a
result: ./b
TODO: [Filename.concat] with care
[file1] is currently compilation file
[file2] is the dependency
*)
let node_relative_path (file1 : t)
(`File file2 as dep_file : [`File of string]) =
let v = Ext_string.find file2 ~sub:node_modules in
let len = String.length file2 in
if v >= 0 then
let rec skip i =
if i >= len then
Ext_pervasives.failwithf ~loc:__LOC__ "invalid path: %s" file2
else
match file2.[i] with
| '/'
| '.' -> skip (i + 1)
| _ -> 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 + node_modules_length))
else
relative_path
(absolute_path dep_file)
(absolute_path file1)
^ node_sep ^
try_chop_extension (Filename.basename file2)
(** [resolve cwd module_name],
[cwd] is current working directory, absolute path
Trying to find paths to load [module_name]
it is sepcialized for option [-bs-package-include] which requires
[npm_package_name/lib/ocaml]
*)
let resolve_bs_package ~cwd name =
let rec aux origin cwd name =
let destdir = cwd // node_modules // name // "lib" // "ocaml" in
if Ext_sys.is_directory_no_exn destdir then destdir
else
let cwd' = Filename.dirname cwd in
if String.length cwd' < String.length cwd then
aux origin cwd' name
else
try
let destdir =
(Sys.getenv "npm_config_prefix"
// "lib" // node_modules
// "lib" // "ocaml"
) in
if Ext_sys.is_directory_no_exn destdir
then destdir
else
Ext_pervasives.failwithf
~loc:__LOC__ " %s not found in %s" name origin
with
Not_found ->
Ext_pervasives.failwithf
~loc:__LOC__ " %s not found in %s" name origin
in
aux cwd cwd name
let find_package_json_dir cwd =
let rec aux cwd =
if Sys.file_exists (cwd // package_json) then cwd
else
let cwd' = Filename.dirname cwd in
if String.length cwd' < String.length cwd then
aux cwd'
else
Ext_pervasives.failwithf
~loc:__LOC__
"package.json not found from %s" cwd
in
aux cwd
let package_dir = lazy (find_package_json_dir (Lazy.force cwd))