git.iko.soy/typeable/stackage2nix
on master help sign in
Add: deriveJSON
commit34abc9ea6c6993edc7d11e2b2921b118c9c78d03 authorDmitry Bushev <bushevdv@gmail.com> dateFri 2 Jun 2017, 00:32 (9 years ago) parentcbc9f89
7 files changed+82−45
src/Distribution/Nixpkgs/Haskell/Stack.hs+17−11 src/Stack/Config.hs+16−11 src/Stack/Config/TH.hs+23 src/Stack/Config/Yaml.hs+16−19 stack2nix.cabal+3 test.sh+3 test/Stack/Config/YamlSpec.hs+4−4
src/Distribution/Nixpkgs/Haskell/Stack.hs+17−11
@@ -1,5 +1,6 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
12{-# LANGUAGE LambdaCase #-}
2−{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving #-}
3+{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE RecordWildCards #-}
45{-# LANGUAGE TemplateHaskell #-}
56
@@ -7,14 +8,15 @@ module Distribution.Nixpkgs.Haskell.Stack where
78
89import Control.Lens
910import Data.Text as T
10−import Distribution.System as System
1111import Distribution.Compiler as Compiler
12−import Distribution.PackageDescription as PackageDescription
1312import Distribution.Nixpkgs.Fetch
1413import Distribution.Nixpkgs.Haskell.Derivation
1514import Distribution.Nixpkgs.Haskell.FromCabal as FromCabal
1615import Distribution.Nixpkgs.Haskell.PackageSourceSpec as PackageSourceSpec
17−import Language.Nix
16+import Distribution.PackageDescription as PackageDescription
17+import Distribution.System as System
18+import Language.Nix as Nix
19+import Network.URI as URI
1820import Stack.Config
1921
2022newtype HackageDb = HackageDb { fromHackageDB :: T.Text }
@@ -38,7 +40,7 @@ makeLenses ''PackageConfig
3840mkPackageConfig :: Platform -> CompilerId -> PackageConfig
3941mkPackageConfig platform compilerId = PackageConfig
4042 { _pcHaskellResolver = const True
41− , _pcNixpkgsResolver = \i -> Just (binding # (i, path # [i]))
43+ , _pcNixpkgsResolver = \i -> Just (Nix.binding # (i, Nix.path # [i]))
4244 , _pcTargetPlatform = platform
4345 , _pcTargetCompiler = unknownCompilerInfo compilerId NoAbiTag
4446 , _pcFlagAssignment = []
@@ -51,22 +53,26 @@ getStackPackageFromDb
5153getStackPackageFromDb optHackageDb stackPackage =
5254 PackageSourceSpec.getPackage
5355 (unHackageDb <$> optHackageDb)
54− (stackLocationToSource $ stackPackage ^. spSource)
56+ (stackLocationToSource $ stackPackage ^. spLocation)
5557
56−stackLocationToSource :: StackLocation -> Source
58+stackLocationToSource :: PackageLocation -> Source
5759stackLocationToSource = \case
5860 HackagePackage p -> Source
5961 { sourceUrl = "cabal://" ++ T.unpack p
6062 , sourceRevision = mempty
6163 , sourceHash = UnknownHash
6264 , sourceCabalDir = mempty }
63− StackPath p -> Source
64− { sourceUrl = T.unpack p
65+ StackFilePath p -> Source
66+ { sourceUrl = p
6567 , sourceRevision = mempty
6668 , sourceHash = UnknownHash
6769 , sourceCabalDir = mempty }
68−
69− StackRepoGit rg -> Source
70+ StackUri uri -> Source
71+ { sourceUrl = URI.uriToString id uri mempty
72+ , sourceRevision = mempty
73+ , sourceHash = UnknownHash
74+ , sourceCabalDir = mempty }
75+ StackRepoGit rg -> Source
7076 { sourceUrl = T.unpack $ rg ^. rgUri
7177 , sourceRevision = T.unpack $ rg ^. rgCommit
7278 , sourceHash = UnknownHash
src/Stack/Config.hs+16−11
@@ -1,6 +1,7 @@
11{-# LANGUAGE LambdaCase #-}
22{-# LANGUAGE RecordWildCards #-}
33{-# LANGUAGE TemplateHaskell #-}
4+{-# LANGUAGE ViewPatterns #-}
45
56module Stack.Config where
67
@@ -12,6 +13,7 @@ import Data.Maybe
1213import Data.Text as T
1314import Data.Yaml
1415import Stack.Config.Yaml as Yaml
16+import Network.URI
1517
1618newtype StackResolver = StackResolver { fromStackResolver :: Text }
1719 deriving (Eq, Ord, Show)
@@ -28,16 +30,17 @@ data RepoGit = RepoGit
2830
2931makeLenses ''RepoGit
3032
31−data StackLocation
33+data PackageLocation
3234 = HackagePackage Text
33− | StackPath Text
35+ | StackFilePath FilePath
36+ | StackUri URI
3437 | StackRepoGit RepoGit
3538 deriving (Eq, Ord, Show)
3639
37−makePrisms ''StackLocation
40+makePrisms ''PackageLocation
3841
3942data StackPackage = StackPackage
40− { _spSource :: StackLocation
43+ { _spLocation :: PackageLocation
4144 , _spExtraDep :: Bool
4245 } deriving (Eq, Ord, Show)
4346
@@ -55,17 +58,19 @@ fromYamlConfig c = StackConfig{..}
5558 where
5659 _scResolver = coerce $ c ^. cResolver
5760 _scPackages =
58− (fromYamlPackage <$> c ^. cPackages) ++
59− (fromYamlExtraDep <$> c ^. cExtraDeps)
61+ (fromYamlPackage <$> c ^. cPackages . to (fromMaybe mempty)) ++
62+ (fromYamlExtraDep <$> c ^. cExtraDeps . to (fromMaybe mempty))
6063
6164fromYamlPackage :: Yaml.Package -> StackPackage
6265fromYamlPackage = \case
63− Yaml.Simple path ->
64− StackPackage (StackPath path) False
65− Yaml.LocationSimple (Yaml.Location path extraDep) ->
66− StackPackage (StackPath path) (fromMaybe False extraDep)
67− Yaml.LocationGit (Location git extraDep) ->
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) ->
6871 StackPackage (StackRepoGit $ fromYamlGit git) (fromMaybe False extraDep)
72+ where
73+ parseSimplePath (T.unpack -> p) = maybe (StackFilePath p) StackUri $ parseURI p
6974
7075fromYamlExtraDep :: Text -> StackPackage
7176fromYamlExtraDep = flip StackPackage True . HackagePackage
src/Stack/Config/TH.hsnew file+23
@@ -0,0 +1,23 @@
1+module Stack.Config.TH where
2+
3+import Data.Aeson.TH
4+import Data.Char as C
5+import Data.List as L
6+import Data.Text as T
7+import Text.Inflections as TI
8+
9+toDashedField :: String -> String
10+toDashedField = toDashed' . dropPrefix
11+
12+toDashed' :: String -> String
13+toDashed' = either (const $ error "toDashed failed") T.unpack
14+ . TI.toDashed
15+ . T.pack
16+
17+dropPrefix :: String -> String
18+dropPrefix = L.dropWhile (not . C.isUpper)
19+
20+jsonOpts :: Options
21+jsonOpts = defaultOptions
22+ { fieldLabelModifier = toDashedField
23+ , omitNothingFields = True }
src/Stack/Config/Yaml.hs+16−19
@@ -1,12 +1,15 @@
1−{-# LANGUAGE TemplateHaskell #-}
21{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE TemplateHaskell #-}
3+{-# LANGUAGE LambdaCase #-}
34
45module Stack.Config.Yaml where
56
67import Control.Applicative
78import Control.Lens
89import Data.Aeson
10+import Data.Aeson.TH
911import Data.Text as T
12+import Stack.Config.TH
1013
1114data Location a = Location
1215 { _lLocation :: a
@@ -15,11 +18,7 @@ data Location a = Location
1518
1619makeLenses ''Location
1720
18−instance FromJSON a => FromJSON (Location a) where
19− parseJSON = withObject "Location" $ \v ->
20− Location <$>
21− v .: "location" <*>
22− v .:? "extra-dep"
21+deriveJSON jsonOpts ''Location
2322
2423data Git = Git
2524 { _gGit :: Text
@@ -28,17 +27,14 @@ data Git = Git
2827
2928makeLenses ''Git
3029
31−instance FromJSON Git where
32− parseJSON = withObject "Git" $ \v ->
33− Git <$>
34− v .: "git" <*>
35− v .: "commit"
30+deriveJSON jsonOpts ''Git
3631
3732data Package
3833 = Simple Text
3934 | LocationSimple (Location Text)
4035 | LocationGit (Location Git)
4136 deriving (Eq, Show)
37+
4238makePrisms ''Package
4339
4440instance FromJSON Package where
@@ -47,17 +43,18 @@ instance FromJSON Package where
4743 (LocationSimple <$> parseJSON v) <|>
4844 (LocationGit <$> parseJSON v)
4945
46+instance ToJSON Package where
47+ toJSON = \case
48+ Simple t -> toJSON t
49+ LocationSimple t -> toJSON t
50+ LocationGit t -> toJSON t
51+
5052data Config = Config
5153 { _cResolver :: Text
52− , _cPackages :: [Package]
53− , _cExtraDeps :: [Text]
54+ , _cPackages :: Maybe [Package]
55+ , _cExtraDeps :: Maybe [Text]
5456 } deriving (Eq, Show)
5557
5658makeLenses ''Config
5759
58−instance FromJSON Config where
59− parseJSON = withObject "Config" $ \v ->
60− Config <$>
61− v .: "resolver" <*>
62− v .:? "packages" .!= [] <*>
63− v .:? "extra-deps" .!= []
60+deriveJSON jsonOpts ''Config
stack2nix.cabal+3
@@ -20,6 +20,7 @@ library
2020 , Runner
2121 , Runner.Cli
2222 , Stack.Config
23+ , Stack.Config.TH
2324 , Stack.Config.Yaml
2425 , Stack.Types
2526 build-depends: base > 4.7 && < 5
@@ -31,8 +32,10 @@ library
3132 , deepseq
3233 , distribution-nixpkgs >= 1.1
3334 , filepath
35+ , inflections
3436 , language-nix
3537 , lens
38+ , network-uri
3639 , optparse-applicative
3740 , pretty
3841 , text
test.shnew file+3
@@ -0,0 +1,3 @@
1+#!/usr/bin/env sh
2+
3+exec stack2nix --nix-stackage-config=nix/configuration-packages.nix --nix-stackage-packages=./nix/packages.nix
test/Stack/Config/YamlSpec.hs+4−4
@@ -36,14 +36,14 @@ extra-deps:
3636configMinimal :: Config
3737configMinimal = Config
3838 { _cResolver = "lts-8.15"
39− , _cPackages = [ Simple "." ]
40− , _cExtraDeps = []
39+ , _cPackages = Just [ Simple "." ]
40+ , _cExtraDeps = Nothing
4141 }
4242
4343config :: Config
4444config = Config
4545 { _cResolver = "lts-3.7"
46− , _cPackages =
46+ , _cPackages = Just
4747 [ Simple "."
4848 , LocationSimple (Location "dir1/dir2" Nothing)
4949 , LocationSimple (Location "https://example.com/foo/bar/baz-0.0.2.tar.gz" (Just True))
@@ -52,7 +52,7 @@ config = Config
5252 "git@github.com:commercialhaskell/stack.git"
5353 "6a86ee32e5b869a877151f74064572225e1a0398")
5454 Nothing]
55− , _cExtraDeps =
55+ , _cExtraDeps = Just
5656 ["acme-missiles-0.3"]
5757 }
5858