mirror of
https://git.sr.ht/~bryan_bennett/flake_env
synced 2025-12-16 22:51:24 +01:00
Core doubles the closure size and adds 8mb to the binary size for dubious benefit. This adds FileUtils to do the file interaction bits that aren't in the stdlib and removes Core in preference to the bundled Stdlib. Tests are passing, but I want to investigate the nix build before I commit to this approach
86 lines
2.2 KiB
ReasonML
86 lines
2.2 KiB
ReasonML
// TODO: Make this more robust to failure by returning stderr content too
|
|
/*** Run a process [name] with args [args], returning (exit_code, stdout text) */
|
|
let run_process = (name, args) => {
|
|
let stdout_chan =
|
|
Unix.open_process_in(name ++ " " ++ (args |> String.concat(" ")));
|
|
let stdout_content = stdout_chan |> In_channel.input_all;
|
|
let exit_code = Unix.close_process_in(stdout_chan);
|
|
(exit_code, stdout_content);
|
|
};
|
|
|
|
/*** Run nix with some fixed arguments, appending a user provided set of arguments. */
|
|
let nix = args =>
|
|
run_process(
|
|
"nix",
|
|
["--extra-experimental-features", "\"nix-command flakes\" ", ...args],
|
|
);
|
|
|
|
let is_file = pth => {
|
|
switch (FileUtil.stat(pth)) {
|
|
| exception (FileUtil.FileDoesntExist(_)) => false
|
|
| stat =>
|
|
switch (stat.kind) {
|
|
| File => true
|
|
| _ => false
|
|
}
|
|
};
|
|
};
|
|
|
|
let is_directory = pth => {
|
|
switch (FileUtil.stat(pth)) {
|
|
| exception (FileUtil.FileDoesntExist(_)) => false
|
|
| stat =>
|
|
switch (stat.kind) {
|
|
| Dir => true
|
|
| _ => false
|
|
}
|
|
};
|
|
};
|
|
|
|
let hash_files = filenames => {
|
|
/*** Hash all entries in [filenames] which represent existing files.
|
|
Returns Some(hex-string) or None if no filenames are found.
|
|
*/
|
|
let ctx = Sha1.init();
|
|
let files_to_hash =
|
|
filenames
|
|
|> Array.to_list
|
|
|> List.filter(f =>
|
|
Sys.file_exists(f)
|
|
? true
|
|
: {
|
|
Printf.eprintf(
|
|
"Cannot find file %s (cwd: %s)\n",
|
|
f,
|
|
Sys.getcwd(),
|
|
);
|
|
false;
|
|
}
|
|
);
|
|
|
|
switch (files_to_hash |> List.length) {
|
|
| 0 => Error("No files found to hash")
|
|
| _ =>
|
|
let () =
|
|
files_to_hash
|
|
|> List.iter(f => {
|
|
f
|
|
|> In_channel.open_bin
|
|
|> In_channel.input_all
|
|
|> Sha1.update_string(ctx)
|
|
});
|
|
Ok(Sha1.finalize(ctx) |> Sha1.to_hex);
|
|
};
|
|
};
|
|
|
|
let get_args = argv => {
|
|
let argc = Array.length(argv);
|
|
switch (argc) {
|
|
| x when x >= 3 =>
|
|
let layout_directory = argv[1];
|
|
let flake_specifier = argv[2];
|
|
let other_args = Array.sub(argv, 3, argc - 3) |> Array.to_list;
|
|
Ok((layout_directory, flake_specifier, other_args));
|
|
| _ => Error()
|
|
};
|
|
};
|