From c70aa26e2b0c2a48c1ee9fc472d1a0f5c4d7b0e9 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Wed, 17 Apr 2024 14:52:06 +1000 Subject: [PATCH] Add pre and post build hooks Run a program (named "preBuildHook") before doing a package build and another program (named "postBuildHook") after the package is built. These two programs need to be in ~/.cabal/hooks/ directory. If two or more projects need to use different hooks, dispatch can be done using environment variables. Co-authored: Moritz Angermann --- .../Client/ProjectBuilding/UnpackedPackage.hs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs b/cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs index 065334d5c6e..72b1995acc2 100644 --- a/cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs +++ b/cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs @@ -101,6 +101,7 @@ import qualified Data.List.NonEmpty as NE import Control.Exception (ErrorCall, Handler (..), SomeAsyncException, assert, catches) import System.Directory (canonicalizePath, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, removeFile) +import System.Environment (getEnv) import System.FilePath (dropDrive, normalise, takeDirectory, (<.>), ()) import System.IO (Handle, IOMode (AppendMode), withFile) import System.Semaphore (SemaphoreName (..)) @@ -678,7 +679,32 @@ buildAndInstallUnpackedPackage runConfigure PBBuildPhase{runBuild} -> do noticeProgress ProgressBuilding - runBuild + hookDir <- ( (".cabal/" "hooks")) <$> getEnv "HOME" + -- run preBuildHook. If it returns with 0, we assume the build was + -- successful. If not, run the build. + code <- + rawSystemExitCode + verbosity + (Just srcdir) + (hookDir "preBuildHook") + [ (unUnitId $ installedUnitId rpkg) + , (getSymbolicPath srcdir) + , (getSymbolicPath builddir) + ] + `catchIO` (\_ -> return (ExitFailure 10)) + when (code /= ExitSuccess) $ do + runBuild + -- not sure, if we want to care about a failed postBuildHook? + void $ + rawSystemExitCode + verbosity + (Just srcdir) + (hookDir "postBuildHook") + [ (unUnitId $ installedUnitId rpkg) + , (getSymbolicPath srcdir) + , (getSymbolicPath builddir) + ] + `catchIO` (\_ -> return (ExitFailure 10)) PBHaddockPhase{runHaddock} -> do noticeProgress ProgressHaddock runHaddock