Skip to content

Commit 1b1dba9

Browse files
authored
Merge pull request #6789 from commercialhaskell/config-cache-docs
Improve Haddock and code documentation
2 parents d760fb2 + 505dce3 commit 1b1dba9

File tree

7 files changed

+115
-41
lines changed

7 files changed

+115
-41
lines changed

src/Stack/Build/Cache.hs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ import Stack.Types.SourceMap ( smRelDir )
8989
import System.PosixCompat.Files
9090
( getFileStatus, modificationTime, setFileTimes )
9191

92-
-- | Directory containing files to mark an executable as installed
92+
-- | Directory containing files to mark an executable as installed.
9393
exeInstalledDir ::
9494
(HasEnvConfig env)
9595
=> InstallLocation
9696
-> RIO env (Path Abs Dir)
9797
exeInstalledDir Snap = (</> relDirInstalledPackages) <$> installationRootDeps
9898
exeInstalledDir Local = (</> relDirInstalledPackages) <$> installationRootLocal
9999

100-
-- | Get all of the installed executables
100+
-- | Get all of the installed executables.
101101
getInstalledExes ::
102102
(HasEnvConfig env)
103103
=> InstallLocation
@@ -115,7 +115,7 @@ getInstalledExes loc = do
115115
map (\x -> (pkgName x, [x])) $
116116
mapMaybe (parsePackageIdentifier . toFilePath . filename) files
117117

118-
-- | Mark the given executable as installed
118+
-- | Mark the given executable as installed.
119119
markExeInstalled ::
120120
(HasEnvConfig env)
121121
=> InstallLocation
@@ -135,7 +135,7 @@ markExeInstalled loc ident = do
135135
-- and invalidate this file in getInstalledExes if they no longer exist
136136
writeBinaryFileAtomic fp "Installed"
137137

138-
-- | Mark the given executable as not installed
138+
-- | Mark the given executable as not installed.
139139
markExeNotInstalled ::
140140
(HasEnvConfig env)
141141
=> InstallLocation
@@ -149,7 +149,9 @@ markExeNotInstalled loc ident = do
149149
buildCacheFile ::
150150
(HasEnvConfig env, MonadReader env m, MonadThrow m)
151151
=> Path Abs Dir
152+
-- ^ Package directory.
152153
-> NamedComponent
154+
-- ^ Package component.
153155
-> m (Path Abs File)
154156
buildCacheFile dir component = do
155157
cachesDir <- buildCachesDir dir
@@ -162,7 +164,9 @@ buildCacheFile dir component = do
162164
tryGetBuildCache ::
163165
HasEnvConfig env
164166
=> Path Abs Dir
167+
-- ^ Package directory.
165168
-> NamedComponent
169+
-- ^ Package component.
166170
-> RIO env (Maybe (Map FilePath FileCacheInfo))
167171
tryGetBuildCache dir component = do
168172
fp <- buildCacheFile dir component
@@ -171,27 +175,30 @@ tryGetBuildCache dir component = do
171175
decode = Yaml.decodeFileThrow (toFilePath fp)
172176
either (const Nothing) (Just . (.times)) <$> liftIO (tryAny decode)
173177

174-
-- | Try to read the dirtiness cache for the given package directory.
178+
-- | Try to read the Cabal configuration cache for the given package directory.
175179
tryGetConfigCache ::
176180
HasEnvConfig env
177181
=> Path Abs Dir
182+
-- ^ Package directory.
178183
-> RIO env (Maybe ConfigCache)
179184
tryGetConfigCache dir =
180185
loadConfigCache $ configCacheKey dir ConfigCacheTypeConfig
181186

182-
-- | Try to read the mod time of the Cabal file from the last build
187+
-- | Try to read the modification time of the Cabal file from the last build.
183188
tryGetCabalMod ::
184189
HasEnvConfig env
185190
=> Path Abs Dir
191+
-- ^ Package directory.
186192
-> RIO env (Maybe CTime)
187193
tryGetCabalMod dir = do
188194
fp <- toFilePath <$> configCabalMod dir
189195
tryGetFileMod fp
190196

