mirror of
https://git.sr.ht/~bryan_bennett/flake_env
synced 2025-12-16 14:41:24 +01:00
80 lines
2.1 KiB
ReasonML
80 lines
2.1 KiB
ReasonML
open Core;
|
|
module Unix = Core_unix;
|
|
|
|
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);
|
|
};
|
|
|
|
let nix = args =>
|
|
run_process(
|
|
"nix",
|
|
["--extra-experimental-features", "\"nix-command flakes\" ", ...args],
|
|
);
|
|
|
|
let hash_files = filenames => {
|
|
/*** Hash all entries in [filenames]
|
|
Returns Some(hex-string) or None if no filenames are found.
|
|
*/
|
|
let ctx = Sha1.init();
|
|
let files_to_hash =
|
|
filenames
|
|
|> Array.filter(~f=f =>
|
|
switch (Sys_unix.file_exists(f)) {
|
|
| `Yes => true
|
|
| _ =>
|
|
// let fullpth = Filename_unix.realpath(f);
|
|
Printf.eprintf(
|
|
"Cannot find file %s (cwd: %s)\n",
|
|
f,
|
|
Core_unix.getcwd(),
|
|
);
|
|
false;
|
|
}
|
|
);
|
|
|
|
switch (files_to_hash |> Array.length) {
|
|
| 0 => Error("No files found to hash")
|
|
| _ =>
|
|
let () =
|
|
files_to_hash
|
|
|> Array.iter(~f=f => {
|
|
f
|
|
|> In_channel.create
|
|
|> In_channel.input_all
|
|
|> Sha1.update_string(ctx)
|
|
});
|
|
Ok(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()
|
|
};
|
|
};
|