Skip to content

Commit 6ba6078

Browse files
committed
add test for get_extension
fix the long standing duplicate path problem in bspack
1 parent 4219b52 commit 6ba6078

18 files changed

+706
-144
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ jscomp/bin/bsb
7777
jscomp/bin/bsc
7878
jscomp/bin/config_whole_compiler.ml
7979
lib/bs
80-
lib/js/jscomp/test
80+
lib/js/jscomp/test

Diff for: jscomp/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ bin/bsdep.ml:./bin/bspack.exe
178178

179179

180180
bin/bsb.ml:./bin/bspack.exe
181-
$< -bs-MD -I common -I ext -I syntax -I depends -I bsb -bs-main Bsb_main -o $@
181+
$< -bs-MD -I common -I ext -I syntax -I depends -I bsb -I ext -bs-main Bsb_main -o $@
182182

183183
# Actually we can not make it correct by including..
184184
# if we force it, then the initial build will fail

Diff for: jscomp/bin/bsb.ml

+60-8
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,30 @@ val combine : string -> string -> string
954954

955955
val normalize_absolute_path : string -> string
956956

957+
(**
958+
TODO: could be highly optimized
959+
if [from] and [to] resolve to the same path, a zero-length string is returned
960+
Given that two paths are directory
961+
962+
A typical use case is
963+
{[
964+
Filename.concat
965+
(rel_normalized_absolute_path cwd (Filename.dirname a))
966+
(Filename.basename a)
967+
]}
968+
*)
969+
val rel_normalized_absolute_path : string -> string -> string
970+
971+
972+
973+
(**
974+
{[
975+
get_extension "a.txt" = ".txt"
976+
get_extension "a" = ""
977+
]}
978+
*)
979+
val get_extension : string -> string
980+
957981
end = struct
958982
#1 "ext_filename.ml"
959983
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -1047,6 +1071,11 @@ let chop_extension_if_any fname =
10471071

10481072

10491073

