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,5 +1,14 @@
(executable
(name flake_env)
(public_name flake_env)
(libraries core core_unix core_unix.filename_unix core_unix.sys_unix ppx_yojson_conv re sha lib)
(preprocess (pps ppx_yojson_conv ppx_jane)))
(libraries
core
core_unix
core_unix.filename_unix
core_unix.sys_unix
ppx_yojson_conv
re
sha
lib)
(preprocess
(pps ppx_yojson_conv ppx_jane)))

View file

@ -6,50 +6,52 @@ open Lib;
let main = () => {
let argv = Sys.get_argv();
switch (Util.get_args(argv)) {
| Ok((layout_directory, flake_specifier, other_args)) => {
| Ok((layout_directory, flake_specifier, other_args)) =>
switch (preflight(layout_directory)) {
| Ok() => {
| Ok () =>
switch (Lib.Watches.get()) {
| Ok(watches) => {
| Ok(watches) =>
let paths = Array.map(~f=watch => watch.path, watches);
let hash = Util.hash_files(paths);
let profile = layout_directory ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc";
switch ((Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile))) {
| (`Yes, `Yes) => {
switch (Sys_unix.is_file(profile_rc), Sys_unix.is_file(profile)) {
| (`Yes, `Yes) =>
let profile_rc_mtime = Unix.stat(profile_rc).st_mtime;
let all_older = Array.map(
~f=watch => watch.modtime, watches)
|> Array.for_all(
~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime)
)
let all_older =
Array.map(~f=watch => watch.modtime, watches)
|> Array.for_all(~f=watch_mtime =>
watch_mtime <= int_of_float(profile_rc_mtime)
);
if (all_older) {
print_cur_cache(profile_rc)
print_cur_cache(profile_rc);
} else {
freshen_cache(
layout_directory,
hash,
flake_specifier,
other_args,
);
};
| _ =>
freshen_cache(layout_directory, hash, flake_specifier, other_args)
}
}
| _ => freshen_cache(layout_directory, hash, flake_specifier, other_args)
}
};
| Error(e) => {
| Error(e) =>
Printf.eprintf("%s\n", e);
exit(1);
}
}
}
| Error(e) => {
| Error(e) =>
Printf.eprintf("%s\n", e);
exit(1);
}
};
}
| Error() => {
Printf.eprintf("%s <layout_directory> <flake specifier> <...args>\n", argv[0]);
| Error () =>
Printf.eprintf(
"%s <layout_directory> <flake specifier> <...args>\n",
argv[0],
);
exit(1);
}
}
};
};
let () = main();

View file

@ -9,5 +9,7 @@
ppx_yojson_conv
re
sha)
(instrumentation (backend bisect_ppx))
(preprocess (pps ppx_yojson_conv ppx_jane )))
(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,24 +52,26 @@ let extract_version_number = cmd => {
let is_new_enough = (cur, needed) => {
switch (cur) {
| Ok(cur) => {
| 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")) {
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!")
@ -81,5 +80,5 @@ let preflight_versions = () => {
| (_, 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)))
| 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")
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])
|> 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());
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 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";
switch (exit_code) {
| Ok() => {
Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc);
| Ok () =>
Out_channel.with_file(
~f=f => Out_channel.output_string(f, stdout_content),
profile_rc,
);
switch (add_gcroot(tmp_profile, profile)) {
| Ok() => {
| Ok () =>
Sys_unix.remove(tmp_profile);
let flake_input_cache_path = layout_dir ++ "/flake-inputs/"
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));
};
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));
| 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));
| 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(_), _) => {
| (Ok(_), _) =>
Unix.mkdir_p(layout_directory);
Ok()
}
Ok();
| (err, _) => err
}
}
};
};

View file

