Skip to content

Commit e1f578c

Browse files
committed
refactor(cabal-install): rebuildTargets
Isolate the common logic between building and only downloading. _Push the ifs up and the loops down_
1 parent 332b6ec commit e1f578c

File tree

2 files changed

+69
-45
lines changed

2 files changed

+69
-45
lines changed

cabal-install/src/Distribution/Client/ProjectBuilding.hs

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -340,23 +340,67 @@ rebuildTargets
340340
-> BuildTimeSettings
341341
-> IO BuildOutcomes
342342
rebuildTargets
343+
verbosity
344+
projectConfig
345+
distDirLayout
346+
storeDirLayout
347+
installPlan
348+
sharedPackageConfig
349+
pkgsBuildStatus
350+
buildSettings
351+
| buildSettingOnlyDownload buildSettings = do
352+
rebuildTargets' verbosity projectConfig distDirLayout installPlan sharedPackageConfig pkgsBuildStatus buildSettings $
353+
\downloadMap _jobControl pkg pkgBuildStatus ->
354+
rebuildTargetOnlyDownload
355+
verbosity
356+
downloadMap
357+
pkg
358+
pkgBuildStatus
359+
| otherwise = do
360+
registerLock <- newLock -- serialise registration
361+
cacheLock <- newLock -- serialise access to setup exe cache
362+
rebuildTargets' verbosity projectConfig distDirLayout installPlan sharedPackageConfig pkgsBuildStatus buildSettings $
363+
\downloadMap jobControl pkg pkgBuildStatus ->
364+
rebuildTarget
365+
verbosity
366+
distDirLayout
367+
storeDirLayout
368+
(jobControlSemaphore jobControl)
369+
buildSettings
370+
downloadMap
371+
registerLock
372+
cacheLock
373+
sharedPackageConfig
374+
installPlan
375+
pkg
376+
pkgBuildStatus
377+
378+
rebuildTargets'
379+
:: Verbosity
380+
-> ProjectConfig
381+
-> DistDirLayout
382+
-> ElaboratedInstallPlan
383+
-> ElaboratedSharedConfig
384+
-> BuildStatusMap
385+
-> BuildTimeSettings
386+
-> (AsyncFetchMap -> JobControl IO (Graph.Key (GenericReadyPackage ElaboratedConfiguredPackage), Either BuildFailure BuildResult) -> GenericReadyPackage ElaboratedConfiguredPackage -> BuildStatus -> IO BuildResult)
387+
-> IO BuildOutcomes
388+
rebuildTargets'
343389
verbosity
344390
ProjectConfig
345391
{ projectConfigBuildOnly = config
346392
}
347-
distDirLayout@DistDirLayout{..}
348-
storeDirLayout
393+
DistDirLayout{..}
349394
installPlan
350395
sharedPackageConfig
351396
pkgsBuildStatus
352397
buildSettings@BuildTimeSettings
353398
{ buildSettingNumJobs
354399
, buildSettingKeepGoing
355400
}
401+
act
356402
| fromFlagOrDefault False (projectConfigOfflineMode config) && not (null packagesToDownload) = return offlineError
357403
| otherwise = do
358-
registerLock <- newLock -- serialise registration
359-
cacheLock <- newLock -- serialise access to setup exe cache
360404
-- TODO: [code cleanup] eliminate setup exe cache
361405
info verbosity $
362406
"Executing install plan "
@@ -384,26 +428,13 @@ rebuildTargets
384428
InstallPlan.execute
385429
jobControl
386430
keepGoing
387-
(BuildFailure Nothing . DependentFailed . packageId)
431+
(BuildFailure Nothing . DependentFailed . Graph.nodeKey)
388432
installPlan
389433
$ \pkg ->
390434
-- TODO: review exception handling
391435
handle (\(e :: BuildFailure) -> return (Left e)) $ fmap Right $ do
392436
let pkgBuildStatus = Map.findWithDefault (error "rebuildTargets") (nodeKey pkg) pkgsBuildStatus
393-
394-
rebuildTarget
395-
verbosity
396-
distDirLayout
397-
storeDirLayout
398-
(jobControlSemaphore jobControl)
399-
buildSettings
400-
downloadMap
401-
registerLock
402-
cacheLock
403-
sharedPackageConfig
404-
installPlan
405-
pkg
406-
pkgBuildStatus
437+
act downloadMap jobControl pkg pkgBuildStatus
407438
where
408439
keepGoing = buildSettingKeepGoing
409440
withRepoCtx =
@@ -433,29 +464,6 @@ rebuildTargets
433464
_ -> pure ()
434465
_ -> pure ()
435466

