Skip to content

Commit cb41f5f

Browse files
committed
refactor(cabal-install): merge two almost identical functions
Merge fromSolverInstallPlan and fromSolverInstallPlanWithProgress.
1 parent f5a1571 commit cb41f5f

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

Cabal/src/Distribution/Utils/LogProgress.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Distribution.Utils.LogProgress
55
( LogProgress
66
, runLogProgress
7+
, runLogProgress'
78
, warnProgress
89
, infoProgress
910
, dieProgress
@@ -61,6 +62,16 @@ runLogProgress verbosity (LogProgress m) =
6162
fail_fn doc = do
6263
dieNoWrap verbosity (render doc)
6364

65+
-- | Run 'LogProgress' ignoring all traces.
66+
runLogProgress' :: LogProgress a -> Either ErrMsg a
67+
runLogProgress' (LogProgress m) = foldProgress (\_ x -> x) Left Right (m env)
68+
where
69+
env =
70+
LogEnv
71+
{ le_verbosity = silent
72+
, le_context = []
73+
}
74+
6475
-- | Output a warning trace message in 'LogProgress'.
6576
warnProgress :: Doc -> LogProgress ()
6677
warnProgress s = LogProgress $ \env ->

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

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -528,35 +528,11 @@ fromSolverInstallPlan
528528
-> SolverInstallPlan
529529
-> GenericInstallPlan ipkg srcpkg
530530
fromSolverInstallPlan f plan =
531-
mkInstallPlan
532-
"fromSolverInstallPlan"
533-
(Graph.fromDistinctList pkgs'')
534-
where
535-
(_, _, pkgs'') =
536-
foldl'
537-
f'
538-
(Map.empty, Map.empty, [])
539-
(SolverInstallPlan.reverseTopologicalOrder plan)
540-
541-
f' (pidMap, ipiMap, pkgs) pkg = (pidMap', ipiMap', pkgs' ++ pkgs)
542-
where
543-
pkgs' = f (mapDep pidMap ipiMap) pkg
544-
545-
(pidMap', ipiMap') =
546-
case nodeKey pkg of
547-
PreExistingId _ uid -> (pidMap, Map.insert uid pkgs' ipiMap)
548-
PlannedId pid -> (Map.insert pid pkgs' pidMap, ipiMap)
549-
550-
mapDep _ ipiMap (PreExistingId _pid uid)
551-
| Just pkgs <- Map.lookup uid ipiMap = pkgs
552-
| otherwise = error ("fromSolverInstallPlan: PreExistingId " ++ prettyShow uid)
553-
mapDep pidMap _ (PlannedId pid)
554-
| Just pkgs <- Map.lookup pid pidMap = pkgs
555-
| otherwise = error ("fromSolverInstallPlan: PlannedId " ++ prettyShow pid)
556-
557-
-- This shouldn't happen, since mapDep should only be called
558-
-- on neighbor SolverId, which must have all been done already
559-
-- by the reverse top-sort (we assume the graph is not broken).
531+
either (error . show) id $
532+
runLogProgress' $
533+
fromSolverInstallPlanWithProgress
534+
(\mapDep planpkg -> return $ f mapDep planpkg)
535+
plan
560536

561537
fromSolverInstallPlanWithProgress
562538
:: (IsUnit ipkg, IsUnit srcpkg)
@@ -585,17 +561,18 @@ fromSolverInstallPlanWithProgress f plan = do
585561
PlannedId pid -> (Map.insert pid pkgs' pidMap, ipiMap)
586562
return (pidMap', ipiMap', pkgs' ++ pkgs)
587563

564+
-- The error below shouldn't happen, since mapDep should only
565+
-- be called on neighbor SolverId, which must have all been done
566+
-- already by the reverse top-sort (we assume the graph is not broken).
567+
--
568+
-- FIXME: stage is ignored
588569
mapDep _ ipiMap (PreExistingId _pid uid)
589570
| Just pkgs <- Map.lookup uid ipiMap = pkgs
590571
| otherwise = error ("fromSolverInstallPlan: PreExistingId " ++ prettyShow uid)
591572
mapDep pidMap _ (PlannedId pid)
592573
| Just pkgs <- Map.lookup pid pidMap = pkgs
593574
| otherwise = error ("fromSolverInstallPlan: PlannedId " ++ prettyShow pid)
594575

595-
-- This shouldn't happen, since mapDep should only be called
596-
-- on neighbor SolverId, which must have all been done already
597-
-- by the reverse top-sort (we assume the graph is not broken).
598-
599576
-- | Conversion of 'SolverInstallPlan' to 'InstallPlan'.
600577
-- Similar to 'elaboratedInstallPlan'
601578
configureInstallPlan :: Cabal.ConfigFlags -> SolverInstallPlan -> InstallPlan

0 commit comments

Comments
 (0)