forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDE.hs
More file actions
101 lines (91 loc) · 3.43 KB
/
Copy pathIDE.hs
File metadata and controls
101 lines (91 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
-- | Types and functions related to Stack's @ide@ command.
module Stack.IDE
( OutputStream (..)
, ListPackagesCmd (..)
, idePackagesCmd
, ideTargetsCmd
, listPackages
, listTargets
) where
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Tuple ( swap )
import Stack.Prelude
import Stack.Runners
( ShouldReexec (..), withBuildConfig, withConfig )
import Stack.Types.BuildConfig
( BuildConfig (..), HasBuildConfig (..) )
import Stack.Types.NamedComponent
( NamedComponent, isCBench, isCExe, isCTest
, renderPkgComponent
)
import Stack.Types.Runner ( Runner )
import Stack.Types.SourceMap
( ProjectPackage (..), SMWanted (..), ppComponentsMaybe )
import System.IO ( putStrLn )
-- Type representing output channel choices for the @stack ide packages@ and
-- @stack ide targets@ commands.
data OutputStream
= OutputLogInfo
-- ^ To the same output channel as other log information.
| OutputStdout
-- ^ To the standard output channel.
-- Type representing output choices for the @stack ide packages@ command.
data ListPackagesCmd
= ListPackageNames
-- ^ Package names.
| ListPackageCabalFiles
-- ^ Paths to Cabal files.
-- | Function underlying the @stack ide packages@ command. List packages in the
-- project.
idePackagesCmd :: (OutputStream, ListPackagesCmd) -> RIO Runner ()
idePackagesCmd =
withConfig NoReexec . withBuildConfig . uncurry listPackages
compTypes :: (Bool, Bool, Bool) -> NamedComponent -> Bool
compTypes (False, False, False) = const True
compTypes (exe, test, bench) =
\x -> (exe && isCExe x) || (test && isCTest x) || (bench && isCBench x)
-- | Function underlying the @stack ide targets@ command. List targets in the
-- project.
ideTargetsCmd :: ((Bool, Bool, Bool), OutputStream) -> RIO Runner ()
ideTargetsCmd = withConfig NoReexec .
withBuildConfig . uncurry listTargets . fmap compTypes . swap
outputFunc :: HasTerm env => OutputStream -> String -> RIO env ()
outputFunc OutputLogInfo = prettyInfo . fromString
outputFunc OutputStdout = liftIO . putStrLn
-- | List the packages inside the current project.
listPackages ::
HasBuildConfig env
=> OutputStream
-> ListPackagesCmd
-> RIO env ()
listPackages stream flag = do
packages <- view $ buildConfigL.to (smwProject . bcSMWanted)
let strs = case flag of
ListPackageNames ->
map packageNameString (Map.keys packages)
ListPackageCabalFiles ->
map (toFilePath . ppCabalFP) (Map.elems packages)
mapM_ (outputFunc stream) strs
-- | List the targets in the current project.
listTargets ::
forall env. HasBuildConfig env
=> OutputStream
-> (NamedComponent -> Bool)
-> RIO env ()
listTargets stream isCompType = do
packages <- view $ buildConfigL.to (smwProject . bcSMWanted)
pairs <- concat <$> Map.traverseWithKey toNameAndComponent packages
outputFunc stream $ T.unpack $ T.intercalate "\n" $
map renderPkgComponent pairs
where
toNameAndComponent ::
PackageName
-> ProjectPackage
-> RIO env [(PackageName, NamedComponent)]
toNameAndComponent pkgName' =
fmap (map (pkgName',) . Set.toList) . ppComponentsMaybe (\x ->
if isCompType x then Just x else Nothing)