mirror of
https://git.sr.ht/~bryan_bennett/flake_env
synced 2025-12-19 16:01:25 +01:00
Compare commits
1 commit
04337c8c5f
...
5e505b178b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e505b178b |
13 changed files with 486 additions and 165 deletions
128
bin/flake_env.re
128
bin/flake_env.re
|
|
@ -1,130 +1,36 @@
|
||||||
open Core;
|
open Core;
|
||||||
module Unix = Core_unix;
|
module Unix = Core_unix;
|
||||||
|
|
||||||
|
open Lib;
|
||||||
|
|
||||||
let preflight = (layout_directory) => {
|
|
||||||
switch (Lib.Versions.preflight_versions(), Sys_unix.is_directory(layout_directory)) {
|
|
||||||
| (Ok(_), `Yes) => Ok()
|
|
||||||
| (Ok(_), _) => {
|
|
||||||
Unix.mkdir_p(layout_directory);
|
|
||||||
Ok()
|
|
||||||
}
|
|
||||||
| (err, _) => err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let hash_files = (filenames) => {
|
|
||||||
let ctx = Sha1.init();
|
|
||||||
let () = filenames
|
|
||||||
|> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) {
|
|
||||||
| `Yes => true
|
|
||||||
| _ => false
|
|
||||||
})
|
|
||||||
|> Array.iter(~f=(f) => {
|
|
||||||
f |> In_channel.create |> In_channel.input_all |> Sha1.update_string(ctx);
|
|
||||||
});
|
|
||||||
Sha1.finalize(ctx) |> Sha1.to_hex
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Maybe make this more terse?
|
|
||||||
let rec rmrf = (path) => {
|
|
||||||
switch (Unix.lstat(path).st_kind) {
|
|
||||||
| exception Unix.Unix_error(_, _, _) => ()
|
|
||||||
| S_REG => Unix.unlink(path)
|
|
||||||
| S_LNK => Unix.unlink(path)
|
|
||||||
| S_DIR => {
|
|
||||||
Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
|
|
||||||
Unix.rmdir(path)
|
|
||||||
}
|
|
||||||
| S_CHR => Printf.eprintf("Don't know how to handle Character Device file\n")
|
|
||||||
| S_BLK => Printf.eprintf("Don't know how to handle Block Device file\n")
|
|
||||||
| S_FIFO => Printf.eprintf("Don't know how to handle FIFO file\n")
|
|
||||||
| S_SOCK => Printf.eprintf("Don't know how to handle Socket file\n")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let clean_old_gcroots = (layout_dir) => {
|
|
||||||
rmrf(layout_dir ++ "/flake-inputs/");
|
|
||||||
rmrf(layout_dir);
|
|
||||||
Unix.mkdir_p(layout_dir ++ "/flake-inputs/");
|
|
||||||
};
|
|
||||||
|
|
||||||
let print_cur_cache = (profile_rc) => {
|
|
||||||
In_channel.read_all(profile_rc) |> Printf.printf("%s")
|
|
||||||
};
|
|
||||||
|
|
||||||
let add_gcroot = (store_path, symlink) => {
|
|
||||||
switch (Lib.Util.nix(["build", "--out-link", symlink, store_path])) {
|
|
||||||
| (Ok(), _) => Ok()
|
|
||||||
| (err, _) => err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let freshen_cache = (layout_dir, hash, flake_specifier) => {
|
|
||||||
clean_old_gcroots(layout_dir);
|
|
||||||
let tmp_profile = layout_dir ++ "flake-tmp-profile." ++ Core.Pid.to_string(Core_unix.getpid());
|
|
||||||
let (exit_code, stdout_content) = Lib.Util.nix(["print-dev-env", "--profile", tmp_profile, flake_specifier]);
|
|
||||||
|
|
||||||
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 = Lib.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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Extend to add support for additional flags, maybe?
|
|
||||||
let main = () => {
|
let main = () => {
|
||||||
let argv = Sys.get_argv();
|
let argv = Sys.get_argv();
|
||||||
switch (Array.length(argv)) {
|
switch (Util.get_args(argv)) {
|
||||||
| 3 => {
|
| Ok((layout_directory, flake_specifier, other_args)) => {
|
||||||
let flake_specifier = argv[1];
|
|
||||||
let layout_directory = argv[2];
|
|
||||||
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 = 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(~f=watch => watch.modtime, watches)
|
let all_older = Array.map(
|
||||||
|> Array.for_all(~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime))
|
~f=watch => watch.modtime, watches)
|
||||||
switch (all_older) {
|
|> Array.for_all(
|
||||||
| true => print_cur_cache(profile_rc)
|
~f=watch_mtime => watch_mtime <= int_of_float(profile_rc_mtime)
|
||||||
| false => freshen_cache(layout_directory, hash, flake_specifier)
|
)
|
||||||
|
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)
|
| _ => freshen_cache(layout_directory, hash, flake_specifier, other_args)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
| Error(e) => {
|
| Error(e) => {
|
||||||
|
|
@ -139,11 +45,11 @@ let main = () => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
| _ => {
|
| Error() => {
|
||||||
Printf.eprintf("%s <flake specifier> <layout_directory>\n", argv[0]);
|
Printf.eprintf("%s <layout_directory> <flake specifier> <...args>\n", argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
let () = main();
|
let () = main();
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
, core
|
, core
|
||||||
, core_unix
|
, core_unix
|
||||||
, findlib
|
, findlib
|
||||||
|
, fmt
|
||||||
, lib
|
, lib
|
||||||
, nix-filter
|
, nix-filter
|
||||||
, ocaml
|
, ocaml
|
||||||
|
|
@ -41,6 +42,7 @@ buildDunePackage {
|
||||||
core
|
core
|
||||||
core_unix
|
core_unix
|
||||||
findlib
|
findlib
|
||||||
|
fmt
|
||||||
ocaml
|
ocaml
|
||||||
ppx_yojson_conv
|
ppx_yojson_conv
|
||||||
ppx_yojson_conv_lib
|
ppx_yojson_conv_lib
|
||||||
|
|
|
||||||
2
direnvrc
2
direnvrc
|
|
@ -41,7 +41,7 @@ use_flake_env() {
|
||||||
local ld=$(direnv_layout_dir)
|
local ld=$(direnv_layout_dir)
|
||||||
export direnv
|
export direnv
|
||||||
|
|
||||||
eval "$(@flake_env@ "$1" "$ld")"
|
eval "$(@flake_env@ "$ld" "$@")"
|
||||||
|
|
||||||
export -n direnv
|
export -n direnv
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,8 @@
|
||||||
devShells.default = pkgs.mkShell {
|
devShells.default = pkgs.mkShell {
|
||||||
inputsFrom = [ self'.packages.default ];
|
inputsFrom = [ self'.packages.default ];
|
||||||
packages = [
|
packages = [
|
||||||
|
pkgs.ocamlPackages.alcotest
|
||||||
|
pkgs.ocamlPackages.bisect_ppx
|
||||||
pkgs.ocamlPackages.dune_3
|
pkgs.ocamlPackages.dune_3
|
||||||
pkgs.ocamlPackages.findlib
|
pkgs.ocamlPackages.findlib
|
||||||
pkgs.ocamlPackages.ocaml
|
pkgs.ocamlPackages.ocaml
|
||||||
|
|
|
||||||
81
lib/#flake_env__versions.re#
Normal file
81
lib/#flake_env__versions.re#
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
open Core;
|
||||||
|
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 required_direnv_version = init(2, 21, 3);
|
||||||
|
|
||||||
|
let required_nix_version = init(2, 10, 0);
|
||||||
|
|
||||||
|
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
|
||||||
|
| _ => 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let extract_version_number = (cmd) => {
|
||||||
|
switch (Util.run_process(cmd, ["--version"])) {
|
||||||
|
| (Ok(), stdout) when String.length(stdout) > 0 => {
|
||||||
|
let substrings = Re.exec(semver_re, stdout);
|
||||||
|
let groups = Re.Group.all(substrings);
|
||||||
|
if ((groups |> Array.length) == 4) {
|
||||||
|
Ok({
|
||||||
|
major: groups[1] |> int_of_string,
|
||||||
|
minor: groups[2] |> int_of_string,
|
||||||
|
point: groups[3] |> int_of_string
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Error(Printf.sprintf("Stdout did not contain a version number for `%s --version`", cmd))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| _ => Error(Printf.sprintf("Failed executing '%s'\n", 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)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
};
|
||||||
11
lib/dune
11
lib/dune
|
|
@ -1,5 +1,14 @@
|
||||||
(library
|
(library
|
||||||
(name lib)
|
(name lib)
|
||||||
(public_name flake_env.lib)
|
(public_name flake_env.lib)
|
||||||
(libraries core core_unix core_unix.filename_unix core_unix.sys_unix ppx_yojson_conv re sha)
|
(libraries
|
||||||
|
core
|
||||||
|
core_unix
|
||||||
|
core_unix.filename_unix
|
||||||
|
core_unix.sys_unix
|
||||||
|
fmt
|
||||||
|
ppx_yojson_conv
|
||||||
|
re
|
||||||
|
sha)
|
||||||
|
(instrumentation (backend bisect_ppx))
|
||||||
(preprocess (pps ppx_yojson_conv ppx_jane ppx_inline_test)))
|
(preprocess (pps ppx_yojson_conv ppx_jane ppx_inline_test)))
|
||||||
|
|
@ -1,10 +1,55 @@
|
||||||
open Core;
|
open Core;
|
||||||
module Unix = Core_unix;
|
module Unix = Core_unix;
|
||||||
|
|
||||||
let nix = (args) => {
|
let run_process = (name, args) => {
|
||||||
let stdout_chan = Unix.open_process_in(
|
/*** Run a process [name] with args [args], returning (exit_code, stdout text) */
|
||||||
"nix --extra-experimental-features \"nix-command flakes\" " ++ (args |> String.concat(~sep=" ")));
|
let stdout_chan = Unix.open_process_in(name ++ " " ++ (args |> String.concat(~sep=" ")));
|
||||||
let stdout_content = stdout_chan |> In_channel.input_all;
|
let stdout_content = stdout_chan |> In_channel.input_all;
|
||||||
let exit_code = Unix.close_process_in(stdout_chan);
|
let exit_code = Unix.close_process_in(stdout_chan);
|
||||||
(exit_code, stdout_content)
|
(exit_code, stdout_content)
|
||||||
}
|
};
|
||||||
|
|
||||||
|
let nix = (args) => run_process("nix", ["--extra-experimental-features", "\"nix-command flakes\" ", ...args]);
|
||||||
|
|
||||||
|
let hash_files = (filenames) => {
|
||||||
|
/*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */
|
||||||
|
let ctx = Sha1.init();
|
||||||
|
let () = filenames
|
||||||
|
|> Array.filter(~f=f => switch (Sys_unix.file_exists(f)) {
|
||||||
|
| `Yes => true
|
||||||
|
| _ => false
|
||||||
|
})
|
||||||
|
|> Array.iter(~f=(f) => {
|
||||||
|
f |> In_channel.create |> In_channel.input_all |> Sha1.update_string(ctx);
|
||||||
|
});
|
||||||
|
Sha1.finalize(ctx) |> Sha1.to_hex
|
||||||
|
};
|
||||||
|
|
||||||
|
let rec rmrf = (path) => {
|
||||||
|
switch (Unix.lstat(path).st_kind) {
|
||||||
|
| exception Unix.Unix_error(_, _, _) => ()
|
||||||
|
| S_REG => Unix.unlink(path)
|
||||||
|
| S_LNK => Unix.unlink(path)
|
||||||
|
| S_DIR => {
|
||||||
|
Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
|
||||||
|
Unix.rmdir(path)
|
||||||
|
}
|
||||||
|
| S_CHR => Printf.eprintf("Don't know how to handle Character Device file\n")
|
||||||
|
| S_BLK => Printf.eprintf("Don't know how to handle Block Device file\n")
|
||||||
|
| S_FIFO => Printf.eprintf("Don't know how to handle FIFO file\n")
|
||||||
|
| S_SOCK => Printf.eprintf("Don't know how to handle Socket file\n")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let get_args = (argv) => {
|
||||||
|
switch (Array.length(argv)) {
|
||||||
|
| x when x >= 3 => {
|
||||||
|
let layout_directory = argv[1];
|
||||||
|
let flake_specifier = argv[2];
|
||||||
|
let other_args = snd(List.split_n(List.of_array(argv), 3));
|
||||||
|
Ok((layout_directory, flake_specifier, other_args))
|
||||||
|
}
|
||||||
|
| _ => Error()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,28 @@
|
||||||
open Core;
|
open Core;
|
||||||
module Unix = Core_unix;
|
module Unix = Core_unix;
|
||||||
|
|
||||||
|
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) => {
|
||||||
|
{major, minor, point}
|
||||||
|
};
|
||||||
|
|
||||||
|
let required_direnv_version = init(2, 21, 3);
|
||||||
|
|
||||||
|
let required_nix_version = init(2, 10, 0);
|
||||||
|
|
||||||
let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|}));
|
let semver_re = Re.compile(Re.Posix.re({|([0-9]+)\.([0-9]+)\.([0-9]+)|}));
|
||||||
|
|
||||||
let required_direnv_version = {
|
let pprint = (pp_fmt, version) => Fmt.pf(pp_fmt, "{ major: %d, minor: %d, point: %d }", version.major, version.minor, version.point);
|
||||||
major: 2,
|
|
||||||
minor: 21,
|
|
||||||
point: 3
|
|
||||||
};
|
|
||||||
|
|
||||||
let required_nix_version = {
|
let compare = (a, b) => {
|
||||||
major: 2,
|
|
||||||
minor: 10,
|
|
||||||
point: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: test this
|
|
||||||
let compare_version_number = (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
|
||||||
|
|
@ -32,46 +32,47 @@ let compare_version_number = (a, b) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let%test_unit "compare_version_number" = [%test_eq: int](compare_version_number({major: 1, minor: 0, point: 0}, {major: 2, minor: 0, point: 0}), -1);
|
|
||||||
|
|
||||||
let extract_version_number = (cmd) => {
|
let extract_version_number = (cmd) => {
|
||||||
let full_cmd = cmd ++ " --version";
|
switch (Util.run_process(cmd, ["--version"])) {
|
||||||
switch (Core_unix.open_process_in(full_cmd) |> In_channel.input_line) {
|
| (Ok(), stdout) when String.length(stdout) > 0 => {
|
||||||
| Some(stdout) => {
|
|
||||||
let substrings = Re.exec(semver_re, stdout);
|
let substrings = Re.exec(semver_re, stdout);
|
||||||
let groups = Re.Group.all(substrings);
|
let groups = Re.Group.all(substrings);
|
||||||
|
if ((groups |> Array.length) == 4) {
|
||||||
Ok({
|
Ok({
|
||||||
major: groups[1] |> int_of_string,
|
major: groups[1] |> int_of_string,
|
||||||
minor: groups[2] |> int_of_string,
|
minor: groups[2] |> int_of_string,
|
||||||
point: groups[3] |> int_of_string
|
point: groups[3] |> int_of_string
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Error(Printf.sprintf("Stdout did not contain a version number for `%s --version`", cmd))
|
||||||
}
|
}
|
||||||
| None => Error(Printf.sprintf("Failed executing '%s'\n", cmd))
|
}
|
||||||
|
| _ => Error(Printf.sprintf("Failed executing '%s'\n", cmd))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_version_new_enough = (cur, needed) => {
|
let is_new_enough = (cur, needed) => {
|
||||||
switch (cur) {
|
switch (cur) {
|
||||||
| Ok(cur) => {
|
| Ok(cur) => {
|
||||||
switch (compare_version_number(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 preflight_versions = () => {
|
let in_direnv = () => switch (Sys.getenv("direnv")) {
|
||||||
let in_direnv = switch (Sys.getenv("direnv")) {
|
|
||||||
| Some(_) => true
|
| Some(_) => true
|
||||||
| None => false
|
| None => false
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_nix_new_enough = is_version_new_enough(extract_version_number("nix"), required_nix_version);
|
let preflight_versions = () => {
|
||||||
let is_direnv_new_enough = is_version_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) {
|
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")
|
||||||
|
|
|
||||||
70
lib/lib.re
70
lib/lib.re
|
|
@ -1,3 +1,73 @@
|
||||||
|
open Core;
|
||||||
|
module Unix = Core_unix;
|
||||||
|
|
||||||
module Util = Flake_env__util;
|
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) => {
|
||||||
|
In_channel.read_all(profile_rc) |> Printf.printf("%s")
|
||||||
|
};
|
||||||
|
|
||||||
|
let clean_old_gcroots = (layout_dir) => {
|
||||||
|
Util.rmrf(layout_dir ++ "/flake-inputs/");
|
||||||
|
Util.rmrf(layout_dir);
|
||||||
|
Unix.mkdir_p(layout_dir ++ "/flake-inputs/");
|
||||||
|
};
|
||||||
|
|
||||||
|
let add_gcroot = (store_path, symlink) => {
|
||||||
|
switch (Util.nix(["build", "--out-link", symlink, store_path])) {
|
||||||
|
| (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 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);
|
||||||
|
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)) {
|
||||||
|
| (Ok(_), `Yes) => Ok()
|
||||||
|
| (Ok(_), _) => {
|
||||||
|
Unix.mkdir_p(layout_directory);
|
||||||
|
Ok()
|
||||||
|
}
|
||||||
|
| (err, _) => err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
7
tests/dune
Normal file
7
tests/dune
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
(tests
|
||||||
|
(names
|
||||||
|
flake_env_test_versions
|
||||||
|
flake_env_test_watches
|
||||||
|
flake_env_test_util)
|
||||||
|
(instrumentation (backend bisect_ppx))
|
||||||
|
(libraries lib alcotest))
|
||||||
39
tests/flake_env_test_util.re
Normal file
39
tests/flake_env_test_util.re
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
open Core;
|
||||||
|
module Unix = Core_unix;
|
||||||
|
|
||||||
|
open Lib.Util;
|
||||||
|
|
||||||
|
let pp_exit_or_signal = (pp_fmt) => (e) => Fmt.pf(pp_fmt, "%s", Unix.Exit_or_signal.to_string_hum(e));
|
||||||
|
let exit_or_signal_eq = (a, b) => Unix.Exit_or_signal.compare(a, b) == 0;
|
||||||
|
let testable_exit_or_signal = Alcotest.testable(pp_exit_or_signal, exit_or_signal_eq);
|
||||||
|
|
||||||
|
let test_run_process_success = () =>
|
||||||
|
Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)))(
|
||||||
|
"Returns expected",
|
||||||
|
(Ok(), ""),
|
||||||
|
run_process("true", []));
|
||||||
|
|
||||||
|
let test_run_process_failure = () =>
|
||||||
|
Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)))(
|
||||||
|
"Returns expected",
|
||||||
|
(Error(`Exit_non_zero(1)), ""),
|
||||||
|
run_process("false", []));
|
||||||
|
|
||||||
|
let test_run_process_stdout = () =>
|
||||||
|
Alcotest.(check(Alcotest.pair(testable_exit_or_signal, string)))(
|
||||||
|
"Returns expected",
|
||||||
|
(Ok(), "echoed\n"),
|
||||||
|
run_process("echo", ["echoed"]));
|
||||||
|
|
||||||
|
let () =
|
||||||
|
Alcotest.(
|
||||||
|
run(
|
||||||
|
"Watches",
|
||||||
|
[("run_process",
|
||||||
|
[
|
||||||
|
test_case("Capture's Stdout", `Quick, test_run_process_stdout),
|
||||||
|
test_case("Success", `Quick, test_run_process_success),
|
||||||
|
test_case("Failure", `Quick, test_run_process_failure),
|
||||||
|
]),
|
||||||
|
]),
|
||||||
|
);
|
||||||
118
tests/flake_env_test_versions.re
Normal file
118
tests/flake_env_test_versions.re
Normal file
|
|
@ -0,0 +1,118 @@
|
||||||
|
open Lib;
|
||||||
|
|
||||||
|
let test_compare_equal = () => {
|
||||||
|
let a = Versions.init(1,0,0);
|
||||||
|
Alcotest.(check(int))("equal", 0, Versions.compare(a, a))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_compare_first_major_greater = () => {
|
||||||
|
let a = Versions.init(2,0,0);
|
||||||
|
let b = Versions.init(1, 0, 0);
|
||||||
|
Alcotest.(check(int))("First major greater", 1, Versions.compare(a, b))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_compare_first_minor_greater = () => {
|
||||||
|
let a = Versions.init(1,1,0);
|
||||||
|
let b = Versions.init(1, 0, 0);
|
||||||
|
Alcotest.(check(int))("First minor greater", 1, Versions.compare(a, b))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_compare_first_point_greater = () => {
|
||||||
|
let a = Versions.init(1, 0, 1);
|
||||||
|
let b = Versions.init(1,0,0);
|
||||||
|
Alcotest.(check(int))("First point greater", 1, Versions.compare(a, b))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_compare_second_major_greater = () => {
|
||||||
|
let a = Versions.init(1,0,0);
|
||||||
|
let b = Versions.init(2, 0, 0);
|
||||||
|
Alcotest.(check(int))("Second major greater", -1, Versions.compare(a, b))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_compare_second_minor_greater = () => {
|
||||||
|
let a = Versions.init(1,0,0);
|
||||||
|
let b = Versions.init(1, 1, 0);
|
||||||
|
Alcotest.(check(int))("Second minor greater", -1, Versions.compare(a, b))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_compare_second_point_greater = () => {
|
||||||
|
let a = Versions.init(1,0,0);
|
||||||
|
let b = Versions.init(1, 0, 1);
|
||||||
|
Alcotest.(check(int))("Second point greater", -1, Versions.compare(a, b))
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_ine_cur_newer = () => {
|
||||||
|
let a = Ok(Versions.init(2,0,0));
|
||||||
|
let b = Versions.init(1, 0, 0);
|
||||||
|
let ine = Versions.is_new_enough(a, b);
|
||||||
|
Alcotest.(check(bool))("Curr newer", true, ine |> Result.get_ok)
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_ine_cur_older = () => {
|
||||||
|
let a = Ok(Versions.init(1,0,0));
|
||||||
|
let b = Versions.init(2, 0, 0);
|
||||||
|
let ine = Versions.is_new_enough(a, b);
|
||||||
|
Alcotest.(check(bool))("Curr older", false, ine |> Result.get_ok)
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_ine_cur_equal = () => {
|
||||||
|
let a = Versions.init(1,0,0);
|
||||||
|
let ine = Versions.is_new_enough(Ok(a), a);
|
||||||
|
Alcotest.(check(bool))("Curr equal", true, ine |> Result.get_ok)
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: figure out typing of `Alcotest.check` here.
|
||||||
|
// I think we just add a `pprint` and `equal` function, but that is verbose
|
||||||
|
// let test_ine_error = () => {
|
||||||
|
// let a = Error("foobarbaz");
|
||||||
|
// let ine = Versions.is_new_enough(a, Versions.init(1, 0, 0));
|
||||||
|
// Alcotest.(check())("Error bubbled", Error("foobarbaz"), ine)
|
||||||
|
// };
|
||||||
|
|
||||||
|
let test_in_direnv_true = () => {
|
||||||
|
Core_unix.putenv(~key="direnv", ~data="direnv");
|
||||||
|
Alcotest.(check(bool))("In direnv", true, Versions.in_direnv())
|
||||||
|
};
|
||||||
|
|
||||||
|
let test_in_direnv_false = () => {
|
||||||
|
Core_unix.unsetenv("direnv");
|
||||||
|
Alcotest.(check(bool))("Not in direnv", false, Versions.in_direnv())
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Test:
|
||||||
|
// * extract_version_number: impure, don't know how to get a concrete version number to test against
|
||||||
|
// * preflight_versions? impure, but m
|
||||||
|
let () =
|
||||||
|
Alcotest.(
|
||||||
|
run(
|
||||||
|
"Versions",
|
||||||
|
[
|
||||||
|
(
|
||||||
|
"compare",
|
||||||
|
[
|
||||||
|
test_case("Versions Equal", `Quick, test_compare_equal),
|
||||||
|
test_case("First Major Greater", `Quick, test_compare_first_major_greater),
|
||||||
|
test_case("First Minor Greater", `Quick, test_compare_first_minor_greater),
|
||||||
|
test_case("First Point Greater", `Quick, test_compare_first_point_greater),
|
||||||
|
test_case("Second Major Greater", `Quick, test_compare_second_major_greater),
|
||||||
|
test_case("Second Minor Greater", `Quick, test_compare_second_minor_greater),
|
||||||
|
test_case("Second Point Greater", `Quick, test_compare_second_point_greater),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"is_new_enough",
|
||||||
|
[
|
||||||
|
test_case("Curr Newer", `Quick, test_ine_cur_newer),
|
||||||
|
test_case("Curr Older", `Quick, test_ine_cur_older),
|
||||||
|
test_case("Curr Equal", `Quick, test_ine_cur_equal),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"in_direnv",
|
||||||
|
[
|
||||||
|
test_case("true", `Quick, test_in_direnv_true),
|
||||||
|
test_case("false", `Quick, test_in_direnv_false),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
])
|
||||||
|
);
|
||||||
41
tests/flake_env_test_watches.re
Normal file
41
tests/flake_env_test_watches.re
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
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 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 () =
|
||||||
|
Alcotest.(
|
||||||
|
run(
|
||||||
|
"Watches",
|
||||||
|
[("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),
|
||||||
|
])
|
||||||
|
]),
|
||||||
|
);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue