@@ -12,10 +12,11 @@ module Stack.Ls
12
12
import Control.Exception (throw )
13
13
import Data.Aeson
14
14
import Data.Array.IArray ((//) , elems )
15
+ import Distribution.Package (mkPackageName )
15
16
import Stack.Prelude hiding (Snapshot (.. ), SnapName (.. ))
16
17
import qualified Data.Aeson.Types as A
17
18
import qualified Data.List as L
18
- import Data.Text hiding (pack , intercalate )
19
+ import Data.Text hiding (filter , intercalate , pack , reverse )
19
20
import qualified Data.Text as T
20
21
import qualified Data.Text.IO as T
21
22
import qualified Data.Vector as V
@@ -24,17 +25,20 @@ import qualified Options.Applicative as OA
24
25
import Options.Applicative (idm )
25
26
import Options.Applicative.Builder.Extra (boolFlags )
26
27
import Path
28
+ import RIO.List (sort )
27
29
import RIO.PrettyPrint (useColorL )
28
30
import RIO.PrettyPrint.DefaultStyles (defaultStyles )
29
31
import RIO.PrettyPrint.Types (StyleSpec )
30
32
import RIO.PrettyPrint.StylesUpdate (StylesUpdate (.. ), stylesUpdateL )
31
33
import Stack.Dot
32
34
import Stack.Runners
33
35
import Stack.Options.DotParser (listDepsOptsParser )
36
+ import Stack.Setup.Installed (Tool (.. ), filterTools , listInstalled , toolString )
34
37
import Stack.Types.Config
35
38
import System.Console.ANSI.Codes (SGR (Reset ), setSGRCode , sgrToCode )
36
39
import System.Process.Pager (pageText )
37
40
import System.Directory (listDirectory )
41
+ import System.IO (putStrLn )
38
42
39
43
data LsView
40
44
= Local
@@ -50,6 +54,7 @@ data LsCmds
50
54
= LsSnapshot SnapshotOpts
51
55
| LsDependencies ListDepsOpts
52
56
| LsStyles ListStylesOpts
57
+ | LsTools ListToolsOpts
53
58
54
59
data SnapshotOpts = SnapshotOpts
55
60
{ soptViewType :: LsView
@@ -63,12 +68,17 @@ data ListStylesOpts = ListStylesOpts
63
68
, coptExample :: Bool
64
69
} deriving (Eq , Ord , Show )
65
70
71
+ newtype ListToolsOpts = ListToolsOpts
72
+ { toptFilter :: String
73
+ }
74
+
66
75
newtype LsCmdOpts = LsCmdOpts
67
76
{ lsView :: LsCmds
68
77
}
69
78
70
79
lsParser :: OA. Parser LsCmdOpts
71
- lsParser = LsCmdOpts <$> OA. hsubparser (lsSnapCmd <> lsDepsCmd <> lsStylesCmd)
80
+ lsParser = LsCmdOpts
81
+ <$> OA. hsubparser (lsSnapCmd <> lsDepsCmd <> lsStylesCmd <> lsToolsCmd)
72
82
73
83
lsCmdOptsParser :: OA. Parser LsCmds
74
84
lsCmdOptsParser = LsSnapshot <$> lsViewSnapCmd
@@ -79,6 +89,9 @@ lsDepOptsParser = LsDependencies <$> listDepsOptsParser
79
89
lsStylesOptsParser :: OA. Parser LsCmds
80
90
lsStylesOptsParser = LsStyles <$> listStylesOptsParser
81
91
92
+ lsToolsOptsParser :: OA. Parser LsCmds
93
+ lsToolsOptsParser = LsTools <$> listToolsOptsParser
94
+
82
95
listStylesOptsParser :: OA. Parser ListStylesOpts
83
96
listStylesOptsParser = ListStylesOpts
84
97
<$> boolFlags False
@@ -98,6 +111,16 @@ listStylesOptsParser = ListStylesOpts
98
111
\report"
99
112
idm
100
113
114
+ listToolsOptsParser :: OA. Parser ListToolsOpts
115
+ listToolsOptsParser = ListToolsOpts
116
+ <$> OA. strOption
117
+ ( OA. long " filter"
118
+ <> OA. metavar " TOOL_NAME"
119
+ <> OA. value " "
120
+ <> OA. help " Filter by a tool name (eg 'ghc', 'ghc-git' or 'msys2') \
121
+ \- case sensitive. The default is no filter"
122
+ )
123
+
101
124
lsViewSnapCmd :: OA. Parser SnapshotOpts
102
125
lsViewSnapCmd =
103
126
SnapshotOpts <$>
@@ -135,6 +158,12 @@ lsStylesCmd =
135
158
(OA. progDesc " View stack's output styles (alias for \
136
159
\'stack-colors')" ))
137
160
161
+ lsToolsCmd :: OA. Mod OA. CommandFields LsCmds
162
+ lsToolsCmd =
163
+ OA. command
164
+ " tools"
165
+ (OA. info lsToolsOptsParser (OA. progDesc " View stack's installed tools" ))
166
+
138
167
data Snapshot = Snapshot
139
168
{ snapId :: Text
140
169
, snapTitle :: Text
@@ -242,6 +271,7 @@ handleLocal lsOpts = do
242
271
_ -> liftIO $ displayLocalSnapshot isStdoutTerminal snapData
243
272
LsDependencies _ -> return ()
244
273
LsStyles _ -> return ()
274
+ LsTools _ -> return ()
245
275
246
276
handleRemote
247
277
:: HasRunner env
@@ -266,6 +296,7 @@ handleRemote lsOpts = do
266
296
_ -> liftIO $ displaySnapshotData isStdoutTerminal snapData
267
297
LsDependencies _ -> return ()
268
298
LsStyles _ -> return ()
299
+ LsTools _ -> return ()
269
300
where
270
301
urlInfo = " https://www.stackage.org/snapshots"
271
302
@@ -278,6 +309,7 @@ lsCmd lsOpts =
278
309
Remote -> handleRemote lsOpts
279
310
LsDependencies depOpts -> listDependenciesCmd False depOpts
280
311
LsStyles stylesOpts -> withConfig NoReexec $ listStylesCmd stylesOpts
312
+ LsTools toolsOpts -> withConfig NoReexec $ listToolsCmd toolsOpts
281
313
282
314
-- | List the dependencies
283
315
listDependenciesCmd :: Bool -> ListDepsOpts -> RIO Runner ()
@@ -325,3 +357,17 @@ listStylesCmd opts = do
325
357
example = " " <> ansi <> " Example" <> reset
326
358
ansi = fromString $ setSGRCode sgrs
327
359
reset = fromString $ setSGRCode [Reset ]
360
+
361
+ -- | List stack's installed tools, sorted (see instance of 'Ord' for 'Tool').
362
+ listToolsCmd :: ListToolsOpts -> RIO Config ()
363
+ listToolsCmd opts = do
364
+ localPrograms <- view $ configL. to configLocalPrograms
365
+ installed <- sort <$> listInstalled localPrograms
366
+ let wanted = case toptFilter opts of
367
+ [] -> installed
368
+ " ghc-git" -> [t | t@ (ToolGhcGit _ _) <- installed]
369
+ pkgName -> filtered pkgName installed
370
+ liftIO $ mapM_ (putStrLn . toolString) wanted
371
+ where
372
+ filtered pkgName installed = Tool <$>
373
+ filterTools (mkPackageName pkgName) (const True ) installed
0 commit comments