Skip to content

Instantly share code, notes, and snippets.

@maxcan
Created September 4, 2013 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcan/6442273 to your computer and use it in GitHub Desktop.
Save maxcan/6442273 to your computer and use it in GitHub Desktop.
Sample Setup.hs file for creating .lock.yaml files for Cabal based Haskell projects
{-# LANGUAGE OverloadedStrings #-}
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import Distribution.PackageDescription
import System.IO (writeFile)
import qualified Data.Text as T
main :: IO ()
main = defaultMainWithHooks simpleUserHooks {postConf = printLockFile}
printLockFile :: Args -> ConfigFlags -> PackageDescription -> LocalBuildInfo -> IO ()
printLockFile _ _ packageDescription localBuildInfo =
writeFile (name ++ ".lock.yaml") $ ("components:\n" ++) $ concat $
flip map (allComponentsInBuildOrder localBuildInfo) $
\(compName, buildInfo) ->
" " ++ showComponentName compName ++ ":\n" ++
concatMap showPkgInfo (componentPackageDeps buildInfo)
where
PackageName name = pkgName $ package packageDescription
showPkgInfo :: (InstalledPackageId, PackageIdentifier) -> String
showPkgInfo (pkgId, ipi) = " - " ++ stripPackageHash pkgId ++ "\n" -- ++
stripPackageHash (InstalledPackageId str) = dropDashEnding str
dropDashEnding str = dropHash `applyAsText` str
where
dropHash = T.intercalate "-" . init' . T.splitOn "-"
applyAsText f = T.unpack . f . T.pack
init' ["builtin_rts"] = []
init' x@(_:_:_) = init x
init' _ = error $ "Setup.hs: init': bad package name: " ++ str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment