Skip to content

Commit 6ba2a2a

Browse files
committed
Move Resolver/AbstractResolver into own module.
1 parent 7a9899d commit 6ba2a2a

14 files changed

Lines changed: 239 additions & 137 deletions

File tree

src/Stack/Build/Source.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import Stack.Types.Config
7070
import Stack.Types.FlagName
7171
import Stack.Types.Package
7272
import Stack.Types.PackageName
73+
import Stack.Types.Resolver
7374
import Stack.Types.StackT
7475
import Stack.Types.Version
7576
import qualified System.Directory as D

src/Stack/BuildPlan.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ import Stack.Types.Version
9393
import Stack.Types.Config
9494
import Stack.Types.Urls
9595
import Stack.Types.Compiler
96+
import Stack.Types.Resolver
9697
import Stack.Types.StackT
9798

9899
data BuildPlanException

src/Stack/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import Stack.Types.Config
9090
import Stack.Types.Docker
9191
import Stack.Types.Internal
9292
import Stack.Types.Nix
93+
import Stack.Types.Resolver
9394
import Stack.Types.StackT
9495
import Stack.Types.Urls
9596
import Stack.Types.Version

src/Stack/Config/Docker.hs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
-- | Docker configuration
44
module Stack.Config.Docker where
55

6-
import Control.Exception.Lifted
7-
import Control.Monad.Catch (MonadThrow)
8-
import Data.List (find)
9-
import Data.Maybe
10-
import Data.Monoid.Extra
6+
import Control.Exception.Lifted
7+
import Control.Monad.Catch (MonadThrow)
8+
import Data.List (find)
9+
import Data.Maybe
10+
import Data.Monoid.Extra
1111
import qualified Data.Text as T
12-
import Data.Typeable (Typeable)
13-
import Distribution.Version (simplifyVersionRange)
14-
import Path
15-
import Stack.Types.BuildPlan
16-
import Stack.Types.Version
17-
import Stack.Types.Config
18-
import Stack.Types.Docker
12+
import Data.Typeable (Typeable)
13+
import Distribution.Version (simplifyVersionRange)
14+
import Path
15+
import Stack.Types.BuildPlan
16+
import Stack.Types.Version
17+
import Stack.Types.Config
18+
import Stack.Types.Docker
19+
import Stack.Types.Resolver
1920

2021
-- | Interprets DockerOptsMonoid options.
2122
dockerOptsFromMonoid

src/Stack/ConfigCmd.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Stack.BuildPlan
3232
import Stack.Config (makeConcreteResolver, getStackYaml)
3333
import Stack.Types.BuildPlan
3434
import Stack.Types.Config
35+
import Stack.Types.Resolver
3536

3637
data ConfigCmdSet
3738
= ConfigCmdSetResolver AbstractResolver

src/Stack/Init.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import Stack.Types.BuildPlan
4848
import Stack.Types.Config
4949
import Stack.Types.FlagName
5050
import Stack.Types.PackageName
51+
import Stack.Types.Resolver
5152
import Stack.Types.StackT (StackM)
5253
import Stack.Types.Version
5354
import qualified System.FilePath as FP

src/Stack/Options/ResolverParser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import qualified Data.Text as T
55
import Options.Applicative
66
import Options.Applicative.Types (readerAsk)
77
import Stack.Options.Utils
8-
import Stack.Types.Config
98
import Stack.Types.Compiler
9+
import Stack.Types.Resolver
1010

1111
-- | Parser for the resolver
1212
abstractResolverOptsParser :: Bool -> Parser AbstractResolver

src/Stack/Solver.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import Stack.Types.Config
7474
import Stack.Types.FlagName
7575
import Stack.Types.PackageIdentifier
7676
import Stack.Types.PackageName
77+
import Stack.Types.Resolver
7778
import Stack.Types.StackT (StackM)
7879
import Stack.Types.Version
7980
import qualified System.Directory as D

src/Stack/Types/Config.hs

