Skip to content

Commit a80fd54

Browse files
committed
Move store-related TH into its own module #4272
1 parent 73e713b commit a80fd54

File tree

8 files changed

+155
-57
lines changed

8 files changed

+155
-57
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ library:
231231
- Stack.Sig.Sign
232232
- Stack.Snapshot
233233
- Stack.Solver
234+
- Stack.StoreTH
234235
- Stack.Types.Build
235236
- Stack.Types.BuildPlan
236237
- Stack.Types.CompilerBuild

src/Stack/Build/Cache.hs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE DeriveGeneric #-}
33
{-# LANGUAGE MultiParamTypeClasses #-}
4-
{-# LANGUAGE TemplateHaskell #-}
54
{-# LANGUAGE TupleSections #-}
65
{-# LANGUAGE ConstraintKinds #-}
76
{-# LANGUAGE OverloadedStrings #-}
@@ -42,18 +41,17 @@ import Data.Char (ord)
4241
import qualified Data.Map as M
4342
import qualified Data.Set as Set
4443
import qualified Data.Store as Store
45-
import Data.Store.VersionTagged
4644
import qualified Data.Text as T
4745
import Path
4846
import Path.IO
4947
import Stack.Constants
5048
import Stack.Constants.Config
49+
import Stack.StoreTH
5150
import Stack.Types.Build
5251
import Stack.Types.Compiler
5352
import Stack.Types.Config
5453
import Stack.Types.GhcPkgId
5554
import Stack.Types.NamedComponent
56-
import Stack.Types.Package
5755
import qualified System.FilePath as FP
5856

5957
-- | Directory containing files to mark an executable as installed
@@ -124,17 +122,17 @@ tryGetBuildCache :: HasEnvConfig env
124122
=> Path Abs Dir
125123
-> NamedComponent
126124
-> RIO env (Maybe (Map FilePath FileCacheInfo))
127-
tryGetBuildCache dir component = liftM (fmap buildCacheTimes) . $(versionedDecodeFile buildCacheVC) =<< buildCacheFile dir component
125+
tryGetBuildCache dir component = liftM (fmap buildCacheTimes) . decodeBuildCache =<< buildCacheFile dir component
128126

129127
-- | Try to read the dirtiness cache for the given package directory.
130128
tryGetConfigCache :: HasEnvConfig env
131129
=> Path Abs Dir -> RIO env (Maybe ConfigCache)
132-
tryGetConfigCache dir = $(versionedDecodeFile configCacheVC) =<< configCacheFile dir
130+
tryGetConfigCache dir = decodeConfigCache =<< configCacheFile dir
133131

134132
-- | Try to read the mod time of the cabal file from the last build
135133
tryGetCabalMod :: HasEnvConfig env
136134
=> Path Abs Dir -> RIO env (Maybe ModTime)
137-
tryGetCabalMod dir = $(versionedDecodeFile modTimeVC) =<< configCabalMod dir
135+
tryGetCabalMod dir = decodeModTime =<< configCabalMod dir
138136

139137
-- | Write the dirtiness cache for this package's files.
140138
writeBuildCache :: HasEnvConfig env
@@ -143,7 +141,7 @@ writeBuildCache :: HasEnvConfig env
143141
-> Map FilePath FileCacheInfo -> RIO env ()
144142
writeBuildCache dir component times = do
145143
fp <- buildCacheFile dir component
146-
$(versionedEncodeFile buildCacheVC) fp BuildCache
144+
encodeBuildCache fp BuildCache
147145
{ buildCacheTimes = times
148146
}
149147

@@ -154,7 +152,7 @@ writeConfigCache :: HasEnvConfig env
154152
-> RIO env ()
155153
writeConfigCache dir x = do
156154
fp <- configCacheFile dir
157-
$(versionedEncodeFile configCacheVC) fp x
155+
encodeConfigCache fp x
158156

159157
-- | See 'tryGetCabalMod'
160158
writeCabalMod :: HasEnvConfig env
@@ -163,7 +161,7 @@ writeCabalMod :: HasEnvConfig env
163161
-> RIO env ()
164162
writeCabalMod dir x = do
165163
fp <- configCabalMod dir
166-
$(versionedEncodeFile modTimeVC) fp x
164+
encodeModTime fp x
167165

168166
-- | Delete the caches for the project.
169167
deleteCaches :: (MonadIO m, MonadReader env m, HasEnvConfig env, MonadThrow m)
@@ -193,7 +191,7 @@ tryGetFlagCache :: HasEnvConfig env
193191
-> RIO env (Maybe ConfigCache)
194192
tryGetFlagCache gid = do
195193
fp <- flagCacheFile gid
196-
$(versionedDecodeFile configCacheVC) fp
194+
decodeConfigCache fp
197195

198196
writeFlagCache :: HasEnvConfig env
199197
=> Installed
@@ -202,23 +200,23 @@ writeFlagCache :: HasEnvConfig env
202200
writeFlagCache gid cache = do
203201
file <- flagCacheFile gid
204202
ensureDir (parent file)
205-
$(versionedEncodeFile configCacheVC) file cache
203+
encodeConfigCache file cache
206204

207205
-- | Mark a test suite as having succeeded
208206
setTestSuccess :: HasEnvConfig env
209207
=> Path Abs Dir
210208
-> RIO env ()
211209
setTestSuccess dir = do
212210
fp <- testSuccessFile dir
213-
$(versionedEncodeFile testSuccessVC) fp True
211+
encodeTestSuccess fp True
214212

215213
-- | Mark a test suite as not having succeeded
216214
unsetTestSuccess :: HasEnvConfig env
217215
=> Path Abs Dir
218216
-> RIO env ()
219217
unsetTestSuccess dir = do
220218
fp <- testSuccessFile dir
221-
$(versionedEncodeFile testSuccessVC) fp False
219+
encodeTestSuccess fp False
222220

223221
-- | Check if the test suite already passed
224222
checkTestSuccess :: HasEnvConfig env
@@ -227,7 +225,7 @@ checkTestSuccess :: HasEnvConfig env
227225
checkTestSuccess dir =
228226
liftM
229227
(fromMaybe False)
230-
($(versionedDecodeFile testSuccessVC) =<< testSuccessFile dir)
228+
(decodeTestSuccess =<< testSuccessFile dir)
231229

232230
--------------------------------------
233231
-- Precompiled Cache
@@ -315,7 +313,7 @@ writePrecompiledCache baseConfigOpts loc copts depIDs mghcPkgId sublibs exes = d
315313
name <- parseRelFile $ T.unpack exe
316314
relPath <- stackRootRelative $ bcoSnapInstallRoot baseConfigOpts </> bindirSuffix </> name
317315
return $ toFilePath relPath
318-
$(versionedEncodeFile precompiledCacheVC) file PrecompiledCache
316+
encodePrecompiledCache file PrecompiledCache
319317
{ pcLibrary = mlibpath
320318
, pcSubLibs = sublibpaths
321319
, pcExes = exes'
@@ -335,7 +333,7 @@ readPrecompiledCache :: forall env. HasEnvConfig env
335333
-> RIO env (Maybe PrecompiledCache)
336334
readPrecompiledCache loc copts depIDs = do
337335
file <- precompiledCacheFile loc copts depIDs
338-
mcache <- $(versionedDecodeFile precompiledCacheVC) file
336+
mcache <- decodePrecompiledCache file
339337
maybe (pure Nothing) (fmap Just . mkAbs) mcache
340338
where
341339
-- Since commit ed9ccc08f327bad68dd2d09a1851ce0d055c0422,

src/Stack/Constants.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ module Stack.Constants
118118
,relFileStackDotExe
119119
,relFileStackDotTmpDotExe
120120
,relFileStackDotTmp
121-
,defaultTemplateName
121+
,ghcShowOptionsOutput
122122
)
123123
where
124124

@@ -127,11 +127,12 @@ import Data.FileEmbed (embedFile, makeRelativeToProject)
127127
import qualified Data.Set as Set
128128
import Distribution.Package (mkPackageName)
129129
import qualified Hpack.Config as Hpack
130+
import qualified Language.Haskell.TH.Syntax as TH (runIO, lift)
130131
import Path as FL
131132
import Stack.Prelude
132133
import Stack.Types.Compiler
133-
import Stack.Types.TemplateName
134134
import System.Permissions (osIsWindows)
135+
import System.Process (readProcess)
135136

136137
-- | Extensions used for Haskell modules. Excludes preprocessor ones.
137138
haskellFileExts :: [Text]
@@ -584,6 +585,8 @@ relFileStackDotTmp = $(mkRelFile "stack.tmp")
584585
relFileStack :: Path Rel File
585586
relFileStack = $(mkRelFile "stack")
586587

587-
-- | The default template name you can use if you don't have one.
588-
defaultTemplateName :: TemplateName
589-
defaultTemplateName = $(mkTemplateName "new-template")
588+
-- Technically, we should be consulting the user's current ghc,
589+
-- but that would require loading up a BuildConfig.
590+
ghcShowOptionsOutput :: [String]
591+
ghcShowOptionsOutput =
592+
$(TH.runIO (readProcess "ghc" ["--show-options"] "") >>= TH.lift . lines)

src/Stack/Options/Completion.hs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE NoImplicitPrelude #-}
22
{-# LANGUAGE OverloadedStrings #-}
3-
{-# LANGUAGE TemplateHaskell #-}
43
{-# LANGUAGE TupleSections #-}
54

65
module Stack.Options.Completion
@@ -21,14 +20,13 @@ import qualified Distribution.Types.UnqualComponentName as C
2120
import Options.Applicative
2221
import Options.Applicative.Builder.Extra
2322
import Stack.Config (getLocalPackages)
23+
import Stack.Constants (ghcShowOptionsOutput)
2424
import Stack.Options.GlobalParser (globalOptsFromMonoid)
2525
import Stack.Runners (loadConfigWithOpts)
26-
import Stack.Prelude hiding (lift)
26+
import Stack.Prelude
2727
import Stack.Setup
2828
import Stack.Types.Config
2929
import Stack.Types.NamedComponent
30-
import System.Process (readProcess)
31-
import Language.Haskell.TH.Syntax (runIO, lift)
3230

3331
ghcOptsCompleter :: Completer
3432
ghcOptsCompleter = mkCompleter $ \inputRaw -> return $
@@ -38,10 +36,7 @@ ghcOptsCompleter = mkCompleter $ \inputRaw -> return $
3836
otherArgs = reverse otherArgsReversed
3937
in if null curArg then [] else
4038
map (otherArgs ++) $
41-
filter (curArg `isPrefixOf`)
42-
-- Technically, we should be consulting the user's current ghc,
43-
-- but that would require loading up a BuildConfig.
44-
$(runIO (readProcess "ghc" ["--show-options"] "") >>= lift . lines)
39+
filter (curArg `isPrefixOf`) ghcShowOptionsOutput
4540

4641
-- TODO: Ideally this would pay attention to --stack-yaml, may require
4742
-- changes to optparse-applicative.

src/Stack/PackageDump.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
{-# LANGUAGE TupleSections #-}
77
{-# LANGUAGE DeriveDataTypeable #-}
88
{-# LANGUAGE DeriveGeneric #-}
9-
{-# LANGUAGE TemplateHaskell #-}
109
module Stack.PackageDump
1110
( Line
1211
, eachSection
@@ -34,14 +33,14 @@ import qualified Data.Conduit.Text as CT
3433
import Data.List (isPrefixOf)
3534
import qualified Data.Map as Map
3635
import qualified Data.Set as Set
37-
import Data.Store.VersionTagged
3836
import qualified RIO.Text as T
3937
import qualified Distribution.License as C
4038
import Distribution.ModuleName (ModuleName)
4139
import qualified Distribution.System as OS
4240
import qualified Distribution.Text as C
4341
import Path.Extra (toFilePathNoTrailingSep)
4442
import Stack.GhcPkg
43+
import Stack.StoreTH
4544
import Stack.Types.Compiler
4645
import Stack.Types.GhcPkgId
4746
import Stack.Types.PackageDump
@@ -100,13 +99,13 @@ newInstalledCache = liftIO $ InstalledCache <$> newIORef (InstalledCacheInner Ma
10099
-- empty cache.
101100
loadInstalledCache :: HasLogFunc env => Path Abs File -> RIO env InstalledCache
102101
loadInstalledCache path = do
103-
m <- $(versionedDecodeOrLoad installedCacheVC) path (return $ InstalledCacheInner Map.empty)
102+
m <- decodeOrLoadInstalledCache path (return $ InstalledCacheInner Map.empty)
104103
liftIO $ InstalledCache <$> newIORef m
105104

106105
-- | Save a @InstalledCache@ to disk
107106
saveInstalledCache :: HasLogFunc env => Path Abs File -> InstalledCache -> RIO env ()
108107
saveInstalledCache path (InstalledCache ref) =
109-
liftIO (readIORef ref) >>= $(versionedEncodeFile installedCacheVC) path
108+
readIORef ref >>= encodeInstalledCache path
110109

111110
-- | Prune a list of possible packages down to those whose dependencies are met.
112111
--

src/Stack/Snapshot.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
{-# LANGUAGE OverloadedStrings #-}
1010
{-# LANGUAGE RecordWildCards #-}
1111
{-# LANGUAGE ScopedTypeVariables #-}
12-
{-# LANGUAGE TemplateHaskell #-}
1312
{-# LANGUAGE TupleSections #-}
1413
{-# LANGUAGE ViewPatterns #-}
1514

@@ -24,7 +23,6 @@ module Stack.Snapshot
2423

2524
import Stack.Prelude hiding (Display (..))
2625
import Control.Monad.State.Strict (get, put, StateT, execStateT)
27-
import Data.Store.VersionTagged
2826
import qualified Data.Conduit.List as CL
2927
import qualified Data.Map as Map
3028
import qualified Data.Set as Set
@@ -44,6 +42,7 @@ import qualified Pantry
4442
import qualified Pantry.SHA256 as SHA256
4543
import Stack.Package
4644
import Stack.PackageDump
45+
import Stack.StoreTH
4746
import Stack.Types.BuildPlan
4847
import Stack.Types.GhcPkgId
4948
import Stack.Types.VersionIntervals
@@ -174,7 +173,7 @@ loadSnapshot mcompiler =
174173
path <- configLoadedSnapshotCache
175174
sd
176175
(maybe GISSnapshotHints GISCompiler mcompiler)
177-
$(versionedDecodeOrLoad loadedSnapshotVC) path (inner sd)
176+
decodeOrLoadLoadedSnapshot path (inner sd)
178177

179178
inner :: SnapshotDef -> RIO env LoadedSnapshot
180179
inner sd = do

0 commit comments

Comments
 (0)