Skip to content

Commit 0628ff1

Browse files
committed
Move PackageIndex into Stack.Types.PackageIndex
Starting on #2407.
1 parent a372a28 commit 0628ff1

File tree

2 files changed

+73
-60
lines changed

2 files changed

+73
-60
lines changed

src/Stack/Types/Config.hs

+5-59
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,19 @@ module Stack.Types.Config
7373
,PackageLocation(..)
7474
,RemotePackageType(..)
7575
-- ** PackageIndex, IndexName & IndexLocation
76+
77+
-- Re-exports
7678
,PackageIndex(..)
7779
,IndexName(..)
80+
,indexNameText
81+
,IndexLocation(..)
82+
-- Config fields
7883
,configPackageIndex
7984
,configPackageIndexCache
8085
,configPackageIndexGz
8186
,configPackageIndexRoot
8287
,configPackageIndexRepo
8388
,configPackageTarball
84-
,indexNameText
85-
,IndexLocation(..)
8689
-- ** Project & ProjectAndConfigMonoid
8790
,Project(..)
8891
,ProjectAndConfigMonoid(..)
@@ -166,15 +169,13 @@ import Data.IORef (IORef)
166169
import Data.List (stripPrefix)
167170
import Data.List.NonEmpty (NonEmpty)
168171
import qualified Data.List.NonEmpty as NonEmpty
169-
import Data.Hashable (Hashable)
170172
import Data.Map (Map)
171173
import qualified Data.Map as Map
172174
import qualified Data.Map.Strict as M
173175
import Data.Maybe
174176
import Data.Monoid.Extra
175177
import Data.Set (Set)
176178
import qualified Data.Set as Set
177-
import Data.Store (Store)
178179
import Data.Text (Text)
179180
import qualified Data.Text as T
180181
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
@@ -341,61 +342,6 @@ instance FromJSON ApplyGhcOptions where
341342
"everything" -> return AGOEverything
342343
_ -> fail $ "Invalid ApplyGhcOptions: " ++ show t
343344

344-
-- | Information on a single package index
345-
data PackageIndex = PackageIndex
346-
{ indexName :: !IndexName
347-
, indexLocation :: !IndexLocation
348-
, indexDownloadPrefix :: !Text
349-
-- ^ URL prefix for downloading packages
350-
, indexGpgVerify :: !Bool
351-
-- ^ GPG-verify the package index during download. Only applies to Git
352-
-- repositories for now.
353-
, indexRequireHashes :: !Bool
354-
-- ^ Require that hashes and package size information be available for packages in this index
355-
}
356-
deriving Show
357-
instance FromJSON (WithJSONWarnings PackageIndex) where
358-
parseJSON = withObjectWarnings "PackageIndex" $ \o -> do
359-
name <- o ..: "name"
360-
prefix <- o ..: "download-prefix"
361-
mgit <- o ..:? "git"
362-
mhttp <- o ..:? "http"
363-
loc <-
364-
case (mgit, mhttp) of
365-
(Nothing, Nothing) -> fail $
366-
"Must provide either Git or HTTP URL for " ++
367-
T.unpack (indexNameText name)
368-
(Just git, Nothing) -> return $ ILGit git
369-
(Nothing, Just http) -> return $ ILHttp http
370-
(Just git, Just http) -> return $ ILGitHttp git http
371-
gpgVerify <- o ..:? "gpg-verify" ..!= False
372-
reqHashes <- o ..:? "require-hashes" ..!= False
373-
return PackageIndex
374-
{ indexName = name
375-
, indexLocation = loc
376-
, indexDownloadPrefix = prefix
377-
, indexGpgVerify = gpgVerify
378-
, indexRequireHashes = reqHashes
379-
}
380-
381-
-- | Unique name for a package index
382-
newtype IndexName = IndexName { unIndexName :: ByteString }
383-
deriving (Show, Eq, Ord, Hashable, Store)
384-
indexNameText :: IndexName -> Text
385-
indexNameText = decodeUtf8 . unIndexName
386-
instance ToJSON IndexName where
387-
toJSON = toJSON . indexNameText
388-
instance FromJSON IndexName where
389-
parseJSON = withText "IndexName" $ \t ->
390-
case parseRelDir (T.unpack t) of
391-
Left e -> fail $ "Invalid index name: " ++ show e
392-
Right _ -> return $ IndexName $ encodeUtf8 t
393-
394-
-- | Location of the package index. This ensures that at least one of Git or
395-
-- HTTP is available.
396-
data IndexLocation = ILGit !Text | ILHttp !Text | ILGitHttp !Text !Text
397-
deriving (Show, Eq, Ord)
398-
399345
-- | Controls which version of the environment is used
400346
data EnvSettings = EnvSettings
401347
{ esIncludeLocals :: !Bool

src/Stack/Types/PackageIndex.hs

+68-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE FlexibleInstances #-}
23
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE TemplateHaskell #-}
@@ -8,21 +9,29 @@ module Stack.Types.PackageIndex
89
( PackageDownload (..)
910
, PackageCache (..)
1011
, PackageCacheMap (..)
12+
-- ** PackageIndex, IndexName & IndexLocation
13+
, PackageIndex(..)
14+
, IndexName(..)
15+
, indexNameText
16+
, IndexLocation(..)
1117
) where
1218

1319
import Control.DeepSeq (NFData)
1420
import Control.Monad (mzero)
1521
import Data.Aeson.Extended
1622
import Data.ByteString (ByteString)
23+
import Data.Hashable (Hashable)
1724
import Data.Int (Int64)
1825
import Data.Map (Map)
1926
import qualified Data.Map.Strict as Map
2027
import Data.Store (Store)
2128
import Data.Store.TypeHash (mkManyHasTypeHash)
2229
import Data.Text (Text)
23-
import Data.Text.Encoding (encodeUtf8)
30+
import qualified Data.Text as T
31+
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
2432
import Data.Word (Word64)
2533
import GHC.Generics (Generic)
34+
import Path
2635
import Stack.Types.PackageIdentifier
2736

2837
data PackageCache = PackageCache
@@ -65,3 +74,61 @@ instance FromJSON PackageDownload where
6574
}
6675

6776
$(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

Comments
 (0)