Lines changed: 6 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,7 @@ module Stack.Types.Config
100100
-- ** PvpBounds
101101
,PvpBounds(..)
102102
,parsePvpBounds
103-
-- ** Resolver & AbstractResolver
104-
,Resolver
105-
,LoadedResolver
106-
,ResolverThat's(..)
107-
,parseResolverText
108-
,resolverDirName
109-
,resolverName
110-
,customResolverHash
111-
,toResolverNotLoaded
112-
,AbstractResolver(..)
113-
,readAbstractResolver
103+
-- ** ColorWhen
114104
,ColorWhen(..)
115105
,readColorWhen
116106
-- ** SCM
@@ -165,13 +155,13 @@ import Control.Applicative
165155
import Control.Arrow ((&&&))
166156
import Control.Exception
167157
import Control.Monad (liftM, mzero, join)
168-
import Control.Monad.Catch (MonadThrow, MonadMask, throwM)
158+
import Control.Monad.Catch (MonadThrow, MonadMask)
169159
import Control.Monad.Logger (LogLevel(..), MonadLoggerIO)
170160
import Control.Monad.Reader (MonadReader, ask, asks, MonadIO, liftIO)
171161
import Control.Monad.Trans.Control
172162
import Data.Aeson.Extended
173163
(ToJSON, toJSON, FromJSON, parseJSON, withText, object,
174-
(.=), (..:), (..:?), (..!=), Value(Bool, String, Object),
164+
(.=), (..:), (..:?), (..!=), Value(Bool, String),
175165
withObjectWarnings, WarningParser, Object, jsonSubWarnings,
176166
jsonSubWarningsT, jsonSubWarningsTT, WithJSONWarnings(..), noJSONWarnings)
177167
import Data.Attoparsec.Args
@@ -191,8 +181,7 @@ import Data.Set (Set)
191181
import qualified Data.Set as Set
192182
import Data.Text (Text)
193183
import qualified Data.Text as T
194-
import Data.Text.Encoding (encodeUtf8, decodeUtf8)
195-
import Data.Text.Read (decimal)
184+
import Data.Text.Encoding (encodeUtf8)
196185
import Data.Typeable
197186
import Data.Yaml (ParseException)
198187
import qualified Data.Yaml as Yaml
@@ -208,7 +197,7 @@ import qualified Options.Applicative as OA
208197
import qualified Options.Applicative.Types as OA
209198
import Path
210199
import qualified Paths_stack as Meta
211-
import Stack.Types.BuildPlan (MiniBuildPlan(..), SnapName, renderSnapName, parseSnapName, SnapshotHash (..), trimmedSnapshotHash)
200+
import Stack.Types.BuildPlan (MiniBuildPlan(..), SnapName, renderSnapName)
212201
import Stack.Types.Compiler
213202
import Stack.Types.CompilerBuild
214203
import Stack.Types.Docker
@@ -218,6 +207,7 @@ import Stack.Types.Nix
218207
import Stack.Types.PackageIdentifier
219208
import Stack.Types.PackageIndex
220209
import Stack.Types.PackageName
210+
import Stack.Types.Resolver
221211
import Stack.Types.TemplateName
222212
import Stack.Types.Urls
223213
import Stack.Types.Version
@@ -459,30 +449,6 @@ instance Monoid GlobalOptsMonoid where
459449
mempty = memptydefault
460450
mappend = mappenddefault
461451

462-
-- | Either an actual resolver value, or an abstract description of one (e.g.,
463-
-- latest nightly).
464-
data AbstractResolver
465-
= ARLatestNightly
466-
| ARLatestLTS
467-
| ARLatestLTSMajor !Int
468-
| ARResolver !Resolver
469-
| ARGlobal
470-
deriving Show
471-
472-
readAbstractResolver :: ReadM AbstractResolver
473-
readAbstractResolver = do
474-
s <- OA.readerAsk
475-
case s of
476-
"global" -> return ARGlobal
477-
"nightly" -> return ARLatestNightly
478-
"lts" -> return ARLatestLTS
479-
'l':'t':'s':'-':x | Right (x', "") <- decimal $ T.pack x ->
480-
return $ ARLatestLTSMajor x'
481-
_ ->
482-
case parseResolverText $ T.pack s of
483-
Left e -> OA.readerError $ show e
484-
Right x -> return $ ARResolver x
485-
486452
-- | Default logging level should be something useful but not crazy.
487453
defaultLogLevel :: LogLevel
488454
defaultLogLevel = LevelInfo
@@ -693,90 +659,6 @@ instance ToJSON Project where
693659
, "extra-package-dbs" .= projectExtraPackageDBs p
694660
]
695661