@ -1,8 +1,5 @@
(tests
(names
flake_env_test_versions
flake_env_test_watches
flake_env_test_util)
(names flake_env_test_versions flake_env_test_watches flake_env_test_util)
(deps spit_version.sh spit_gibberish.sh)
(libraries
lib

View file

@ -111,7 +111,11 @@ let test_get_args_just_enough = () => {
};
let test_get_args_error = () => {
check_get_args("Errors on too few args", Error(), get_args([|"000", "111"|]))
check_get_args(
"Errors on too few args",
Error(),
get_args([|"000", "111"|]),
);
};
let () =
@ -150,8 +154,12 @@ let () =
"get_args",
[
test_case("Parses Args", `Quick, test_get_args_simple),
test_case("Parses just enough args", `Quick, test_get_args_just_enough),
test_case("Handles too few args", `Quick, test_get_args_error)
test_case(
"Parses just enough args",
`Quick,
test_get_args_just_enough,
),
test_case("Handles too few args", `Quick, test_get_args_error),
],
),
],

View file

@ -105,15 +105,24 @@ let test_extract_version_number_success = () => {
let test_extract_version_number_no_version = () => {
let result = Versions.extract_version_number("../tests/spit_gibberish.sh");
check_version("Versions", Error("Stdout did not contain a version number for `../tests/spit_gibberish.sh --version`"), result);
check_version(
"Versions",
Error(
"Stdout did not contain a version number for `../tests/spit_gibberish.sh --version`",
),
result,
);
};
let test_extract_version_number_nonexistent = () => {
let result = Versions.extract_version_number("nonexistent.sh");
check_version("Versions", Error("Failed executing 'nonexistent.sh'"), result);
check_version(
"Versions",
Error("Failed executing 'nonexistent.sh'"),
result,
);
};
// TODO: Test:
// * preflight_versions? impure, but m
let () =
@ -177,8 +186,16 @@ let () =
"extract_version_number",
[
test_case("success", `Quick, test_extract_version_number_success),
test_case("no version number", `Quick, test_extract_version_number_no_version),
test_case("missing binary", `Quick, test_extract_version_number_nonexistent),
test_case(
"no version number",
`Quick,
test_extract_version_number_no_version,
),
test_case(
"missing binary",
`Quick,
test_extract_version_number_nonexistent,
),
],
),
],

View file

@ -1,41 +1,60 @@
open Lib.Watches;
let test_get_path_removes_prefix = () => {
let input = `Assoc([
("path", `String("aaaaaaaaaaabbbbb"))
]);
Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input))
let input = `Assoc([("path", `String("aaaaaaaaaaabbbbb"))]);
Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input));
};
let test_get_paths_from_doc = () => {
let input = `Assoc([
let input =
`Assoc([
("path", `String("aaaaaaaaaaabbbbb")),
("inputs", `Assoc([
("foo", `Assoc([
(
"inputs",
`Assoc([
(
"foo",
`Assoc([
("path", `String("aaaaaaaaaaaccccc")),
("inputs", `Assoc([
("bar", `Assoc([
(
"inputs",
`Assoc([
(
"bar",
`Assoc([
("path", `String("aaaaaaaaaaaddddd")),
("inputs", `Assoc([]))
]))
]))
]))
]))
("inputs", `Assoc([])),
]),
),
]),
),
]),
),
]),
),
]);
Alcotest.(check(list(string)))("Gathers all inputs", ["bbbbb", "ccccc", "ddddd"], get_paths_from_doc(input, []))
Alcotest.(check(list(string)))(
"Gathers all inputs",
["bbbbb", "ccccc", "ddddd"],
get_paths_from_doc(input, []),
);
};
let () =
Alcotest.(
run(
"Watches",
[("get_path",
[
(
"get_path",
[
test_case("Removes prefix", `Quick, test_get_path_removes_prefix),
]),
("get_paths_from_doc",
[
test_case("Collects all paths", `Quick, test_get_paths_from_doc),
])
]),
);
],
),
(
"get_paths_from_doc",
[test_case("Collects all paths", `Quick, test_get_paths_from_doc)],
),
],
)
);