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
|
|
|
|
|
2024-01-10 10:16:24 +01:00
|
|
|
{ config, src, lib, flakelight, genSystems, ... }:
|
2023-08-25 06:14:55 +02:00
|
|
|
let
|
2023-11-24 01:40:06 +01:00
|
|
|
inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList;
|
2024-02-07 09:54:03 +01:00
|
|
|
inherit (lib.types) functionTo lazyAttrsOf package str;
|
|
|
|
inherit (flakelight.types) nullable optFunctionTo;
|
2023-08-25 06:14:55 +02:00
|
|
|
in
|
|
|
|
{
|
2023-11-24 01:40:06 +01:00
|
|
|
options = {
|
|
|
|
formatter = mkOption {
|
2024-02-07 09:54:03 +01:00
|
|
|
type = nullable (functionTo package);
|
2023-11-24 01:40:06 +01:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
formatters = mkOption {
|
2024-02-07 09:54:03 +01:00
|
|
|
type = nullable (optFunctionTo (lazyAttrsOf str));
|
2023-11-24 01:40:06 +01:00
|
|
|
default = null;
|
|
|
|
};
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
|
2023-11-24 01:40:06 +01:00
|
|
|
config = mkMerge [
|
|
|
|
(mkIf (config.formatter != null) {
|
2024-01-10 10:16:24 +01:00
|
|
|
outputs.formatter = genSystems config.formatter;
|
2023-11-24 01:40:06 +01:00
|
|
|
})
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2023-11-24 01:40:06 +01:00
|
|
|
(mkIf (config.formatters != null) {
|
2024-01-10 10:16:24 +01:00
|
|
|
outputs.formatter = mkDefault (genSystems
|
|
|
|
({ pkgs, lib, fd, coreutils, ... }:
|
2024-02-19 11:03:21 +01:00
|
|
|
let
|
|
|
|
packages =
|
|
|
|
if config.devShell == null then [ ]
|
|
|
|
else (config.devShell pkgs).packages pkgs;
|
|
|
|
in
|
2024-01-10 10:16:24 +01:00
|
|
|
pkgs.writeShellScriptBin "formatter" ''
|
2024-02-19 11:03:21 +01:00
|
|
|
PATH=${lib.makeBinPath packages}
|
2024-01-10 10:16:24 +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
|
|
|
|
''));
|
2023-11-24 01:40:06 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
(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
|
|
|
}
|