Skip to content

Commit 68320f1

Browse files
authored
Merge pull request #6745 from haskell/install-commandui
Make NixStyleOptions
2 parents 674747a + 5f6d274 commit 68320f1

File tree

7 files changed

+99
-127
lines changed

7 files changed

+99
-127
lines changed

cabal-install/Distribution/Client/CmdHaddock.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ module Distribution.Client.CmdHaddock (
1616
import Distribution.Client.ProjectOrchestration
1717
import Distribution.Client.CmdErrorMessages
1818

19+
import Distribution.Client.NixStyleOptions
20+
( NixStyleFlags, nixStyleOptions, defaultNixStyleFlags )
1921
import Distribution.Client.Setup
2022
( GlobalFlags, ConfigFlags(..), ConfigExFlags, InstallFlags )
21-
import qualified Distribution.Client.Setup as Client
2223
import Distribution.Simple.Setup
2324
( HaddockFlags(..), TestFlags, BenchmarkFlags(..), fromFlagOrDefault )
2425
import Distribution.Simple.Command
@@ -31,10 +32,8 @@ import Distribution.Simple.Utils
3132
import Control.Monad (when)
3233

3334

34-
haddockCommand :: CommandUI ( ConfigFlags, ConfigExFlags, InstallFlags
35-
, HaddockFlags, TestFlags, BenchmarkFlags
36-
)
37-
haddockCommand = Client.installCommand {
35+
haddockCommand :: CommandUI (NixStyleFlags ())
36+
haddockCommand = CommandUI {
3837
commandName = "v2-haddock",
3938
commandSynopsis = "Build Haddock documentation",
4039
commandUsage = usageAlternatives "v2-haddock" [ "[FLAGS] TARGET" ],
@@ -61,7 +60,9 @@ haddockCommand = Client.installCommand {
6160
++ " Build documentation for the package named pkgname\n\n"
6261

6362
++ cmdCommonHelpTextNewBuildBeta
64-
}
63+
, commandOptions = nixStyleOptions (const [])
64+
, commandDefaultFlags = defaultNixStyleFlags ()
65+
}
6566
--TODO: [nice to have] support haddock on specific components, not just
6667
-- whole packages and the silly --executables etc modifiers.
6768

@@ -71,10 +72,10 @@ haddockCommand = Client.installCommand {
7172
-- "Distribution.Client.ProjectOrchestration"
7273
--
7374
haddockAction :: ( ConfigFlags, ConfigExFlags, InstallFlags
74-
, HaddockFlags, TestFlags, BenchmarkFlags )
75+
, HaddockFlags, TestFlags, BenchmarkFlags, () )
7576
-> [String] -> GlobalFlags -> IO ()
7677
haddockAction ( configFlags, configExFlags, installFlags
77-
, haddockFlags, testFlags, benchmarkFlags )
78+
, haddockFlags, testFlags, benchmarkFlags, () )
7879
targetStrings globalFlags = do
7980

8081
baseCtx <- establishProjectBaseContext verbosity cliConfig HaddockCommand

cabal-install/Distribution/Client/CmdInstall.hs

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ import Distribution.Client.CmdInstall.ClientInstallFlags
3333
import Distribution.Client.CmdInstall.ClientInstallTargetSelector
3434

3535
import Distribution.Client.Setup
36-
( GlobalFlags(..), ConfigFlags(..), ConfigExFlags, InstallFlags(..)
37-
, configureExOptions, haddockOptions, installOptions, testOptions
38-
, benchmarkOptions, configureOptions, liftOptions )
39-
import Distribution.Solver.Types.ConstraintSource
40-
( ConstraintSource(..) )
36+
( GlobalFlags(..), ConfigFlags(..), ConfigExFlags, InstallFlags(..) )
4137
import Distribution.Client.Types
4238
( PackageSpecifier(..), PackageLocation(..), UnresolvedSourcePackage
4339
, SourcePackageDb(..) )
@@ -50,6 +46,8 @@ import Distribution.Client.ProjectConfig
5046
( ProjectPackageLocation(..)
5147
, fetchAndReadSourcePackages
5248
)
49+
import Distribution.Client.NixStyleOptions
50+
( NixStyleFlags, nixStyleOptions, defaultNixStyleFlags )
5351
import Distribution.Client.ProjectConfig.Types
5452
( ProjectConfig(..), ProjectConfigShared(..)
5553
, ProjectConfigBuildOnly(..), PackageConfig(..)
@@ -99,7 +97,7 @@ import Distribution.Simple.Setup
9997
import Distribution.Solver.Types.SourcePackage
10098
( SourcePackage(..) )
10199
import Distribution.Simple.Command
102-
( CommandUI(..), OptionField(..), usageAlternatives )
100+
( CommandUI(..), usageAlternatives )
103101
import Distribution.Simple.Configure
104102
( configCompilerEx )
105103
import Distribution.Simple.Compiler
@@ -149,10 +147,7 @@ import System.Directory
149147
import System.FilePath
150148
( (</>), (<.>), takeDirectory, takeBaseName )
151149

152-
installCommand :: CommandUI ( ConfigFlags, ConfigExFlags, InstallFlags
153-
, HaddockFlags, TestFlags, BenchmarkFlags
154-
, ClientInstallFlags
155-
)
150+
installCommand :: CommandUI (NixStyleFlags ClientInstallFlags)
156151
installCommand = CommandUI
157152
{ commandName = "v2-install"
158153
, commandSynopsis = "Install packages."
@@ -179,44 +174,9 @@ installCommand = CommandUI
179174
++ " Install the package in the ./pkgfoo directory\n"
180175

181176
++ cmdCommonHelpTextNewBuildBeta
182-
, commandOptions = \showOrParseArgs ->
183-
liftOptions get1 set1
184-
-- Note: [Hidden Flags]
185-
-- hide "constraint", "dependency", and
186-
-- "exact-configuration" from the configure options.
187-
(filter ((`notElem` ["constraint", "dependency"
188-
, "exact-configuration"])
189-
. optionName) $ configureOptions showOrParseArgs)
190-
++ liftOptions get2 set2 (configureExOptions showOrParseArgs
191-
ConstraintSourceCommandlineFlag)
192-
++ liftOptions get3 set3
193-
-- hide "target-package-db" and "symlink-bindir" flags from the
194-
-- install options.
195-
-- "symlink-bindir" is obsoleted by "installdir" in ClientInstallFlags
196-
(filter ((`notElem` ["target-package-db", "symlink-bindir"])
197-
. optionName) $
198-
installOptions showOrParseArgs)
199-
++ liftOptions get4 set4
200-
-- hide "verbose" and "builddir" flags from the
201-
-- haddock options.
202-
(filter ((`notElem` ["v", "verbose", "builddir"])
203-
. optionName) $
204-
haddockOptions showOrParseArgs)
205-
++ liftOptions get5 set5 (testOptions showOrParseArgs)
206-
++ liftOptions get6 set6 (benchmarkOptions showOrParseArgs)
207-
++ liftOptions get7 set7 (clientInstallOptions showOrParseArgs)
208-
, commandDefaultFlags = ( mempty, mempty, mempty, mempty, mempty, mempty
209-
, defaultClientInstallFlags )
177+
, commandOptions = nixStyleOptions clientInstallOptions
178+
, commandDefaultFlags = defaultNixStyleFlags defaultClientInstallFlags
210179
}
211-
where
212-
get1 (a,_,_,_,_,_,_) = a; set1 a (_,b,c,d,e,f,g) = (a,b,c,d,e,f,g)
213-
get2 (_,b,_,_,_,_,_) = b; set2 b (a,_,c,d,e,f,g) = (a,b,c,d,e,f,g)
214-
get3 (_,_,c,_,_,_,_) = c; set3 c (a,b,_,d,e,f,g) = (a,b,c,d,e,f,g)
215-
get4 (_,_,_,d,_,_,_) = d; set4 d (a,b,c,_,e,f,g) = (a,b,c,d,e,f,g)
216-
get5 (_,_,_,_,e,_,_) = e; set5 e (a,b,c,d,_,f,g) = (a,b,c,d,e,f,g)
217-
get6 (_,_,_,_,_,f,_) = f; set6 f (a,b,c,d,e,_,g) = (a,b,c,d,e,f,g)
218-
get7 (_,_,_,_,_,_,g) = g; set7 g (a,b,c,d,e,f,_) = (a,b,c,d,e,f,g)
219-
220180

221181
-- | The @install@ command actually serves four different needs. It installs:
222182
-- * exes:

cabal-install/Distribution/Client/CmdRepl.hs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import Distribution.Client.Compat.Prelude
2323
import Distribution.Compat.Lens
2424
import qualified Distribution.Types.Lens as L
2525

26+
import Distribution.Client.NixStyleOptions
27+
( NixStyleFlags, nixStyleOptions, defaultNixStyleFlags )
2628
import Distribution.Client.CmdErrorMessages
2729
import qualified Distribution.Client.InstallPlan as InstallPlan
2830
import Distribution.Client.ProjectBuilding
@@ -45,7 +47,7 @@ import Distribution.Simple.Setup
4547
, fromFlagOrDefault, replOptions
4648
, Flag(..), toFlag, trueArg, falseArg )
4749
import Distribution.Simple.Command
48-
( CommandUI(..), liftOption, usageAlternatives, option
50+
( CommandUI(..), liftOptionL, usageAlternatives, option
4951
, ShowOrParseArgs, OptionField, reqArg )
5052
import Distribution.Compiler
5153
( CompilerFlavor(GHC) )
@@ -144,10 +146,7 @@ envOptions _ =
144146
("couldn't parse dependencies: " ++)
145147
(parsecCommaList parsec)
146148

147-
replCommand :: CommandUI ( ConfigFlags, ConfigExFlags, InstallFlags
148-
, HaddockFlags, TestFlags, BenchmarkFlags
149-
, ReplFlags, EnvFlags
150-
)
149+
replCommand :: CommandUI (NixStyleFlags (ReplFlags, EnvFlags))
151150
replCommand = Client.installCommand {
152151
commandName = "v2-repl",
153152
commandSynopsis = "Open an interactive session for the given component.",
@@ -185,31 +184,11 @@ replCommand = Client.installCommand {
185184
++ "to the default component (or no component if there is no project present)\n"
186185

187186
++ cmdCommonHelpTextNewBuildBeta,
188-
commandDefaultFlags = ( configFlags, configExFlags, installFlags
189-
, haddockFlags, testFlags, benchmarkFlags
190-
, [], defaultEnvFlags
191-
),
192-
commandOptions = \showOrParseArgs ->
193-
map liftOriginal (commandOptions Client.installCommand showOrParseArgs)
194-
++ map liftReplOpts (replOptions showOrParseArgs)
195-
++ map liftEnvOpts (envOptions showOrParseArgs)
196-
}
197-
where
198-
(configFlags,configExFlags,installFlags,haddockFlags,testFlags,benchmarkFlags)
199-
= commandDefaultFlags Client.installCommand
200-
201-
liftOriginal = liftOption projectOriginal updateOriginal
202-
liftReplOpts = liftOption projectReplOpts updateReplOpts
203-
liftEnvOpts = liftOption projectEnvOpts updateEnvOpts
204-
205-
projectOriginal (a,b,c,d,e,f,_,_) = (a,b,c,d,e,f)
206-
updateOriginal (a,b,c,d,e,f) (_,_,_,_,_,_,g,h) = (a,b,c,d,e,f,g,h)
207-
208-
projectReplOpts (_,_,_,_,_,_,g,_) = g
209-
updateReplOpts g (a,b,c,d,e,f,_,h) = (a,b,c,d,e,f,g,h)
210-
211-
projectEnvOpts (_,_,_,_,_,_,_,h) = h
212-
updateEnvOpts h (a,b,c,d,e,f,g,_) = (a,b,c,d,e,f,g,h)
187+
commandDefaultFlags = defaultNixStyleFlags ([], defaultEnvFlags),
188+
commandOptions = nixStyleOptions $ \showOrParseArgs ->
189+
map (liftOptionL _1) (replOptions showOrParseArgs) ++
190+
map (liftOptionL _2) (envOptions showOrParseArgs)
191+
}
213192

214193
-- | The @repl@ command is very much like @build@. It brings the install plan
215194
-- up to date, selects that part of the plan needed by the given or implicit
@@ -224,11 +203,11 @@ replCommand = Client.installCommand {
224203
--
225204
replAction :: ( ConfigFlags, ConfigExFlags, InstallFlags
226205
, HaddockFlags, TestFlags, BenchmarkFlags
227-
, ReplFlags, EnvFlags )
206+
, (ReplFlags, EnvFlags) )
228207
-> [String] -> GlobalFlags -> IO ()
229208
replAction ( configFlags, configExFlags, installFlags
230209
, haddockFlags, testFlags, benchmarkFlags
231-
, replFlags, envFlags )
210+
, (replFlags, envFlags) )
232211
targetStrings globalFlags = do
233212
let
234213
ignoreProject = fromFlagOrDefault False (envIgnoreProject envFlags)

cabal-install/Distribution/Client/CmdRun.hs

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ import Distribution.Client.CmdErrorMessages
2525

2626
import Distribution.Client.CmdRun.ClientRunFlags
2727

28+
import Distribution.Client.NixStyleOptions
29+
( NixStyleFlags, nixStyleOptions, defaultNixStyleFlags )
2830
import Distribution.Client.Setup
29-
( GlobalFlags(..), ConfigFlags(..), ConfigExFlags, InstallFlags(..)
30-
, configureExOptions, haddockOptions, installOptions, testOptions
31-
, benchmarkOptions, configureOptions, liftOptions )
32-
import Distribution.Solver.Types.ConstraintSource
33-
( ConstraintSource(..) )
31+
( GlobalFlags(..), ConfigFlags(..), ConfigExFlags, InstallFlags(..) )
3432
import Distribution.Client.GlobalFlags
3533
( defaultGlobalFlags )
3634
import Distribution.Simple.Setup
3735
( HaddockFlags, TestFlags, BenchmarkFlags, fromFlagOrDefault )
3836
import Distribution.Simple.Command
39-
( CommandUI(..), OptionField (..), usageAlternatives )
37+
( CommandUI(..), usageAlternatives )
4038
import Distribution.Types.ComponentName
4139
( showComponentName )
4240
import Distribution.Deprecated.Text
@@ -109,10 +107,7 @@ import System.FilePath
109107
( (</>), isValid, isPathSeparator, takeExtension )
110108

111109

112-
runCommand :: CommandUI ( ConfigFlags, ConfigExFlags, InstallFlags
113-
, HaddockFlags, TestFlags, BenchmarkFlags
114-
, ClientRunFlags
115-
)
110+
runCommand :: CommandUI (NixStyleFlags ClientRunFlags)
116111
runCommand = CommandUI
117112
{ commandName = "v2-run"
118113
, commandSynopsis = "Run an executable."
@@ -148,37 +143,9 @@ runCommand = CommandUI
148143
++ " Build with '-O2' and run the program, passing it extra arguments.\n\n"
149144

150145
++ cmdCommonHelpTextNewBuildBeta
151-
, commandDefaultFlags = (mempty, mempty, mempty, mempty, mempty, mempty, mempty)
152-
, commandOptions = \showOrParseArgs ->
153-
liftOptions get1 set1
154-
-- Note: [Hidden Flags]
155-
-- hide "constraint", "dependency", and
156-
-- "exact-configuration" from the configure options.
157-
(filter ((`notElem` ["constraint", "dependency"
158-
, "exact-configuration"])
159-
. optionName) $
160-
configureOptions showOrParseArgs)
161-
++ liftOptions get2 set2 (configureExOptions showOrParseArgs ConstraintSourceCommandlineFlag)
162-
++ liftOptions get3 set3
163-
-- hide "target-package-db" flag from the
164-
-- install options.
165-
(filter ((`notElem` ["target-package-db"])
166-
. optionName) $
167-
installOptions showOrParseArgs)
168-
++ liftOptions get4 set4 (haddockOptions showOrParseArgs)
169-
++ liftOptions get5 set5 (testOptions showOrParseArgs)
170-
++ liftOptions get6 set6 (benchmarkOptions showOrParseArgs)
171-
++ liftOptions get7 set7 (clientRunOptions showOrParseArgs)
146+
, commandDefaultFlags = defaultNixStyleFlags mempty
147+
, commandOptions = nixStyleOptions clientRunOptions
172148
}
173-
where
174-
get1 (a,_,_,_,_,_,_) = a; set1 a (_,b,c,d,e,f,g) = (a,b,c,d,e,f,g)
175-
get2 (_,b,_,_,_,_,_) = b; set2 b (a,_,c,d,e,f,g) = (a,b,c,d,e,f,g)
176-
get3 (_,_,c,_,_,_,_) = c; set3 c (a,b,_,d,e,f,g) = (a,b,c,d,e,f,g)
177-
get4 (_,_,_,d,_,_,_) = d; set4 d (a,b,c,_,e,f,g) = (a,b,c,d,e,f,g)
178-
get5 (_,_,_,_,e,_,_) = e; set5 e (a,b,c,d,_,f,g) = (a,b,c,d,e,f,g)
179-
get6 (_,_,_,_,_,f,_) = f; set6 f (a,b,c,d,e,_,g) = (a,b,c,d,e,f,g)
180-
get7 (_,_,_,_,_,_,g) = g; set7 g (a,b,c,d,e,f,_) = (a,b,c,d,e,f,g)
181-
182149

183150
-- | The @run@ command runs a specified executable-like component, building it
184151
-- first if necessary. The component can be either an executable, a test,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
-- | Command line options for nix-style / v2 commands.
2+
--
3+
-- The commands take a lot of the same options, which affect how install plan
4+
-- is constructed.
5+
module Distribution.Client.NixStyleOptions (
6+
NixStyleFlags, nixStyleOptions, defaultNixStyleFlags,
7+
) where
8+
9+
import Distribution.Client.Compat.Prelude
10+
import Prelude ()
11+
12+
import Distribution.Simple.Command (OptionField (..), ShowOrParseArgs)
13+
import Distribution.Simple.Setup (BenchmarkFlags, HaddockFlags, TestFlags)
14+
import Distribution.Solver.Types.ConstraintSource (ConstraintSource (..))
15+
16+
import Distribution.Client.Setup
17+
(ConfigExFlags, ConfigFlags (..), InstallFlags (..), benchmarkOptions, configureExOptions,
18+
configureOptions, haddockOptions, installOptions, liftOptions, testOptions)
19+
20+
-- TODO: turn into data record
21+
-- Then we could use RecordWildCards in command implementation.
22+
type NixStyleFlags a = (ConfigFlags, ConfigExFlags, InstallFlags, HaddockFlags, TestFlags, BenchmarkFlags, a)
23+
24+
nixStyleOptions
25+
:: (ShowOrParseArgs -> [OptionField a])
26+
-> ShowOrParseArgs -> [OptionField (NixStyleFlags a)]
27+
nixStyleOptions commandOptions showOrParseArgs =
28+
liftOptions get1 set1
29+
-- Note: [Hidden Flags]
30+
-- hide "constraint", "dependency", and
31+
-- "exact-configuration" from the configure options.
32+
(filter ((`notElem` ["constraint", "dependency"
33+
, "exact-configuration"])
34+
. optionName) $ configureOptions showOrParseArgs)
35+
++ liftOptions get2 set2 (configureExOptions showOrParseArgs
36+
ConstraintSourceCommandlineFlag)
37+
++ liftOptions get3 set3
38+
-- hide "target-package-db" and "symlink-bindir" flags from the
39+
-- install options.
40+
-- "symlink-bindir" is obsoleted by "installdir" in ClientInstallFlags
41+
(filter ((`notElem` ["target-package-db", "symlink-bindir"])
42+
. optionName) $
43+
installOptions showOrParseArgs)
44+
++ liftOptions get4 set4
45+
-- hide "verbose" and "builddir" flags from the
46+
-- haddock options.
47+
(filter ((`notElem` ["v", "verbose", "builddir"])
48+
. optionName) $
49+
haddockOptions showOrParseArgs)
50+
++ liftOptions get5 set5 (testOptions showOrParseArgs)
51+
++ liftOptions get6 set6 (benchmarkOptions showOrParseArgs)
52+
++ liftOptions get7 set7 (commandOptions showOrParseArgs)
53+
where
54+
get1 (a,_,_,_,_,_,_) = a; set1 a (_,b,c,d,e,f,g) = (a,b,c,d,e,f,g)
55+
get2 (_,b,_,_,_,_,_) = b; set2 b (a,_,c,d,e,f,g) = (a,b,c,d,e,f,g)
56+
get3 (_,_,c,_,_,_,_) = c; set3 c (a,b,_,d,e,f,g) = (a,b,c,d,e,f,g)
57+
get4 (_,_,_,d,_,_,_) = d; set4 d (a,b,c,_,e,f,g) = (a,b,c,d,e,f,g)
58+
get5 (_,_,_,_,e,_,_) = e; set5 e (a,b,c,d,_,f,g) = (a,b,c,d,e,f,g)
59+
get6 (_,_,_,_,_,f,_) = f; set6 f (a,b,c,d,e,_,g) = (a,b,c,d,e,f,g)
60+
get7 (_,_,_,_,_,_,g) = g; set7 g (a,b,c,d,e,f,_) = (a,b,c,d,e,f,g)
61+
62+
defaultNixStyleFlags :: a -> NixStyleFlags a
63+
defaultNixStyleFlags x = ( mempty, mempty, mempty, mempty, mempty, mempty, x )

cabal-install/cabal-install.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ executable cabal
220220
Distribution.Client.Manpage
221221
Distribution.Client.ManpageFlags
222222
Distribution.Client.Nix
223+
Distribution.Client.NixStyleOptions
223224
Distribution.Client.Outdated
224225
Distribution.Client.PackageHash
225226
Distribution.Client.PackageUtils

cabal-install/cabal-install.cabal.pp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
Distribution.Client.Manpage
160160
Distribution.Client.ManpageFlags
161161
Distribution.Client.Nix
162+
Distribution.Client.NixStyleOptions
162163
Distribution.Client.Outdated
163164
Distribution.Client.PackageHash
164165
Distribution.Client.PackageUtils

0 commit comments

Comments
 (0)