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,149 +1,55 @@
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 profile = layout_directory ++ "/flake-profile-" ++ hash;
let profile_rc = profile ++ ".rc";
| 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))
switch (all_older) {
| true => print_cur_cache(profile_rc)
| false => freshen_cache(layout_directory, hash, flake_specifier)
}
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)
}
};
| 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);
exit(1);
}
}
}
| Error(e) => {
Printf.eprintf("%s\n", e);
exit(1);
}
};
}
| _ => {
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();