Skip to content

Commit c7a3d42

Browse files
committed
Add the stack dot command
Pinging @mboes
1 parent 8dddd96 commit c7a3d42

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Don't require build plan to upload [#400](https://github.com/commercialhaskell/stack/issues/400)
1313
* Specifying test components only builds/runs those tests [#398](https://github.com/commercialhaskell/stack/issues/398)
1414
* `STACK_EXE` environment variable
15+
* Add the `stack dot` command
1516

1617
Bug fixes:
1718

src/Stack/Build/Source.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Stack.Build.Source
1010
, SourceMap
1111
, PackageSource (..)
1212
, localFlags
13+
, loadLocals
1314
) where
1415

1516
import Network.HTTP.Client.Conduit (HasHttpManager)

src/Stack/Dot.hs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE RecordWildCards #-}
3+
{-# LANGUAGE TemplateHaskell #-}
4+
module Stack.Dot where
5+
6+
7+
import Control.Monad (when)
8+
import Control.Monad.Catch (MonadCatch)
9+
import Control.Monad.IO.Class (MonadIO)
10+
import Control.Monad.Logger (MonadLogger, logInfo)
11+
import Control.Monad.Reader (MonadReader)
12+
import qualified Data.Foldable as F
13+
import Data.Monoid ((<>))
14+
import qualified Data.Map as Map
15+
import qualified Data.Set as Set
16+
import qualified Data.Text as T
17+
import Stack.Build.Source
18+
import Stack.Build.Types
19+
import Stack.Package
20+
import Stack.Types
21+
22+
-- | Convert a package name to a graph node name.
23+
nodeName :: PackageName -> T.Text
24+
nodeName name = "\"" <> T.pack (packageNameString name) <> "\""
25+
26+
dot :: (MonadReader env m, HasBuildConfig env, MonadIO m, MonadLogger m, MonadCatch m,HasEnvConfig env)
27+
=> m ()
28+
dot = do
29+
(locals, _names, _idents) <- loadLocals
30+
BuildOpts
31+
{ boptsTargets = []
32+
, boptsLibProfile = False
33+
, boptsExeProfile = False
34+
, boptsEnableOptimizations = Nothing
35+
, boptsHaddock = False
36+
, boptsHaddockDeps = Nothing
37+
, boptsFinalAction = DoNothing
38+
, boptsDryrun = False
39+
, boptsGhcOptions = []
40+
, boptsFlags = Map.empty
41+
, boptsInstallExes = False
42+
, boptsPreFetch = False
43+
, boptsTestArgs = []
44+
, boptsOnlySnapshot = False
45+
, boptsCoverage = False
46+
}
47+
Map.empty
48+
let localNames = Set.fromList $ map (packageName . lpPackage) locals
49+
50+
$logInfo "digraph deps {"
51+
$logInfo "splines=polyline;"
52+
53+
F.forM_ locals $ \lp -> do
54+
let deps = Set.intersection localNames $ packageAllDeps $ lpPackage lp
55+
F.forM_ deps $ \dep ->
56+
$logInfo $ T.concat
57+
[ nodeName $ packageName $ lpPackage lp
58+
, " -> "
59+
, nodeName dep
60+
, ";"
61+
]
62+
when (Set.null deps) $
63+
$logInfo $ T.concat
64+
[ "{rank=max; "
65+
, nodeName $ packageName $ lpPackage lp
66+
, "}"
67+
]
68+
69+
$logInfo "}"

src/main/Main.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import Stack.Build.Types
4040
import Stack.Config
4141
import Stack.Constants
4242
import qualified Stack.Docker as Docker
43+
import Stack.Dot
4344
import Stack.Exec
4445
import Stack.Fetch
4546
import Stack.Init
@@ -140,6 +141,10 @@ main =
140141
"Upload a package to Hackage"
141142
uploadCmd
142143
(many $ strArgument $ metavar "TARBALL/DIR")
144+
addCommand "dot"
145+
"Visualize your project's dependency graph using Graphviz dot"
146+
dotCmd
147+
(pure ())
143148
addCommand "exec"
144149
"Execute a command"
145150
execCmd
@@ -789,3 +794,7 @@ solverOptsParser = boolFlags False
789794
"modify-stack-yaml"
790795
"Automatically modify stack.yaml with the solver's recommendations"
791796
idm
797+
798+
-- | Visualize dependencies
799+
dotCmd :: () -> GlobalOpts -> IO ()
800+
dotCmd () go = withBuildConfig go ThrowException dot

stack.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ library
4747
Stack.Constants
4848
Stack.Docker
4949
Stack.Docker.GlobalDB
50+
Stack.Dot
5051
Stack.Fetch
5152
Stack.Exec
5253
Stack.GhcPkg

0 commit comments

Comments
 (0)