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
|
2024-02-19 11:16:16 +01:00
|
|
|
inherit (builtins) all hasContext;
|
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
|
2024-02-19 11:16:16 +01:00
|
|
|
inherit (lib) attrValues makeBinPath;
|
|
|
|
formatters = config.formatters pkgs;
|
|
|
|
fullContext = all hasContext (attrValues formatters);
|
2024-02-19 11:03:21 +01:00
|
|
|
packages =
|
|
|
|
if config.devShell == null then [ ]
|
|
|
|
else (config.devShell pkgs).packages pkgs;
|
2024-02-19 11:16:16 +01:00
|
|
|
caseArms = toString (mapAttrsToList
|
|
|
|
(n: v: "\n ${n}) ${v} \"$f\" & ;;")
|
|
|
|
formatters);
|
2024-02-19 11:03:21 +01:00
|
|
|
in
|
2024-01-10 10:16:24 +01:00
|
|
|
pkgs.writeShellScriptBin "formatter" ''
|
2024-02-19 11:16:16 +01:00
|
|
|
PATH=${if fullContext then "" else 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
|
2024-02-19 11:16:16 +01:00
|
|
|
case "$(${coreutils}/bin/basename "$f")" in${caseArms}
|
2024-01-10 10:16:24 +01:00
|
|
|
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
|
|
|
}
|