436-
-- createPackageDBIfMissing _ _ _ _ = return ()
437-
438-
-- -- all the package dbs we may need to create
439-
-- (Set.toList . Set.fromList)
440-
-- [ pkgdb
441-
-- | InstallPlan.Configured elab <- InstallPlan.toList installPlan
442-
-- , pkgdb <-
443-
-- concat
444-
-- [ elabBuildPackageDBStack elab
445-
-- , elabRegisterPackageDBStack elab
446-
-- , elabSetupPackageDBStack elab
447-
-- ]
448-
-- ]
449-
-- createPackageDBIfMissing
450-
-- verbosity
451-
-- compiler
452-
-- progdb
453-
-- (SpecificPackageDB dbPath) = do
454-
-- exists <- Cabal.doesPackageDBExist dbPath
455-
-- unless exists $ do
456-
-- createDirectoryIfMissingVerbose verbosity True (takeDirectory dbPath)
457-
-- Cabal.createPackageDB verbosity compiler progdb False dbPath
458-
-- createPackageDBIfMissing _ _ _ _ = return ()
459467
offlineError :: BuildOutcomes
460468
offlineError = Map.fromList . map makeBuildOutcome $ packagesToDownload
461469
where
@@ -624,6 +632,23 @@ rebuildTarget
624632
srcdir
625633
builddir
626634

635+
rebuildTargetOnlyDownload
636+
:: Verbosity
637+
-> AsyncFetchMap
638+
-> GenericReadyPackage ElaboratedConfiguredPackage
639+
-> BuildStatus
640+
-> IO BuildResult
641+
rebuildTargetOnlyDownload
642+
verbosity
643+
downloadMap
644+
(ReadyPackage pkg)
645+
pkgBuildStatus = do
646+
case pkgBuildStatus of
647+
BuildStatusDownload ->
648+
void $ waitAsyncPackageDownload verbosity downloadMap pkg
649+
_ -> return ()
650+
return $ BuildResult DocsNotTried TestsNotTried Nothing
651+
627652
-- TODO: [nice to have] do we need to use a with-style for the temp
628653
-- files for downloading http packages, or are we going to cache them
629654
-- persistently?

cabal-install/src/Distribution/Client/ProjectBuilding/Types.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ import Prelude ()
2525
import Distribution.Client.FileMonitor (MonitorChangedReason (..))
2626
import Distribution.Client.Types (DocsResult, TestsResult)
2727

28-
import Distribution.Client.ProjectPlanning.Types (ElaboratedPlanPackage)
28+
import Distribution.Client.ProjectPlanning.Types (ElaboratedConfiguredPackage, ElaboratedPlanPackage)
2929
import qualified Distribution.Compat.Graph as Graph
3030
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
31-
import Distribution.Package (PackageId)
3231
import Distribution.Simple.LocalBuildInfo (ComponentName)
3332

3433
------------------------------------------------------------------------------
@@ -162,7 +161,7 @@ instance Exception BuildFailure
162161

163162
-- | Detail on the reason that a package failed to build.
164163
data BuildFailureReason
165-
= DependentFailed PackageId
164+
= DependentFailed (Graph.Key ElaboratedConfiguredPackage)
166165
| GracefulFailure String
167166
| DownloadFailed SomeException
168167
| UnpackFailed SomeException

0 commit comments

Comments
 (0)