Fix special case of alternate if statements
[hiphop-php.git] / hphp / hack / src / hhi / hhi.ml
blobfd268d38ca0b41cfb16ba9e9ce160fe3b8d2c495
1 (**
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
10 (* OCaml handles the value restriction much better than SML. <3 *)
11 let root = ref None
13 (* Compiler embeds the hhi contents directly into the source *)
14 let hhi_contents = [%hhi_contents]
16 let get_raw_hhi_contents () = hhi_contents
18 let write_hhi dir (filename, contents) =
19 let file = Path.(concat dir filename |> to_string) in
20 (* Make sure the subdirectory exists; this structure is nested *)
21 Sys_utils.mkdir_p (Filename.dirname file);
22 Sys_utils.write_file ~file contents
24 let extract_hhis () =
25 let tmpdir = Path.make (Tmp.temp_dir GlobalConfig.tmp_dir "hhi") in
26 Array.iter (write_hhi tmpdir) hhi_contents;
27 tmpdir
29 (* Touch functionality for all hhis below root *)
30 let touch_root r =
31 let filter file = Filename.check_suffix file ".hhi" in
32 Find.iter_files ~filter [ r ] (Sys_utils.try_touch ~follow_symlinks:true)
34 let touch () =
35 match !root with
36 | Some r -> touch_root r
37 | _ -> ()
39 (* Entry points to actually extract the files and set up the hhi path.
41 * We want this to be idempotent so that later code can check if a given file
42 * came from the hhi unarchive directory or not, to provide better error
43 * messages. *)
44 let get_hhi_root () =
45 match !root with
46 | Some r -> r
47 | None -> begin
48 let r = extract_hhis () in
49 root := Some r;
50 Relative_path.set_path_prefix Relative_path.Hhi r;
52 end
54 let set_hhi_root_for_unit_test dir =
55 (* no need to call realpath() on this; we never extract the hhi files for our
56 * unit tests, so this is just a dummy value and does not need to be a real
57 * path*)
58 root := Some dir;
59 Relative_path.set_path_prefix Relative_path.Hhi dir