{-# LANGUAGE NoImplicitPrelude #-}
module Stack.Types.SourceMap
(
SMWanted (..)
, SMActual (..)
, Target (..)
, PackageType (..)
, SMTargets (..)
, SourceMap (..)
, FromSnapshot (..)
, DepPackage (..)
, ProjectPackage (..)
, ppComponents
, ppComponentsMaybe
, ppGPD
, ppRoot
, ppVersion
, CommonPackage (..)
, GlobalPackageVersion (..)
, GlobalPackage (..)
, isReplacedGlobal
, SourceMapHash (..)
, smRelDir
) where
import qualified Data.Set as Set
import qualified Data.Text as T
import Distribution.PackageDescription ( GenericPackageDescription )
import qualified Distribution.PackageDescription as C
import qualified Pantry.SHA256 as SHA256
import Path ( parent, parseRelDir )
import Stack.Prelude
import Stack.Types.Compiler ( ActualCompiler )
import Stack.Types.NamedComponent ( NamedComponent (..) )
data CommonPackage = CommonPackage
{ CommonPackage -> IO GenericPackageDescription
cpGPD :: !(IO GenericPackageDescription)
, CommonPackage -> PackageName
cpName :: !PackageName
, CommonPackage -> Map FlagName Bool
cpFlags :: !(Map FlagName Bool)
, CommonPackage -> [Text]
cpGhcOptions :: ![Text]
, CommonPackage -> [Text]
cpCabalConfigOpts :: ![Text]
, CommonPackage -> Bool
cpHaddocks :: !Bool
}
data FromSnapshot
= FromSnapshot
| NotFromSnapshot
deriving Int -> FromSnapshot -> ShowS
[FromSnapshot] -> ShowS
FromSnapshot -> String
(Int -> FromSnapshot -> ShowS)
-> (FromSnapshot -> String)
-> ([FromSnapshot] -> ShowS)
-> Show FromSnapshot
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FromSnapshot -> ShowS
showsPrec :: Int -> FromSnapshot -> ShowS
$cshow :: FromSnapshot -> String
show :: FromSnapshot -> String
$cshowList :: [FromSnapshot] -> ShowS
showList :: [FromSnapshot] -> ShowS
Show
data DepPackage = DepPackage
{ DepPackage -> CommonPackage
dpCommon :: !CommonPackage
, DepPackage -> PackageLocation
dpLocation :: !PackageLocation
, DepPackage -> Bool
dpHidden :: !Bool
, DepPackage -> FromSnapshot
dpFromSnapshot :: !FromSnapshot
}
data ProjectPackage = ProjectPackage
{ ProjectPackage -> CommonPackage
ppCommon :: !CommonPackage
, ProjectPackage -> Path Abs File
ppCabalFP :: !(Path Abs File)
, ProjectPackage -> ResolvedPath Dir
ppResolvedDir :: !(ResolvedPath Dir)
}
data GlobalPackage
= GlobalPackage !Version
| ReplacedGlobalPackage ![PackageName]
deriving GlobalPackage -> GlobalPackage -> Bool
(GlobalPackage -> GlobalPackage -> Bool)
-> (GlobalPackage -> GlobalPackage -> Bool) -> Eq GlobalPackage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: GlobalPackage -> GlobalPackage -> Bool
== :: GlobalPackage -> GlobalPackage -> Bool
$c/= :: GlobalPackage -> GlobalPackage -> Bool
/= :: GlobalPackage -> GlobalPackage -> Bool
Eq
isReplacedGlobal :: GlobalPackage -> Bool
isReplacedGlobal :: GlobalPackage -> Bool
isReplacedGlobal (ReplacedGlobalPackage [PackageName]
_) = Bool
True
isReplacedGlobal (GlobalPackage Version
_) = Bool
False
data SMWanted = SMWanted
{ SMWanted -> WantedCompiler
smwCompiler :: !WantedCompiler
, SMWanted -> Map PackageName ProjectPackage
smwProject :: !(Map PackageName ProjectPackage)
, SMWanted -> Map PackageName DepPackage
smwDeps :: !(Map PackageName DepPackage)
, SMWanted -> RawSnapshotLocation
smwSnapshotLocation :: !RawSnapshotLocation
}
data SMActual global = SMActual
{ forall global. SMActual global -> ActualCompiler
smaCompiler :: !ActualCompiler
, forall global. SMActual global -> Map PackageName ProjectPackage
smaProject :: !(Map PackageName ProjectPackage)
, forall global. SMActual global -> Map PackageName DepPackage
smaDeps :: !(Map PackageName DepPackage)
, forall global. SMActual global -> Map PackageName global
smaGlobal :: !(Map PackageName global)
}
newtype GlobalPackageVersion
= GlobalPackageVersion Version
data Target
= TargetAll !PackageType
| TargetComps !(Set NamedComponent)
data PackageType = PTProject | PTDependency
deriving (PackageType -> PackageType -> Bool
(PackageType -> PackageType -> Bool)
-> (PackageType -> PackageType -> Bool) -> Eq PackageType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PackageType -> PackageType -> Bool
== :: PackageType -> PackageType -> Bool
$c/= :: PackageType -> PackageType -> Bool
/= :: PackageType -> PackageType -> Bool
Eq, Int -> PackageType -> ShowS
[PackageType] -> ShowS
PackageType -> String
(Int -> PackageType -> ShowS)
-> (PackageType -> String)
-> ([PackageType] -> ShowS)
-> Show PackageType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PackageType -> ShowS
showsPrec :: Int -> PackageType -> ShowS
$cshow :: PackageType -> String
show :: PackageType -> String
$cshowList :: [PackageType] -> ShowS
showList :: [PackageType] -> ShowS
Show)
data SMTargets = SMTargets
{ SMTargets -> Map PackageName Target
smtTargets :: !(Map PackageName Target)
, SMTargets -> Map PackageName DepPackage
smtDeps :: !(Map PackageName DepPackage)
}
data SourceMap = SourceMap
{ SourceMap -> SMTargets
smTargets :: !SMTargets
, SourceMap -> ActualCompiler
smCompiler :: !ActualCompiler
, SourceMap -> Map PackageName ProjectPackage
smProject :: !(Map PackageName ProjectPackage)
, SourceMap -> Map PackageName DepPackage
smDeps :: !(Map PackageName DepPackage)
, SourceMap -> Map PackageName GlobalPackage
smGlobal :: !(Map PackageName GlobalPackage)
}
newtype SourceMapHash
= SourceMapHash SHA256
smRelDir :: (MonadThrow m) => SourceMapHash -> m (Path Rel Dir)
smRelDir :: forall (m :: * -> *).
MonadThrow m =>
SourceMapHash -> m (Path Rel Dir)
smRelDir (SourceMapHash SHA256
smh) = String -> m (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => String -> m (Path Rel Dir)
parseRelDir (String -> m (Path Rel Dir)) -> String -> m (Path Rel Dir)
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ SHA256 -> Text
SHA256.toHexText SHA256
smh
ppGPD :: MonadIO m => ProjectPackage -> m GenericPackageDescription
ppGPD :: forall (m :: * -> *).
MonadIO m =>
ProjectPackage -> m GenericPackageDescription
ppGPD = IO GenericPackageDescription -> m GenericPackageDescription
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO GenericPackageDescription -> m GenericPackageDescription)
-> (ProjectPackage -> IO GenericPackageDescription)
-> ProjectPackage
-> m GenericPackageDescription
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommonPackage -> IO GenericPackageDescription
cpGPD (CommonPackage -> IO GenericPackageDescription)
-> (ProjectPackage -> CommonPackage)
-> ProjectPackage
-> IO GenericPackageDescription
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProjectPackage -> CommonPackage
ppCommon
ppRoot :: ProjectPackage -> Path Abs Dir
ppRoot :: ProjectPackage -> Path Abs Dir
ppRoot = Path Abs File -> Path Abs Dir
forall b t. Path b t -> Path b Dir
parent (Path Abs File -> Path Abs Dir)
-> (ProjectPackage -> Path Abs File)
-> ProjectPackage
-> Path Abs Dir
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProjectPackage -> Path Abs File
ppCabalFP
ppComponents :: MonadIO m => ProjectPackage -> m (Set NamedComponent)
ppComponents :: forall (m :: * -> *).
MonadIO m =>
ProjectPackage -> m (Set NamedComponent)
ppComponents = (NamedComponent -> Maybe NamedComponent)
-> ProjectPackage -> m (Set NamedComponent)
forall (m :: * -> *).
MonadIO m =>
(NamedComponent -> Maybe NamedComponent)
-> ProjectPackage -> m (Set NamedComponent)
ppComponentsMaybe NamedComponent -> Maybe NamedComponent
forall a. a -> Maybe a
Just
ppComponentsMaybe ::
MonadIO m
=> (NamedComponent -> Maybe NamedComponent)
-> ProjectPackage
-> m (Set NamedComponent)
ppComponentsMaybe :: forall (m :: * -> *).
MonadIO m =>
(NamedComponent -> Maybe NamedComponent)
-> ProjectPackage -> m (Set NamedComponent)
ppComponentsMaybe NamedComponent -> Maybe NamedComponent
compType ProjectPackage
pp = do
GenericPackageDescription
gpd <- ProjectPackage -> m GenericPackageDescription
forall (m :: * -> *).
MonadIO m =>
ProjectPackage -> m GenericPackageDescription
ppGPD ProjectPackage
pp
Set NamedComponent -> m (Set NamedComponent)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Set NamedComponent -> m (Set NamedComponent))
-> Set NamedComponent -> m (Set NamedComponent)
forall a b. (a -> b) -> a -> b
$ [NamedComponent] -> Set NamedComponent
forall a. Ord a => [a] -> Set a
Set.fromList ([NamedComponent] -> Set NamedComponent)
-> [NamedComponent] -> Set NamedComponent
forall a b. (a -> b) -> a -> b
$ [[NamedComponent]] -> [NamedComponent]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [NamedComponent]
-> (CondTree ConfVar [Dependency] Library -> [NamedComponent])
-> Maybe (CondTree ConfVar [Dependency] Library)
-> [NamedComponent]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ([NamedComponent]
-> CondTree ConfVar [Dependency] Library -> [NamedComponent]
forall a b. a -> b -> a
const ([NamedComponent]
-> CondTree ConfVar [Dependency] Library -> [NamedComponent])
-> [NamedComponent]
-> CondTree ConfVar [Dependency] Library
-> [NamedComponent]
forall a b. (a -> b) -> a -> b
$ [Maybe NamedComponent] -> [NamedComponent]
forall a. [Maybe a] -> [a]
catMaybes [NamedComponent -> Maybe NamedComponent
compType NamedComponent
CLib]) (GenericPackageDescription
-> Maybe (CondTree ConfVar [Dependency] Library)
C.condLibrary GenericPackageDescription
gpd)
, (Text -> Maybe NamedComponent)
-> [UnqualComponentName] -> [NamedComponent]
go (NamedComponent -> Maybe NamedComponent
compType (NamedComponent -> Maybe NamedComponent)
-> (Text -> NamedComponent) -> Text -> Maybe NamedComponent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NamedComponent
CExe) ((UnqualComponentName, CondTree ConfVar [Dependency] Executable)
-> UnqualComponentName
forall a b. (a, b) -> a
fst ((UnqualComponentName, CondTree ConfVar [Dependency] Executable)
-> UnqualComponentName)
-> [(UnqualComponentName,
CondTree ConfVar [Dependency] Executable)]
-> [UnqualComponentName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericPackageDescription
-> [(UnqualComponentName,
CondTree ConfVar [Dependency] Executable)]
C.condExecutables GenericPackageDescription
gpd)
, (Text -> Maybe NamedComponent)
-> [UnqualComponentName] -> [NamedComponent]
go (NamedComponent -> Maybe NamedComponent
compType (NamedComponent -> Maybe NamedComponent)
-> (Text -> NamedComponent) -> Text -> Maybe NamedComponent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NamedComponent
CTest) ((UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)
-> UnqualComponentName
forall a b. (a, b) -> a
fst ((UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)
-> UnqualComponentName)
-> [(UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)]
-> [UnqualComponentName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericPackageDescription
-> [(UnqualComponentName, CondTree ConfVar [Dependency] TestSuite)]
C.condTestSuites GenericPackageDescription
gpd)
, (Text -> Maybe NamedComponent)
-> [UnqualComponentName] -> [NamedComponent]
go (NamedComponent -> Maybe NamedComponent
compType (NamedComponent -> Maybe NamedComponent)
-> (Text -> NamedComponent) -> Text -> Maybe NamedComponent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> NamedComponent
CBench) ((UnqualComponentName, CondTree ConfVar [Dependency] Benchmark)
-> UnqualComponentName
forall a b. (a, b) -> a
fst ((UnqualComponentName, CondTree ConfVar [Dependency] Benchmark)
-> UnqualComponentName)
-> [(UnqualComponentName, CondTree ConfVar [Dependency] Benchmark)]
-> [UnqualComponentName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericPackageDescription
-> [(UnqualComponentName, CondTree ConfVar [Dependency] Benchmark)]
C.condBenchmarks GenericPackageDescription
gpd)
]
where
go ::
(T.Text -> Maybe NamedComponent)
-> [C.UnqualComponentName]
-> [NamedComponent]
go :: (Text -> Maybe NamedComponent)
-> [UnqualComponentName] -> [NamedComponent]
go Text -> Maybe NamedComponent
wrapper = (UnqualComponentName -> Maybe NamedComponent)
-> [UnqualComponentName] -> [NamedComponent]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Text -> Maybe NamedComponent
wrapper (Text -> Maybe NamedComponent)
-> (UnqualComponentName -> Text)
-> UnqualComponentName
-> Maybe NamedComponent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text)
-> (UnqualComponentName -> String) -> UnqualComponentName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnqualComponentName -> String
C.unUnqualComponentName)
ppVersion :: MonadIO m => ProjectPackage -> m Version
ppVersion :: forall (m :: * -> *). MonadIO m => ProjectPackage -> m Version
ppVersion = (GenericPackageDescription -> Version)
-> m GenericPackageDescription -> m Version
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GenericPackageDescription -> Version
gpdVersion (m GenericPackageDescription -> m Version)
-> (ProjectPackage -> m GenericPackageDescription)
-> ProjectPackage
-> m Version
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProjectPackage -> m GenericPackageDescription
forall (m :: * -> *).
MonadIO m =>
ProjectPackage -> m GenericPackageDescription
ppGPD