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 programs are project local and need to be in the `cabalHooks`
directory which is in the same directory as the `cabal.project` file.

Co-authored: Moritz Angermann <[email protected]>
  • Loading branch information
erikd committed May 7, 2024
1 parent fd8020f commit 6db998e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 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 @@ -100,8 +101,8 @@ import qualified Data.ByteString.Lazy.Char8 as LBS.Char8
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.Directory (canonicalizePath, createDirectoryIfMissing, doesDirectoryExist, doesFileExist, getCurrentDirectory, removeFile)
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,32 @@ buildAndInstallUnpackedPackage
runConfigure
PBBuildPhase{runBuild} -> do
noticeProgress ProgressBuilding
runBuild
hooksDir <- (</> "cabalHooks") <$> getCurrentDirectory
-- run preBuildHook. If it returns with 0, we assume the build was
-- successful. If not, run the build.
code <-
rawSystemExitCode
verbosity
(Just srcdir)
(hooksDir </> "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)
(hooksDir </> "postBuildHook")
[ (unUnitId $ installedUnitId rpkg)
, (getSymbolicPath srcdir)
, (getSymbolicPath builddir)
]
`catchIO` (\_ -> return (ExitFailure 10))
PBHaddockPhase{runHaddock} -> do
noticeProgress ProgressHaddock
runHaddock
Expand Down
14 changes: 14 additions & 0 deletions changelog.d/pr-9899
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
synopsis: Add pre and post build hooks
packages: cabal-install
prs: #9899
issues: #9892
significance: significant

description: {

- Run a program (named "preBuildHook") before doing a package build and another program
(named "postBuildHook") after the package is built.
- These programs are project local and need to be in the `cabalHooks` directory which is
in the same directory as the `cabal.project` file.
- The absence of these programs will be ignored.
}

0 comments on commit 6db998e

Please sign in to comment.