Merge pull request #757 from rgrinberg/path-refactors

Path refactorings.
This commit is contained in:
Rudi Grinberg 2018-05-09 17:53:56 +07:00 committed by GitHub
commit 28f451f33c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 58 deletions

View File

@ -149,12 +149,10 @@ let create ~(kind : Kind.t) ~path ~env ~name ~merlin ~targets ~profile () =
the contents of the variable, but "ocamlfind printconf conf" the contents of the variable, but "ocamlfind printconf conf"
still prints the configuration file set at the configuration still prints the configuration file set at the configuration
time of ocamlfind, sigh... *) time of ocamlfind, sigh... *)
match Env.get env "OCAMLFIND_CONF" with (match Env.get env "OCAMLFIND_CONF" with
| Some s -> Fiber.return (Path.absolute s) | Some s -> Fiber.return s
| None -> | None -> Process.run_capture_line ~env Strict fn ["printconf"; "conf"])
Process.run_capture_line ~env Strict >>| Path.absolute)
fn ["printconf"; "conf"]
>>| Path.absolute)
in in
let create_one ~name ~implicit ?findlib_toolchain ?host ~merlin () = let create_one ~name ~implicit ?findlib_toolchain ?host ~merlin () =

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,32 +400,28 @@ 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 =
if is_root t then match explode t with
[] | Some s -> s
else if is_local t then | None -> Exn.code_error "Path.explode_exn"
String.split t ~on:'/' ["path", Sexp.atom_or_quoted_string t]
else
Exn.code_error "Path.explode_exn"
["path", Sexp.atom_or_quoted_string t]
let exists t = let exists t =
try Sys.file_exists (to_string t) try Sys.file_exists (to_string t)