Skip to content

Commit 088af9e

Browse files
committed
Reduce code duplication
Moves `packagesParser` into its own module, and uses it in modules `Stack.Options.GhciParser` and `Stack.Options.ScriptParser`. Creates `loadCommonPackage'` and uses it in `loadCommonPackage` and `loadLocalPackage`. Also adds missing Haddock documentation.
1 parent cfd3489 commit 088af9e

File tree

7 files changed

+67
-43
lines changed

7 files changed

+67
-43
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ library:
244244
- Stack.Options.NewParser
245245
- Stack.Options.NixParser
246246
- Stack.Options.PackageParser
247+
- Stack.Options.PackagesParser
247248
- Stack.Options.PathParser
248249
- Stack.Options.PvpBoundsParser
249250
- Stack.Options.SDistParser

src/Stack/Build/Source.hs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import Stack.Types.Platform ( HasPlatform (..) )
7474
import Stack.Types.SourceMap
7575
( CommonPackage (..), DepPackage (..), ProjectPackage (..)
7676
, SMActual (..), SMTargets (..), SourceMap (..)
77-
, SourceMapHash (..), Target (..), ppGPD, ppRoot
77+
, SourceMapHash (..), Target (..), ppRoot
7878
)
7979
import Stack.Types.UnusedFlags ( FlagSource (..), UnusedFlags (..) )
8080
import System.FilePath ( takeFileName )
@@ -330,18 +330,28 @@ generalGhcOptions bconfig boptsCli isTarget isLocal = concat
330330
AGOLocals -> isLocal
331331
AGOEverything -> True
332332

333+
-- | Yield a t'Package' from the settings common to dependency and project
334+
-- packages.
333335
loadCommonPackage ::
334336
forall env. (HasBuildConfig env, HasSourceMap env)
335337
=> CommonPackage
336338
-> RIO env Package
337339
loadCommonPackage common = do
340+
(_, _, pkg) <- loadCommonPackage' common
341+
pure pkg
342+
343+
loadCommonPackage' ::
344+
forall env. (HasBuildConfig env, HasSourceMap env)
345+
=> CommonPackage
346+
-> RIO env (PackageConfig, C.GenericPackageDescription, Package)
347+
loadCommonPackage' common = do
338348
config <-
339349
getPackageConfig
340350
common.flags
341351
common.ghcOptions
342352
common.cabalConfigOpts
343353
gpkg <- liftIO common.gpd
344-
pure $ resolvePackage config gpkg
354+
pure (config, gpkg, resolvePackage config gpkg)
345355

346356
-- | Upgrade the initial project package info to a full-blown @LocalPackage@
347357
-- based on the selected components
@@ -354,11 +364,7 @@ loadLocalPackage pp = do
354364
let common = pp.projectCommon
355365
bopts <- view buildOptsL
356366
mcurator <- view $ buildConfigL . to (.curator)
357-
config <- getPackageConfig
358-
common.flags
359-
common.ghcOptions
360-
common.cabalConfigOpts
361-
gpkg <- ppGPD pp
367+
(config, gpkg, pkg) <- loadCommonPackage' common
362368
let name = common.name
363369
mtarget = M.lookup name sm.targets.targets
364370
(exeCandidates, testCandidates, benchCandidates) =
@@ -426,7 +432,6 @@ loadLocalPackage pp = do
426432
-- --enable-benchmarks or --enable-tests are configured. This allows us to
427433
-- do an optimization where these are passed if the deps are present. This
428434
-- can avoid doing later unnecessary reconfigures.
429-
pkg = resolvePackage config gpkg
430435
btpkg
431436
| Set.null tests && Set.null benches = Nothing
432437
| otherwise = Just (resolvePackage btconfig gpkg)

