Skip to content

Commit 3dd464d

Browse files
committed
refactoring: put installed stuff into a dedicated types package
1 parent 53090b1 commit 3dd464d

File tree

12 files changed

+199
-161
lines changed

12 files changed

+199
-161
lines changed

.stan.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,25 @@
5454

5555
# Anti-pattern: Data.ByteString.Char8.pack
5656
[[ignore]]
57-
id = "OBS-STAN-0203-fki0nd-1128:21"
57+
id = "OBS-STAN-0203-fki0nd-1129:21"
5858
# ✦ Description: Usage of 'pack' function that doesn't handle Unicode characters
5959
# ✦ Category: #AntiPattern
6060
# ✦ File: src\Stack\Build\Execute.hs
6161
#
62-
# 1127
63-
# 1128 ┃ newProjectRoot <- S8.pack . toFilePath <$> view projectRootL
64-
# 1129 ┃ ^^^^^^^
62+
# 1128
63+
# 1129 ┃ newProjectRoot <- S8.pack . toFilePath <$> view projectRootL
64+
# 1130 ┃ ^^^^^^^
6565

6666
# Anti-pattern: Data.ByteString.Char8.pack
6767
[[ignore]]
68-
id = "OBS-STAN-0203-fki0nd-2672:3"
68+
id = "OBS-STAN-0203-fki0nd-2673:3"
6969
# ✦ Description: Usage of 'pack' function that doesn't handle Unicode characters
7070
# ✦ Category: #AntiPattern
7171
# ✦ File: src\Stack\Build\Execute.hs
7272
#
73-
# 2671
74-
# 2672 ┃ S8.pack . formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S%6Q"
75-
# 2673 ┃ ^^^^^^^
73+
# 2672
74+
# 2673 ┃ S8.pack . formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S%6Q"
75+
# 2674 ┃ ^^^^^^^
7676

7777
# Anti-pattern: Data.ByteString.Char8.pack
7878
[[ignore]]

src/Stack/Build/Cache.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ import Stack.Types.EnvConfig
7171
, platformGhcRelDir
7272
)
7373
import Stack.Types.GhcPkgId ( GhcPkgId, ghcPkgIdString )
74-
import Stack.Types.NamedComponent ( NamedComponent (..) )
75-
import Stack.Types.Package
74+
import Stack.Types.Installed
7675
(InstalledLibraryInfo (..), installedGhcPkgId )
76+
import Stack.Types.NamedComponent ( NamedComponent (..) )
7777
import Stack.Types.SourceMap ( smRelDir )
7878
import System.PosixCompat.Files
7979
( modificationTime, getFileStatus, setFileTimes )

