Optionally remove .pub suffix from file names

This commit is contained in:
Nicolas Jeannerod 2024-12-13 12:19:26 +01:00
parent 9407af8ac8
commit 377ad0ea6e
Signed by untrusted user: Niols
GPG key ID: 35DB9EC8886E1CB8

View file

@ -1,14 +1,30 @@
let let
inherit (builtins) inherit (builtins)
attrValues
elemAt elemAt
foldl'
mapAttrs mapAttrs
match match
readDir readDir
readFile readFile
; ;
## `mergeAttrs` and `concatMapAttrs` are in `lib.trivial` and `lib.attrsets`,
## but we would rather avoid a dependency in nixpkgs for this file.
mergeAttrs = x: y: x // y;
concatMapAttrs = f: v: foldl' mergeAttrs { } (attrValues (mapAttrs f v));
removePubSuffix =
s:
let
maybeMatch = match "(.*)\.pub" s;
in
if maybeMatch == null then s else elemAt maybeMatch 0;
removeTrailingWhitespace = s: elemAt (match "(.*[^[:space:]])[[:space:]]*" s) 0; removeTrailingWhitespace = s: elemAt (match "(.*[^[:space:]])[[:space:]]*" s) 0;
collectKeys = collectKeys =
dir: mapAttrs (name: _: removeTrailingWhitespace (readFile (dir + "/${name}"))) (readDir dir); dir:
concatMapAttrs (name: _: {
"${removePubSuffix name}" = removeTrailingWhitespace (readFile (dir + "/${name}"));
}) (readDir dir);
in in
{ {
contributors = collectKeys ./contributors; contributors = collectKeys ./contributors;