Skip to content

Commit 20da080

Browse files
committed
Reformat foldOnGhcPkgId'
1 parent f42b3d3 commit 20da080

File tree

4 files changed

+86
-51
lines changed

4 files changed

+86
-51
lines changed

src/Stack/Build/Cache.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,17 @@ writePrecompiledCache
397397
libPath <- pathFromPkgId stackRootRelative ghcPkgId
398398
pc <- pcAction
399399
pure $ case libName of
400-
Nothing -> pc{library = Just libPath}
401-
_ -> pc{subLibs = libPath : pc.subLibs}
402-
precompiled <- foldOnGhcPkgId' installedLibToPath mghcPkgId (pure PrecompiledCache
400+
Nothing -> pc { library = Just libPath }
401+
_ -> pc { subLibs = libPath : pc.subLibs }
402+
precompiled <- foldOnGhcPkgId'
403+
installedLibToPath
404+
mghcPkgId
405+
( pure PrecompiledCache
403406
{ library = Nothing
404407
, subLibs = []
405408
, exes = exes'
406-
})
409+
}
410+
)
407411
savePrecompiledCache key precompiled
408412
-- reuse precompiled cache with haddocks also in case when haddocks are
409413
-- not required

src/Stack/Build/ExecutePackage.hs

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ import Stack.Types.BuildOpts
101101
import Stack.Types.BuildOptsCLI ( BuildOptsCLI (..) )
102102
import Stack.Types.CompCollection
103103
( collectionKeyValueList, collectionLookup
104-
, getBuildableListText, foldComponentToAnotherCollection
104+
, foldComponentToAnotherCollection, getBuildableListText
105105
)
106106
import Stack.Types.Compiler
107107
( ActualCompiler (..), WhichCompiler (..), getGhcVersion
@@ -637,11 +637,22 @@ singleBuild
637637
when (hasLibrary || hasSubLibraries) $ cabal KeepTHLoading ["register"]
638638

639639
copyDdumpFilesIfNeeded buildingFinals ee.buildOpts.ddumpDir
640-
installedPkg <- fetchAndMarkInstalledPackage ee (taskLocation task) package pkgId
641-
postProcessRemotePackage task.taskType ac cache ee installedPkg package pkgId pkgDir
640+
installedPkg <-
641+
fetchAndMarkInstalledPackage ee (taskLocation task) package pkgId
642+
postProcessRemotePackage
643+
task.taskType
644+
ac
645+
cache
646+
ee
647+
installedPkg
648+
package
649+
pkgId
650+
pkgDir
642651
pure installedPkg
643652

644-
postProcessRemotePackage :: (HasEnvConfig env)
653+
-- | Action in the case that the task relates to a remote package.
654+
postProcessRemotePackage ::
655+
(HasEnvConfig env)
645656
=> TaskType
646657
-> ActionContext
647658
-> ConfigCache
@@ -651,64 +662,77 @@ postProcessRemotePackage :: (HasEnvConfig env)
651662
-> PackageIdentifier
652663
-> Path b Dir
653664
-> RIO env ()
654-
postProcessRemotePackage taskType ac cache ee installedPackage package pkgId pkgDir = do
655-
case taskType of
656-
TTRemotePackage isMutable _ loc -> do
657-
when (isMutable == Immutable) $ writePrecompiledCache
658-
ee.baseConfigOpts
659-
loc
660-
cache.configureOpts
661-
cache.buildHaddocks
662-
installedPackage
663-
(buildableExes package)
664-
-- For packages from a package index, pkgDir is in the tmp directory. We
665-
-- eagerly delete it if no other tasks require it, to reduce space usage
666-
-- in tmp (#3018).
667-
let remaining =
668-
Set.filter
669-
(\(ActionId x _) -> x == pkgId)
670-
ac.remaining
671-
when (null remaining) $ removeDirRecur pkgDir
672-
_ -> pure ()
673-
674-
-- | Once all the cabal related tasks have run for a package, we should be able to gather
675-
-- the information needed to create an @Installed@ package value.
676-
-- For now, either there's a main library in which case we consider the package's libraries
677-
-- ghcPkgIds or we just consider it's an executable
665+
postProcessRemotePackage
666+
taskType
667+
ac
668+
cache
669+
ee
670+
installedPackage
671+
package
672+
pkgId
673+
pkgDir
674+
= case taskType of
675+
TTRemotePackage isMutable _ loc -> do
676+
when (isMutable == Immutable) $ writePrecompiledCache
677+
ee.baseConfigOpts
678+
loc
679+
cache.configureOpts
680+
cache.buildHaddocks
681+
installedPackage
682+
(buildableExes package)
683+
-- For packages from a package index, pkgDir is in the tmp directory. We
684+
-- eagerly delete it if no other tasks require it, to reduce space usage
685+
-- in tmp (#3018).
686+
let remaining =
687+
Set.filter
688+
(\(ActionId x _) -> x == pkgId)
689+
ac.remaining
690+
when (null remaining) $ removeDirRecur pkgDir
691+
_ -> pure ()
692+
693+
-- | Once all the Cabal-related tasks have run for a package, we should be able
694+
-- to gather the information needed to create an 'Installed' package value. For
695+
-- now, either there's a main library (in which case we consider the 'GhcPkgId'
696+
-- values of the package's libraries) or we just consider it's an executable
678697
-- (and mark all the executables as installed, if any).
679698
--
680-
-- Note that this also modifies the installedDumpPkgsTVar which is used for generating Haddocks.
699+
-- Note that this also modifies the installedDumpPkgsTVar which is used for
700+
-- generating Haddocks.
681701
--
682702
fetchAndMarkInstalledPackage ::
683-
(HasTerm env, HasEnvConfig env)
703+
(HasTerm env, HasEnvConfig env)
684704
=> ExecuteEnv
685705
-> InstallLocation
686706
-> Package
687707
-> PackageIdentifier
688708
-> RIO env Installed
689709
fetchAndMarkInstalledPackage ee taskInstallLocation package pkgId = do
690710
let baseConfigOpts = ee.baseConfigOpts
691-
let (installedPkgDb, installedDumpPkgsTVar) =
711+
(installedPkgDb, installedDumpPkgsTVar) =
692712
case taskInstallLocation of
693713
Snap ->
694714
( baseConfigOpts.snapDB
695715
, ee.snapshotDumpPkgs )
696716
Local ->
697717
( baseConfigOpts.localDB
698718
, ee.localDumpPkgs )
699-
-- let ident = PackageIdentifier package.name package.version
700-
-- only pure the sub-libraries to cache them if we also cache the main
719+
-- Only pure the sub-libraries to cache them if we also cache the main
701720
-- library (that is, if it exists)
702721
if hasBuildableMainLibrary package
703722
then do
704-
let getAndStoreGhcPkgId = loadInstalledPkg [installedPkgDb] installedDumpPkgsTVar
705-
let foldSubLibToMap subLib mapInMonad = do
723+
let getAndStoreGhcPkgId =
724+
loadInstalledPkg [installedPkgDb] installedDumpPkgsTVar
725+
foldSubLibToMap subLib mapInMonad = do
706726
let mungedName = toCabalMungedPackageName package.name subLib.name
707-
maybeGhcpkgId <- getAndStoreGhcPkgId (encodeCompatPackageName mungedName)
727+
maybeGhcpkgId <-
728+
getAndStoreGhcPkgId (encodeCompatPackageName mungedName)
708729
mapInMonad <&> case maybeGhcpkgId of
709730
Just v -> Map.insert subLib.name v
710731
_ -> id
711-
subLibsPkgIds <- foldComponentToAnotherCollection package.subLibraries foldSubLibToMap mempty
732+
subLibsPkgIds <- foldComponentToAnotherCollection
733+
package.subLibraries
734+
foldSubLibToMap
735+
mempty
712736
mGhcPkgId <- getAndStoreGhcPkgId package.name
713737
case mGhcPkgId of
714738
Nothing -> throwM $ Couldn'tFindPkgId package.name

src/Stack/Types/CompCollection.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ module Stack.Types.CompCollection
3030
, foldComponentToAnotherCollection
3131
) where
3232

33+
import qualified Data.Map as M
3334
import qualified Data.Set as Set
3435
import Stack.Prelude
3536
import Stack.Types.Component
3637
( HasBuildInfo, HasName, StackBuildInfo (..)
3738
, StackUnqualCompName (..)
3839
)
39-
import qualified Data.Map as M
4040

4141
-- | A type representing collections of components, distinguishing buildable
4242
-- components and non-buildable components.
@@ -66,10 +66,11 @@ instance Foldable CompCollection where
6666
foldr' fn c collection = M.foldr' fn c collection.buildableOnes
6767
null = M.null . (.buildableOnes)
6868

69-
-- | While the @HashMap@ is a more suitable choice for @Text@ based keys in general
70-
-- (it scales better), constant factors are largely dominant for maps with less than
71-
-- 1000 keys. Package with more than 100 components are extremely unlikely, so we keep
72-
-- a simple @Map@.
69+
-- | The 'Data.HashMap.Strict.HashMap' type is a more suitable choice than 'Map'
70+
-- for 'Data.Text.Text' based keys in general (it scales better). However,
71+
-- constant factors are largely dominant for maps with less than 1000 keys.
72+
-- Packages with more than 100 components are extremely unlikely, so we use a
73+
-- 'Map'.
7374
type InnerCollection component = Map StackUnqualCompName component
7475

7576
-- | A function to add a component to a collection of components. Ensures that

src/Stack/Types/Installed.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,18 @@ installedPackageIdentifier :: Installed -> PackageIdentifier
134134
installedPackageIdentifier (Library pid _) = pid
135135
installedPackageIdentifier (Executable pid) = pid
136136

137-
-- | A strict fold over the @GhcPkgId@ of the given installed package.
138-
-- This will iterate on both sub and main librarie(s) if any.
139-
foldOnGhcPkgId' :: (Maybe StackUnqualCompName -> GhcPkgId -> resT -> resT) -> Installed -> resT -> resT
137+
-- | A strict fold over the 'GhcPkgId' of the given installed package. This will
138+
-- iterate on both sub and main libraries, if any.
139+
foldOnGhcPkgId' ::
140+
(Maybe StackUnqualCompName -> GhcPkgId -> resT -> resT)
141+
-> Installed
142+
-> resT
143+
-> resT
140144
foldOnGhcPkgId' _ Executable{} res = res
141-
foldOnGhcPkgId' fn (Library _ libInfo) res = M.foldrWithKey' (fn . Just) (base res) libInfo.subLib
142-
where base = fn Nothing libInfo.ghcPkgId
145+
foldOnGhcPkgId' fn (Library _ libInfo) res =
146+
M.foldrWithKey' (fn . Just) (base res) libInfo.subLib
147+
where
148+
base = fn Nothing libInfo.ghcPkgId
143149

144150
-- | Get the installed Version.
145151
installedVersion :: Installed -> Version

0 commit comments

Comments
 (0)