Format with ocamlformat

This commit is contained in:
Bryan Bennett 2024-01-08 15:40:13 -05:00
parent 5d0923569f
commit fa7b93e3da
No known key found for this signature in database
GPG key ID: EE149E4215408DE9
11 changed files with 298 additions and 206 deletions

View file

@ -1,13 +1,15 @@
(library
(name lib)
(public_name flake_env.lib)
(libraries
core
core_unix
core_unix.filename_unix
core_unix.sys_unix
ppx_yojson_conv
re
sha)
(instrumentation (backend bisect_ppx))
(preprocess (pps ppx_yojson_conv ppx_jane )))
(name lib)
(public_name flake_env.lib)
(libraries
core
core_unix
core_unix.filename_unix
core_unix.sys_unix
ppx_yojson_conv
re
sha)
(instrumentation
(backend bisect_ppx))
(preprocess
(pps ppx_yojson_conv ppx_jane)))

View file

@ -39,12 +39,16 @@ let hash_files = filenames => {
let rec rmrf = path => {
switch (Unix.lstat(path).st_kind) {
| exception (Unix.Unix_error(_, _, _)) => ()
| S_REG | S_LNK => Unix.unlink(path)
| S_REG
| S_LNK => Unix.unlink(path)
| S_DIR =>
Sys_unix.readdir(path)
|> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
Unix.rmdir(path);
| _ => Printf.eprintf("Unsupported file type (Chr or Block device, FIFO, or Socket)\n")
| _ =>
Printf.eprintf(
"Unsupported file type (Chr or Block device, FIFO, or Socket)\n",
)
};
};

View file

@ -3,16 +3,13 @@ module Unix = Core_unix;
module Util = Flake_env__util;
type t = {
major: int,
minor: int,
point: int,
};
let init = (major, minor, point) => {
{major, minor, point}
};
let init = (major, minor, point) => {major, minor, point};
let required_direnv_version = init(2, 21, 3);
@ -23,12 +20,12 @@ let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|}));
let compare = (a, b) => {
switch (a, b) {
| (a, b) when a.major == b.major && a.minor == b.minor && a.point == b.point => 0
| (a, b) when a.major < b.major => -1
| (a, b) when a.major == b.major && a.minor < b.minor => -1
| (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => -1
| (a, b) when a.major < b.major => (-1)
| (a, b) when a.major == b.major && a.minor < b.minor => (-1)
| (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => (-1)
| _ => 1
}
}
};
};
let extract_version_number = cmd => {
switch (Util.run_process(cmd, ["--version"])) {
@ -55,31 +52,33 @@ let extract_version_number = cmd => {
let is_new_enough = (cur, needed) => {
switch (cur) {
| Ok(cur) => {
switch (compare(cur, needed)) {
| x when x < 0 => Ok(false)
| _ => Ok(true)
}
}
| Error(e) => Error(e)
}
| Ok(cur) =>
switch (compare(cur, needed)) {
| x when x < 0 => Ok(false)
| _ => Ok(true)
}
| Error(e) => Error(e)
};
};
let in_direnv = () => switch (Sys.getenv("direnv")) {
| Some(_) => true
| None => false
};
let in_direnv = () =>
switch (Sys.getenv("direnv")) {
| Some(_) => true
| None => false
};
let preflight_versions = () => {
let is_nix_new_enough = is_new_enough(extract_version_number("nix"), required_nix_version);
let is_direnv_new_enough = is_new_enough(extract_version_number("direnv"), required_direnv_version);
let is_nix_new_enough =
is_new_enough(extract_version_number("nix"), required_nix_version);
let is_direnv_new_enough =
is_new_enough(extract_version_number("direnv"), required_direnv_version);
switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) {
| (false, _, _) => Error("Not in direnv!")
| (_, Ok(false), _) => Error("Direnv version is not new enough")
| (_, _, Ok(false)) => Error("Nix version is not new enough")
| (_, Error(e), _) => Error(e)
| (_, _, Error(e)) => Error(e)
| (true, Ok(true), Ok(true)) => Ok()
}
| (false, _, _) => Error("Not in direnv!")
| (_, Ok(false), _) => Error("Direnv version is not new enough")
| (_, _, Ok(false)) => Error("Nix version is not new enough")
| (_, Error(e), _) => Error(e)
| (_, _, Error(e)) => Error(e)
| (true, Ok(true), Ok(true)) => Ok()
};
};

View file

