@@ -30,13 +30,14 @@ import Bootstrap.Data.ProjectType
3030 )
3131import Bootstrap.Nix.Expr
3232 ( Binding (BNameValue ),
33- Expr (EGrouping , EIdent , EList , ELit , ESet , EWith ),
33+ Expr (EGrouping , EIdent , ELetIn , EList , ELit , ESet , EWith ),
3434 FunctionArgs (FASet ),
3535 Identifier (Identifier ),
3636 IsNixExpr (toNixExpr ),
3737 Literal (LBool , LString ),
3838 Property (PCons , PIdent ),
3939 nix ,
40+ nixargs ,
4041 nixbinding ,
4142 nixident ,
4243 nixproperty ,
@@ -45,6 +46,8 @@ import Bootstrap.Nix.Expr
4546 (|:) ,
4647 (|=) ,
4748 )
49+ import Control.Lens (Field2 (_2 ), filtered , folded , (^..) )
50+ import Relude.Extra.Tuple (dup )
4851
4952data NixPreCommitHookConfig = NixPreCommitHookConfig
5053 { nixPreCommitHookConfigHooks :: [PreCommitHook ],
@@ -64,25 +67,53 @@ instance IsNixExpr NixPreCommitHookConfig where
6467 :| [[nixident |nixpkgs|] | nixPreCommitHookConfigRequiresNixpkgs]
6568 <> [[nixident |system|] | nixPreCommitHookConfigUsingFlakeLib]
6669 )
67- |: ESet
68- False
69- [ [nixproperty |hooks|] |= run |* ESet False runArgs,
70- [nixproperty |tools|]
71- |= if nixPreCommitHookConfigRequiresNixpkgs
72- then defaultToolsExpr |++ nixpkgsToolsExpr
73- else defaultToolsExpr
74- ]
70+ |: ELetIn
71+ ( [nixbinding |# Function to make a set of pre-commit hooks|]
72+ :| [ [nixproperty |makeHooks|]
73+ |= ( [nixargs |hooks:|]
74+ |: ( run
75+ |* [nix |{
76+ inherit hooks;
77+ src = ../.;
78+ }|]
79+ )
80+ ),
81+ [nixbinding |# Hooks which don't depend on running in a dev environment|],
82+ [nixproperty |pureHooks|] |= ESet False pureHooks,
83+ [nixbinding |# Hooks which can run on pre-commit but not in CI|],
84+ [nixproperty |impureHooks|] |= ESet False impureHooks
85+ ]
86+ )
87+ ( ESet
88+ False
89+ [ [nixbinding |pureHooks = makeHooks pureHooks;|],
90+ [nixbinding |allHooks = makeHooks (pureHooks // impureHooks);|],
91+ [nixproperty |tools|]
92+ |= if nixPreCommitHookConfigRequiresNixpkgs
93+ then defaultToolsExpr |++ nixpkgsToolsExpr
94+ else defaultToolsExpr
95+ ]
96+ )
7597 where
98+ allHooks :: [(Bool , Binding )]
99+ allHooks =
100+ sort nixPreCommitHookConfigHooks
101+ <&> \ case
102+ (_, Nothing ) -> Nothing
103+ (a, Just b) -> Just (a, b)
104+ . bimap preCommitHookIsPure toBinding
105+ . dup
106+ & catMaybes
107+ pureHooks :: [Binding ]
108+ impureHooks :: [Binding ]
109+ (pureHooks, impureHooks) =
110+ let hooksByCondition c = allHooks ^.. folded . filtered c . _2
111+ in (hooksByCondition fst , hooksByCondition (not . fst ))
76112 run :: Expr
77113 run =
78114 if nixPreCommitHookConfigUsingFlakeLib
79115 then [nix |pre-commit-hooks-lib.lib.${system}.run|]
80116 else [nix |pre-commit-hooks-lib.run|]
81- runArgs :: [Binding ]
82- runArgs =
83- [ [nixbinding |src = ../.;|],
84- [nixproperty |hooks|] |= ESet False (catMaybes (toBinding <$> sort nixPreCommitHookConfigHooks))
85- ]
86117 mkToolsExpr :: Expr -> (PreCommitHookTool -> Bool ) -> Expr
87118 mkToolsExpr parentSet condition =
88119 EGrouping . EWith parentSet $
@@ -160,6 +191,9 @@ preCommitHookToolIsFromNixpkgs :: PreCommitHookTool -> Bool
160191preCommitHookToolIsFromNixpkgs (NixpkgsPreCommitHookTool _) = True
161192preCommitHookToolIsFromNixpkgs _ = False
162193
194+ preCommitHookIsPure :: PreCommitHook -> Bool
195+ preCommitHookIsPure h = h /= elmReview
196+
163197-- | Gets the attribute name within its containing set (not including the containing set)
164198preCommitHookToolSubAttrName :: PreCommitHookTool -> Maybe Text
165199preCommitHookToolSubAttrName (DefaultPreCommitHookTool attrName) = Just attrName
0 commit comments