Skip to content

Commit 4c822fc

Browse files
committed
Refactor Stack's config command
Also adds missing Haddock documentation.
1 parent 572476e commit 4c822fc

File tree

10 files changed

+251
-175
lines changed

10 files changed

+251
-175
lines changed

.stan.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@
7272

7373
# Anti-pattern: Data.ByteString.Char8.pack
7474
[[ignore]]
75-
id = "OBS-STAN-0203-erw24B-1042:3"
75+
id = "OBS-STAN-0203-erw24B-1044:3"
7676
# ✦ Description: Usage of 'pack' function that doesn't handle Unicode characters
7777
# ✦ Category: #AntiPattern
7878
# ✦ File: src\Stack\Build\ExecuteEnv.hs
7979
#
80-
# 1041
81-
# 1042 ┃ S8.pack . formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S%6Q"
82-
# 1043 ┃ ^^^^^^^
80+
# 1043
81+
# 1044 ┃ S8.pack . formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S%6Q"
82+
# 1045 ┃ ^^^^^^^
8383

8484
# Anti-pattern: Data.ByteString.Char8.pack
8585
[[ignore]]

package.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ library:
224224
- Stack.Options.BuildParser
225225
- Stack.Options.CleanParser
226226
- Stack.Options.ConfigParser
227+
- Stack.Options.ConfigEnvParser
228+
- Stack.Options.ConfigSetParser
227229
- Stack.Options.Completion
228230
- Stack.Options.DockerParser
229231
- Stack.Options.DotParser
@@ -294,6 +296,7 @@ library:
294296
- Stack.Types.Config
295297
- Stack.Types.Config.Exception
296298
- Stack.Types.ConfigMonoid
299+
- Stack.Types.ConfigSetOpts
297300
- Stack.Types.ConfigureOpts
298301
- Stack.Types.Curator
299302
- Stack.Types.Dependency
@@ -339,6 +342,7 @@ library:
339342
- Stack.Types.SCM
340343
- Stack.Types.SDistOpts
341344
- Stack.Types.SetupInfo
345+
- Stack.Types.SetupOpts
342346
- Stack.Types.Snapshot
343347
- Stack.Types.SourceMap
344348
- Stack.Types.StackYamlLoc

src/GHC/Utils/GhcPkg/Main/Compat.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ with:
2323
* consistency checks are not performed,
2424
* use Stack program name,
2525
* use "Stack.Prelude" rather than "Prelude",
26-
* use t
27-
'RIO' @env@ monad,
26+
* use t'RIO' @env@ monad,
2827
* use well-typed representations of paths from the @path@ package,
2928
* add pretty messages and exceptions,
3029
* redundant code deleted,