1074+
1075+
1076+
let os_path_separator_char = String.unsafe_get Filename.dir_sep 0
1077+
1078+
10501079
(** example
10511080
{[
10521081
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib/external/pervasives.cmj"
@@ -1068,7 +1097,7 @@ let chop_extension_if_any fname =
10681097
]}
10691098
*)
10701099
let relative_path file_or_dir_1 file_or_dir_2 =
1071-
let sep_char = Filename.dir_sep.[0] in
1100+
let sep_char = os_path_separator_char in
10721101
let relevant_dir1 =
10731102
(match file_or_dir_1 with
10741103
| `Dir x -> x
@@ -1094,11 +1123,6 @@ let relative_path file_or_dir_1 file_or_dir_2 =
10941123
String.concat node_sep @@ node_current :: ys
10951124

10961125

1097-
1098-
1099-
1100-
let os_path_separator_char = String.unsafe_get Filename.dir_sep 0
1101-
11021126
(** path2: a/b
11031127
path1: a
11041128
result: ./b
@@ -1208,8 +1232,27 @@ let split_aux p =
12081232
else go dir (Filename.basename p :: acc)
12091233
in go p []
12101234

1211-
1212-
1235+
(**
1236+
TODO: optimization
1237+
if [from] and [to] resolve to the same path, a zero-length string is returned
1238+
*)
1239+
let rel_normalized_absolute_path from to_ =
1240+
let root1, paths1 = split_aux from in
1241+
let root2, paths2 = split_aux to_ in
1242+
if root1 <> root2 then root2 else
1243+
let rec go xss yss =
1244+
match xss, yss with
1245+
| x::xs, y::ys ->
1246+
if x = y then go xs ys
1247+
else
1248+
let start =
1249+
List.fold_left (fun acc _ -> acc // ".." ) ".." xs in
1250+
List.fold_left (fun acc v -> acc // v) start yss
1251+
| [], [] -> ""
1252+
| [], y::ys -> List.fold_left (fun acc x -> acc // x) y ys
1253+
| x::xs, [] ->
1254+
List.fold_left (fun acc _ -> acc // ".." ) ".." xs in
1255+
go paths1 paths2
12131256

12141257
(*TODO: could be hgighly optimized later
12151258
{[
@@ -1255,6 +1298,12 @@ let normalize_absolute_path x =
12551298
| last :: rest -> go last rest
12561299

12571300

1301+
let get_extension x =
1302+
try
1303+
let pos = String.rindex x '.' in
1304+
Ext_string.tail_from x pos
1305+
with Not_found -> ""
1306+
12581307
end
12591308
module String_map : sig
12601309
#1 "string_map.mli"
@@ -3372,6 +3421,8 @@ val sort_via_array :
33723421

33733422
val last : 'a list -> 'a
33743423

3424+
3425+
33753426
end = struct
33763427
#1 "ext_list.ml"
33773428
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -3734,6 +3785,7 @@ let rec last xs =
37343785
| _ :: tl -> last tl
37353786
| [] -> invalid_arg "Ext_list.last"
37363787

3788+
37373789
end
37383790
module Bs_build_util : sig
37393791
#1 "bs_build_util.mli"

Diff for: jscomp/bin/bsdep.ml

+74-13
Original file line numberDiff line numberDiff line change
@@ -3750,6 +3750,30 @@ val combine : string -> string -> string
37503750

37513751
val normalize_absolute_path : string -> string
37523752

3753+
(**
3754+
TODO: could be highly optimized
3755+
if [from] and [to] resolve to the same path, a zero-length string is returned
3756+
Given that two paths are directory
3757+
3758+
A typical use case is
3759+
{[
3760+
Filename.concat
3761+
(rel_normalized_absolute_path cwd (Filename.dirname a))
3762+
(Filename.basename a)
3763+
]}
3764+
*)
3765+
val rel_normalized_absolute_path : string -> string -> string
3766+
3767+
3768+
3769+
(**
3770+
{[
3771+
get_extension "a.txt" = ".txt"
3772+
get_extension "a" = ""
3773+
]}
3774+
*)
3775+
val get_extension : string -> string
3776+
37533777
end = struct
37543778
#1 "ext_filename.ml"
37553779
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -3843,6 +3867,11 @@ let chop_extension_if_any fname =
38433867

38443868

38453869

3870+
3871+
3872+
let os_path_separator_char = String.unsafe_get Filename.dir_sep 0
3873+
3874+
38463875
(** example
38473876
{[
38483877
"/bb/mbigc/mbig2899/bgit/bucklescript/jscomp/stdlib/external/pervasives.cmj"
@@ -3864,7 +3893,7 @@ let chop_extension_if_any fname =
38643893
]}
38653894
*)
38663895
let relative_path file_or_dir_1 file_or_dir_2 =
3867-
let sep_char = Filename.dir_sep.[0] in
3896+
let sep_char = os_path_separator_char in
38683897
let relevant_dir1 =
38693898
(match file_or_dir_1 with
38703899
| `Dir x -> x
@@ -3890,11 +3919,6 @@ let relative_path file_or_dir_1 file_or_dir_2 =
38903919
String.concat node_sep @@ node_current :: ys
38913920

38923921

3893-
3894-
3895-
3896-
let os_path_separator_char = String.unsafe_get Filename.dir_sep 0
3897-
38983922
(** path2: a/b
38993923
path1: a
39003924
result: ./b
@@ -4004,8 +4028,27 @@ let split_aux p =
40044028
else go dir (Filename.basename p :: acc)
40054029
in go p []
40064030

4007-
4008-
4031+
(**
4032+
TODO: optimization
4033+
if [from] and [to] resolve to the same path, a zero-length string is returned
4034+
*)
4035+
let rel_normalized_absolute_path from to_ =
4036+
let root1, paths1 = split_aux from in
4037+
let root2, paths2 = split_aux to_ in
4038+
if root1 <> root2 then root2 else
4039+
let rec go xss yss =
4040+
match xss, yss with
4041+
| x::xs, y::ys ->
4042+
if x = y then go xs ys
4043+
else
4044+
let start =
4045+
List.fold_left (fun acc _ -> acc // ".." ) ".." xs in
4046+
List.fold_left (fun acc v -> acc // v) start yss
4047+
| [], [] -> ""
4048+
| [], y::ys -> List.fold_left (fun acc x -> acc // x) y ys
4049+
| x::xs, [] ->
4050+
List.fold_left (fun acc _ -> acc // ".." ) ".." xs in
4051+
go paths1 paths2
40094052

40104053
(*TODO: could be hgighly optimized later
40114054
{[
@@ -4051,6 +4094,12 @@ let normalize_absolute_path x =
40514094
| last :: rest -> go last rest
40524095

40534096

4097+
let get_extension x =
4098+
try
4099+
let pos = String.rindex x '.' in
4100+
Ext_string.tail_from x pos
4101+
with Not_found -> ""
4102+
40544103
end
40554104
module String_map : sig
40564105
#1 "string_map.mli"
@@ -5387,6 +5436,8 @@ val sort_via_array :
53875436

53885437
val last : 'a list -> 'a
53895438

5439+
5440+
53905441
end = struct
53915442
#1 "ext_list.ml"
53925443
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -5749,6 +5800,7 @@ let rec last xs =
57495800
| _ :: tl -> last tl
57505801
| [] -> invalid_arg "Ext_list.last"
57515802

5803+
57525804
end
57535805
module Js_config : sig
57545806
#1 "js_config.mli"
@@ -6217,13 +6269,18 @@ val collect_ast_map :
62176269
(Format.formatter -> string -> 'b) ->
62186270
('a, 'b) t String_map.t
62196271

6272+
type dir_spec =
6273+
{ dir : string ;
6274+
mutable excludes : string list
6275+
}
62206276

62216277
(** If the genereated queue is empty, it means
62226278
1. The main module does not exist (does not exist due to typo)
62236279
2. It does exist but not in search path
6280+
The order matters from head to tail
62246281
*)
62256282
val collect_from_main :
6226-
?extra_dirs:[`Dir of string | `Dir_with_excludes of string * string list] list ->
6283+
?extra_dirs:dir_spec list ->
62276284
?excludes : string list ->
62286285
Format.formatter ->
62296286
(Format.formatter -> string -> 'a) ->
@@ -6459,8 +6516,11 @@ let collect_ast_map ppf files parse_implementation parse_interface =
64596516
module_name} acc
64606517
end
64616518
) String_map.empty files
6462-
6463-
6519+
;;
6520+
type dir_spec =
6521+
{ dir : string ;
6522+
mutable excludes : string list
6523+
}
64646524

64656525
let collect_from_main
64666526
?(extra_dirs=[])
@@ -6475,8 +6535,9 @@ let collect_from_main
64756535
List.fold_left (fun acc dir_spec ->
64766536
let dirname, excludes =
64776537
match dir_spec with
6478-
| `Dir dirname -> dirname, excludes
6479-
| `Dir_with_excludes (dirname, dir_excludes) ->
6538+
| { dir = dirname; excludes = dir_excludes} ->
6539+
(* dirname, excludes *)
6540+
(* | `Dir_with_excludes (dirname, dir_excludes) -> *)
64806541
dirname,
64816542
Ext_list.flat_map
64826543
(fun x -> [x ^ ".ml" ; x ^ ".mli" ])

0 commit comments

Comments
 (0)