public
Created

Cabal Setup.hs to copy scripts during postInst:

  • Download Gist
Setup.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
import Control.Monad ( unless )
import Distribution.Simple
import Distribution.Simple.Program.Types ( Program(..), simpleProgram )
import Distribution.PackageDescription ( PackageDescription(..), executables,
hsSourceDirs, exeName, buildInfo )
import Distribution.Simple.LocalBuildInfo ( LocalBuildInfo(..), buildDir, bindir
, substPathTemplate, absoluteInstallDirs )
import Distribution.Simple.Setup ( InstallFlags(..), BuildFlags, Flag, fromFlag
, buildVerbose, CopyFlags(..), defaultCopyFlags )
import Distribution.Simple.Utils ( rawSystemExit, findProgramVersion)
import Distribution.Verbosity ( Verbosity )
 
import System.Directory ( doesDirectoryExist, copyFile )
import System.FilePath ( (</>) )
 
main = defaultMainWithHooks $
simpleUserHooks { hookedPrograms = [cabalInstallProgram]
, postInst = postInstCp (postInst simpleUserHooks)
}
 
type PostInstHook = Args -> InstallFlags -> PackageDescription -> LocalBuildInfo -> IO ()
 
postInstCp :: PostInstHook -> Args -> InstallFlags -> PackageDescription -> LocalBuildInfo -> IO ()
postInstCp oldHook args iflags pDesc lbi = do
let -- The filename to copy from. This assumes 'srcFileName.sh' is adjacent to Setup.hs:
inFile :: FilePath
inFile = "srcFileName.sh"
 
-- The filename to copy to:
outFile :: FilePath
outFile = "destFileName.sh"
 
prefix = fromFlag $ installDistPref iflags
 
-- Make a concrete binDir from the LocalBuildInfo & PackageDescription:
instBinDir :: FilePath
instBinDir = bindir $ absoluteInstallDirs pDesc lbi
(fromFlag $ copyDest defaultCopyFlags)
 
-- layer of indirection, in case we wanted to get a specific
-- src directory from the cabal file:
src :: FilePath
src = inFile
 
-- qualify the destination.
dest :: FilePath
dest = instBinDir </> outFile
 
-- Do the copy, creating outFile in the bin dir:
copyFile src dest
 
-- now invoke the old hook:
oldHook args iflags pDesc lbi
 
 
cabalInstallProgram :: Program
cabalInstallProgram = (simpleProgram "cabal") {
programFindVersion = findProgramVersion "--numeric-version" id
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.