on 4de7dfd help sign in
stackage/Stackage/HaskellPlatform.hs
53 lines 2.1 kB history raw
1module Stackage.HaskellPlatform
2 ( loadHaskellPlatform
3 ) where
5import Control.Monad (guard)
6import Data.Char (isSpace)
7import Data.List (foldl', isInfixOf, isPrefixOf, stripPrefix)
8import Data.Maybe (mapMaybe)
9import Data.Monoid (Monoid (..))
10import Data.Set (singleton)
11import Distribution.Text (simpleParse)
12import Stackage.Types
14loadHaskellPlatform :: BuildSettings -> IO HaskellPlatform
15loadHaskellPlatform = fmap parseHP . readFile . haskellPlatformCabal
17data HPLine = HPLPackage PackageIdentifier
18 | HPLBeginCore
19 | HPLEndCore
20 | HPLBeginPlatform
21 | HPLEndPlatform
22 deriving Show
24toHPLine :: String -> Maybe HPLine
25toHPLine s
26 | "begin core packages" `isInfixOf` s = Just HPLBeginCore
27 | "end core packages" `isInfixOf` s = Just HPLEndCore
28 | "begin platform packages" `isInfixOf` s = Just HPLBeginPlatform
29 | "end platform packages" `isInfixOf` s = Just HPLEndPlatform
30 | otherwise = do
31 let s1 = dropWhile isSpace s
32 guard $ not $ "--" `isPrefixOf` s1
33 guard $ not $ null s1
34 guard $ "==" `isInfixOf` s1
35 let (package', s2) = break (== '=') s1
36 package = takeWhile (not . isSpace) package'
37 s3 <- stripPrefix "==" s2
38 version <- simpleParse $ takeWhile (/= ',') s3
39 Just $ HPLPackage $ PackageIdentifier (PackageName package) version
41parseHP :: String -> HaskellPlatform
42parseHP =
43 snd . foldl' addLine (notInBlock, mempty) . mapMaybe toHPLine . lines
44 where
45 notInBlock _ = mempty
46 inCore x = HaskellPlatform (singleton x) mempty
47 inPlatform x = HaskellPlatform mempty (singleton x)
49 addLine (fromPackage, hp) (HPLPackage vp) = (fromPackage, fromPackage vp `mappend` hp)
50 addLine (_, hp) HPLBeginCore = (inCore, hp)
51 addLine (_, hp) HPLEndCore = (notInBlock, hp)
52 addLine (_, hp) HPLBeginPlatform = (inPlatform, hp)
53 addLine (_, hp) HPLEndPlatform = (notInBlock, hp)