Skip to content

Commit 8bfab88

Browse files
committed
WIP
1 parent 12311d9 commit 8bfab88

11 files changed

+159
-59
lines changed

jscomp/ext_filename.ml

+43-18
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,31 @@ let node_current = "."
3636

3737
let cwd = lazy (Sys.getcwd ())
3838

39+
let (//) = Filename.concat
40+
41+
let combine path1 path2 =
42+
if path1 = "" then
43+
path2
44+
else if path2 = "" then path1
45+
else
46+
if Filename.is_relative path2 then
47+
path1// path2
48+
else
49+
path2
50+
51+
(* Note that [.//] is the same as [./] *)
52+
let path_as_directory x =
53+
if x = "" then x
54+
else
55+
if Ext_string.ends_with x Filename.dir_sep then
56+
x
57+
else
58+
x ^ Filename.dir_sep
3959

4060
let absolute_path s =
4161
let s =
4262
if Filename.is_relative s then
43-
Filename.concat (Lazy.force cwd) s
63+
Lazy.force cwd // s
4464
else s in
4565
(* Now simplify . and .. components *)
4666
let rec aux s =
@@ -49,14 +69,15 @@ let absolute_path s =
4969
if dir = s then dir
5070
else if base = Filename.current_dir_name then aux dir
5171
else if base = Filename.parent_dir_name then Filename.dirname (aux dir)
52-
else Filename.concat (aux dir) base
72+
else aux dir // base
5373
in
5474
aux s
5575

5676
let chop_extension ?(loc="") name =
5777
try Filename.chop_extension name
5878
with Invalid_argument _ ->
59-
invalid_arg ("Filename.chop_extension (" ^ loc ^ ":" ^ name ^ ")")
79+
Ext_pervasives.invalid_argf
80+
"Filename.chop_extension ( %s : %s )" loc name
6081

6182
let try_chop_extension s = try Filename.chop_extension s with _ -> s
6283

@@ -103,20 +124,26 @@ let node_modules = "node_modules"
103124
let node_modules_length = String.length "node_modules"
104125
let package_json = "package.json"
105126

127+
128+
129+
106130
(** path2: a/b
107131
path1: a
108132
result: ./b
109133
TODO: [Filename.concat] with care
134+
135+
[file1] is currently compilation file
136+
[file2] is the dependency
110137
*)
111-
let node_relative_path path1 path2 =
112-
let v = Ext_string.find path2 ~sub:node_modules in
113-
let len = String.length path2 in
138+
let node_relative_path file1 file2 =
139+
let v = Ext_string.find file2 ~sub:node_modules in
140+
let len = String.length file2 in
114141
if v >= 0 then
115142
let rec skip i =
116143
if i >= len then
117-
failwith ("invalid path: " ^ path2)
144+
Ext_pervasives.failwithf "invalid path: %s" file2
118145
else
119-
match path2.[i] with
146+
match file2.[i] with
120147
| '/'
121148
| '.' -> skip (i + 1)
122149
| _ -> i
@@ -129,22 +156,22 @@ let node_relative_path path1 path2 =
129156
This seems weird though
130157
*)
131158
in
132-
Ext_string.tail_from path2
159+
Ext_string.tail_from file2
133160
(skip (v + node_modules_length))
134161
else
135-
(relative_path
136-
(try_chop_extension (absolute_path path2))
137-
(try_chop_extension (absolute_path path1))
138-
) ^ node_sep ^
139-
(try_chop_extension (Filename.basename path2))
162+
relative_path
163+
(absolute_path file2)
164+
(absolute_path file1)
165+
^ node_sep ^
166+
try_chop_extension (Filename.basename file2)
140167

141168

142169

143170
(** [resolve cwd module_name], [cwd] is current working directory, absolute path
144171
*)
145172
let resolve ~cwd module_name =
146173
let rec aux origin cwd module_name =
147-
let v = Filename.concat (Filename.concat cwd node_modules) module_name
174+
let v = ( cwd // node_modules) // module_name
148175
in
149176
if Sys.is_directory v then v
150177
else
@@ -158,9 +185,7 @@ let resolve ~cwd module_name =
158185

159186
let resolve_package cwd =
160187
let rec aux cwd =
161-
let v = Filename.concat cwd package_json
162-
in
163-
if Sys.file_exists v then cwd
188+
if Sys.file_exists (cwd // package_json) then cwd
164189
else
165190
let cwd' = Filename.dirname cwd in
166191
if String.length cwd' < String.length cwd then

jscomp/ext_filename.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
library but rather specific to JS Module name convention.
3232
*)
3333

34-
34+
val combine : string -> string -> string
35+
val path_as_directory : string -> string
3536

3637
(** An extension module to calculate relative path follow node/npm style.
3738
TODO : this short name will have to change upon renaming the file.

jscomp/ext_pervasives.ml

+2
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ let is_pos_pow n =
6262
try aux 0 n with M.E -> -1
6363

6464
let failwithf fmt = Format.ksprintf failwith fmt
65+
66+
let invalid_argf fmt = Format.ksprintf invalid_arg fmt

jscomp/ext_pervasives.mli

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ val with_file_as_pp : string -> (Format.formatter -> 'a) -> 'a
4242
val is_pos_pow : Int32.t -> int
4343

4444
val failwithf : ('a, unit, string, 'b) format4 -> 'a
45+
46+
val invalid_argf : ('a, unit, string, 'b) format4 -> 'a

jscomp/js_config.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,20 @@ let npm_package_path = ref None
7878
let set_npm_package_path s = npm_package_path := Some s
7979
let get_npm_package_path () = !npm_package_path
8080

81+
let (//) = Filename.concat
8182
(* for a single pass compilation, [output_dir]
8283
can be cached
8384
*)
8485
let get_output_dir filename =
8586
match get_npm_package_path () with
8687
| None ->
8788
if Filename.is_relative filename then
88-
Filename.concat (Lazy.force Ext_filename.cwd)
89-
(Filename.dirname filename)
89+
Lazy.force Ext_filename.cwd //
90+
Filename.dirname filename
9091
else
9192
Filename.dirname filename
9293
| Some x ->
93-
(Filename.concat
94-
(Lazy.force Ext_filename.package_dir) x)
94+
Lazy.force Ext_filename.package_dir // x
9595

9696

9797

jscomp/js_program_loader.ml

+7-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ module S = Js_stmt_make
3636

3737
open Js_output.Ops
3838

39+
let (//) = Filename.concat
40+
3941
let string_of_module_id (x : Lam_module_ident.t) : string =
4042
match x.kind with
4143
| Runtime
@@ -69,18 +71,15 @@ let string_of_module_id (x : Lam_module_ident.t) : string =
6971
(* for some primitive files, no cmj support *)
7072
| exception Not_found ->
7173
if String_set.mem filename Js_config.runtime_set then
72-
let path =
73-
(* For the runtime, only [JS] files are needed, and
74+
(* For the runtime, only [JS] files are needed, and
7475
unlike the stdlib, [bsc] have some pre-built knowledge
7576
about where it is, since in general, [runtime]
7677
is *transparent* to the user
77-
*)
78-
Filename.concat
79-
(Filename.dirname (Filename.dirname Sys.executable_name))
80-
"runtime"
81-
in
78+
*)
79+
8280
Ext_filename.node_relative_path !Location.input_name
83-
(Filename.concat path filename)
81+
(Filename.dirname (Filename.dirname Sys.executable_name)
82+
// "runtime" // filename)
8483
else
8584
begin
8685
Ext_log.warn __LOC__ "@[%s not found in search path - while compiling %s @] "

jscomp/test/a_filename_test.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,27 @@ function eq(loc, x, y) {
2626
return /* () */0;
2727
}
2828

29-
eq('File "a_filename_test.ml", line 11, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "./a/u/g.c"), "./u/g");
29+
eq('File "a_filename_test.ml", line 10, characters 5-12', /* tuple */[
30+
Ext_filename.combine("/tmp", "subdir/file.txt"),
31+
Ext_filename.combine("/tmp", "/a/tmp.txt"),
32+
Ext_filename.combine("/a/tmp.txt", "subdir/file.txt")
33+
], /* tuple */[
34+
"/tmp/subdir/file.txt",
35+
"/a/tmp.txt",
36+
"/a/tmp.txt/subdir/file.txt"
37+
]);
3038

31-
eq('File "a_filename_test.ml", line 16, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "xxxghsoghos/ghsoghso/node_modules/buckle-stdlib/list.js"), "buckle-stdlib/list.js");
39+
eq('File "a_filename_test.ml", line 22, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "./a/u/g.c"), "./u/g");
3240

33-
eq('File "a_filename_test.ml", line 22, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "xxxghsoghos/ghsoghso/node_modules//buckle-stdlib/list.js"), "buckle-stdlib/list.js");
41+
eq('File "a_filename_test.ml", line 27, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "xxxghsoghos/ghsoghso/node_modules/buckle-stdlib/list.js"), "buckle-stdlib/list.js");
3442

35-
eq('File "a_filename_test.ml", line 28, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "xxxghsoghos/ghsoghso/node_modules/./buckle-stdlib/list.js"), "buckle-stdlib/list.js");
43+
eq('File "a_filename_test.ml", line 33, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "xxxghsoghos/ghsoghso/node_modules//buckle-stdlib/list.js"), "buckle-stdlib/list.js");
3644

37-
eq('File "a_filename_test.ml", line 34, characters 5-12', Ext_filename.node_relative_path("./a/c.js", "./a/b"), "./b");
45+
eq('File "a_filename_test.ml", line 39, characters 5-12', Ext_filename.node_relative_path("./a/b.c", "xxxghsoghos/ghsoghso/node_modules/./buckle-stdlib/list.js"), "buckle-stdlib/list.js");
3846

39-
eq('File "a_filename_test.ml", line 39, characters 5-12', Ext_filename.node_relative_path("./a/c", "./a/b.js"), "./b");
47+
eq('File "a_filename_test.ml", line 45, characters 5-12', Ext_filename.node_relative_path("./a/c.js", "./a/b"), "./b");
4048

41-
eq('File "a_filename_test.ml", line 44, characters 5-12', Ext_filename.node_relative_path("./a/", "./a/b.js"), "./b");
49+
eq('File "a_filename_test.ml", line 50, characters 5-12', Ext_filename.node_relative_path("./a/c", "./a/b.js"), "./b");
4250

4351
Mt.from_pair_suites("a_filename_test.ml", suites[0]);
4452

jscomp/test/a_filename_test.ml

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ let eq loc x y =
77

88

99
let () =
10-
10+
eq __LOC__
11+
(let (//) = Ext_filename.combine in
12+
("/tmp"// "subdir/file.txt",
13+
"/tmp"// "/a/tmp.txt",
14+
"/a/tmp.txt" // "subdir/file.txt"
15+
))
16+
("/tmp/subdir/file.txt",
17+
"/a/tmp.txt",
18+
"/a/tmp.txt/subdir/file.txt"
19+
)
20+
21+
;
1122
eq __LOC__
1223
(Ext_filename.node_relative_path
1324
"./a/b.c"

0 commit comments

Comments
 (0)