696-
data IsLoaded = Loaded | NotLoaded
697-
698-
type LoadedResolver = ResolverThat's 'Loaded
699-
type Resolver = ResolverThat's 'NotLoaded
700-
701-
-- TODO: once GHC 8.0 is the lowest version we support, make these into
702-
-- actual haddock comments...
703-
704-
-- | How we resolve which dependencies to install given a set of packages.
705-
data ResolverThat's (l :: IsLoaded) where
706-
-- Use an official snapshot from the Stackage project, either an LTS
707-
-- Haskell or Stackage Nightly.
708-
ResolverSnapshot :: !SnapName -> ResolverThat's l
709-
-- Require a specific compiler version, but otherwise provide no
710-
-- build plan. Intended for use cases where end user wishes to
711-
-- specify all upstream dependencies manually, such as using a
712-
-- dependency solver.
713-
ResolverCompiler :: !CompilerVersion -> ResolverThat's l
714-
-- A custom resolver based on the given name and URL. When a URL is
715-
-- provided, it file is to be completely immutable. Filepaths are
716-
-- always loaded. This constructor is used before the build-plan has
717-
-- been loaded, as we do not yet know the custom snapshot's hash.
718-
ResolverCustom :: !Text -> !Text -> ResolverThat's 'NotLoaded
719-
-- Like 'ResolverCustom', but after loading the build-plan, so we
720-
-- have a hash. This is necessary in order to identify the location
721-
-- files are stored for the resolver.
722-
ResolverCustomLoaded :: !Text -> !Text -> !SnapshotHash -> ResolverThat's 'Loaded
723-
724-
deriving instance Eq (ResolverThat's k)
725-
deriving instance Show (ResolverThat's k)
726-
727-
instance ToJSON (ResolverThat's k) where
728-
toJSON x = case x of
729-
ResolverSnapshot{} -> toJSON $ resolverName x
730-
ResolverCompiler{} -> toJSON $ resolverName x
731-
ResolverCustom n l -> handleCustom n l
732-
ResolverCustomLoaded n l _ -> handleCustom n l
733-
where
734-
handleCustom n l = object
735-
[ "name" .= n
736-
, "location" .= l
737-
]
738-
instance FromJSON (WithJSONWarnings (ResolverThat's 'NotLoaded)) where
739-
-- Strange structuring is to give consistent error messages
740-
parseJSON v@(Object _) = withObjectWarnings "Resolver" (\o -> ResolverCustom
741-
<$> o ..: "name"
742-
<*> o ..: "location") v
743-
744-
parseJSON (String t) = either (fail . show) return (noJSONWarnings <$> parseResolverText t)
745-
746-
parseJSON _ = fail "Invalid Resolver, must be Object or String"
747-
748-
-- | Convert a Resolver into its @Text@ representation, as will be used by
749-
-- directory names
750-
resolverDirName :: LoadedResolver -> Text
751-
resolverDirName (ResolverSnapshot name) = renderSnapName name
752-
resolverDirName (ResolverCompiler v) = compilerVersionText v
753-
resolverDirName (ResolverCustomLoaded name _ hash) = "custom-" <> name <> "-" <> decodeUtf8 (trimmedSnapshotHash hash)
754-
755-
-- | Convert a Resolver into its @Text@ representation for human
756-
-- presentation.
757-
resolverName :: ResolverThat's l -> Text
758-
resolverName (ResolverSnapshot name) = renderSnapName name
759-
resolverName (ResolverCompiler v) = compilerVersionText v
760-
resolverName (ResolverCustom name _) = "custom-" <> name
761-
resolverName (ResolverCustomLoaded name _ _) = "custom-" <> name
762-
763-
customResolverHash :: LoadedResolver-> Maybe SnapshotHash
764-
customResolverHash (ResolverCustomLoaded _ _ hash) = Just hash
765-
customResolverHash _ = Nothing
766-
767-
-- | Try to parse a @Resolver@ from a @Text@. Won't work for complex resolvers (like custom).
768-
parseResolverText :: MonadThrow m => Text -> m Resolver
769-
parseResolverText t
770-
| Right x <- parseSnapName t = return $ ResolverSnapshot x
771-
| Just v <- parseCompilerVersion t = return $ ResolverCompiler v
772-
| otherwise = throwM $ ParseResolverException t
773-
774-
toResolverNotLoaded :: LoadedResolver -> Resolver
775-
toResolverNotLoaded r = case r of
776-
ResolverSnapshot s -> ResolverSnapshot s
777-
ResolverCompiler v -> ResolverCompiler v
778-
ResolverCustomLoaded n l _ -> ResolverCustom n l
779-
780662
-- | Class for environment values which have access to the stack root
781663
class HasStackRoot env where
782664
getStackRoot :: env -> Path Abs Dir

src/Stack/Types/Config.hs-boot

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE ExistentialQuantification #-}
2+
3+
module Stack.Types.Config where
4+
5+
import Control.Exception
6+
import Data.List.NonEmpty (NonEmpty)
7+
import Distribution.Version
8+
import Data.Text (Text)
9+
import Data.Typeable
10+
import Data.Yaml (ParseException)
11+
import Path
12+
import Stack.Types.BuildPlan (SnapName)
13+
import {-# SOURCE #-} Stack.Types.Resolver (Resolver, ResolverThat's)
14+
15+
data WhichSolverCmd
16+
17+
data ConfigException
18+
= ParseConfigFileException (Path Abs File) ParseException
19+
| ParseCustomSnapshotException Text ParseException
20+
| ParseResolverException Text
21+
| NoProjectConfigFound (Path Abs Dir) (Maybe Text)
22+
| UnexpectedArchiveContents [Path Abs Dir] [Path Abs File]
23+
| UnableToExtractArchive Text (Path Abs File)
24+
| BadStackVersionException VersionRange
25+
| NoMatchingSnapshot WhichSolverCmd (NonEmpty SnapName)
26+
| forall l. ResolverMismatch WhichSolverCmd (ResolverThat's l) String
27+
| ResolverPartial WhichSolverCmd Resolver String
28+
| NoSuchDirectory FilePath
29+
| ParseGHCVariantException String
30+
| BadStackRoot (Path Abs Dir)
31+
| Won'tCreateStackRootInDirectoryOwnedByDifferentUser (Path Abs Dir) (Path Abs Dir) -- ^ @$STACK_ROOT@, parent dir
32+
| UserDoesn'tOwnDirectory (Path Abs Dir)
33+
| FailedToCloneRepo String
34+
| ManualGHCVariantSettingsAreIncompatibleWithSystemGHC
35+
| NixRequiresSystemGhc
36+
deriving Typeable
37+
instance Exception ConfigException

0 commit comments

Comments
 (0)