@@ -0,0 +1,78 @@
1+{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE RecordWildCards #-}
3+{-# LANGUAGE TemplateHaskell #-}
4+
5+module Stack.Config where
6+
7+import Control.Lens
8+import Data.Coerce
9+import Data.Maybe
10+import Data.Text as T
11+import Stack.Config.Yaml as Yaml
12+
13+newtype StackResolver = StackResolver { fromStackResolver :: Text }
14+ deriving (Eq, Ord, Show)
15+
16+makePrisms ''StackResolver
17+
18+unStackResolver :: StackResolver -> String
19+unStackResolver = T.unpack . fromStackResolver
20+
21+data RepoGit = RepoGit
22+ { _rgUri :: Text
23+ , _rgCommit :: Text
24+ } deriving (Eq, Ord, Show)
25+
26+makeLenses ''RepoGit
27+
28+data StackLocation
29+ = StackPath Text
30+ | StackRepoGit RepoGit
31+ deriving (Eq, Ord, Show)
32+
33+makePrisms ''StackLocation
34+
35+data StackPackage = StackPackage
36+ { _spLocation :: StackLocation
37+ , _spExtraDep :: Bool
38+ } deriving (Eq, Ord, Show)
39+
40+makeLenses ''StackPackage
41+
42+newtype StackExtraDep = StackExtraDep { fromStackExtraDep :: Text }
43+ deriving (Eq, Ord, Show)
44+
45+makePrisms ''StackExtraDep
46+
47+unExtraDep :: StackExtraDep -> String
48+unExtraDep = T.unpack . fromStackExtraDep
49+
50+data StackConfig = StackConfig
51+ { _scResolver :: StackResolver
52+ , _scPackages :: [StackPackage]
53+ , _scExtraDeps :: [StackExtraDep]
54+ } deriving (Eq, Ord, Show)
55+
56+makeLenses ''StackConfig
57+
58+fromYamlConfig :: Yaml.Config -> StackConfig
59+fromYamlConfig c = StackConfig{..}
60+ where
61+ _scResolver = coerce $ c ^. cResolver
62+ _scPackages = fromYamlPackage <$> c ^. cPackages
63+ _scExtraDeps = StackExtraDep <$> c ^. cExtraDeps
64+
65+fromYamlPackage :: Yaml.Package -> StackPackage
66+fromYamlPackage = \case
67+ Yaml.Simple path ->
68+ StackPackage (StackPath path) False
69+ Yaml.LocationSimple (Yaml.Location path extraDep) ->
70+ StackPackage (StackPath path) (fromMaybe False extraDep)
71+ Yaml.LocationGit (Location git extraDep) ->
72+ StackPackage (StackRepoGit $ fromYamlGit git) (fromMaybe False extraDep)
73+
74+fromYamlGit :: Yaml.Git -> RepoGit
75+fromYamlGit yg = RepoGit{..}
76+ where
77+ _rgUri = yg ^. gGit
78+ _rgCommit = yg ^. gCommit