flake_env/lib/flake_env__watches.re
2024-08-17 09:36:21 -04:00

65 lines
1.5 KiB
ReasonML

open Yojson.Safe.Util;
module StringSet = Set.Make(String);
module Util = Flake_env__util;
[@deriving yojson]
type watch = {
exists: bool,
modtime: int,
path: string,
};
[@deriving yojson]
type watches = array(watch);
let get = () => {
let direnv_watch_str = Sys.getenv("DIRENV_WATCHES");
let stdout_read_ch =
Unix.open_process_in("direnv show_dump " ++ direnv_watch_str);
switch (Yojson.Safe.from_channel(stdout_read_ch)) {
| exception (Yojson.Json_error(msg)) => Error(msg)
| safe_t =>
switch (watches_of_yojson(safe_t)) {
| exception _ =>
Error("Failed parsing watches; Has the direnv JSON shape changed?")
| body => Ok(body)
}
};
};
let get_extant = () => {
get()
|> Result.map(watches =>
watches |> Array.to_list |> List.filter(a => a.exists)
);
};
let get_path = doc => {
let pth = doc |> member("path") |> to_string;
String.sub(pth, 11, String.length(pth) - 11);
};
let rec get_paths_from_doc = (doc, paths) => {
let p = get_path(doc);
let sub_paths =
List.concat(
doc
|> member("inputs")
|> to_assoc
|> List.map(((_k, v)) => get_paths_from_doc(v, paths)),
);
List.concat([[p], sub_paths]);
};
let get_input_paths = () => {
switch (Util.nix(["flake", "archive", "--json", "--no-write-lock-file"])) {
| (WEXITED(0), output) =>
get_paths_from_doc(Yojson.Safe.from_string(output), [])
| _ =>
Printf.eprintf(
"Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n",
);
[];
};
};