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 ### 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 The `formatters` option allows you to configure formatting tools that will be
used by `nix fmt`. If formatters are set, Flakelight will export used by `nix fmt`. If formatters are set, Flakelight will export
`formatter.${system}` outputs which apply all the configured formatters. `formatter.${system}` outputs which apply all the configured formatters.

View File

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

View File

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