src/Stack/Config.hs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ spitting out a warning that "you should run `stk init` to make things better".
2525
module Stack.Config
2626
( loadConfig
2727
, loadConfigYaml
28-
, packagesParser
2928
, getImplicitGlobalProjectDir
3029
, getSnapshots
3130
, makeConcreteSnapshot
@@ -67,7 +66,6 @@ import qualified Hpack
6766
import GHC.Conc ( getNumProcessors )
6867
import Network.HTTP.StackClient
6968
( httpJSON, parseUrlThrow, getResponseBody )
70-
import Options.Applicative ( Parser, help, long, metavar, strOption )
7169
import Pantry ( loadSnapshot )
7270
import Path
7371
( PathException (..), (</>), parent, parseAbsDir
@@ -1291,32 +1289,27 @@ getDefaultUserConfigPath configRoot = do
12911289
liftIO $ writeBinaryFileAtomic userConfigPath defaultConfigYaml
12921290
pure userConfigPath
12931291

1294-
packagesParser :: Parser [String]
1295-
packagesParser = many (strOption
1296-
(long "package" <>
1297-
metavar "PACKAGE" <>
1298-
help "Add a package (can be specified multiple times)"))
1299-
1292+
-- | The contents of the default Stack global configuration file.
13001293
defaultConfigYaml :: (IsString s, Semigroup s) => s
13011294
defaultConfigYaml =
1302-
"# This file contains default non-project-specific settings for Stack, used\n" <>
1303-
"# in all projects. For more information about Stack's configuration, see\n" <>
1304-
"# http://docs.haskellstack.org/en/stable/configure/yaml/\n" <>
1305-
"\n" <>
1306-
"# The following parameters are used by 'stack new' to automatically fill fields\n" <>
1307-
"# in the Cabal file. We recommend uncommenting them and filling them out if\n" <>
1308-
"# you intend to use 'stack new'.\n" <>
1309-
"# See https://docs.haskellstack.org/en/stable/configure/yaml/non-project/#templates\n" <>
1310-
"templates:\n" <>
1311-
" params:\n" <>
1312-
"# author-name:\n" <>
1313-
"# author-email:\n" <>
1314-
"# copyright:\n" <>
1315-
"# github-username:\n" <>
1316-
"\n" <>
1317-
"# The following parameter specifies Stack's output styles; STYLES is a\n" <>
1318-
"# colon-delimited sequence of key=value, where 'key' is a style name and\n" <>
1319-
"# 'value' is a semicolon-delimited list of 'ANSI' SGR (Select Graphic\n" <>
1320-
"# Rendition) control codes (in decimal). Use 'stack ls stack-colors --basic'\n" <>
1321-
"# to see the current sequence.\n" <>
1322-
"# stack-colors: STYLES\n"
1295+
"# This file contains default non-project-specific settings for Stack, used\n\
1296+
\# in all projects. For more information about Stack's configuration, see\n\
1297+
\# http://docs.haskellstack.org/en/stable/configure/yaml/\n\
1298+
\\n\
1299+
\# The following parameters are used by 'stack new' to automatically fill fields\n\
1300+
\# in the Cabal file. We recommend uncommenting them and filling them out if\n\
1301+
\# you intend to use 'stack new'.\n\
1302+
\# See https://docs.haskellstack.org/en/stable/configure/yaml/non-project/#templates\n\
1303+
\templates:\n\
1304+
\ params:\n\
1305+
\# author-name:\n\
1306+
\# author-email:\n\
1307+
\# copyright:\n\
1308+
\# github-username:\n\
1309+
\\n\
1310+
\# The following parameter specifies Stack's output styles; STYLES is a\n\
1311+
\# colon-delimited sequence of key=value, where 'key' is a style name and\n\
1312+
\# 'value' is a semicolon-delimited list of 'ANSI' SGR (Select Graphic\n\
1313+
\# Rendition) control codes (in decimal). Use 'stack ls stack-colors --basic'\n\
1314+
\# to see the current sequence.\n\
1315+
\# stack-colors: STYLES\n"

src/Stack/Options/GhciParser.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import Options.Applicative.Builder.Extra
1919
( boolFlags, boolFlagsNoDefault, fileExtCompleter
2020
, textArgument, textOption
2121
)
22-
import Stack.Config ( packagesParser )
2322
import Stack.Ghci ( GhciOpts (..) )
2423
import Stack.Options.Completion ( ghcOptsCompleter, targetCompleter )
2524
import Stack.Options.FlagsParser ( flagsParser )
25+
import Stack.Options.PackagesParser ( packagesParser )
2626
import Stack.Prelude
2727

2828
-- | Parser for GHCI options

src/Stack/Options/PackagesParser.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
4+
{-|
5+
Module : Stack.Options.PackagesParser
6+
Description : Parser for one or more package names.
7+
License : BSD-3-Clause
8+
9+
Parser for one or more package names.
10+
-}
11+
12+
module Stack.Options.PackagesParser
13+
( packagesParser
14+
) where
15+
16+
import Options.Applicative ( Parser, help, long, metavar, strOption )
17+
import Stack.Prelude
18+
19+
-- | Parser for one or more package names.
20+
packagesParser :: Parser [String]
21+
packagesParser = many
22+
( strOption
23+
( long "package"
24+
<> metavar "PACKAGE"
25+
<> help "Add a package (can be specified multiple times)"
26+
)
27+
)

src/Stack/Options/ScriptParser.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@ import Options.Applicative
1919
import Options.Applicative.Builder.Extra
2020
( boolFlags, fileExtCompleter )
2121
import Stack.Options.Completion ( ghcOptsCompleter )
22+
import Stack.Options.PackagesParser ( packagesParser )
2223
import Stack.Prelude
2324
import Stack.Script
2425
( ScriptExecute (..), ScriptOpts (..), ShouldRun (..) )
2526

2627
-- | Parse command line arguments for Stack's @script@ command.
2728
scriptOptsParser :: Parser ScriptOpts
2829
scriptOptsParser = ScriptOpts
29-
<$> many (strOption
30-
( long "package"
31-
<> metavar "PACKAGE"
32-
<> help "Add a package (can be specified multiple times)."
33-
))
30+
<$> packagesParser
3431
<*> strArgument
3532
( metavar "FILE"
3633
<> completer (fileExtCompleter [".hs", ".lhs"])

stack.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ library
280280
Stack.Options.NewParser
281281
Stack.Options.NixParser
282282
Stack.Options.PackageParser
283+
Stack.Options.PackagesParser
283284
Stack.Options.PathParser
284285
Stack.Options.PvpBoundsParser
285286
Stack.Options.SDistParser

0 commit comments

Comments
 (0)