mirror of
https://git.sr.ht/~bryan_bennett/flake_env
synced 2025-12-19 16:01:25 +01:00
WIP
This commit is contained in:
parent
31cac9373c
commit
5e505b178b
13 changed files with 486 additions and 165 deletions
81
lib/#flake_env__versions.re#
Normal file
81
lib/#flake_env__versions.re#
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
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 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
|
||||
| (a, b) when a.major == b.major && a.minor < b.minor => -1
|
||||
| (a, b) when a.major == b.major && a.minor == b.minor && a.point < b.point => -1
|
||||
| _ => 1
|
||||
}
|
||||
}
|
||||
|
||||
let extract_version_number = (cmd) => {
|
||||
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))
|
||||
}
|
||||
}
|
||||
| _ => Error(Printf.sprintf("Failed executing '%s'\n", cmd))
|
||||
}
|
||||
};
|
||||
|
||||
let is_new_enough = (cur, needed) => {
|
||||
switch (cur) {
|
||||
| Ok(cur) => {
|
||||
switch (compare(cur, needed)) {
|
||||
| x when x < 0 => Ok(false)
|
||||
| _ => Ok(true)
|
||||
}
|
||||
}
|
||||
| Error(e) => Error(e)
|
||||
}
|
||||
};
|
||||
|
||||
let in_direnv = () => switch (Sys.getenv("direnv")) {
|
||||
| Some(_) => true
|
||||
| None => false
|
||||
};
|
||||
|
||||
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) {
|
||||
| (false, _, _) => Error("Not in direnv!")
|
||||
| (_, Ok(false), _) => Error("Direnv version is not new enough")
|
||||
| (_, _, Ok(false)) => Error("Nix version is not new enough")
|
||||
| (_, Error(e), _) => Error(e)
|
||||
| (_, _, Error(e)) => Error(e)
|
||||
| (true, Ok(true), Ok(true)) => Ok()
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue