Continue moving heavy-lifting into library

This commit is contained in:
Bryan Bennett 2024-01-08 14:57:59 -05:00
parent 21c8ff71e5
commit 7759d2ac6a
No known key found for this signature in database
GPG key ID: EE149E4215408DE9
7 changed files with 194 additions and 176 deletions

View file

@ -1,130 +1,36 @@
open Core;
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 argv = Sys.get_argv();
switch (Array.length(argv)) {
| 3 => {
let flake_specifier = argv[1];
let layout_directory = argv[2];
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 = hash_files(paths);
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))
switch (all_older) {
| true => print_cur_cache(profile_rc)
| false => freshen_cache(layout_directory, hash, flake_specifier)
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)
| _ => freshen_cache(layout_directory, hash, flake_specifier, other_args)
}
};
| Error(e) => {
@ -139,11 +45,11 @@ let main = () => {
}
};
}
| _ => {
Printf.eprintf("%s <flake specifier> <layout_directory>\n", argv[0]);
| Error() => {
Printf.eprintf("%s <layout_directory> <flake specifier> <...args>\n", argv[0]);
exit(1);
}
}
}
};
let () = main();

View file

@ -41,7 +41,7 @@ use_flake_env() {
local ld=$(direnv_layout_dir)
export direnv
eval "$(@flake_env@ "$1" "$ld")"
eval "$(@flake_env@ "$ld" "$@")"
export -n direnv

View file

@ -1,18 +1,11 @@
(lang dune 3.11)
(lang dune 3.12)
(name flake_env)
(generate_opam_files true)
(source
(sourcehut bryan_bennett/flake_env))
(source (sourcehut bryan_bennett/flake_env))
(authors "Bryan Bennett")
(maintainers "Bryan Bennett")
(license MIT)
(documentation https://git.sr.ht/~bryan_bennett/flake_env)
(package

View file

@ -10,7 +10,7 @@ doc: "https://git.sr.ht/~bryan_bennett/flake_env"
bug-reports: "https://todo.sr.ht/~bryan_bennett/flake_env"
depends: [
"ocaml"
"dune" {>= "3.11"}
"dune" {>= "3.12"}
"odoc" {with-doc}
]
build: [

View file

@ -1,10 +1,60 @@
open Core;
module Unix = Core_unix;
let nix = (args) => {
let stdout_chan = Unix.open_process_in(
"nix --extra-experimental-features \"nix-command flakes\" " ++ (args |> String.concat(~sep=" ")));
let run_process = (name, args) => {
/*** Run a process [name] with args [args], returning (exit_code, stdout text) */
let stdout_chan =
Unix.open_process_in(name ++ " " ++ (args |> String.concat(~sep=" ")));
let stdout_content = stdout_chan |> In_channel.input_all;
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 | 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")
};
};
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()
};
};

View file

@ -1,28 +1,26 @@
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 required_direnv_version = {
major: 2,
minor: 21,
point: 3
};
let required_nix_version = {
major: 2,
minor: 10,
point: 0
};
// TODO: test this
let compare_version_number = (a, b) => {
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
@ -32,46 +30,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 full_cmd = cmd ++ " --version";
switch (Core_unix.open_process_in(full_cmd) |> In_channel.input_line) {
| Some(stdout) => {
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))
}
| 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) {
| Ok(cur) => {
switch (compare_version_number(cur, needed)) {
switch (compare(cur, needed)) {
| x when x < 0 => Ok(false)
| _ => Ok(true)
}
}
| Error(e) => Error(e)
}
}
};
let preflight_versions = () => {
let in_direnv = switch (Sys.getenv("direnv")) {
let in_direnv = () => switch (Sys.getenv("direnv")) {
| Some(_) => true
| None => false
};
let is_nix_new_enough = is_version_new_enough(extract_version_number("nix"), required_nix_version);
let is_direnv_new_enough = is_version_new_enough(extract_version_number("direnv"), required_direnv_version);
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) {
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")

View file

@ -1,3 +1,73 @@
open Core;
module Unix = Core_unix;
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 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
}
}