191-
-- | Try to read the mod time of setup-config file from the last build
197+
-- | Try to read the modification time of setup-config file from the last build.
192198
tryGetSetupConfigMod ::
193199
HasEnvConfig env
194200
=> Path Abs Dir
201+
-- ^ Package directory.
195202
-> RIO env (Maybe CTime)
196203
tryGetSetupConfigMod dir = do
197204
fp <- toFilePath <$> configSetupConfigMod dir
@@ -202,7 +209,7 @@ tryGetFileMod fp =
202209
liftIO $ either (const Nothing) (Just . modificationTime) <$>
203210
tryIO (getFileStatus fp)
204211

205-
-- | Try to read the project root from the last build of a package
212+
-- | Try to read the project root from the last build of a package.
206213
tryGetPackageProjectRoot ::
207214
HasEnvConfig env
208215
=> Path Abs Dir
@@ -220,17 +227,21 @@ tryReadFileBinary fp =
220227
writeBuildCache ::
221228
HasEnvConfig env
222229
=> Path Abs Dir
230+
-- ^ Package directory.
223231
-> NamedComponent
232+
-- ^ Package component.
224233
-> Map FilePath FileCacheInfo -> RIO env ()
225234
writeBuildCache dir component times = do
226235
fp <- toFilePath <$> buildCacheFile dir component
227236
liftIO $ Yaml.encodeFile fp BuildCache { times = times }
228237

229-
-- | Write the dirtiness cache for this package's configuration.
238+
-- | Write the given Cabal configuration cache for the given package directory.
230239
writeConfigCache ::
231240
HasEnvConfig env
232241
=> Path Abs Dir
242+
-- ^ Package directory.
233243
-> ConfigCache
244+
-- ^ Cabal configuration cache.
234245
-> RIO env ()
235246
writeConfigCache dir =
236247
saveConfigCache (configCacheKey dir ConfigCacheTypeConfig)
@@ -270,15 +281,21 @@ writePackageProjectRoot dir projectRoot = do
270281
fp <- configPackageProjectRoot dir
271282
writeBinaryFileAtomic fp (byteString projectRoot)
272283

273-
-- | Delete the caches for the project.
274-
deleteCaches :: HasEnvConfig env => Path Abs Dir -> RIO env ()
284+
-- | Delete the Cabal configuration cache for the given package directory.
285+
deleteCaches ::
286+
HasEnvConfig env
287+
=> Path Abs Dir
288+
-- ^ Package directory.
289+
-> RIO env ()
275290
deleteCaches dir =
276291
{- FIXME confirm that this is acceptable to remove
277292
bfp <- buildCacheFile dir
278293
removeFileIfExists bfp
279294
-}
280295
deactiveConfigCache $ configCacheKey dir ConfigCacheTypeConfig
281296

297+
-- | For the given installed item, yields the key used to retrieve a record from
298+
-- the library Cabal flag cache or executable Cabal flag cache.
282299
flagCacheKey :: (HasEnvConfig env) => Installed -> RIO env ConfigCacheKey
283300
flagCacheKey installed = do
284301
installationRoot <- installationRootLocal
@@ -289,7 +306,7 @@ flagCacheKey installed = do
289306
Executable ident -> pure $
290307
configCacheKey installationRoot (ConfigCacheTypeFlagExecutable ident)
291308

292-
-- | Loads the flag cache for the given installed extra-deps.
309+
-- | Loads the Cabal flag cache for the given installed extra-deps.
293310
tryGetFlagCache ::
294311
HasEnvConfig env
295312
=> Installed
@@ -298,7 +315,7 @@ tryGetFlagCache gid = do
298315
key <- flagCacheKey gid
299316
loadConfigCache key
300317

301-
-- | Write the flag cache for the given installed extra-deps.
318+
-- | Write the Cabal flag cache for the given installed extra-deps.
302319
writeFlagCache ::
303320
HasEnvConfig env
304321
=> Installed
@@ -313,17 +330,22 @@ successBS = "success"
313330
failureBS = "failure"
314331
unknownBS = "unknown"
315332

316-
-- | Status of a test suite
333+
-- | Status of test suite(s).
317334
data TestStatus
318335
= TSSuccess
336+
-- ^ The test suite(s) succeeded.
319337
| TSFailure
338+
-- ^ One or more test suites failed.
320339
| TSUnknown
340+
-- ^ The outcome of the test suite(s) is unknown.
321341

