Created
September 4, 2013 20:13
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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