Skip to content

Commit

Permalink
feat(hook order): add before, after options to specify hooks order
Browse files Browse the repository at this point in the history
  • Loading branch information
sinrohit committed Dec 14, 2024
1 parent ac756a3 commit 06d077b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
30 changes: 20 additions & 10 deletions modules/hook.nix
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,6 @@ in
default = true;
};

priority = mkOption {
type = types.number;
description = ''
Defines the priority order in which hooks are executed.
Lower the number, the higher the precedence.
'';
default = 0;
};

fail_fast = mkOption {
type = types.bool;
description = ''
Expand Down Expand Up @@ -193,12 +184,31 @@ in
'';
default = [ ];
};

before = mkOption {
type = types.listOf types.str;
description =
''
List of hooks that should run after this hook.
'';
default = [ ];
};

after = mkOption {
type = types.listOf types.str;
description =
''
List of hooks that should run before this hook.
'';
default = [ ];
};

};

config = {
raw =
{
inherit (config) name entry language files types types_or exclude_types pass_filenames priority fail_fast require_serial stages verbose always_run args;
inherit (config) name entry language files types types_or exclude_types pass_filenames fail_fast require_serial stages verbose always_run args before after;
id = config.name;
exclude = mergeExcludes config.excludes;
};
Expand Down
10 changes: 5 additions & 5 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ let
enabledExtraPackages = builtins.concatLists (mapAttrsToList (_: value: value.extraPackages) enabledHooks);
processedHooks =
let
# Sort the list of hooks by priority (lower number = higher priority)
sortedHooks = builtins.sort
(a: b: a.priority < b.priority)
sortedHooks = lib.toposort
(a: b: builtins.elem b.id a.before || builtins.elem a.id b.after)
(mapAttrsToList
(id: value:
value.raw // {
inherit id;
priority = value.raw.priority;
before = value.raw.before;
after = value.raw.after;
}
)
enabledHooks
);
in
sortedHooks;
sortedHooks.result;

configFile =
performAssertions (
Expand Down

0 comments on commit 06d077b

Please sign in to comment.