diff --git a/bin/dune b/bin/dune index 9b0e082..186c456 100644 --- a/bin/dune +++ b/bin/dune @@ -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))) \ No newline at end of file + (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))) diff --git a/bin/flake_env.re b/bin/flake_env.re index 2615e9c..4567cb9 100644 --- a/bin/flake_env.re +++ b/bin/flake_env.re @@ -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)) => { - switch (preflight(layout_directory)) { - | Ok() => { - switch (Lib.Watches.get()) { - | 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"; + | Ok((layout_directory, flake_specifier, other_args)) => + switch (preflight(layout_directory)) { + | Ok () => + switch (Lib.Watches.get()) { + | 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) => { - 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) - ) - if (all_older) { - print_cur_cache(profile_rc) - } else { - freshen_cache(layout_directory, hash, flake_specifier, other_args) - } - } - | _ => freshen_cache(layout_directory, hash, flake_specifier, other_args) - } - }; - | Error(e) => { - Printf.eprintf("%s\n", e); - exit(1); - } - } - } - | Error(e) => { - Printf.eprintf("%s\n", e); - exit(1); - } - }; - } - | Error() => { - Printf.eprintf("%s <...args>\n", argv[0]); + 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) + ); + if (all_older) { + print_cur_cache(profile_rc); + } else { + freshen_cache( + layout_directory, + hash, + flake_specifier, + other_args, + ); + }; + | _ => + freshen_cache(layout_directory, hash, flake_specifier, other_args) + }; + | Error(e) => + Printf.eprintf("%s\n", e); + exit(1); + } + | Error(e) => + Printf.eprintf("%s\n", e); exit(1); } - } + | Error () => + Printf.eprintf( + "%s <...args>\n", + argv[0], + ); + exit(1); + }; }; let () = main(); diff --git a/lib/dune b/lib/dune index e268194..168d405 100644 --- a/lib/dune +++ b/lib/dune @@ -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 ))) \ No newline at end of file + (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))) diff --git a/lib/flake_env__util.re b/lib/flake_env__util.re index 0e8dfc5..1b5aa7b 100644 --- a/lib/flake_env__util.re +++ b/lib/flake_env__util.re @@ -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", + ) }; }; diff --git a/lib/flake_env__versions.re b/lib/flake_env__versions.re index 1fbcd71..c36fb04 100644 --- a/lib/flake_env__versions.re +++ b/lib/flake_env__versions.re @@ -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() + }; }; diff --git a/lib/flake_env__watches.re b/lib/flake_env__watches.re index db4cfc7..5f0ee3e 100644 --- a/lib/flake_env__watches.re +++ b/lib/flake_env__watches.re @@ -9,40 +9,52 @@ module Util = Flake_env__util; type watch = { exists: bool, modtime: int, - path: string + path: string, }; [@deriving yojson] -type watches = array; +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", + ); + []; + }; }; diff --git a/lib/lib.re b/lib/lib.re index a9a1f6e..e411503 100644 --- a/lib/lib.re +++ b/lib/lib.re @@ -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 - } -} + }; +}; diff --git a/tests/dune b/tests/dune index deb6af3..dce3e4c 100644 --- a/tests/dune +++ b/tests/dune @@ -1,13 +1,10 @@ (tests - (names - flake_env_test_versions - flake_env_test_watches - flake_env_test_util) - (deps spit_version.sh spit_gibberish.sh) - (libraries - lib - alcotest - core - core_unix - core_unix.sys_unix - core_unix.filename_unix)) \ No newline at end of file + (names flake_env_test_versions flake_env_test_watches flake_env_test_util) + (deps spit_version.sh spit_gibberish.sh) + (libraries + lib + alcotest + core + core_unix + core_unix.sys_unix + core_unix.filename_unix)) diff --git a/tests/flake_env_test_util.re b/tests/flake_env_test_util.re index 6f8e11f..e45ccaf 100644 --- a/tests/flake_env_test_util.re +++ b/tests/flake_env_test_util.re @@ -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 () = @@ -148,11 +152,15 @@ 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 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), + ], ), ], ) diff --git a/tests/flake_env_test_versions.re b/tests/flake_env_test_versions.re index ac5214a..b0f7208 100644 --- a/tests/flake_env_test_versions.re +++ b/tests/flake_env_test_versions.re @@ -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, + ), ], ), ], diff --git a/tests/flake_env_test_watches.re b/tests/flake_env_test_watches.re index 38d4226..0a60efb 100644 --- a/tests/flake_env_test_watches.re +++ b/tests/flake_env_test_watches.re @@ -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([ - ("path", `String("aaaaaaaaaaabbbbb")), - ("inputs", `Assoc([ - ("foo", `Assoc([ - ("path", `String("aaaaaaaaaaaccccc")), - ("inputs", `Assoc([ - ("bar", `Assoc([ - ("path", `String("aaaaaaaaaaaddddd")), - ("inputs", `Assoc([])) - ])) - ])) - ])) - ])) - ]); - Alcotest.(check(list(string)))("Gathers all inputs", ["bbbbb", "ccccc", "ddddd"], get_paths_from_doc(input, [])) + let input = + `Assoc([ + ("path", `String("aaaaaaaaaaabbbbb")), + ( + "inputs", + `Assoc([ + ( + "foo", + `Assoc([ + ("path", `String("aaaaaaaaaaaccccc")), + ( + "inputs", + `Assoc([ + ( + "bar", + `Assoc([ + ("path", `String("aaaaaaaaaaaddddd")), + ("inputs", `Assoc([])), + ]), + ), + ]), + ), + ]), + ), + ]), + ), + ]); + Alcotest.(check(list(string)))( + "Gathers all inputs", + ["bbbbb", "ccccc", "ddddd"], + get_paths_from_doc(input, []), + ); }; let () = Alcotest.( - run( - "Watches", - [("get_path", + run( + "Watches", [ - 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_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)], + ), + ], + ) + );