Remove dependency on Janestreet Core

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
This commit is contained in:
Bryan Bennett 2024-08-12 12:13:53 -04:00
parent 3f5f7a602d
commit 5c01142933
No known key found for this signature in database
GPG key ID: EE149E4215408DE9
11 changed files with 184 additions and 232 deletions

View file

@ -1,10 +1,8 @@
open Core;
module Unix = Core_unix;
// 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(~sep=" ")));
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);
@ -17,6 +15,28 @@ let nix = args =>
["--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.
@ -24,27 +44,28 @@ let hash_files = filenames => {
let ctx = Sha1.init();
let files_to_hash =
filenames
|> Array.filter(~f=f =>
switch (Sys_unix.file_exists(f)) {
| `Yes => true
| _ =>
Printf.eprintf(
"Cannot find file %s (cwd: %s)\n",
f,
Core_unix.getcwd(),
);
false;
}
|> 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 |> Array.length) {
switch (files_to_hash |> List.length) {
| 0 => Error("No files found to hash")
| _ =>
let () =
files_to_hash
|> Array.iter(~f=f => {
|> List.iter(f => {
f
|> In_channel.create
|> In_channel.open_bin
|> In_channel.input_all
|> Sha1.update_string(ctx)
});
@ -52,29 +73,13 @@ let hash_files = filenames => {
};
};
let rec rmrf = path => {
/*** Remove [path] recursively */
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)) {
let argc = Array.length(argv);
switch (argc) {
| 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));
let other_args = Array.sub(argv, 3, argc - 3) |> Array.to_list;
Ok((layout_directory, flake_specifier, other_args));
| _ => Error()
};