2023-08-27 07:48:57 +02:00
|
|
|
# flakelight -- Framework for simplifying flake setup
|
2023-08-25 06:14:55 +02:00
|
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-08-27 07:48:57 +02:00
|
|
|
{ config, src, lib, flakelight, ... }:
|
2023-08-25 06:14:55 +02:00
|
|
|
let
|
2023-11-24 01:40:06 +01:00
|
|
|
inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList;
|
|
|
|
inherit (lib.types) lazyAttrsOf nullOr package str;
|
2023-08-27 07:48:57 +02:00
|
|
|
inherit (flakelight.types) optFunctionTo;
|
2023-08-25 06:14:55 +02:00
|
|
|
in
|
|
|
|
{
|
2023-11-24 01:40:06 +01:00
|
|
|
options = {
|
|
|
|
formatter = mkOption {
|
|
|
|
type = nullOr (optFunctionTo package);
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
formatters = mkOption {
|
|
|
|
type = nullOr (optFunctionTo (lazyAttrsOf str));
|
|
|
|
default = null;
|
|
|
|
};
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
|
2023-11-24 01:40:06 +01:00
|
|
|
config = mkMerge [
|
|
|
|
(mkIf (config.formatter != null) {
|
|
|
|
perSystem = pkgs: {
|
|
|
|
formatter = config.formatter pkgs;
|
|
|
|
};
|
|
|
|
})
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2023-11-24 01:40:06 +01:00
|
|
|
(mkIf (config.formatters != null) {
|
|
|
|
perSystem = { pkgs, lib, fd, coreutils, ... }: {
|
|
|
|
formatter = mkDefault (pkgs.writeShellScriptBin "formatter" ''
|
2023-12-05 11:06:23 +01:00
|
|
|
PATH=${lib.makeBinPath ((config.devShell.packages or (_: [ ])) pkgs)}
|
2023-11-24 01:40:06 +01:00
|
|
|
for f in "$@"; do
|
|
|
|
if [ -d "$f" ]; then
|
|
|
|
${fd}/bin/fd "$f" -Htf -x "$0" &
|
|
|
|
else
|
|
|
|
case "$(${coreutils}/bin/basename "$f")" in
|
|
|
|
${toString (mapAttrsToList
|
|
|
|
(n: v: "${n}) ${v} \"$f\" & ;;") (config.formatters pkgs))}
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
done &>/dev/null
|
|
|
|
wait
|
|
|
|
'');
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf ((config.formatters != null) || (config.formatter != null)) {
|
|
|
|
checks.formatting = { lib, outputs', diffutils, ... }: ''
|
|
|
|
${lib.getExe outputs'.formatter} .
|
|
|
|
${diffutils}/bin/diff -qr ${src} . |\
|
|
|
|
sed 's/Files .* and \(.*\) differ/File \1 not formatted/g'
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
];
|
2023-08-25 06:14:55 +02:00
|
|
|
}
|