1
1
{-# LANGUAGE DeriveGeneric #-}
2
+ {-# LANGUAGE FlexibleInstances #-}
2
3
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3
4
{-# LANGUAGE OverloadedStrings #-}
4
5
{-# LANGUAGE TemplateHaskell #-}
@@ -8,21 +9,29 @@ module Stack.Types.PackageIndex
8
9
( PackageDownload (.. )
9
10
, PackageCache (.. )
10
11
, PackageCacheMap (.. )
12
+ -- ** PackageIndex, IndexName & IndexLocation
13
+ , PackageIndex (.. )
14
+ , IndexName (.. )
15
+ , indexNameText
16
+ , IndexLocation (.. )
11
17
) where
12
18
13
19
import Control.DeepSeq (NFData )
14
20
import Control.Monad (mzero )
15
21
import Data.Aeson.Extended
16
22
import Data.ByteString (ByteString )
23
+ import Data.Hashable (Hashable )
17
24
import Data.Int (Int64 )
18
25
import Data.Map (Map )
19
26
import qualified Data.Map.Strict as Map
20
27
import Data.Store (Store )
21
28
import Data.Store.TypeHash (mkManyHasTypeHash )
22
29
import Data.Text (Text )
23
- import Data.Text.Encoding (encodeUtf8 )
30
+ import qualified Data.Text as T
31
+ import Data.Text.Encoding (encodeUtf8 , decodeUtf8 )
24
32
import Data.Word (Word64 )
25
33
import GHC.Generics (Generic )
34
+ import Path
26
35
import Stack.Types.PackageIdentifier
27
36
28
37
data PackageCache = PackageCache
@@ -65,3 +74,61 @@ instance FromJSON PackageDownload where
65
74
}
66
75
67
76
$ (mkManyHasTypeHash [ [t | PackageCacheMap |] ])
77
+
78
+
79
+ -- | Unique name for a package index
80
+ newtype IndexName = IndexName { unIndexName :: ByteString }
81
+ deriving (Show , Eq , Ord , Hashable , Store )
82
+ indexNameText :: IndexName -> Text
83
+ indexNameText = decodeUtf8 . unIndexName
84
+ instance ToJSON IndexName where
85
+ toJSON = toJSON . indexNameText
86
+
87
+ instance FromJSON IndexName where
88
+ parseJSON = withText " IndexName" $ \ t ->
89
+ case parseRelDir (T. unpack t) of
90
+ Left e -> fail $ " Invalid index name: " ++ show e
91
+ Right _ -> return $ IndexName $ encodeUtf8 t
92
+
93
+ -- | Location of the package index. This ensures that at least one of Git or
94
+ -- HTTP is available.
95
+ data IndexLocation = ILGit ! Text | ILHttp ! Text | ILGitHttp ! Text ! Text
96
+ deriving (Show , Eq , Ord )
97
+
98
+
99
+ -- | Information on a single package index
100
+ data PackageIndex = PackageIndex
101
+ { indexName :: ! IndexName
102
+ , indexLocation :: ! IndexLocation
103
+ , indexDownloadPrefix :: ! Text
104
+ -- ^ URL prefix for downloading packages
105
+ , indexGpgVerify :: ! Bool
106
+ -- ^ GPG-verify the package index during download. Only applies to Git
107
+ -- repositories for now.
108
+ , indexRequireHashes :: ! Bool
109
+ -- ^ Require that hashes and package size information be available for packages in this index
110
+ }
111
+ deriving Show
112
+ instance FromJSON (WithJSONWarnings PackageIndex ) where
113
+ parseJSON = withObjectWarnings " PackageIndex" $ \ o -> do
114
+ name <- o ..: " name"
115
+ prefix <- o ..: " download-prefix"
116
+ mgit <- o ..:? " git"
117
+ mhttp <- o ..:? " http"
118
+ loc <-
119
+ case (mgit, mhttp) of
120
+ (Nothing , Nothing ) -> fail $
121
+ " Must provide either Git or HTTP URL for " ++
122
+ T. unpack (indexNameText name)
123
+ (Just git, Nothing ) -> return $ ILGit git
124
+ (Nothing , Just http) -> return $ ILHttp http
125
+ (Just git, Just http) -> return $ ILGitHttp git http
126
+ gpgVerify <- o ..:? " gpg-verify" ..!= False
127
+ reqHashes <- o ..:? " require-hashes" ..!= False
128
+ return PackageIndex
129
+ { indexName = name
130
+ , indexLocation = loc
131
+ , indexDownloadPrefix = prefix
132
+ , indexGpgVerify = gpgVerify
133
+ , indexRequireHashes = reqHashes
134
+ }
0 commit comments