mirror of
https://github.com/nix-community/dream2nix.git
synced 2024-12-22 14:01:41 +03:00
refactor(utils): vendor semver.nix from poetry2nix
This commit is contained in:
parent
41c56eb788
commit
c69488718b
@ -20,7 +20,7 @@
|
||||
writeScriptBin
|
||||
;
|
||||
|
||||
poetry2nixSemver = import "${config.externalSources.poetry2nix}/semver.nix" {
|
||||
poetry2nixSemver = import ./poetry2nix/semver.nix {
|
||||
inherit (config) lib;
|
||||
# copied from poetry2nix
|
||||
ireplace = idx: value: list: (
|
||||
|
1821
src/modules/utils/poetry2nix/LICENSE
Normal file
1821
src/modules/utils/poetry2nix/LICENSE
Normal file
File diff suppressed because one or more lines are too long
87
src/modules/utils/poetry2nix/semver.nix
Normal file
87
src/modules/utils/poetry2nix/semver.nix
Normal file
@ -0,0 +1,87 @@
|
||||
{ lib, ireplace }:
|
||||
let
|
||||
inherit (builtins) elemAt match;
|
||||
operators =
|
||||
let
|
||||
matchWildCard = s: match "([^*])(\\.[*])" s;
|
||||
mkComparison = ret: version: v: builtins.compareVersions version v == ret;
|
||||
mkIdxComparison = idx: version: v:
|
||||
let
|
||||
ver = builtins.splitVersion v;
|
||||
minor = builtins.toString (lib.toInt (elemAt ver idx) + 1);
|
||||
upper = builtins.concatStringsSep "." (ireplace idx minor ver);
|
||||
in
|
||||
operators.">=" version v && operators."<" version upper;
|
||||
dropWildcardPrecision = f: version: constraint:
|
||||
let
|
||||
m = matchWildCard constraint;
|
||||
hasWildcard = m != null;
|
||||
c = if hasWildcard then (elemAt m 0) else constraint;
|
||||
v =
|
||||
if hasWildcard then (builtins.substring 0 (builtins.stringLength c) version)
|
||||
else version;
|
||||
in
|
||||
f v c;
|
||||
in
|
||||
{
|
||||
# Prefix operators
|
||||
"==" = dropWildcardPrecision (mkComparison 0);
|
||||
">" = dropWildcardPrecision (mkComparison 1);
|
||||
"<" = dropWildcardPrecision (mkComparison (-1));
|
||||
"!=" = v: c: ! operators."==" v c;
|
||||
">=" = v: c: operators."==" v c || operators.">" v c;
|
||||
"<=" = v: c: operators."==" v c || operators."<" v c;
|
||||
# Semver specific operators
|
||||
"~" = mkIdxComparison 1;
|
||||
"^" = mkIdxComparison 0;
|
||||
"~=" = v: c:
|
||||
let
|
||||
# Prune constraint
|
||||
parts = builtins.splitVersion c;
|
||||
pruned = lib.take ((builtins.length parts) - 1) parts;
|
||||
upper = builtins.toString (
|
||||
(lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1
|
||||
);
|
||||
upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned);
|
||||
in
|
||||
operators.">=" v c && operators."<" v upperConstraint;
|
||||
# Infix operators
|
||||
"-" = version: v: operators.">=" version v.vl && operators."<=" version v.vu;
|
||||
# Arbitrary equality clause, just run simple comparison
|
||||
"===" = v: c: v == c;
|
||||
#
|
||||
};
|
||||
re = {
|
||||
operators = "([=><!~^]+)";
|
||||
version = "([0-9.*x]+)";
|
||||
};
|
||||
parseConstraint = constraint:
|
||||
let
|
||||
constraintStr = builtins.replaceStrings [ " " ] [ "" ] constraint;
|
||||
# The common prefix operators
|
||||
mPre = match "${re.operators} *${re.version}" constraintStr;
|
||||
# There is also an infix operator to match ranges
|
||||
mIn = match "${re.version} *(-) *${re.version}" constraintStr;
|
||||
in
|
||||
(
|
||||
if mPre != null then {
|
||||
op = elemAt mPre 0;
|
||||
v = elemAt mPre 1;
|
||||
}
|
||||
# Infix operators are range matches
|
||||
else if mIn != null then {
|
||||
op = elemAt mIn 1;
|
||||
v = {
|
||||
vl = (elemAt mIn 0);
|
||||
vu = (elemAt mIn 2);
|
||||
};
|
||||
}
|
||||
else throw "Constraint \"${constraintStr}\" could not be parsed"
|
||||
);
|
||||
satisfiesSemver = version: constraint:
|
||||
let
|
||||
inherit (parseConstraint constraint) op v;
|
||||
in
|
||||
if constraint == "*" then true else operators."${op}" version v;
|
||||
in
|
||||
{ inherit satisfiesSemver; }
|
@ -5,6 +5,7 @@ excludes = [
|
||||
"overrides/nodejs/electron/*",
|
||||
"src/templates/translators/*",
|
||||
"src/modules/_template/*",
|
||||
"src/modules/utils/poetry2nix/semver.nix",
|
||||
]
|
||||
|
||||
[formatter.py]
|
||||
|
Loading…
Reference in New Issue
Block a user