on 360b181 help sign in
stackage/Stackage/BuildPlan.hs
138 lines 4.6 kB history raw
1{-# LANGUAGE FlexibleInstances #-}
2{-# LANGUAGE RecordWildCards #-}
3module Stackage.BuildPlan
4 ( readBuildPlan
5 , writeBuildPlan
6 ) where
8import qualified Data.Map as Map
9import qualified Data.Set as Set
10import Distribution.Text (display, simpleParse)
11import Stackage.Types
13readBuildPlan :: FilePath -> IO BuildPlan
14readBuildPlan fp = do
15 str <- readFile fp
16 case fromString str of
17 Left s -> error $ "Could not read build plan: " ++ s
18 Right (x, "") -> return x
19 Right (_, _:_) -> error "Trailing content when reading build plan"
21writeBuildPlan :: FilePath -> BuildPlan -> IO ()
22writeBuildPlan fp bp = writeFile fp $ toString bp
24class AsString a where
25 toString :: a -> String
26 fromString :: String -> Either String (a, String)
28instance AsString BuildPlan where
29 toString BuildPlan {..} = concat
30 [ makeSection "tools" bpTools
31 , makeSection "packages" $ Map.toList bpPackages
32 , makeSection "core" $ Set.toList bpCore
33 , makeSection "optional-core" $ Map.toList bpOptionalCore
34 ]
35 fromString s1 = do
36 (tools, s2) <- getSection "tools" s1
37 (packages, s3) <- getSection "packages" s2
38 (core, s4) <- getSection "core" s3
39 (optionalCore, s5) <- getSection "optional-core" s4
40 let bp = BuildPlan
41 { bpTools = tools
42 , bpPackages = Map.fromList packages
43 , bpCore = Set.fromList core
44 , bpOptionalCore = Map.fromList optionalCore
45 }
46 return (bp, s5)
48makeSection :: AsString a => String -> [a] -> String
49makeSection title contents = unlines
50 $ ("-- BEGIN " ++ title)
51 : map toString contents
52 ++ ["-- END " ++ title, ""]
54instance AsString String where
55 toString = id
56 fromString s = Right (s, "")
58instance AsString PackageName where
59 toString (PackageName pn) = pn
60 fromString s = Right (PackageName s, "")
62instance AsString a => AsString (PackageName, a) where
63 toString (PackageName pn, s) = concat [pn, " ", toString s]
64 fromString s = do
65 (pn, rest) <- takeWord s
66 (rest', s') <- fromString rest
67 return ((PackageName pn, rest'), s')
69takeWord :: AsString a => String -> Either String (a, String)
70takeWord s =
71 case break (== ' ') s of
72 (x, _:y) -> do
73 (x', s') <- fromString x
74 if null s'
75 then Right (x', y)
76 else Left $ "Unconsumed input in takeWord call"
77 (_, []) -> Left "takeWord failed"
79instance AsString SelectedPackageInfo where
80 toString SelectedPackageInfo {..} = unwords
81 [ display spiVersion
82 , toString spiHasTests
83 , maybe "@" ("@" ++) spiGithubUser
84 , unMaintainer spiMaintainer
85 ]
86 fromString s1 = do
87 (version, s2) <- takeWord s1
88 (hasTests, s3) <- takeWord s2
89 (gu, m) <- takeWord s3
90 Right (SelectedPackageInfo
91 { spiVersion = version
92 , spiHasTests = hasTests
93 , spiGithubUser = gu
94 , spiMaintainer = Maintainer m
95 }, "")
97instance AsString (Maybe String) where
98 toString Nothing = "@"
99 toString (Just x) = "@" ++ x
100 fromString "@" = Right (Nothing, "")
101 fromString ('@':rest) = Right (Just rest, "")
102 fromString x = Left $ "Invalid Github user: " ++ x
104instance AsString Bool where
105 toString True = "test"
106 toString False = "notest"
107 fromString "test" = Right (True, "")
108 fromString "notest" = Right (False, "")
109 fromString x = Left $ "Invalid test value: " ++ x
111instance AsString Version where
112 toString = display
113 fromString s =
114 case simpleParse s of
115 Nothing -> Left $ "Invalid version: " ++ s
116 Just v -> Right (v, "")
118getSection :: AsString a => String -> String -> Either String ([a], String)
119getSection title orig =
120 case lines orig of
121 [] -> Left "Unexpected EOF when looking for a section"
122 l1:ls1
123 | l1 == begin ->
124 case break (== end) ls1 of
125 (here, _:"":rest) -> do
126 here' <- mapM fromString' here
127 Right (here', unlines rest)
128 (_, _) -> Left $ "Could not find section end: " ++ title
129 | otherwise -> Left $ "Could not find section start: " ++ title
130 where
131 begin = "-- BEGIN " ++ title
132 end = "-- END " ++ title
134 fromString' x = do
135 (y, z) <- fromString x
136 if null z
137 then return y
138 else Left $ "Unconsumed input on line: " ++ x