322-
-- | Mark test suite status
342+
-- | Mark test suite status.
323343
setTestStatus ::
324344
HasEnvConfig env
325345
=> Path Abs Dir
346+
-- ^ Package directory.
326347
-> TestStatus
348+
-- ^ The status of the test suite(s).
327349
-> RIO env ()
328350
setTestStatus dir status = do
329351
fp <- testSuccessFile dir
@@ -333,10 +355,11 @@ setTestStatus dir status = do
333355
TSFailure -> failureBS
334356
TSUnknown -> unknownBS
335357

336-
-- | Check if the test suite already passed
358+
-- | Check if the test suite(s) already passed.
337359
getTestStatus ::
338360
HasEnvConfig env
339361
=> Path Abs Dir
362+
-- ^ Package directory.
340363
-> RIO env TestStatus
341364
getTestStatus dir = do
342365
fp <- testSuccessFile dir

src/Stack/Build/ConstructPlan.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,15 @@ checkDirtiness ps installed package present buildHaddocks = do
10821082
tell mempty { wDirty = Map.singleton package.name reason }
10831083
pure True
10841084

1085-
describeConfigDiff :: Config -> ConfigCache -> ConfigCache -> Maybe Text
1085+
-- | If the new Cabal configuration cache is the same as the old, yields
1086+
-- 'Nothing'. Otherwise yields 'Just' a textual explanation of how they differ.
1087+
describeConfigDiff ::
1088+
Config
1089+
-> ConfigCache
1090+
-- ^ The old Cabal configuration cache.
1091+
-> ConfigCache
1092+
-- ^ The new Cabal configuration cache.
1093+
-> Maybe Text
10861094
describeConfigDiff config old new
10871095
| old.pkgSrc /= new.pkgSrc = Just $
10881096
"switching from " <>

src/Stack/Build/ExecutePackage.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ import System.PosixCompat.Files
151151
( createLink, getFileStatus, modificationTime )
152152
import System.Random ( randomIO )
153153

154-
-- | Generate the ConfigCache
154+
-- | Generate the t'ConfigCache' value.
155155
getConfigCache ::
156156
HasEnvConfig env
157157
=> ExecuteEnv
@@ -257,15 +257,14 @@ ensureConfig newConfigCache pkgDir buildOpts announce cabal cabalFP task = do
257257
-- need proper hashes for package identifiers.
258258
then pure True
259259
else do
260-
-- We can ignore the components portion of the config
261-
-- cache, because it's just used to inform 'construct
262-
-- plan that we need to plan to build additional
263-
-- components. These components don't affect the actual
264-
-- package configuration.
260+
-- We can ignore the components field of the Cabal configuration cache,
261+
-- because it is only used to inform 'construct plan' that we need to
262+
-- plan to build additional components. These components don't affect
263+
-- the Cabal configuration for the package.
265264
let ignoreComponents :: ConfigCache -> ConfigCache
266265
ignoreComponents cc = cc { ConfigCache.components = Set.empty }
267-
-- Determine the old and new configuration in the local directory, to
268-
-- determine if we need to reconfigure.
266+
-- Determine the old and new Cabal configuration for the package
267+
-- directory, to determine if we need to reconfigure.
269268
mOldConfigCache <- tryGetConfigCache pkgDir
270269

271270
mOldCabalMod <- tryGetCabalMod pkgDir

src/Stack/Storage/Project.hs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
{-|
1111
Module : Stack.Storage.Project
12-
Description : Work with SQLite DB for caches across a project.
12+
Description : Work with the SQLite database for a project's caches.
1313
License : BSD-3-Clause
1414
15-
Work with SQLite database used for caches across a single project.
15+
Work with the SQLite database used for a project's caches.
1616
-}
1717

1818
module Stack.Storage.Project
@@ -51,6 +51,25 @@ import Stack.Types.ConfigureOpts
5151
import Stack.Types.GhcPkgId ( GhcPkgId )
5252
import Stack.Types.Storage ( ProjectStorage (..) )
5353

