forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsb_real_path.ml
37 lines (31 loc) · 887 Bytes
/
bsb_real_path.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
let (//) = Filename.concat
let normalize_exn (s : string) : string =
let old_cwd = Sys.getcwd () in
Unix.chdir s ;
let normalized = Sys.getcwd () in
Unix.chdir old_cwd;
normalized
let real_path p =
match (try Some (Sys.is_directory p) with _ -> None) with
| None ->
let rec resolve dir =
if Sys.file_exists dir then normalize_exn dir else
let parent = Filename.dirname dir in
if dir = parent then dir
else (resolve parent) // (Filename.basename dir)
in
let p =
if Filename.is_relative p then (Sys.getcwd ()) // p
else p
in
resolve p
| Some true -> normalize_exn p
| Some false ->
let dir = normalize_exn (Filename.dirname p) in
match Filename.basename p with
| "." -> dir
| base -> dir // base
let is_same_paths_via_io a b =
if a = b
then true
else (real_path a) = (real_path b)