1
1
forked from extern/flakelight

Allow overriding formatter entirely

Setting the `formatter` option now allows for setting the formatter
directly instead of using the provided formatting functionality with
`formatters`.
This commit is contained in:
Archit Gupta 2023-11-23 16:40:06 -08:00
parent 50982daa63
commit cca0b23070
3 changed files with 52 additions and 30 deletions

View File

@ -577,6 +577,11 @@ For example:
### formatters
The `formatter` option allows you to set `formatter.${system}` outputs. It can
be set to a function that takes packages and returns the package to use. This
overrides the `formatters` functionality described below though, so for
configuring formatters for a file type, you likely want to use `formatters`.
The `formatters` option allows you to configure formatting tools that will be
used by `nix fmt`. If formatters are set, Flakelight will export
`formatter.${system}` outputs which apply all the configured formatters.

View File

@ -8,7 +8,8 @@ let
in
{
options.flakelight.builtinFormatters =
mkEnableOption "default formatters" // { default = true; };
mkEnableOption "default formatters" //
{ default = config.formatter == null; };
config = mkIf config.flakelight.builtinFormatters {
devShell.packages = pkgs: [

View File

@ -4,38 +4,54 @@
{ config, src, lib, flakelight, ... }:
let
inherit (lib) mkOption mkIf mapAttrsToList;
inherit (lib.types) lazyAttrsOf nullOr str;
inherit (lib) mkDefault mkMerge mkOption mkIf mapAttrsToList;
inherit (lib.types) lazyAttrsOf nullOr package str;
inherit (flakelight.types) optFunctionTo;
in
{
options.formatters = mkOption {
type = nullOr (optFunctionTo (lazyAttrsOf str));
default = null;
};
config = mkIf (config.formatters != null) {
perSystem = { pkgs, lib, fd, coreutils, ... }: {
formatter = pkgs.writeShellScriptBin "formatter" ''
PATH=${lib.makeBinPath (config.devShell.packages 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
'';
options = {
formatter = mkOption {
type = nullOr (optFunctionTo package);
default = null;
};
formatters = mkOption {
type = nullOr (optFunctionTo (lazyAttrsOf str));
default = 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'
'';
};
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 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'
'';
})
];
}