Skip to content

Commit

Permalink
Add pre and post build hooks
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
erikd committed Apr 24, 2024
1 parent 3395fa1 commit 7f4ea63
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Distribution.Client.ProjectBuilding.UnpackedPackage
import Distribution.Client.Compat.Prelude
import Prelude ()

import Distribution.Client.Config
import Distribution.Client.PackageHash (renderPackageHashInputs)
import Distribution.Client.ProjectBuilding.Types
import Distribution.Client.ProjectConfig
Expand Down Expand Up @@ -101,7 +102,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.FilePath (dropDrive, normalise, takeDirectory, (<.>), (</>))
import System.FilePath (dropDrive, dropFileName, normalise, takeDirectory, (<.>), (</>))
import System.IO (Handle, IOMode (AppendMode), withFile)
import System.Semaphore (SemaphoreName (..))

Expand Down Expand Up @@ -678,7 +679,33 @@ buildAndInstallUnpackedPackage
runConfigure
PBBuildPhase{runBuild} -> do
noticeProgress ProgressBuilding
runBuild
cabalDir <- dropFileName <$> defaultConfigFile
let hookDir = cabalDir </> "hooks"
-- 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
Expand Down

0 comments on commit 7f4ea63

Please sign in to comment.