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