src/Stack/CLI.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ import Stack.BuildInfo ( hpackVersion, versionString' )
3333
import Stack.Clean ( CleanCommand (..), cleanCmd )
3434
import Stack.ConfigCmd
3535
( cfgCmdBuildFiles, cfgCmdBuildFilesName, cfgCmdEnv
36-
, cfgCmdEnvName, configCmdEnvParser, cfgCmdName, cfgCmdSet
37-
, cfgCmdSetName, configCmdSetParser
36+
, cfgCmdEnvName, cfgCmdName, cfgCmdSet, cfgCmdSetName
3837
)
3938
import Stack.Constants
4039
( globalFooter, osIsWindows, relFileStack, relFileStackDotExe
@@ -60,6 +59,8 @@ import Stack.New ( newCmd )
6059
import qualified Stack.Nix as Nix
6160
import Stack.Options.BuildParser ( buildOptsParser )
6261
import Stack.Options.CleanParser ( cleanOptsParser )
62+
import Stack.Options.ConfigEnvParser ( configCmdEnvParser )
63+
import Stack.Options.ConfigSetParser ( configCmdSetParser )
6364
import Stack.Options.DotParser ( dotOptsParser )
6465
import Stack.Options.EvalParser ( evalOptsParser )
6566
import Stack.Options.ExecParser ( execOptsParser )

src/Stack/ConfigCmd.hs

Lines changed: 10 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ Make changes to project or global configuration.
1313
-}
1414

1515
module Stack.ConfigCmd
16-
( ConfigCmdSet (..)
17-
, CommandScope
18-
, configCmdSetParser
19-
, cfgCmdSet
16+
( cfgCmdSet
2017
, cfgCmdSetName
21-
, configCmdEnvParser
2218
, cfgCmdEnv
2319
, cfgCmdEnvName
2420
, cfgCmdBuildFiles
@@ -35,9 +31,6 @@ import Data.Attoparsec.Text as P
3531
import qualified Data.Map.Merge.Strict as Map
3632
import qualified Data.Text as T
3733
import qualified Data.Yaml as Yaml
38-
import qualified Options.Applicative as OA
39-
import Options.Applicative.Builder.Extra
40-
import qualified Options.Applicative.Types as OA
4134
import Pantry ( loadSnapshot )
4235
import Path ( (</>), parent )
4336
import qualified RIO.Map as Map
@@ -58,13 +51,15 @@ import Stack.Types.ConfigMonoid
5851
, configMonoidRecommendStackUpgradeName
5952
, configMonoidSystemGHCName
6053
)
54+
import Stack.Types.ConfigSetOpts
55+
( CommandScope (..), ConfigCmdSet (..) ,configCmdSetScope )
6156
import Stack.Types.EnvConfig ( EnvConfig )
6257
import Stack.Types.EnvSettings ( EnvSettings (..) )
6358
import Stack.Types.GHCVariant ( HasGHCVariant )
6459
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
6560
import Stack.Types.ProjectConfig ( ProjectConfig (..) )
6661
import Stack.Types.Runner ( globalOptsL )
67-
import Stack.Types.Snapshot ( AbstractSnapshot, readAbstractSnapshot )
62+
import Stack.Types.Snapshot ( AbstractSnapshot )
6863
import System.Environment ( getEnvironment )
6964

7065
-- | Type repesenting exceptions thrown by functions exported by the
@@ -78,31 +73,7 @@ instance Exception ConfigCmdException where
7873
"Error: [S-3136]\n"
7974
++ "'config' command used when no project configuration available."
8075

81-
data ConfigCmdSet
82-
= ConfigCmdSetSnapshot !(Unresolved AbstractSnapshot)
83-
| ConfigCmdSetResolver !(Unresolved AbstractSnapshot)
84-
| ConfigCmdSetSystemGhc !CommandScope !Bool
85-
| ConfigCmdSetInstallGhc !CommandScope !Bool
86-
| ConfigCmdSetInstallMsys !CommandScope !Bool
87-
| ConfigCmdSetRecommendStackUpgrade !CommandScope !Bool
88-
| ConfigCmdSetDownloadPrefix !CommandScope !Text
89-
90-
data CommandScope
91-
= CommandScopeGlobal
92-
-- ^ Apply changes to the global configuration,
93-
-- typically at @~/.stack/config.yaml@.
94-
| CommandScopeProject
95-
-- ^ Apply changes to the project @stack.yaml@.
96-
97-
configCmdSetScope :: ConfigCmdSet -> CommandScope
98-
configCmdSetScope (ConfigCmdSetSnapshot _) = CommandScopeProject
99-
configCmdSetScope (ConfigCmdSetResolver _) = CommandScopeProject
100-
configCmdSetScope (ConfigCmdSetSystemGhc scope _) = scope
101-
configCmdSetScope (ConfigCmdSetInstallGhc scope _) = scope
102-
configCmdSetScope (ConfigCmdSetInstallMsys scope _) = scope
103-
configCmdSetScope (ConfigCmdSetRecommendStackUpgrade scope _) = scope
104-
configCmdSetScope (ConfigCmdSetDownloadPrefix scope _) = scope
105-
76+
-- | Function underlying Stack's @config set@ command.
10677
cfgCmdSet ::
10778
(HasConfig env, HasGHCVariant env)
10879
=> ConfigCmdSet -> RIO env ()
@@ -309,148 +280,26 @@ cfgCmdSetKeys (ConfigCmdSetRecommendStackUpgrade _ _) =
309280
cfgCmdSetKeys (ConfigCmdSetDownloadPrefix _ _) =
310281
[["package-index", "download-prefix"]]
311282

283+
-- | The name of Stack's @config@ command.
312284
cfgCmdName :: String
313285
cfgCmdName = "config"
314286

287+
-- | The name of Stack's @config@ command's @set@ subcommand.
315288
cfgCmdSetName :: String
316289
cfgCmdSetName = "set"
317290

291+
-- | The name of Stack's @config@ command's @env@ subcommand.
318292
cfgCmdEnvName :: String
319293
cfgCmdEnvName = "env"
320294

295+
-- | The name of Stack's @config@ command's @build-files@ subcommand.
321296
cfgCmdBuildFilesName :: String
322297
cfgCmdBuildFilesName = "build-files"
323298

324-
configCmdSetParser :: OA.Parser ConfigCmdSet
325-
configCmdSetParser =
326-
OA.hsubparser $
327-
mconcat
328-
[ OA.command "snapshot"
329-
( OA.info
330-
( ConfigCmdSetSnapshot
331-
<$> OA.argument
332-
readAbstractSnapshot
333-
( OA.metavar "SNAPSHOT"
334-
<> OA.help "E.g. \"nightly\" or \"lts-22.8\"" ))
335-
( OA.progDesc
336-
"Change the snapshot of the current project." ))
337-
, OA.command "resolver"
338-
( OA.info
339-
( ConfigCmdSetResolver
340-
<$> OA.argument
341-
readAbstractSnapshot
342-
( OA.metavar "SNAPSHOT"
343-
<> OA.help "E.g. \"nightly\" or \"lts-22.8\"" ))
344-
( OA.progDesc
345-
"Change the snapshot of the current project, using the \
346-
\resolver key." ))
347-
, OA.command (T.unpack configMonoidSystemGHCName)
348-
( OA.info
349-
( ConfigCmdSetSystemGhc
350-
<$> globalScopeFlag
351-
<*> boolArgument )
352-
( OA.progDesc
353-
"Configure whether or not Stack should use a system GHC \
354-
\installation." ))
355-
, OA.command (T.unpack configMonoidInstallGHCName)
356-
( OA.info
357-
( ConfigCmdSetInstallGhc
358-
<$> globalScopeFlag
359-
<*> boolArgument )
360-
( OA.progDesc
361-
"Configure whether or not Stack should automatically install \
362-
\GHC when necessary." ))
363-
, OA.command (T.unpack configMonoidInstallMsysName)
364-
( OA.info
365-
( ConfigCmdSetInstallMsys
366-
<$> globalScopeFlag
367-
<*> boolArgument )
368-
( OA.progDesc
369-
"Configure whether or not Stack should automatically install \
370-
\MSYS2 when necessary." ))
371-
, OA.command (T.unpack configMonoidRecommendStackUpgradeName)
372-
( OA.info
373-
( ConfigCmdSetRecommendStackUpgrade
374-
<$> projectScopeFlag
375-
<*> boolArgument )
376-
( OA.progDesc
377-
"Configure whether or not Stack should notify the user if it \
378-
\identifes a new version of Stack is available." ))
379-
, OA.command "package-index"
380-
( OA.info
381-
( OA.hsubparser $
382-
OA.command "download-prefix"
383-
( OA.info
384-
( ConfigCmdSetDownloadPrefix
385-
<$> globalScopeFlag
386-
<*> urlArgument )
387-
( OA.progDesc
388-
"Configure download prefix for Stack's package \
389-
\index." )))
390-
( OA.progDesc
391-
"Configure Stack's package index" ))
392-
]
393-
394-
globalScopeFlag :: OA.Parser CommandScope
395-
globalScopeFlag = OA.flag
396-
CommandScopeProject
397-
CommandScopeGlobal
398-
( OA.long "global"
399-
<> OA.help
400-
"Modify the user-specific global configuration file ('config.yaml') \
401-
\instead of the project-level configuration file ('stack.yaml')."
402-
)
403-
404-
projectScopeFlag :: OA.Parser CommandScope
405-
projectScopeFlag = OA.flag
406-
CommandScopeGlobal
407-
CommandScopeProject
408-
( OA.long "project"
409-
<> OA.help
410-
"Modify the project-level configuration file ('stack.yaml') instead of \
411-
\the user-specific global configuration file ('config.yaml')."
412-
)
413-
414-
readBool :: OA.ReadM Bool
415-
readBool = do
416-
s <- OA.readerAsk
417-
case s of
418-
"true" -> pure True
419-
"false" -> pure False
420-
_ -> OA.readerError ("Invalid value " ++ show s ++
421-
": Expected \"true\" or \"false\"")
422-
423-
boolArgument :: OA.Parser Bool
424-
boolArgument = OA.argument
425-
readBool
426-
( OA.metavar "true|false"
427-
<> OA.completeWith ["true", "false"]
428-
)
429-
430-
urlArgument :: OA.Parser Text
431-
urlArgument = OA.strArgument
432-
( OA.metavar "URL"
433-
<> OA.value defaultDownloadPrefix
434-
<> OA.showDefault
435-
<> OA.help
436-
"Location of package index. It is highly recommended to use only the \
437-
\official Hackage server or a mirror."
438-
)
439-
440-
configCmdEnvParser :: OA.Parser EnvSettings
441-
configCmdEnvParser = EnvSettings
442-
<$> boolFlags True "locals" "include information about local packages" mempty
443-
<*> boolFlags True
444-
"ghc-package-path" "set GHC_PACKAGE_PATH environment variable" mempty
445-
<*> boolFlags True "stack-exe" "set STACK_EXE environment variable" mempty
446-
<*> boolFlags False
447-
"locale-utf8" "set the GHC_CHARENC environment variable to UTF-8" mempty
448-
<*> boolFlags False
449-
"keep-ghc-rts" "keep any GHCRTS environment variable" mempty
450-
451299
data EnvVarAction = EVASet !Text | EVAUnset
452300
deriving Show
453301

302+
-- | Function underlying Stack's @config env@ command.
454303
cfgCmdEnv :: EnvSettings -> RIO EnvConfig ()
455304
cfgCmdEnv es = do
456305
origEnv <- liftIO $ Map.fromList . map (first fromString) <$> getEnvironment

src/Stack/Options/ConfigEnvParser.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
{-|
4+
Module : Stack.Options.ConfigEnvParser
5+
License : BSD-3-Clause
6+
7+
Functions to parse command line arguments for Stack's @config env@ command.
8+
-}
9+
10+
module Stack.Options.ConfigEnvParser
11+
( configCmdEnvParser
12+
) where
13+
14+
import qualified Options.Applicative as OA
15+
import Options.Applicative.Builder.Extra ( boolFlags )
16+
import Stack.Prelude
17+
import Stack.Types.EnvSettings ( EnvSettings (..) )
18+
19+
-- | Parse command line arguments for Stack's @config env@ command.
20+
configCmdEnvParser :: OA.Parser EnvSettings
21+
configCmdEnvParser = EnvSettings
22+
<$> boolFlags True "locals" "include information about local packages" mempty
23+
<*> boolFlags True
24+
"ghc-package-path" "set GHC_PACKAGE_PATH environment variable" mempty
25+
<*> boolFlags True "stack-exe" "set STACK_EXE environment variable" mempty
26+
<*> boolFlags False
27+
"locale-utf8" "set the GHC_CHARENC environment variable to UTF-8" mempty
28+
<*> boolFlags False
29+
"keep-ghc-rts" "keep any GHCRTS environment variable" mempty

0 commit comments

Comments
 (0)