src/Stack/Build/ConstructPlan.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ import Stack.Types.EnvSettings
6666
( EnvSettings (..), minimalEnvSettings )
6767
import Stack.Types.GhcPkgId ( GhcPkgId )
6868
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
69+
import Stack.Types.Installed
70+
( InstallLocation (..), Installed (..), InstalledMap
71+
, installedVersion )
6972
import Stack.Types.IsMutable ( IsMutable (..) )
7073
import Stack.Types.NamedComponent ( exeComponents, renderComponent )
7174
import Stack.Types.Package
72-
( ExeName (..), InstallLocation (..), Installed (..)
73-
, InstalledMap, LocalPackage (..), Package (..)
74-
, PackageSource (..), installedMapGhcPkgId, installedVersion
75+
( ExeName (..), LocalPackage (..), Package (..)
76+
, PackageSource (..), installedMapGhcPkgId
7577
, packageIdentifier, psVersion, runMemoizedWith
7678
)
7779
import Stack.Types.ProjectConfig ( isPCGlobalProject )

src/Stack/Build/Execute.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,17 @@ import Stack.Types.EnvConfig
178178
import Stack.Types.EnvSettings ( EnvSettings (..) )
179179
import Stack.Types.GhcPkgId ( GhcPkgId, ghcPkgIdString, unGhcPkgId )
180180
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
181+
import Stack.Types.Installed
182+
( InstallLocation (..), Installed (..), InstalledMap
183+
, InstalledLibraryInfo (..), installedPackageIdentifier )
181184
import Stack.Types.IsMutable ( IsMutable (..) )
182185
import Stack.Types.NamedComponent
183186
( NamedComponent, benchComponents, exeComponents, isCBench
184187
, isCTest, renderComponent, testComponents
185188
)
186189
import Stack.Types.Package
187-
( InstallLocation (..), Installed (..)
188-
, InstalledLibraryInfo (..), InstalledMap, LocalPackage (..)
189-
, Package (..), installedMapGhcPkgId
190-
, installedPackageIdentifier, packageIdentifier
190+
( LocalPackage (..), Package (..)
191+
, installedMapGhcPkgId, packageIdentifier
191192
, runMemoizedWith, simpleInstalledLib
192193
, toCabalMungedPackageName
193194
)

src/Stack/Build/Installed.hs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ import Stack.Types.EnvConfig
2929
, packageDatabaseLocal
3030
)
3131
import Stack.Types.GhcPkgId ( GhcPkgId )
32-
import Stack.Types.Package
33-
( InstallLocation (..), InstallMap, Installed (..)
34-
, InstalledLibraryInfo (..), InstalledMap
35-
, InstalledPackageLocation (..), PackageDatabase (..)
36-
, PackageDbVariety (..), toPackageDbVariety
37-
)
32+
import Stack.Types.Installed
33+
( InstallLocation (..), Installed (..), InstalledLibraryInfo (..)
34+
, InstallMap, InstalledMap, InstalledPackageLocation (..)
35+
, PackageDatabase (..), PackageDbVariety (..), toPackageDbVariety )
3836
import Stack.Types.SourceMap
3937
( DepPackage (..), ProjectPackage (..), SourceMap (..) )
4038

src/Stack/Ghci.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ import Stack.Types.EnvConfig
6868
, shaPathForBytes
6969
)
7070
import Stack.Types.EnvSettings ( defaultEnvSettings )
71+
import Stack.Types.Installed ( InstallMap, InstalledMap)
7172
import Stack.Types.NamedComponent
7273
( NamedComponent (..), isCLib, renderPkgComponent )
7374
import Stack.Types.Package
74-
( BuildInfoOpts (..), InstallMap, InstalledMap
75-
, LocalPackage (..), Package (..), PackageConfig (..)
76-
, dotCabalCFilePath, dotCabalGetPath, dotCabalMainPath
75+
( BuildInfoOpts (..), LocalPackage (..), Package (..)
76+
, PackageConfig (..), dotCabalCFilePath, dotCabalGetPath
77+
, dotCabalMainPath
7778
)
7879
import Stack.Types.PackageFile ( PackageComponentFile (..) )
7980
import Stack.Types.Platform ( HasPlatform (..) )

src/Stack/Package.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ import Stack.Types.Dependency
9393
, libraryDepFromVersionRange
9494
)
9595
import Stack.Types.EnvConfig ( HasEnvConfig )
96+
97+
import Stack.Types.Installed
98+
( Installed (..), InstallMap, InstalledMap
99+
, installedToPackageIdOpt )
96100
import Stack.Types.NamedComponent
97101
( NamedComponent (..), subLibComponents )
98102
import Stack.Types.Package
99-
( BioInput(..), BuildInfoOpts (..), InstallMap
100-
, Installed (..), InstalledMap, Package (..)
103+
( BioInput(..), BuildInfoOpts (..), Package (..)
101104
, PackageConfig (..), PackageException (..)
102-
, dotCabalCFilePath, installedToPackageIdOpt
103-
, packageIdentifier
105+
, dotCabalCFilePath, packageIdentifier
104106
)
105107
import Stack.Types.PackageFile
106108
( DotCabalPath, PackageComponentFile (..) )
@@ -293,7 +295,7 @@ generateBuildInfoOpts BioInput {..} =
293295
deps =
294296
concat
295297
[ case M.lookup name biInstalledMap of
296-
Just (_, Stack.Types.Package.Library _ident installedInfo) ->
298+
Just (_, Stack.Types.Installed.Library _ident installedInfo) ->
297299
installedToPackageIdOpt installedInfo
298300
_ -> ["-package=" <> packageNameString name <>
299301
maybe "" -- This empty case applies to e.g. base.

src/Stack/SDist.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ import Stack.Types.Config ( Config (..), HasConfig (..) )
6969
import Stack.Types.EnvConfig
7070
( EnvConfig (..), HasEnvConfig (..), actualCompilerVersionL )
7171
import Stack.Types.GhcPkgId ( GhcPkgId )
72+
import Stack.Types.Installed
73+
( InstallMap, Installed (..), InstalledMap
74+
, InstalledLibraryInfo (..), installedVersion
75+
)
7276
import Stack.Types.Package
73-
( InstallMap, Installed (..), InstalledLibraryInfo (..)
74-
, InstalledMap, LocalPackage (..), Package (..)
75-
, PackageConfig (..), installedVersion, packageIdentifier
77+
( LocalPackage (..), Package (..)
78+
, PackageConfig (..), packageIdentifier
7679
)
7780
import Stack.Types.Platform ( HasPlatform (..) )
7881
import Stack.Types.PvpBounds ( PvpBounds (..), PvpBoundsType (..) )

src/Stack/Types/Build/ConstructPlan.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ import Stack.Types.EnvConfig
3636
( EnvConfig (..), HasEnvConfig (..), HasSourceMap (..) )
3737
import Stack.Types.GhcPkgId ( GhcPkgId )
3838
import Stack.Types.GHCVariant ( HasGHCVariant (..) )
39+
import Stack.Types.Installed
40+
( InstallLocation, Installed (..)
41+
, installedVersion)
3942
import Stack.Types.Package
40-
( ExeName (..), InstallLocation, Installed (..)
41-
, LocalPackage (..), Package (..), PackageSource (..)
42-
, installedVersion
43+
( ExeName (..), LocalPackage (..), Package (..)
44+
, PackageSource (..)
4345
)
4446
import Stack.Types.ParentMap ( ParentMap )
4547
import Stack.Types.Platform ( HasPlatform (..) )

src/Stack/Types/Installed.hs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
-- | This module contains all the types related to the idea of installing a package
4+
-- in the pkg-db or an executable on the file system.
5+
module Stack.Types.Installed
6+
( InstallLocation (..)
7+
, InstalledPackageLocation (..)
8+
, PackageDatabase (..)
9+
, PackageDbVariety (..)
10+
, InstallMap
11+
, Installed (..)
12+
, InstalledMap
13+
, InstalledLibraryInfo (..)
14+
, toPackageDbVariety
15+
, installedLibraryInfoFromGhcPkgId
16+
, simpleInstalledLib
17+
, installedToPackageIdOpt
18+
, installedPackageIdentifier
19+
, installedGhcPkgId
20+
, installedVersion
21+
) where
22+
23+
import qualified Distribution.SPDX.License as SPDX
24+
import Distribution.License (License)
25+
import Stack.Prelude
26+
import Stack.Types.ComponentUtils (StackUnqualCompName)
27+
import Stack.Types.GhcPkgId (GhcPkgId, ghcPkgIdString)
28+
import qualified Data.Map as M
29+
30+
-- | Type representing user package databases that packages can be installed
31+
-- into.
32+
data InstallLocation
33+
= Snap
34+
-- ^ The write-only package database, formerly known as the snapshot
35+
-- database.
36+
| Local
37+
-- ^ The mutable package database, formerly known as the local database.
38+
deriving (Eq, Show)
39+
40+
instance Semigroup InstallLocation where
41+
Local <> _ = Local
42+
_ <> Local = Local
43+
Snap <> Snap = Snap
44+
45+
instance Monoid InstallLocation where
46+
mempty = Snap
47+
mappend = (<>)
48+
49+
-- | Type representing user (non-global) package databases that can provide
50+
-- installed packages.
51+
data InstalledPackageLocation
52+
= InstalledTo InstallLocation
53+
-- ^ A package database that a package can be installed into.
54+
| ExtraPkgDb
55+
-- ^ An \'extra\' package database, specified by @extra-package-dbs@.
56+
deriving (Eq, Show)
57+
58+
-- | Type representing package databases that can provide installed packages.
59+
data PackageDatabase
60+
= GlobalPkgDb
61+
-- ^ GHC's global package database.
62+
| UserPkgDb InstalledPackageLocation (Path Abs Dir)
63+
-- ^ A user package database.
64+
deriving (Eq, Show)
65+
66+
-- | A function to yield the variety of package database for a given
67+
-- package database that can provide installed packages.
68+
toPackageDbVariety :: PackageDatabase -> PackageDbVariety
69+
toPackageDbVariety GlobalPkgDb = GlobalDb
70+
toPackageDbVariety (UserPkgDb ExtraPkgDb _) = ExtraDb
71+
toPackageDbVariety (UserPkgDb (InstalledTo Snap) _) = WriteOnlyDb
72+
toPackageDbVariety (UserPkgDb (InstalledTo Local) _) = MutableDb
73+
74+
-- | Type representing varieties of package databases that can provide
75+
-- installed packages.
76+
data PackageDbVariety
77+
= GlobalDb
78+
-- ^ GHC's global package database.
79+
| ExtraDb
80+
-- ^ An \'extra\' package database, specified by @extra-package-dbs@.
81+
| WriteOnlyDb
82+
-- ^ The write-only package database, for immutable packages.
83+
| MutableDb
84+
-- ^ The mutable package database.
85+
deriving (Eq, Show)
86+
87+
-- | Type synonym representing dictionaries of package names for a project's
88+
-- packages and dependencies, and pairs of their relevant database (write-only
89+
-- or mutable) and package versions.
90+
type InstallMap = Map PackageName (InstallLocation, Version)
91+
92+
-- | Type synonym representing dictionaries of package names, and a pair of in
93+
-- which package database the package is installed (write-only or mutable) and
94+
-- information about what is installed.
95+
type InstalledMap = Map PackageName (InstallLocation, Installed)
96+
97+
data InstalledLibraryInfo = InstalledLibraryInfo
98+
{ iliId :: GhcPkgId
99+
, iliLicense :: Maybe (Either SPDX.License License)
100+
, iliSublib :: Map StackUnqualCompName GhcPkgId
101+
}
102+
deriving (Eq, Show)
103+
104+
-- | Type representing information about what is installed.
105+
data Installed
106+
= Library PackageIdentifier InstalledLibraryInfo
107+
-- ^ A library, including its installed package id and, optionally, its
108+
-- license.
109+
| Executable PackageIdentifier
110+
-- ^ An executable.
111+
deriving (Eq, Show)
112+
113+
installedLibraryInfoFromGhcPkgId :: GhcPkgId -> InstalledLibraryInfo
114+
installedLibraryInfoFromGhcPkgId ghcPkgId =
115+
InstalledLibraryInfo ghcPkgId Nothing mempty
116+
117+
simpleInstalledLib ::
118+
PackageIdentifier
119+
-> GhcPkgId
120+
-> Map StackUnqualCompName GhcPkgId
121+
-> Installed
122+
simpleInstalledLib pkgIdentifier ghcPkgId =
123+
Library pkgIdentifier . InstalledLibraryInfo ghcPkgId Nothing
124+
125+
installedToPackageIdOpt :: InstalledLibraryInfo -> [String]
126+
installedToPackageIdOpt libInfo =
127+
M.foldr' (iterator (++)) (pure $ toStr (iliId libInfo)) (iliSublib libInfo)
128+
where
129+
toStr ghcPkgId = "-package-id=" <> ghcPkgIdString ghcPkgId
130+
iterator op ghcPkgId acc = pure (toStr ghcPkgId) `op` acc
131+
132+
installedPackageIdentifier :: Installed -> PackageIdentifier
133+
installedPackageIdentifier (Library pid _) = pid
134+
installedPackageIdentifier (Executable pid) = pid
135+
136+
installedGhcPkgId :: Installed -> Maybe GhcPkgId
137+
installedGhcPkgId (Library _ libInfo) = Just $ iliId libInfo
138+
installedGhcPkgId (Executable _) = Nothing
139+
140+
-- | Get the installed Version.
141+
installedVersion :: Installed -> Version
142+
installedVersion i =
143+
let PackageIdentifier _ version = installedPackageIdentifier i
144+
in version

0 commit comments

Comments
 (0)