Skip to content

Commit ed08c4e

Browse files
committed
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 <[email protected]>
1 parent 50a5cb6 commit ed08c4e

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import qualified Data.List.NonEmpty as NE
101101

102102
import Control.Exception (ErrorCall, Handler (..), SomeAsyncException, assert, catches)
103103
import System.Directory (canonicalizePath, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, removeFile)
104+
import System.Environment (getEnv)
104105
import System.FilePath (dropDrive, normalise, takeDirectory, (<.>), (</>))
105106
import System.IO (Handle, IOMode (AppendMode), withFile)
106107
import System.Semaphore (SemaphoreName (..))
@@ -678,7 +679,32 @@ buildAndInstallUnpackedPackage
678679
runConfigure
679680
PBBuildPhase{runBuild} -> do
680681
noticeProgress ProgressBuilding
681-
runBuild
682+
hookDir <- (</> ".cabal/hooks") <$> getEnv "HOME"
683+
-- run preBuildHook. If it returns with 0, we assume the build was
684+
-- successful. If not, run the build.
685+
code <-
686+
rawSystemExitCode
687+
verbosity
688+
(Just srcdir)
689+
(hookDir </> "preBuildHook")
690+
[ (unUnitId $ installedUnitId rpkg)
691+
, (getSymbolicPath srcdir)
692+
, (getSymbolicPath builddir)
693+
]
694+
`catchIO` (\_ -> return (ExitFailure 10))
695+
when (code /= ExitSuccess) $ do
696+
runBuild
697+
-- not sure, if we want to care about a failed postBuildHook?
698+
void $
699+
rawSystemExitCode
700+
verbosity
701+
(Just srcdir)
702+
(hookDir </> "postBuildHook")
703+
[ (unUnitId $ installedUnitId rpkg)
704+
, (getSymbolicPath srcdir)
705+
, (getSymbolicPath builddir)
706+
]
707+
`catchIO` (\_ -> return (ExitFailure 10))
682708
PBHaddockPhase{runHaddock} -> do
683709
noticeProgress ProgressHaddock
684710
runHaddock

0 commit comments

Comments
 (0)