From 7e9906679d384472849272e5a5eef7adbdb1d87f Mon Sep 17 00:00:00 2001 From: awwpotato <153149335+awwpotato@users.noreply.github.com> Date: Thu, 20 Mar 2025 11:56:23 -0700 Subject: [PATCH] stylix: check whether maintainers list is sorted (#1014) Link: https://github.com/danth/stylix/pull/1014 Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com> --- flake.nix | 2 + stylix/check-maintainers-sorted.nix | 87 +++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 stylix/check-maintainers-sorted.nix diff --git a/flake.nix b/flake.nix index fd6ee111..d19a2f12 100644 --- a/flake.nix +++ b/flake.nix @@ -148,6 +148,8 @@ src = ./.; }; + + maintainers-sorted = (import ./stylix/check-maintainers-sorted.nix) pkgs; } self.packages.${system}; devShells = { diff --git a/stylix/check-maintainers-sorted.nix b/stylix/check-maintainers-sorted.nix new file mode 100644 index 00000000..cf990291 --- /dev/null +++ b/stylix/check-maintainers-sorted.nix @@ -0,0 +1,87 @@ +# slight modification from nixpkgs's script: https://github.com/NixOS/nixpkgs/commit/4a694fc50007076566a204d6ea623fd5fc7ddbfa +pkgs: +let + inherit (pkgs.lib) + add + attrNames + attrValues + elemAt + foldl' + genList + length + replaceStrings + sort + toLower + trace + ; + + maintainers = import ./maintainers.nix; + simplify = replaceStrings [ "-" "_" ] [ "" "" ]; + namesSorted = sort (a: b: a.key < b.key) ( + map ( + n: + let + pos = builtins.unsafeGetAttrPos n maintainers; + in + assert pos == null -> throw "maintainers entry ${n} is malformed"; + { + name = n; + inherit (pos) line; + key = toLower (simplify n); + } + ) (attrNames maintainers) + ); + before = + { + key, + ... + }: + foldl' ( + acc: n: if n.key < key && (acc == null || n.key > acc.key) then n else acc + ) null namesSorted; + errors = foldl' add 0 ( + map ( + i: + let + a = elemAt namesSorted i; + b = elemAt namesSorted (i + 1); + lim = + let + t = before a; + in + if t == null then "the initial {" else t.name; + in + if a.line >= b.line then + trace ( + "maintainer ${a.name} (line ${toString a.line}) should be listed " + + "after ${lim}, not after ${b.name} (line ${toString b.line})" + ) 1 + else + 0 + ) (genList (i: i) (length namesSorted - 1)) + ); +in +assert length (attrValues maintainers) < 1 || errors == 0; +pkgs.runCommandLocal "maintainers-sorted" { } "mkdir $out" + +# generate edit commands to sort the list. +# may everything following the last current entry (closing } ff) in the wrong place +# with lib; +# concatStringsSep +# "\n" +# (let first = foldl' (acc: n: if n.line < acc then n.line else acc) 999999999 namesSorted; +# commands = map +# (i: let e = elemAt namesSorted i; +# begin = foldl' +# (acc: n: if n.line < e.line && n.line > acc then n.line else acc) +# 1 +# namesSorted; +# end = +# foldl' (acc: n: if n.line > e.line && n.line < acc then n.line else acc) +# 999999999 +# namesSorted; +# in "${toString e.line},${toString (end - 1)} p") +# (genList (i: i) (length namesSorted)); +# in map +# (c: "sed -ne '${c}' maintainers/maintainer-list.nix") +# ([ "1,${toString (first - 1)} p" ] ++ commands))