@ -9,40 +9,52 @@ module Util = Flake_env__util;
type watch = {
exists: bool,
modtime: int,
path: string
path: string,
};
[@deriving yojson]
type watches = array<watch>;
type watches = array(watch);
let get = () => {
let direnv_watch_str = Sys.getenv("DIRENV_WATCHES") |> Option.value_exn(~message="Environment missing DIRENV_WATCHES");
let proc_info = Unix.create_process(~prog="direnv", ~args=["show_dump", direnv_watch_str]);
let direnv_watch_str =
Sys.getenv("DIRENV_WATCHES")
|> Option.value_exn(~message="Environment missing DIRENV_WATCHES");
let proc_info =
Unix.create_process(
~prog="direnv",
~args=["show_dump", direnv_watch_str],
);
let sub_stdout = Unix.in_channel_of_descr(proc_info.stdout);
switch (Unix.waitpid(proc_info.pid)) {
| Ok() => Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout)))
| _ => Error("Failed to parse watches")
}
| Ok () => Ok(watches_of_yojson(Yojson.Safe.from_channel(sub_stdout)))
| _ => Error("Failed to parse watches")
};
};
let get_path = (doc) => String.drop_prefix(doc |> member("path") |> to_string, 11);
let get_path = doc =>
String.drop_prefix(doc |> member("path") |> to_string, 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(~f=((_k, v)) => get_paths_from_doc(v, paths)));
List.concat([[p], sub_paths])
let sub_paths =
List.concat(
doc
|> member("inputs")
|> to_assoc
|> List.map(~f=((_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"])) {
| (Ok(), output) => get_paths_from_doc(Yojson.Safe.from_string(output), [])
| (Error(_), _) => {
Printf.eprintf("Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n");
[]
}
}
| (Ok (), output) =>
get_paths_from_doc(Yojson.Safe.from_string(output), [])
| (Error(_), _) =>
Printf.eprintf(
"Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n",
);
[];
};
};

View file

@ -5,11 +5,11 @@ module Util = Flake_env__util;
module Watches = Flake_env__watches;
module Versions = Flake_env__versions;
let print_cur_cache = (profile_rc) => {
In_channel.read_all(profile_rc) |> Printf.printf("%s")
let print_cur_cache = profile_rc => {
In_channel.read_all(profile_rc) |> Printf.printf("%s");
};
let clean_old_gcroots = (layout_dir) => {
let clean_old_gcroots = layout_dir => {
Util.rmrf(layout_dir ++ "/flake-inputs/");
Util.rmrf(layout_dir);
Unix.mkdir_p(layout_dir ++ "/flake-inputs/");
@ -17,57 +17,80 @@ let clean_old_gcroots = (layout_dir) => {
let add_gcroot = (store_path, symlink) => {
switch (Util.nix(["build", "--out-link", symlink, store_path])) {
| (Ok(), _) => Ok()
| (Ok (), _) => Ok()
| (err, _) => err
}
};
};
let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => {
clean_old_gcroots(layout_dir);
let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ Core.Pid.to_string(Core_unix.getpid());
clean_old_gcroots(layout_dir);
let tmp_profile =
layout_dir
++ "flake-tmp-profile."
++ Core.Pid.to_string(Core_unix.getpid());
let pde_args = ["print-dev-env", "--profile", tmp_profile, flake_specifier, ...other_args];
let (exit_code, stdout_content) = Util.nix(pde_args);
let pde_args = [
"print-dev-env",
"--profile",
tmp_profile,
flake_specifier,
...other_args,
];
let (exit_code, stdout_content) = Util.nix(pde_args);
let profile = layout_dir ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc";
let profile = layout_dir ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc";
switch (exit_code) {
| Ok() => {
Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc);
switch (add_gcroot(tmp_profile, profile)) {
| Ok() => {
Sys_unix.remove(tmp_profile);
let flake_input_cache_path = layout_dir ++ "/flake-inputs/"
let flake_inputs = Watches.get_input_paths();
flake_inputs |> List.iter(~f=(inpt) => {
switch (add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)) {
| Ok() => ()
| err => Printf.eprintf("Failed creating flake-input gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err));
};
});
print_cur_cache(profile_rc);
}
| err => {
Printf.eprintf("Failed creating gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err));
exit(1);
}
};
}
| err => {
Printf.eprintf("Failed evaluating flake: %s\n", Core_unix.Exit_or_signal.to_string_hum(err));
exit(1);
}
switch (exit_code) {
| Ok () =>
Out_channel.with_file(
~f=f => Out_channel.output_string(f, stdout_content),
profile_rc,
);
switch (add_gcroot(tmp_profile, profile)) {
| Ok () =>
Sys_unix.remove(tmp_profile);
let flake_input_cache_path = layout_dir ++ "/flake-inputs/";
let flake_inputs = Watches.get_input_paths();
flake_inputs
|> List.iter(~f=inpt => {
switch (
add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)
) {
| Ok () => ()
| err =>
Printf.eprintf(
"Failed creating flake-input gcroot: %s\n",
Core_unix.Exit_or_signal.to_string_hum(err),
)
}
});
print_cur_cache(profile_rc);
| err =>
Printf.eprintf(
"Failed creating gcroot: %s\n",
Core_unix.Exit_or_signal.to_string_hum(err),
);
exit(1);
};
| err =>
Printf.eprintf(
"Failed evaluating flake: %s\n",
Core_unix.Exit_or_signal.to_string_hum(err),
);
exit(1);
};
};
let preflight = (layout_directory) => {
switch (Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) {
let preflight = layout_directory => {
switch (
Versions.preflight_versions(),
Sys_unix.is_directory(layout_directory),
) {
| (Ok(_), `Yes) => Ok()
| (Ok(_), _) => {
Unix.mkdir_p(layout_directory);
Ok()
}
| (Ok(_), _) =>
Unix.mkdir_p(layout_directory);
Ok();
| (err, _) => err
}
}
};
};