mirror of
https://github.com/nix-community/flakelight.git
synced 2024-11-22 15:33:10 +01:00
27f9ac981c
The formatter uses `devShell.packages` for its path which is not available when devShell is null. A default value of empty list should be used when devShell is null.
58 lines
1.7 KiB
Nix
58 lines
1.7 KiB
Nix
# flakelight -- Framework for simplifying flake setup
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
{ config, src, lib, flakelight, ... }:
|
|
let
|
|
inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList;
|
|
inherit (lib.types) lazyAttrsOf nullOr package str;
|
|
inherit (flakelight.types) optFunctionTo;
|
|
in
|
|
{
|
|
options = {
|
|
formatter = mkOption {
|
|
type = nullOr (optFunctionTo package);
|
|
default = null;
|
|
};
|
|
formatters = mkOption {
|
|
type = nullOr (optFunctionTo (lazyAttrsOf str));
|
|
default = null;
|
|
};
|
|
};
|
|
|
|
config = mkMerge [
|
|
(mkIf (config.formatter != null) {
|
|
perSystem = pkgs: {
|
|
formatter = config.formatter pkgs;
|
|
};
|
|
})
|
|
|
|
(mkIf (config.formatters != null) {
|
|
perSystem = { pkgs, lib, fd, coreutils, ... }: {
|
|
formatter = mkDefault (pkgs.writeShellScriptBin "formatter" ''
|
|
PATH=${lib.makeBinPath ((config.devShell.packages or (_: [ ])) pkgs)}
|
|
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'
|
|
'';
|
|
})
|
|
];
|
|
}
|