From 1352321f9db7b75d07308c8dfebabbbb99a71166 Mon Sep 17 00:00:00 2001 From: Tatiana Racheva Date: Fri, 24 May 2019 15:19:44 -0700 Subject: [PATCH] Move get_root into a more common location Summary: I would like to use this root-getting logic in another place, and it seems like it belongs in Wwwroot, anyway. Reviewed By: ljw1004 Differential Revision: D15491499 fbshipit-source-id: 2036f7ff18163aca182b4f83ade3b0450524d617 --- hphp/hack/src/client/clientArgs.ml | 18 +++++++++--------- hphp/hack/src/client/clientArgsUtils.ml | 31 ------------------------------- hphp/hack/src/client/clientLsp.ml | 4 ++-- hphp/hack/src/utils/wwwroot.ml | 17 +++++++++++++++++ hphp/hack/src/utils/wwwroot.mli | 1 + 5 files changed, 29 insertions(+), 42 deletions(-) delete mode 100644 hphp/hack/src/client/clientArgsUtils.ml diff --git a/hphp/hack/src/client/clientArgs.ml b/hphp/hack/src/client/clientArgs.ml index c01da1dc974..4cd5b2f04bf 100644 --- a/hphp/hack/src/client/clientArgs.ml +++ b/hphp/hack/src/client/clientArgs.ml @@ -527,9 +527,9 @@ let parse_check_args cmd = let root, paths = match mode, args with | MODE_LINT, _ - | MODE_FILE_DEPENDENTS, _ -> ClientArgsUtils.get_root None, args - | _, [] -> ClientArgsUtils.get_root None, [] - | _, [x] -> ClientArgsUtils.get_root (Some x), [] + | MODE_FILE_DEPENDENTS, _ -> Wwwroot.get None, args + | _, [] -> Wwwroot.get None, [] + | _, [x] -> Wwwroot.get (Some x), [] | _, _ -> Printf.fprintf stderr "Error: please provide at most one www directory\n%!"; @@ -628,8 +628,8 @@ let parse_start_env command = let args = parse_without_command options usage command in let root = match args with - | [] -> ClientArgsUtils.get_root None - | [x] -> ClientArgsUtils.get_root (Some x) + | [] -> Wwwroot.get None + | [x] -> Wwwroot.get (Some x) | _ -> Printf.fprintf stderr "Error: please provide at most one www directory\n%!"; @@ -673,8 +673,8 @@ let parse_stop_args () = let args = parse_without_command options usage "stop" in let root = match args with - | [] -> ClientArgsUtils.get_root None - | [x] -> ClientArgsUtils.get_root (Some x) + | [] -> Wwwroot.get None + | [x] -> Wwwroot.get (Some x) | _ -> Printf.fprintf stderr "Error: please provide at most one www directory\n%!"; @@ -721,8 +721,8 @@ let parse_debug_args () = let args = parse_without_command options usage "debug" in let root = match args with - | [] -> ClientArgsUtils.get_root None - | [x] -> ClientArgsUtils.get_root (Some x) + | [] -> Wwwroot.get None + | [x] -> Wwwroot.get (Some x) | _ -> Printf.printf "%s\n" usage; exit 2 in CDebug { ClientDebug. root; diff --git a/hphp/hack/src/client/clientArgsUtils.ml b/hphp/hack/src/client/clientArgsUtils.ml deleted file mode 100644 index 0746f20f92c..00000000000 --- a/hphp/hack/src/client/clientArgsUtils.ml +++ /dev/null @@ -1,31 +0,0 @@ -(** - * Copyright (c) 2016, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the "hack" directory of this source tree. - * - *) - - -(*****************************************************************************) -(** Utils for processing parsed client args. *) -(*****************************************************************************) - -let rec guess_root config start recursion_limit : Path.t option = - if start = Path.parent start then None (* Reach fs root, nothing to do. *) - else if Wwwroot.is_www_directory ~config start then Some start - else if recursion_limit <= 0 then None - else guess_root config (Path.parent start) (recursion_limit - 1) - - -let get_root ?(config=".hhconfig") path_opt = - let start_str = match path_opt with - | None -> "." - | Some s -> s in - let start_path = Path.make start_str in - let root = match guess_root config start_path 50 with - | None -> start_path - | Some r -> r in - Wwwroot.assert_www_directory ~config root; - root diff --git a/hphp/hack/src/client/clientLsp.ml b/hphp/hack/src/client/clientLsp.ml index df9f6d8e1bc..0d89fdead37 100644 --- a/hphp/hack/src/client/clientLsp.ml +++ b/hphp/hack/src/client/clientLsp.ml @@ -256,13 +256,13 @@ let get_root_opt () : Path.t option = None (* haven't yet received initialize so we don't know *) | Some initialize_params -> let path = Some (Lsp_helpers.get_root initialize_params) in - Some (ClientArgsUtils.get_root path) + Some (Wwwroot.get path) let get_root_wait () : Path.t Lwt.t = let%lwt initialize_params = initialize_params_promise in let path = Lsp_helpers.get_root initialize_params in - Lwt.return (ClientArgsUtils.get_root (Some path)) + Lwt.return (Wwwroot.get (Some path)) let read_hhconfig_version () : string = diff --git a/hphp/hack/src/utils/wwwroot.ml b/hphp/hack/src/utils/wwwroot.ml index 98121d2b872..7f9fbaca518 100644 --- a/hphp/hack/src/utils/wwwroot.ml +++ b/hphp/hack/src/utils/wwwroot.ml @@ -33,3 +33,20 @@ let assert_www_directory ?(config=".hhconfig") (path : Path.t) : unit = flush stderr; exit 1 end + +let rec guess_root config start ~recursion_limit : Path.t option = + if start = Path.parent start then None (* Reached file system root *) + else if is_www_directory ~config start then Some start + else if recursion_limit <= 0 then None + else guess_root config (Path.parent start) (recursion_limit - 1) + +let get ?(config=".hhconfig") (path: string option): Path.t = + let start_str = match path with + | None -> "." + | Some s -> s in + let start_path = Path.make start_str in + let root = match guess_root config start_path ~recursion_limit:50 with + | None -> start_path + | Some r -> r in + assert_www_directory ~config root; + root diff --git a/hphp/hack/src/utils/wwwroot.mli b/hphp/hack/src/utils/wwwroot.mli index 0b86b6b326e..e00c4665102 100644 --- a/hphp/hack/src/utils/wwwroot.mli +++ b/hphp/hack/src/utils/wwwroot.mli @@ -15,3 +15,4 @@ shape. *) val is_www_directory : ?config:string -> Path.t -> bool val assert_www_directory : ?config:string -> Path.t -> unit +val get : ?config:string -> string option -> Path.t -- 2.11.4.GIT