Skip to content

Commit 246c6cf

Browse files
committed
Refactor: Separate Stack.Options.IdeParser and Stack.Types.IdeOpts
1 parent 93f258e commit 246c6cf

File tree

6 files changed

+115
-61
lines changed

6 files changed

+115
-61
lines changed

package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ library:
238238
- Stack.Options.GlobalParser
239239
- Stack.Options.HaddockParser
240240
- Stack.Options.HpcReportParser
241+
- Stack.Options.IdeParser
241242
- Stack.Options.InitParser
242243
- Stack.Options.LogLevelParser
243244
- Stack.Options.LsParser
@@ -323,6 +324,7 @@ library:
323324
- Stack.Types.GlobalOpts
324325
- Stack.Types.GlobalOptsMonoid
325326
- Stack.Types.HpcReportOpts
327+
- Stack.Types.IdeOpts
326328
- Stack.Types.Installed
327329
- Stack.Types.InterfaceOpt
328330
- Stack.Types.IsMutable

src/Stack/CLI.hs

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import Data.Char ( toLower )
1515
import qualified Data.List as L
1616
import Data.List.NonEmpty ( prependList )
1717
import Options.Applicative
18-
( Parser, ParserFailure, ParserHelp, ParserResult (..), flag, switch
18+
( Parser, ParserFailure, ParserHelp, ParserResult (..)
1919
, handleParseResult, help, helpError, idm, long, metavar
20-
, overFailure, renderFailure, strArgument, switch )
20+
, overFailure, renderFailure, strArgument, switch
21+
)
2122
import Options.Applicative.Help ( errorHelp, stringChunk, vcatChunks )
2223
import Options.Applicative.Builder.Extra
2324
( boolFlags, extraHelpOption )
@@ -48,10 +49,7 @@ import Stack.Exec ( SpecialExecCmd (..), execCmd )
4849
import Stack.Eval ( evalCmd )
4950
import Stack.Ghci ( ghciCmd )
5051
import Stack.Hoogle ( hoogleCmd )
51-
import Stack.IDE
52-
( ListPackagesCmd (..), OutputStream (..), idePackagesCmd
53-
, ideTargetsCmd
54-
)
52+
import Stack.IDE ( idePackagesCmd, ideTargetsCmd )
5553
import Stack.Init ( initCmd )
5654
import Stack.List ( listCmd )
5755
import Stack.Ls ( lsCmd )
@@ -67,6 +65,7 @@ import Stack.Options.ExecParser ( execOptsParser )
6765
import Stack.Options.GhciParser ( ghciOptsParser )
6866
import Stack.Options.GlobalParser ( globalOptsParser )
6967
import Stack.Options.HpcReportParser ( hpcReportOptsParser )
68+
import Stack.Options.IdeParser ( idePackagesParser, ideTargetsParser )
7069
import Stack.Options.InitParser ( initOptsParser )
7170
import Stack.Options.LsParser ( lsOptsParser )
7271
import Stack.Options.NewParser ( newOptsParser )
@@ -358,46 +357,17 @@ commandLineHandler currentDir progName mExecutablePath isInterpreter =
358357
ide = addSubCommands'
359358
"ide"
360359
"IDE-specific commands."
361-
( let outputFlag = flag
362-
OutputLogInfo
363-
OutputStdout
364-
( long "stdout"
365-
<> help "Send output to the standard output stream instead of the \
366-
\default, the standard error stream."
367-
)
368-
cabalFileFlag = flag
369-
ListPackageNames
370-
ListPackageCabalFiles
371-
( long "cabal-files"
372-
<> help "Print paths to package Cabal files instead of package \
373-
\names."
374-
)
375-
exeFlag = switch
376-
( long "exes"
377-
<> help "Include executables."
378-
)
379-
testFlag = switch
380-
( long "tests"
381-
<> help "Include test suites."
382-
)
383-
benchFlag = switch
384-
( long "benchmarks"
385-
<> help "Include benchmarks."
386-
)
387-
in do
388-
addCommand'
389-
"packages"
390-
"List all available local loadable packages."
391-
idePackagesCmd
392-
((,) <$> outputFlag <*> cabalFileFlag)
393-
addCommand'
394-
"targets"
395-
"List all targets or pick component types to list."
396-
ideTargetsCmd
397-
( (,)
398-
<$> ((,,) <$> exeFlag <*> testFlag <*> benchFlag)
399-
<*> outputFlag
400-
)
360+
( do
361+
addCommand'
362+
"packages"
363+
"List all available local loadable packages."
364+
idePackagesCmd
365+
idePackagesParser
366+
addCommand'
367+
"targets"
368+
"List all targets or pick component types to list."
369+
ideTargetsCmd
370+
ideTargetsParser
401371
)
402372

403373
init = addCommand'

src/Stack/IDE.hs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import Stack.Runners
2828
( ShouldReexec (..), withBuildConfig, withConfig )
2929
import Stack.Types.BuildConfig
3030
( BuildConfig (..), HasBuildConfig (..) )
31+
import Stack.Types.IdeOpts ( ListPackagesCmd (..), OutputStream (..) )
3132
import Stack.Types.NamedComponent
3233
( NamedComponent, isCBench, isCExe, isCTest
3334
, renderPkgComponent
@@ -37,21 +38,6 @@ import Stack.Types.SourceMap
3738
( ProjectPackage (..), SMWanted (..), ppComponentsMaybe )
3839
import System.IO ( putStrLn )
3940

40-
-- | Type representing output stream choices for the @stack ide packages@ and
41-
-- @stack ide targets@ commands.
42-
data OutputStream
43-
= OutputLogInfo
44-
-- ^ To the same output stream as other log information.
45-
| OutputStdout
46-
-- ^ To the standard output stream.
47-
48-
-- | Type representing output choices for the @stack ide packages@ command.
49-
data ListPackagesCmd
50-
= ListPackageNames
51-
-- ^ Package names.
52-
| ListPackageCabalFiles
53-
-- ^ Paths to Cabal files.
54-
5541
-- | Function underlying the @stack ide packages@ command. List packages in the
5642
-- project.
5743
idePackagesCmd :: (OutputStream, ListPackagesCmd) -> RIO Runner ()

src/Stack/Options/IdeParser.hs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
4+
{-|
5+
Module : Stack.Options.IdeParser
6+
Description : Parse arguments for Stack's @ide@ commands.
7+
License : BSD-3-Clause
8+
9+
Functions to parse command line arguments for Stack's @ide@ commands.
10+
-}
11+
12+
module Stack.Options.IdeParser
13+
( idePackagesParser
14+
, ideTargetsParser
15+
) where
16+
17+
import Options.Applicative ( Parser, flag, help, long, switch )
18+
import Stack.Prelude
19+
import Stack.Types.IdeOpts ( ListPackagesCmd (..), OutputStream (..) )
20+
21+
-- | Parse command line arguments for Stack's @ide packages@ command.
22+
idePackagesParser :: Parser (OutputStream, ListPackagesCmd)
23+
idePackagesParser = (,) <$> outputFlag <*> cabalFileFlag
24+
25+
-- | Parse command line arguments for Stack's @ide targets@ command.
26+
ideTargetsParser :: Parser ((Bool, Bool, Bool), OutputStream)
27+
ideTargetsParser =
28+
(,) <$> ((,,) <$> exeFlag <*> testFlag <*> benchFlag) <*> outputFlag
29+
30+
outputFlag :: Parser OutputStream
31+
outputFlag = flag
32+
OutputLogInfo
33+
OutputStdout
34+
( long "stdout"
35+
<> help "Send output to the standard output stream instead of the \
36+
\default, the standard error stream."
37+
)
38+
39+
cabalFileFlag :: Parser ListPackagesCmd
40+
cabalFileFlag = flag
41+
ListPackageNames
42+
ListPackageCabalFiles
43+
( long "cabal-files"
44+
<> help "Print paths to package Cabal files instead of package \
45+
\names."
46+
)
47+
48+
exeFlag :: Parser Bool
49+
exeFlag = switch
50+
( long "exes"
51+
<> help "Include executables."
52+
)
53+
54+
testFlag :: Parser Bool
55+
testFlag = switch
56+
( long "tests"
57+
<> help "Include test suites."
58+
)
59+
60+
benchFlag :: Parser Bool
61+
benchFlag = switch
62+
( long "benchmarks"
63+
<> help "Include benchmarks."
64+
)

src/Stack/Types/IdeOpts.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
{-# LANGUAGE NoFieldSelectors #-}
3+
4+
{-|
5+
Module : Stack.Types.IdeOpts
6+
Description : Types for command line options for Stack's @ide@ commands.
7+
License : BSD-3-Clause
8+
9+
Types for command line options for Stack's @ide@ commands.
10+
-}
11+
12+
module Stack.Types.IdeOpts
13+
( OutputStream (..)
14+
, ListPackagesCmd (..)
15+
) where
16+
17+
-- | Type representing output stream choices for the @stack ide packages@ and
18+
-- @stack ide targets@ commands.
19+
data OutputStream
20+
= OutputLogInfo
21+
-- ^ To the same output stream as other log information.
22+
| OutputStdout
23+
-- ^ To the standard output stream.
24+
25+
-- | Type representing output choices for the @stack ide packages@ command.
26+
data ListPackagesCmd
27+
= ListPackageNames
28+
-- ^ Package names.
29+
| ListPackageCabalFiles
30+
-- ^ Paths to Cabal files.

stack.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ library
274274
Stack.Options.GlobalParser
275275
Stack.Options.HaddockParser
276276
Stack.Options.HpcReportParser
277+
Stack.Options.IdeParser
277278
Stack.Options.InitParser
278279
Stack.Options.LogLevelParser
279280
Stack.Options.LsParser
@@ -359,6 +360,7 @@ library
359360
Stack.Types.GlobalOpts
360361
Stack.Types.GlobalOptsMonoid
361362
Stack.Types.HpcReportOpts
363+
Stack.Types.IdeOpts
362364
Stack.Types.Installed
363365
Stack.Types.InterfaceOpt
364366
Stack.Types.IsMutable

0 commit comments

Comments
 (0)