Skip to content

Commit

Permalink
Re #2407 move ProjectPackage helpers out of Stack.Types.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem committed Apr 19, 2023
1 parent a0fc868 commit 589fd5d
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 78 deletions.
11 changes: 5 additions & 6 deletions src/Stack/Build/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ import Stack.Types.CabalConfigKey ( CabalConfigKey (..) )
import Stack.Types.CompilerPaths ( HasCompiler, getCompilerPath )
import Stack.Types.Config
( ApplyCLIFlag (..), BuildOpts (..), BuildOptsCLI (..)
, Config (..), Curator (..), HasConfig (..)
, ProjectPackage (..), TestOpts (..), buildOptsL, ppGPD
, ppRoot
, Config (..), Curator (..), HasConfig (..), TestOpts (..)
, buildOptsL
)
import Stack.Types.EnvConfig
( EnvConfig (..), HasEnvConfig (..), HasSourceMap (..)
Expand All @@ -58,9 +57,9 @@ import Stack.Types.Package
import Stack.Types.PackageFile ( PackageWarning, getPackageFiles )
import Stack.Types.Platform ( HasPlatform (..) )
import Stack.Types.SourceMap
( CommonPackage (..), DepPackage (..), SMActual (..)
, SMTargets (..), SourceMap (..), SourceMapHash (..)
, Target (..)
( CommonPackage (..), DepPackage (..), ProjectPackage (..)
, SMActual (..), SMTargets (..), SourceMap (..)
, SourceMapHash (..), Target (..), ppGPD, ppRoot
)
import System.FilePath ( takeFileName )
import System.IO.Error ( isDoesNotExistError )
Expand Down
6 changes: 2 additions & 4 deletions src/Stack/Build/Target.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,14 @@ import Stack.Prelude
import Stack.Types.BuildConfig
( BuildConfig (..), HasBuildConfig (..) )
import Stack.Types.Config
( BuildOptsCLI (..), Config (..), ProjectConfig (..)
, ppComponents, ppRoot
)
( BuildOptsCLI (..), Config (..), ProjectConfig (..) )
import Stack.Types.NamedComponent
( NamedComponent (..), renderComponent )
import Stack.Types.Build ( BuildPrettyException (..) )
import Stack.Types.SourceMap
( DepPackage (..), GlobalPackage (..), PackageType (..)
, ProjectPackage, SMActual (..), SMTargets (..)
, SMWanted (..), Target (..)
, SMWanted (..), Target (..), ppComponents, ppRoot
)

-- | Do we need any targets? For example, `stack build` will fail if
Expand Down
4 changes: 2 additions & 2 deletions src/Stack/Clean.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import Stack.Prelude
import Stack.Runners ( ShouldReexec (..), withConfig )
import Stack.Types.BuildConfig
( BuildConfig (..), HasBuildConfig (..), getProjectWorkDir )
import Stack.Types.Config ( Config, ppRoot )
import Stack.Types.Config ( Config )
import Stack.Types.Runner ( Runner )
import Stack.Types.SourceMap ( SMWanted (..) )
import Stack.Types.SourceMap ( SMWanted (..), ppRoot )

-- | Type representing exceptions thrown by functions exported by the
-- "Stack.Clean" module.
Expand Down
5 changes: 2 additions & 3 deletions src/Stack/Coverage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ import Stack.Types.BuildConfig
( BuildConfig (..), HasBuildConfig (..) )
import Stack.Types.Compiler ( getGhcVersion )
import Stack.Types.CompilerPaths ( cabalVersionL )
import Stack.Types.Config
( BuildOptsCLI (..), defaultBuildOptsCLI, ppRoot )
import Stack.Types.Config ( BuildOptsCLI (..), defaultBuildOptsCLI )
import Stack.Types.EnvConfig
( EnvConfig (..), HasEnvConfig (..), actualCompilerVersionL
, hpcReportDir
Expand All @@ -58,7 +57,7 @@ import Stack.Types.Package
import Stack.Types.Runner ( Runner )
import Stack.Types.SourceMap
( PackageType (..), SMTargets (..), SMWanted (..)
, SourceMap (..), Target (..)
, SourceMap (..), Target (..), ppRoot
)
import System.FilePath ( isPathSeparator )
import Trace.Hpc.Tix ( Tix (..), TixModule (..), readTix, writeTix )
Expand Down
7 changes: 3 additions & 4 deletions src/Stack/Exec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ import Stack.Types.BuildConfig
import Stack.Types.CompilerPaths
( CompilerPaths (..), HasCompiler (..), getGhcPkgExe )
import Stack.Types.Config
( BuildOptsCLI (..), EnvSettings (..), HasConfig (..)
, configProcessContextSettings, defaultBuildOptsCLI
, ppComponents
( BuildOptsCLI (..), Config (..), EnvSettings (..)
, HasConfig (..), defaultBuildOptsCLI
)
import Stack.Types.EnvConfig ( EnvConfig )
import Stack.Types.Runner ( Runner )
import Stack.Types.SourceMap ( SMWanted (..) )
import Stack.Types.SourceMap ( SMWanted (..), ppComponents )
import System.Directory ( withCurrentDirectory )
import System.FilePath ( isValid )

Expand Down
4 changes: 2 additions & 2 deletions src/Stack/IDE.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import Stack.Runners
( ShouldReexec (..), withBuildConfig, withConfig )
import Stack.Types.BuildConfig
( BuildConfig (..), HasBuildConfig (..) )
import Stack.Types.Config ( ppComponents )
import Stack.Types.NamedComponent
( NamedComponent, renderPkgComponent )
import Stack.Types.Runner ( Runner )
import Stack.Types.SourceMap ( ProjectPackage (..), SMWanted (..) )
import Stack.Types.SourceMap
( ProjectPackage (..), SMWanted (..), ppComponents )
import System.IO ( putStrLn )

-- Type representing output channel choices for the @stack ide packages@ and
Expand Down
4 changes: 2 additions & 2 deletions src/Stack/SDist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import Stack.Types.BuildConfig
( BuildConfig (..), HasBuildConfig (..), stackYamlL )
import Stack.Types.Config
( BuildOpts (..), Config (..), HasConfig (..)
, defaultBuildOpts, defaultBuildOptsCLI, ppRoot
, defaultBuildOpts, defaultBuildOptsCLI
)
import Stack.Types.EnvConfig
( EnvConfig (..), HasEnvConfig (..), actualCompilerVersionL )
Expand All @@ -83,7 +83,7 @@ import Stack.Types.PvpBounds ( PvpBounds (..), PvpBoundsType (..) )
import Stack.Types.Runner ( HasRunner, Runner )
import Stack.Types.SourceMap
( CommonPackage (..), ProjectPackage (..), SMWanted (..)
, SourceMap (..)
, SourceMap (..), ppRoot
)
import Stack.Types.Version
( intersectVersionRanges, nextMajorVersion )
Expand Down
55 changes: 2 additions & 53 deletions src/Stack/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ module Stack.Types.Config
, HasConfig (..)
, askLatestSnapshotUrl
, configProjectRoot
-- ** BuildConfig & HasBuildConfig
, ProjectPackage (..)
, DepPackage (..)
, ppRoot
, ppVersion
, ppComponents
, ppGPD
-- * Details
-- ** EnvSettings
, EnvSettings (..)
Expand Down Expand Up @@ -89,10 +82,7 @@ import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Yaml as Yaml
import Distribution.PackageDescription ( GenericPackageDescription )
import qualified Distribution.PackageDescription as C
import Distribution.System ( Platform )
import qualified Distribution.Text ( display )
import Generics.Deriving.Monoid ( mappenddefault, memptydefault )
import Options.Applicative ( ReadM )
import qualified Options.Applicative.Types as OA
Expand All @@ -116,17 +106,15 @@ import Stack.Types.Docker ( DockerOpts )
import Stack.Types.DumpLogs ( DumpLogs )
import Stack.Types.GHCVariant ( GHCVariant (..), HasGHCVariant (..) )
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
import Stack.Types.NamedComponent ( NamedComponent (..) )
import Stack.Types.Nix ( NixOpts )
import Stack.Types.Platform
( HasPlatform (..), PlatformVariant, platformVariantSuffix )
( HasPlatform (..), PlatformVariant, platformOnlyRelDir
)
import Stack.Types.PvpBounds ( PvpBounds )
import Stack.Types.Resolver ( AbstractResolver )
import Stack.Types.Runner ( HasRunner (..), Runner, globalOptsL )
import Stack.Types.SCM ( SCM )
import Stack.Types.SetupInfo ( SetupInfo )
import Stack.Types.SourceMap
( CommonPackage (..), DepPackage (..), ProjectPackage (..) )
import Stack.Types.Storage ( UserStorage )
import Stack.Types.TemplateName ( TemplateName )
import Stack.Types.Version ( VersionCheck (..), VersionRange )
Expand Down Expand Up @@ -303,33 +291,6 @@ defaultLogLevel = LevelInfo
readStyles :: ReadM StylesUpdate
readStyles = parseStylesUpdateFromString <$> OA.readerAsk

ppGPD :: MonadIO m => ProjectPackage -> m GenericPackageDescription
ppGPD = liftIO . cpGPD . ppCommon

-- | Root directory for the given 'ProjectPackage'
ppRoot :: ProjectPackage -> Path Abs Dir
ppRoot = parent . ppCabalFP

-- | All components available in the given 'ProjectPackage'
ppComponents :: MonadIO m => ProjectPackage -> m (Set NamedComponent)
ppComponents pp = do
gpd <- ppGPD pp
pure $ Set.fromList $ concat
[ maybe [] (const [CLib]) (C.condLibrary gpd)
, go CExe (fst <$> C.condExecutables gpd)
, go CTest (fst <$> C.condTestSuites gpd)
, go CBench (fst <$> C.condBenchmarks gpd)
]
where
go :: (T.Text -> NamedComponent)
-> [C.UnqualComponentName]
-> [NamedComponent]
go wrapper = map (wrapper . T.pack . C.unUnqualComponentName)

-- | Version for the given 'ProjectPackage
ppVersion :: MonadIO m => ProjectPackage -> m Version
ppVersion = fmap gpdVersion . ppGPD

-- | A project is a collection of packages. We can have multiple stack.yaml
-- files, but only one of them may contain project information.
data Project = Project
Expand Down Expand Up @@ -428,18 +389,6 @@ ghcInstallHook = do
hd <- hooksDir
pure (hd </> [relfile|ghc-install.sh|])

-- | Relative directory for the platform identifier
platformOnlyRelDir ::
(MonadReader env m, HasPlatform env, MonadThrow m)
=> m (Path Rel Dir)
platformOnlyRelDir = do
platform <- view platformL
platformVariant <- view platformVariantL
parseRelDir
( Distribution.Text.display platform
++ platformVariantSuffix platformVariant
)

-- | This is an attempt to shorten Stack paths on Windows to decrease our
-- chances of hitting 260 symbol path limit. The idea is to calculate
-- SHA1 hash of the path used on other architectures, encode with base
Expand Down
15 changes: 15 additions & 0 deletions src/Stack/Types/Platform.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ module Stack.Types.Platform
( PlatformVariant (..)
, HasPlatform (..)
, platformVariantSuffix
, platformOnlyRelDir
) where

import Distribution.System ( Platform )
import Distribution.Text ( display )
import Lens.Micro ( _1, _2 )
import Path ( parseRelDir )
import Stack.Prelude

-- | A variant of the platform, used to differentiate Docker builds from host
Expand All @@ -28,3 +31,15 @@ instance HasPlatform (Platform, PlatformVariant) where
platformVariantSuffix :: PlatformVariant -> String
platformVariantSuffix PlatformVariantNone = ""
platformVariantSuffix (PlatformVariant v) = "-" ++ v

-- | Relative directory for the platform identifier
platformOnlyRelDir ::
(MonadReader env m, HasPlatform env, MonadThrow m)
=> m (Path Rel Dir)
platformOnlyRelDir = do
platform <- view platformL
platformVariant <- view platformVariantL
parseRelDir
( Distribution.Text.display platform
++ platformVariantSuffix platformVariant
)
37 changes: 35 additions & 2 deletions src/Stack/Types/SourceMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module Stack.Types.SourceMap
, FromSnapshot (..)
, DepPackage (..)
, ProjectPackage (..)
, ppComponents
, ppGPD
, ppRoot
, ppVersion
, CommonPackage (..)
, GlobalPackageVersion (..)
, GlobalPackage (..)
Expand All @@ -23,13 +27,15 @@ module Stack.Types.SourceMap
, 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 ( parseRelDir )
import Path ( parent, parseRelDir )
import Stack.Prelude
import Stack.Types.Compiler ( ActualCompiler )
import Stack.Types.NamedComponent ( NamedComponent )
import Stack.Types.NamedComponent ( NamedComponent (..) )

-- | Common settings for both dependency and project package.
data CommonPackage = CommonPackage
Expand Down Expand Up @@ -158,3 +164,30 @@ newtype SourceMapHash
-- | Returns relative directory name with source map's hash
smRelDir :: (MonadThrow m) => SourceMapHash -> m (Path Rel Dir)
smRelDir (SourceMapHash smh) = parseRelDir $ T.unpack $ SHA256.toHexText smh

ppGPD :: MonadIO m => ProjectPackage -> m GenericPackageDescription
ppGPD = liftIO . cpGPD . ppCommon

-- | Root directory for the given 'ProjectPackage'
ppRoot :: ProjectPackage -> Path Abs Dir
ppRoot = parent . ppCabalFP

-- | All components available in the given 'ProjectPackage'
ppComponents :: MonadIO m => ProjectPackage -> m (Set NamedComponent)
ppComponents pp = do
gpd <- ppGPD pp
pure $ Set.fromList $ concat
[ maybe [] (const [CLib]) (C.condLibrary gpd)
, go CExe (fst <$> C.condExecutables gpd)
, go CTest (fst <$> C.condTestSuites gpd)
, go CBench (fst <$> C.condBenchmarks gpd)
]
where
go :: (T.Text -> NamedComponent)
-> [C.UnqualComponentName]
-> [NamedComponent]
go wrapper = map (wrapper . T.pack . C.unUnqualComponentName)

-- | Version for the given 'ProjectPackage
ppVersion :: MonadIO m => ProjectPackage -> m Version
ppVersion = fmap gpdVersion . ppGPD

0 comments on commit 589fd5d

Please sign in to comment.