git.iko.soy/typeable/stackage2nix
on be94366 help sign in
86 lines 2.3 kB history raw
1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE ViewPatterns #-}
6module Stack.Config where
8import Control.Lens
9import Data.Bifunctor
10import Data.ByteString as BS
11import Data.Coerce
12import Data.Maybe
13import Data.Text as T
14import Data.Yaml
15import Stack.Config.Yaml as Yaml
16import Network.URI
18newtype StackResolver = StackResolver { fromStackResolver :: Text }
19 deriving (Eq, Ord, Show)
21makePrisms ''StackResolver
23unStackResolver :: StackResolver -> String
24unStackResolver = T.unpack . fromStackResolver
26data RepoGit = RepoGit
27 { _rgUri :: !Text
28 , _rgCommit :: !Text
29 } deriving (Eq, Ord, Show)
31makeLenses ''RepoGit
33data PackageLocation
34 = HackagePackage Text
35 | StackFilePath FilePath
36 | StackUri URI
37 | StackRepoGit RepoGit
38 deriving (Eq, Ord, Show)
40makePrisms ''PackageLocation
42data StackPackage = StackPackage
43 { _spLocation :: !PackageLocation
44 , _spExtraDep :: !Bool
45 } deriving (Eq, Ord, Show)
47makeLenses ''StackPackage
49data StackConfig = StackConfig
50 { _scResolver :: !StackResolver
51 , _scPackages :: ![StackPackage]
52 } deriving (Eq, Ord, Show)
54makeLenses ''StackConfig
56fromYamlConfig :: Yaml.Config -> StackConfig
57fromYamlConfig c = StackConfig{..}
58 where
59 _scResolver = coerce $ c ^. cResolver
60 _scPackages =
61 (fromYamlPackage <$> c ^. cPackages . to (fromMaybe mempty)) ++
62 (fromYamlExtraDep <$> c ^. cExtraDeps . to (fromMaybe mempty))
64fromYamlPackage :: Yaml.Package -> StackPackage
65fromYamlPackage = \case
66 Yaml.Simple p ->
67 StackPackage (parseSimplePath p) False
68 Yaml.LocationSimple (Yaml.Location p extraDep) ->
69 StackPackage (parseSimplePath p) (fromMaybe False extraDep)
70 Yaml.LocationGit (Location git extraDep) ->
71 StackPackage (StackRepoGit $ fromYamlGit git) (fromMaybe False extraDep)
72 where
73 parseSimplePath (T.unpack -> p) = maybe (StackFilePath p) StackUri $ parseURI p
75fromYamlExtraDep :: Text -> StackPackage
76fromYamlExtraDep = flip StackPackage True . HackagePackage
78fromYamlGit :: Yaml.Git -> RepoGit
79fromYamlGit yg = RepoGit{..}
80 where
81 _rgUri = yg ^. gGit
82 _rgCommit = yg ^. gCommit
84readStackConfig :: FilePath -> IO (Either String StackConfig)
85readStackConfig stackYaml =
86 second fromYamlConfig . decodeEither <$> BS.readFile stackYaml