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

View file

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

View file

@ -39,12 +39,16 @@ let hash_files = filenames => {
let rec rmrf = path => { let rec rmrf = path => {
switch (Unix.lstat(path).st_kind) { switch (Unix.lstat(path).st_kind) {
| exception (Unix.Unix_error(_, _, _)) => () | exception (Unix.Unix_error(_, _, _)) => ()
| S_REG | S_LNK => Unix.unlink(path) | S_REG
| S_LNK => Unix.unlink(path)
| S_DIR => | S_DIR =>
Sys_unix.readdir(path) Sys_unix.readdir(path)
|> Array.iter(~f=name => rmrf(Filename.concat(path, name))); |> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
Unix.rmdir(path); 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; module Util = Flake_env__util;
type t = { type t = {
major: int, major: int,
minor: int, minor: int,
point: int, point: int,
}; };
let init = (major, minor, point) => { let init = (major, minor, point) => {major, minor, point};
{major, minor, point}
};
let required_direnv_version = init(2, 21, 3); 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) => { let compare = (a, b) => {
switch (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 && a.minor == b.minor && a.point == b.point => 0
| (a, b) when a.major < b.major => -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 => (-1)
| (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => -1 | (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => (-1)
| _ => 1 | _ => 1
} };
} };
let extract_version_number = cmd => { let extract_version_number = cmd => {
switch (Util.run_process(cmd, ["--version"])) { switch (Util.run_process(cmd, ["--version"])) {
@ -55,31 +52,33 @@ let extract_version_number = cmd => {
let is_new_enough = (cur, needed) => { let is_new_enough = (cur, needed) => {
switch (cur) { switch (cur) {
| Ok(cur) => { | Ok(cur) =>
switch (compare(cur, needed)) { switch (compare(cur, needed)) {
| x when x < 0 => Ok(false) | x when x < 0 => Ok(false)
| _ => Ok(true) | _ => Ok(true)
} }
} | Error(e) => Error(e)
| Error(e) => Error(e) };
}
}; };
let in_direnv = () => switch (Sys.getenv("direnv")) { let in_direnv = () =>
| Some(_) => true switch (Sys.getenv("direnv")) {
| None => false | Some(_) => true
}; | None => false
};
let preflight_versions = () => { let preflight_versions = () => {
let is_nix_new_enough = is_new_enough(extract_version_number("nix"), required_nix_version); let is_nix_new_enough =
let is_direnv_new_enough = is_new_enough(extract_version_number("direnv"), required_direnv_version); 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) { switch (in_direnv(), is_direnv_new_enough, is_nix_new_enough) {
| (false, _, _) => Error("Not in direnv!") | (false, _, _) => Error("Not in direnv!")
| (_, Ok(false), _) => Error("Direnv version is not new enough") | (_, Ok(false), _) => Error("Direnv version is not new enough")
| (_, _, Ok(false)) => Error("Nix version is not new enough") | (_, _, Ok(false)) => Error("Nix version is not new enough")
| (_, Error(e), _) => Error(e) | (_, Error(e), _) => Error(e)
| (_, _, Error(e)) => Error(e) | (_, _, Error(e)) => Error(e)
| (true, Ok(true), Ok(true)) => Ok() | (true, Ok(true), Ok(true)) => Ok()
} };
}; };

View file

@ -9,40 +9,52 @@ module Util = Flake_env__util;
type watch = { type watch = {
exists: bool, exists: bool,
modtime: int, modtime: int,
path: string path: string,
}; };
[@deriving yojson] [@deriving yojson]
type watches = array<watch>; type watches = array(watch);
let get = () => { let get = () => {
let direnv_watch_str = Sys.getenv("DIRENV_WATCHES") |> Option.value_exn(~message="Environment missing DIRENV_WATCHES"); let direnv_watch_str =
let proc_info = Unix.create_process(~prog="direnv", ~args=["show_dump", 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); let sub_stdout = Unix.in_channel_of_descr(proc_info.stdout);
switch (Unix.waitpid(proc_info.pid)) { 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") | _ => 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 rec get_paths_from_doc = (doc, paths) => {
let p = get_path(doc); let p = get_path(doc);
let sub_paths = List.concat( let sub_paths =
doc |> member("inputs") List.concat(
|> to_assoc doc
|> List.map(~f=((_k, v)) => get_paths_from_doc(v, paths))); |> member("inputs")
List.concat([[p], sub_paths]) |> to_assoc
|> List.map(~f=((_k, v)) => get_paths_from_doc(v, paths)),
);
List.concat([[p], sub_paths]);
}; };
let get_input_paths = () => { let get_input_paths = () => {
switch (Util.nix(["flake", "archive", "--json", "--no-write-lock-file"])) { switch (Util.nix(["flake", "archive", "--json", "--no-write-lock-file"])) {
| (Ok(), output) => get_paths_from_doc(Yojson.Safe.from_string(output), []) | (Ok (), output) =>
| (Error(_), _) => { get_paths_from_doc(Yojson.Safe.from_string(output), [])
Printf.eprintf("Failed to parse output of `nix flake archive --json`. Ignorning flake inputs. \n"); | (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 Watches = Flake_env__watches;
module Versions = Flake_env__versions; module Versions = Flake_env__versions;
let print_cur_cache = (profile_rc) => { let print_cur_cache = profile_rc => {
In_channel.read_all(profile_rc) |> Printf.printf("%s") 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 ++ "/flake-inputs/");
Util.rmrf(layout_dir); Util.rmrf(layout_dir);
Unix.mkdir_p(layout_dir ++ "/flake-inputs/"); Unix.mkdir_p(layout_dir ++ "/flake-inputs/");
@ -17,57 +17,80 @@ let clean_old_gcroots = (layout_dir) => {
let add_gcroot = (store_path, symlink) => { let add_gcroot = (store_path, symlink) => {
switch (Util.nix(["build", "--out-link", symlink, store_path])) { switch (Util.nix(["build", "--out-link", symlink, store_path])) {
| (Ok(), _) => Ok() | (Ok (), _) => Ok()
| (err, _) => err | (err, _) => err
} };
}; };
let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => { let freshen_cache = (layout_dir, hash, flake_specifier, other_args) => {
clean_old_gcroots(layout_dir); 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 = [
let (exit_code, stdout_content) = Util.nix(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 = layout_dir ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc"; let profile_rc = profile ++ ".rc";
switch (exit_code) { switch (exit_code) {
| Ok() => { | Ok () =>
Out_channel.with_file(~f=f=> Out_channel.output_string(f, stdout_content), profile_rc); Out_channel.with_file(
switch (add_gcroot(tmp_profile, profile)) { ~f=f => Out_channel.output_string(f, stdout_content),
| Ok() => { profile_rc,
Sys_unix.remove(tmp_profile); );
let flake_input_cache_path = layout_dir ++ "/flake-inputs/" switch (add_gcroot(tmp_profile, profile)) {
let flake_inputs = Watches.get_input_paths(); | Ok () =>
flake_inputs |> List.iter(~f=(inpt) => { Sys_unix.remove(tmp_profile);
switch (add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)) { let flake_input_cache_path = layout_dir ++ "/flake-inputs/";
| Ok() => () let flake_inputs = Watches.get_input_paths();
| 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 (
print_cur_cache(profile_rc); add_gcroot("/nix/store/" ++ inpt, flake_input_cache_path ++ inpt)
} ) {
| err => { | Ok () => ()
Printf.eprintf("Failed creating gcroot: %s\n", Core_unix.Exit_or_signal.to_string_hum(err)); | err =>
exit(1); Printf.eprintf(
} "Failed creating flake-input gcroot: %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); 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) => { let preflight = layout_directory => {
switch (Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) { switch (
Versions.preflight_versions(),
Sys_unix.is_directory(layout_directory),
) {
| (Ok(_), `Yes) => Ok() | (Ok(_), `Yes) => Ok()
| (Ok(_), _) => { | (Ok(_), _) =>
Unix.mkdir_p(layout_directory); Unix.mkdir_p(layout_directory);
Ok() Ok();
}
| (err, _) => err | (err, _) => err
} };
} };

View file

@ -1,13 +1,10 @@
(tests (tests
(names (names flake_env_test_versions flake_env_test_watches flake_env_test_util)
flake_env_test_versions (deps spit_version.sh spit_gibberish.sh)
flake_env_test_watches (libraries
flake_env_test_util) lib
(deps spit_version.sh spit_gibberish.sh) alcotest
(libraries core
lib core_unix
alcotest core_unix.sys_unix
core core_unix.filename_unix))
core_unix
core_unix.sys_unix
core_unix.filename_unix))

View file

@ -111,7 +111,11 @@ let test_get_args_just_enough = () => {
}; };
let test_get_args_error = () => { 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 () = let () =
@ -148,11 +152,15 @@ let () =
), ),
( (
"get_args", "get_args",
[ [
test_case("Parses Args", `Quick, test_get_args_simple), test_case("Parses Args", `Quick, test_get_args_simple),
test_case("Parses just enough args", `Quick, test_get_args_just_enough), test_case(
test_case("Handles too few args", `Quick, test_get_args_error) "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 test_extract_version_number_no_version = () => {
let result = Versions.extract_version_number("../tests/spit_gibberish.sh"); 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 test_extract_version_number_nonexistent = () => {
let result = Versions.extract_version_number("nonexistent.sh"); 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: // TODO: Test:
// * preflight_versions? impure, but m // * preflight_versions? impure, but m
let () = let () =
@ -177,8 +186,16 @@ let () =
"extract_version_number", "extract_version_number",
[ [
test_case("success", `Quick, test_extract_version_number_success), test_case("success", `Quick, test_extract_version_number_success),
test_case("no version number", `Quick, test_extract_version_number_no_version), test_case(
test_case("missing binary", `Quick, test_extract_version_number_nonexistent), "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; open Lib.Watches;
let test_get_path_removes_prefix = () => { let test_get_path_removes_prefix = () => {
let input = `Assoc([ let input = `Assoc([("path", `String("aaaaaaaaaaabbbbb"))]);
("path", `String("aaaaaaaaaaabbbbb")) Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input));
]);
Alcotest.(check(string))("Prefix removed", "bbbbb", get_path(input))
}; };
let test_get_paths_from_doc = () => { let test_get_paths_from_doc = () => {
let input = `Assoc([ let input =
("path", `String("aaaaaaaaaaabbbbb")), `Assoc([
("inputs", `Assoc([ ("path", `String("aaaaaaaaaaabbbbb")),
("foo", `Assoc([ (
("path", `String("aaaaaaaaaaaccccc")), "inputs",
("inputs", `Assoc([ `Assoc([
("bar", `Assoc([ (
("path", `String("aaaaaaaaaaaddddd")), "foo",
("inputs", `Assoc([])) `Assoc([
])) ("path", `String("aaaaaaaaaaaccccc")),
])) (
])) "inputs",
])) `Assoc([
]); (
Alcotest.(check(list(string)))("Gathers all inputs", ["bbbbb", "ccccc", "ddddd"], get_paths_from_doc(input, [])) "bar",
`Assoc([
("path", `String("aaaaaaaaaaaddddd")),
("inputs", `Assoc([])),
]),
),
]),
),
]),
),
]),
),
]);
Alcotest.(check(list(string)))(
"Gathers all inputs",
["bbbbb", "ccccc", "ddddd"],
get_paths_from_doc(input, []),
);
}; };
let () = let () =
Alcotest.( Alcotest.(
run( run(
"Watches", "Watches",
[("get_path",
[ [
test_case("Removes prefix", `Quick, test_get_path_removes_prefix), (
]), "get_path",
("get_paths_from_doc", [
[ test_case("Removes prefix", `Quick, test_get_path_removes_prefix),
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)],
),
],
)
);