54+
-- Uses the Persistent entity syntax to generate entities for five tables in a
55+
-- SQLite database:
56+
--
57+
-- config_cache
58+
-- config_cache_dir_option
59+
-- config_cache_no_dir_option
60+
-- config_cache_dep
61+
-- config_cache_component
62+
--
63+
-- The ID column for each table is automatically generated.
64+
--
65+
-- The other tables have a foreign key referring to the config_cache table, via:
66+
--
67+
-- parent ConfigCacheParentId sql="config_cache_id" OnDeleteCascade
68+
--
69+
-- The tables have UNIQUE constraints on multiple columns.
70+
--
71+
-- Creates a function migrateAll to perform all migrations for the generated
72+
-- entities.
5473
share [ mkPersist sqlSettings
5574
, mkMigrate "migrateAll"
5675
]
@@ -92,11 +111,14 @@ ConfigCacheComponent
92111
deriving Show
93112
|]
94113

95-
-- | Initialize the database.
114+
-- | Initialize the project database for caches.
96115
initProjectStorage ::
97116
HasLogFunc env
98-
=> Path Abs File -- ^ storage file
117+
=> Path Abs File
118+
-- ^ The storage file.
99119
-> (ProjectStorage -> RIO env a)
120+
-- ^ Action, given a SQL database connection to the project database for
121+
-- caches.
100122
-> RIO env a
101123
initProjectStorage fp f = handleMigrationException $
102124
initStorage "Stack" migrateAll fp $ f . ProjectStorage
@@ -110,11 +132,19 @@ withProjectStorage inner = do
110132
storage <- view (buildConfigL . to (.projectStorage.projectStorage))
111133
withStorage_ storage inner
112134

113-
-- | Key used to retrieve configuration or flag cache
135+
-- | Type synonym representing keys used to retrieve a record from the Cabal
136+
-- configuration cache or the library or executable Cabal flag cache.
114137
type ConfigCacheKey = Unique ConfigCacheParent
115138

116-
-- | Build key used to retrieve configuration or flag cache
117-
configCacheKey :: Path Abs Dir -> ConfigCacheType -> ConfigCacheKey
139+
-- | For the given directory and type of cache, yields the key used to retrieve
140+
-- a record from the Cabal configuration cache or the library or executable
141+
-- Cabal flag cache.
142+
configCacheKey ::
143+
Path Abs Dir
144+
-- ^ Directory.
145+
-> ConfigCacheType
146+
-- ^ Type of cache.
147+
-> ConfigCacheKey
118148
configCacheKey dir = UniqueConfigCacheParent (toFilePath dir)
119149

120150
-- | Internal helper to read the t'ConfigCache'
@@ -150,7 +180,7 @@ readConfigCache (Entity parentId configCacheParent) = do
150180
, pathEnvVar
151181
}
152182

153-
-- | Load t'ConfigCache' from the database.
183+
-- | Load a t'ConfigCache' value from the project database for caches.
154184
loadConfigCache ::
155185
(HasBuildConfig env, HasLogFunc env)
156186
=> ConfigCacheKey
@@ -164,7 +194,7 @@ loadConfigCache key =
164194
Just <$> readConfigCache parentEntity
165195
| otherwise -> pure Nothing
166196

167-
-- | Insert or update t'ConfigCache' to the database.
197+
-- | Insert or update a t'ConfigCache' value to the project database for caches.
168198
saveConfigCache ::
169199
(HasBuildConfig env, HasLogFunc env)
170200
=> ConfigCacheKey

src/Stack/Storage/Util.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ updateCollection ::
3737
, Foldable collection
3838
)
3939
=> (collection rawValue -> collection rawValue -> ([Filter record], [value]))
40+
-- ^ Function to yield items in old not in new, to delete, and values in
41+
-- new not in old, to add, from the old and new collections of values.
4042
-> (value -> record)
43+
-- ^ Function to yield new records from values in new not in old.
4144
-> [Filter record]
45+
-- ^ Extra items to delete, if there are other items to delete.
4246
-> collection rawValue
47+
-- ^ The old collection of values.
4348
-> collection rawValue
49+
-- ^ The new collection of values.
4450
-> ReaderT backend m ()
4551
updateCollection fnDiffer recordCons extra old new =
4652
when (old /= new) $ do

0 commit comments

Comments
 (0)