Skip to content

Commit

Permalink
Implement CLI for "why-depends" command
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Mar 22, 2024
1 parent 7f18560 commit 8c88656
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src-exe/cabal-plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ data Command
| TopoCommand (Maybe SearchPlanJson) (Flag TopoReverse) (Flag ShowFlags)
| LicenseReport (Maybe FilePath) Pattern
| DiffCommand SearchPlanJson SearchPlanJson
| WhyDependsCommand (Maybe SearchPlanJson) Pattern Pattern

data RunDot = PNG | PDF

Expand Down Expand Up @@ -311,6 +312,11 @@ main = do
(_, plan) <- findPlan s
doTopo optsUseColors optsShowBuiltin optsShowGlobal plan rev showFlags
LicenseReport mfp pat -> doLicenseReport mfp pat
WhyDependsCommand s compPat depPat -> do
(_, plan) <- findPlan s
let [(_, compId, _)] = findUnit plan compPat
let [(_, depId, _)] = findUnit plan depPat
doWhyDepends optsUseColors optsUseAscii plan compId depId
where
findPlan search = do
cwd <- getCurrentDirectory
Expand Down Expand Up @@ -414,6 +420,10 @@ main = do
<*> patternArgument
[ metavar "PATTERN", help "Pattern to match.", completer $ patternCompleter False ]
<**> helper
, subCommand "why-depends" "Why does a package depend on another package" $ WhyDependsCommand
<$> planParser
<*> patternArgument [ metavar "TARGET", help "Target with dependencies", completer $ patternCompleter False ]
<*> patternArgument [ metavar "DEPENDENCY", help "Dependency, might be local or global", completer $ patternCompleter False ]
]

defaultCommand = pure (InfoCommand Nothing)
Expand Down Expand Up @@ -1274,10 +1284,23 @@ doTopo useColors showBuiltin showGlobal plan rev showFlags = do

doWhyDepends :: UseColors -> UseAscii -> PlanJson -> UnitId -> UnitId -> IO ()
doWhyDepends useColors useAscii (PlanJson { pjUnits = pm}) unitId pkg = do
let (_u, newPm) = runState (buildDependencyGraph unitId) M.empty
let pm' = M.mapMaybe id newPm
runCWriterIO useColors useAscii (evalStateT (writePlanJson pm' unitId) S.empty)
let (unitWithStrippedDeps, newPm) = runState (buildDependencyGraph unitId) M.empty
case unitWithStrippedDeps of
Nothing ->
runCWriterIO useColors useAscii $ do
putCTextLn $ "The unit " <> prettyUnitId unitId <> " does not depend on " <> prettyUnitId pkg
Just _ -> do
let pm' = M.mapMaybe id newPm
runCWriterIO useColors useAscii $ evalStateT (writePlanJson pm' unitId) S.empty
where
prettyUnitId :: UnitId -> CText
prettyUnitId uid =
let
Unit {..} = M.findWithDefault undefined uid pm
in
underline (colorifyText White $ dispPkgId uPId)


buildDependencyGraph :: UnitId -> State (Map UnitId (Maybe Unit)) (Maybe Unit)
buildDependencyGraph uid = do
munit <- gets (uid `M.lookup`)
Expand Down

0 comments on commit 8c88656

Please sign in to comment.