Re-implement many path functions using kind

This will make it easier to port them to symbolic paths as this check is also
necessary there.
This commit is contained in:
Rudi Grinberg 2018-05-03 22:15:25 +07:00
parent 92b351de30
commit c563fc1db8
1 changed files with 45 additions and 45 deletions

View File

@ -279,31 +279,32 @@ let absolute fn =
fn fn
let to_absolute_filename t ~root = let to_absolute_filename t ~root =
if is_local t then begin match kind t with
| Local t ->
assert (not (Filename.is_relative root)); assert (not (Filename.is_relative root));
Filename.concat root (to_string t) Filename.concat root (Local.to_string t)
end else | External t -> t
t
let reach t ~from = let reach t ~from =
match is_local t, is_local from with match kind t, kind from with
| false, _ -> t | External _, _ -> t
| true, false -> | Local _, External _ ->
Exn.code_error "Path.reach called with invalid combination" Exn.code_error "Path.reach called with invalid combination"
[ "t" , sexp_of_t t [ "t" , sexp_of_t t
; "from", sexp_of_t from ; "from", sexp_of_t from
] ]
| true, true -> Local.reach t ~from | Local t, Local from ->
Local.reach t ~from
let reach_for_running t ~from = let reach_for_running t ~from =
match is_local t, is_local from with match kind t, kind from with
| false, _ -> t | External _, _ -> t
| true, false -> | Local _, External _ ->
Exn.code_error "Path.reach_for_running called with invalid combination" Exn.code_error "Path.reach_for_running called with invalid combination"
[ "t" , sexp_of_t t [ "t" , sexp_of_t t
; "from", sexp_of_t from ; "from", sexp_of_t from
] ]
| true, true -> | Local t, Local from ->
let s = Local.reach t ~from in let s = Local.reach t ~from in
if String.is_prefix s ~prefix:"../" then if String.is_prefix s ~prefix:"../" then
s s
@ -311,46 +312,46 @@ let reach_for_running t ~from =
"./" ^ s "./" ^ s
let descendant t ~of_ = let descendant t ~of_ =
if is_local t && is_local of_ then match kind t, kind of_ with
Local.descendant t ~of_ | Local t, Local of_ -> Local.descendant t ~of_
else | _, _ -> None
None
let is_descendant t ~of_ = let is_descendant t ~of_ =
if is_local t && is_local of_ then match kind t, kind of_ with
Local.is_descendant t ~of_ | Local t, Local of_ -> Local.is_descendant t ~of_
else | _, _ -> false
false
let append a b = let append a b =
if not (is_local b) then match kind b with
| External _ ->
Exn.code_error "Path.append called with non-local second path" Exn.code_error "Path.append called with non-local second path"
[ "a", sexp_of_t a [ "a", sexp_of_t a
; "b", sexp_of_t b ; "b", sexp_of_t b
]; ]
if is_local a then | Local b ->
Local.append a b begin match kind a with
else | Local a -> Local.append a b
Filename.concat a b | External a -> Filename.concat a b
end
let basename t = let basename t =
if is_local t then match kind t with
Local.basename t | Local t -> Local.basename t
else | External t -> Filename.basename t
Filename.basename t
let parent t = let parent t =
if is_local t then match kind t with
Local.parent t | Local t -> Local.parent t
else | External t -> Filename.dirname t
Filename.dirname t
let build_prefix = "_build/" let build_prefix = "_build/"
let build_dir = "_build" let build_dir = "_build"
let is_in_build_dir t = let is_in_build_dir t =
String.is_prefix t ~prefix:build_prefix match kind t with
| Local t -> String.is_prefix t ~prefix:build_prefix
| External _ -> false
let is_in_source_tree t = is_local t && not (is_in_build_dir t) let is_in_source_tree t = is_local t && not (is_in_build_dir t)
@ -399,23 +400,22 @@ let drop_optional_build_context t =
| Some (_, t) -> t | Some (_, t) -> t
let split_first_component t = let split_first_component t =
if is_local t && not (is_root t)then match kind t, is_root t with
match String.index t '/' with | Local t, false ->
begin match String.index t '/' with
| None -> Some (t, root) | None -> Some (t, root)
| Some i -> | Some i ->
Some Some
(String.sub t ~pos:0 ~len:i, (String.sub t ~pos:0 ~len:i,
String.sub t ~pos:(i + 1) ~len:(String.length t - i - 1)) String.sub t ~pos:(i + 1) ~len:(String.length t - i - 1))
else end
None | _, _ -> None
let explode t = let explode t =
if is_root t then match kind t with
Some [] | Local "" -> Some []
else if is_local t then | Local s -> Some (String.split s ~on:'/')
Some (String.split t ~on:'/') | External _ -> None
else
None
let explode_exn t = let explode_exn t =
match explode t with match explode t with