Setup.hs 71.7 KB
Newer Older
1 2
-----------------------------------------------------------------------------
-- |
3
-- Module      :  Distribution.Client.Setup
4 5 6 7 8 9 10 11 12
-- Copyright   :  (c) David Himmelstrup 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
--
-----------------------------------------------------------------------------
13
module Distribution.Client.Setup
14
    ( globalCommand, GlobalFlags(..), defaultGlobalFlags, globalRepos
15
    , configureCommand, ConfigFlags(..), filterConfigureFlags
16 17
    , configureExCommand, ConfigExFlags(..), defaultConfigExFlags
                        , configureExOptions
18
    , buildCommand, BuildFlags(..), BuildExFlags(..), SkipAddSourceDepsCheck(..)
19
    , replCommand, testCommand, benchmarkCommand
20
    , installCommand, InstallFlags(..), installOptions, defaultInstallFlags
21
    , listCommand, ListFlags(..)
Duncan Coutts's avatar
Duncan Coutts committed
22
    , updateCommand
ijones's avatar
ijones committed
23
    , upgradeCommand
24
    , infoCommand, InfoFlags(..)
25
    , fetchCommand, FetchFlags(..)
26
    , freezeCommand, FreezeFlags(..)
27
    , getCommand, unpackCommand, GetFlags(..)
28
    , checkCommand
29
    , formatCommand
30
    , uploadCommand, UploadFlags(..)
31
    , reportCommand, ReportFlags(..)
refold's avatar
refold committed
32
    , runCommand
33
    , initCommand, IT.InitFlags(..)
34
    , sdistCommand, SDistFlags(..), SDistExFlags(..), ArchiveFormat(..)
35
    , win32SelfUpgradeCommand, Win32SelfUpgradeFlags(..)
refold's avatar
refold committed
36
    , sandboxCommand, defaultSandboxLocation, SandboxFlags(..)
37
    , execCommand, ExecFlags(..)
38
    , userConfigCommand, UserConfigFlags(..)
Duncan Coutts's avatar
Duncan Coutts committed
39 40

    , parsePackageArgs
41 42 43
    --TODO: stop exporting these:
    , showRepo
    , parseRepo
44 45
    ) where

46
import Distribution.Client.Types
47
         ( Username(..), Password(..), Repo(..), RemoteRepo(..), LocalRepo(..) )
48 49
import Distribution.Client.BuildReports.Types
         ( ReportLevel(..) )
50
import Distribution.Client.Dependency.Types
51
         ( AllowNewer(..), PreSolver(..) )
52 53
import qualified Distribution.Client.Init.Types as IT
         ( InitFlags(..), PackageType(..) )
54 55
import Distribution.Client.Targets
         ( UserConstraint, readUserConstraint )
56 57
import Distribution.Utils.NubList
         ( NubList, toNubList, fromNubList)
58

59
import Distribution.Simple.Compiler (PackageDB)
60 61
import Distribution.Simple.Program
         ( defaultProgramConfiguration )
62 63
import Distribution.Simple.Command hiding (boolOpt, boolOpt')
import qualified Distribution.Simple.Command as Command
Duncan Coutts's avatar
Duncan Coutts committed
64
import qualified Distribution.Simple.Setup as Cabal
65
import Distribution.Simple.Setup
66 67
         ( ConfigFlags(..), BuildFlags(..), ReplFlags
         , TestFlags(..), BenchmarkFlags(..)
68
         , SDistFlags(..), HaddockFlags(..)
69
         , readPackageDbList, showPackageDbList
70
         , Flag(..), toFlag, fromFlag, flagToMaybe, flagToList
71
         , optionVerbosity, boolOpt, boolOpt', trueArg, falseArg, optionNumJobs )
72
import Distribution.Simple.InstallDirs
73 74
         ( PathTemplate, InstallDirs(sysconfdir)
         , toPathTemplate, fromPathTemplate )
75
import Distribution.Version
Duncan Coutts's avatar
Duncan Coutts committed
76
         ( Version(Version), anyVersion, thisVersion )
77
import Distribution.Package
78
         ( PackageIdentifier, packageName, packageVersion, Dependency(..) )
79 80
import Distribution.PackageDescription
         ( RepoKind(..) )
81
import Distribution.Text
82
         ( Text(..), display )
83
import Distribution.ReadE
84
         ( ReadE(..), readP_to_E, succeedReadE )
85
import qualified Distribution.Compat.ReadP as Parse
86
         ( ReadP, readP_to_S, readS_to_P, char, munch1, pfail, sepBy1, (+++) )
87 88
import Distribution.Verbosity
         ( Verbosity, normal )
89 90
import Distribution.Simple.Utils
         ( wrapText )
91

92 93
import Data.Char
         ( isSpace, isAlphaNum )
94 95
import Data.List
         ( intercalate )
96
import Data.Maybe
97
         ( listToMaybe, maybeToList, fromMaybe )
98 99 100 101 102 103 104 105
import Data.Monoid
         ( Monoid(..) )
import Control.Monad
         ( liftM )
import System.FilePath
         ( (</>) )
import Network.URI
         ( parseAbsoluteURI, uriToString )
106

107 108 109 110 111 112
-- ------------------------------------------------------------
-- * Global flags
-- ------------------------------------------------------------

-- | Flags that apply at the top level, not to any sub-command.
data GlobalFlags = GlobalFlags {
113 114 115 116
    globalVersion           :: Flag Bool,
    globalNumericVersion    :: Flag Bool,
    globalConfigFile        :: Flag FilePath,
    globalSandboxConfigFile :: Flag FilePath,
117
    globalRemoteRepos       :: NubList RemoteRepo,     -- ^ Available Hackage servers.
118
    globalCacheDir          :: Flag FilePath,
119
    globalLocalRepos        :: NubList FilePath,
120
    globalLogsDir           :: Flag FilePath,
121
    globalWorldFile         :: Flag FilePath,
122 123
    globalRequireSandbox    :: Flag Bool,
    globalIgnoreSandbox     :: Flag Bool
124
  }
Duncan Coutts's avatar
Duncan Coutts committed
125

126 127
defaultGlobalFlags :: GlobalFlags
defaultGlobalFlags  = GlobalFlags {
128 129 130 131
    globalVersion           = Flag False,
    globalNumericVersion    = Flag False,
    globalConfigFile        = mempty,
    globalSandboxConfigFile = mempty,
132
    globalRemoteRepos       = mempty,
133 134 135
    globalCacheDir          = mempty,
    globalLocalRepos        = mempty,
    globalLogsDir           = mempty,
136
    globalWorldFile         = mempty,
137 138
    globalRequireSandbox    = Flag False,
    globalIgnoreSandbox     = Flag False
139 140 141 142 143 144
  }

globalCommand :: CommandUI GlobalFlags
globalCommand = CommandUI {
    commandName         = "",
    commandSynopsis     = "",
145 146 147 148
    commandUsage        = \_ ->
         "This program is the command line interface "
           ++ "to the Haskell Cabal infrastructure.\n"
      ++ "See http://www.haskell.org/cabal/ for more information.\n",
149
    commandDescription  = Just $ \pname ->
150 151 152 153 154 155
         "For more information about a command use:\n"
      ++ "  " ++ pname ++ " COMMAND --help\n\n"
      ++ "To install Cabal packages from hackage use:\n"
      ++ "  " ++ pname ++ " install foo [--dry-run]\n\n"
      ++ "Occasionally you need to update the list of available packages:\n"
      ++ "  " ++ pname ++ " update\n",
156
    commandNotes = Nothing,
157
    commandDefaultFlags = mempty,
158
    commandOptions      = \showOrParseArgs ->
159
      (case showOrParseArgs of ShowArgs -> take 6; ParseArgs -> id)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      [option ['V'] ["version"]
         "Print version information"
         globalVersion (\v flags -> flags { globalVersion = v })
         trueArg

      ,option [] ["numeric-version"]
         "Print just the version number"
         globalNumericVersion (\v flags -> flags { globalNumericVersion = v })
         trueArg

      ,option [] ["config-file"]
         "Set an alternate location for the config file"
         globalConfigFile (\v flags -> flags { globalConfigFile = v })
         (reqArgFlag "FILE")

175 176 177 178 179 180
      ,option [] ["sandbox-config-file"]
         "Set an alternate location for the sandbox config file \
         \(default: './cabal.sandbox.config')"
         globalConfigFile (\v flags -> flags { globalSandboxConfigFile = v })
         (reqArgFlag "FILE")

181
      ,option [] ["require-sandbox"]
Mikhail Glushenkov's avatar
Mikhail Glushenkov committed
182
         "requiring the presence of a sandbox for sandbox-aware commands"
183 184 185
         globalRequireSandbox (\v flags -> flags { globalRequireSandbox = v })
         (boolOpt' ([], ["require-sandbox"]) ([], ["no-require-sandbox"]))

186 187 188 189 190
      ,option [] ["ignore-sandbox"]
         "Ignore any existing sandbox"
         globalIgnoreSandbox (\v flags -> flags { globalIgnoreSandbox = v })
         trueArg

191 192 193
      ,option [] ["remote-repo"]
         "The name and url for a remote repository"
         globalRemoteRepos (\v flags -> flags { globalRemoteRepos = v })
194
         (reqArg' "NAME:URL" (toNubList . maybeToList . readRepo) (map showRepo . fromNubList))
195 196 197 198 199 200 201 202 203

      ,option [] ["remote-repo-cache"]
         "The location where downloads from all remote repos are cached"
         globalCacheDir (\v flags -> flags { globalCacheDir = v })
         (reqArgFlag "DIR")

      ,option [] ["local-repo"]
         "The location of a local repository"
         globalLocalRepos (\v flags -> flags { globalLocalRepos = v })
204
         (reqArg' "DIR" (\x -> toNubList [x]) fromNubList)
205

206 207 208 209 210
      ,option [] ["logs-dir"]
         "The location to put log files"
         globalLogsDir (\v flags -> flags { globalLogsDir = v })
         (reqArgFlag "DIR")

211 212 213 214
      ,option [] ["world-file"]
         "The location of the world file"
         globalWorldFile (\v flags -> flags { globalWorldFile = v })
         (reqArgFlag "FILE")
215 216 217 218 219
      ]
  }

instance Monoid GlobalFlags where
  mempty = GlobalFlags {
220 221 222 223 224 225 226 227
    globalVersion           = mempty,
    globalNumericVersion    = mempty,
    globalConfigFile        = mempty,
    globalSandboxConfigFile = mempty,
    globalRemoteRepos       = mempty,
    globalCacheDir          = mempty,
    globalLocalRepos        = mempty,
    globalLogsDir           = mempty,
228
    globalWorldFile         = mempty,
229 230
    globalRequireSandbox    = mempty,
    globalIgnoreSandbox     = mempty
231 232
  }
  mappend a b = GlobalFlags {
233 234 235 236 237 238 239 240
    globalVersion           = combine globalVersion,
    globalNumericVersion    = combine globalNumericVersion,
    globalConfigFile        = combine globalConfigFile,
    globalSandboxConfigFile = combine globalConfigFile,
    globalRemoteRepos       = combine globalRemoteRepos,
    globalCacheDir          = combine globalCacheDir,
    globalLocalRepos        = combine globalLocalRepos,
    globalLogsDir           = combine globalLogsDir,
241
    globalWorldFile         = combine globalWorldFile,
242 243
    globalRequireSandbox    = combine globalRequireSandbox,
    globalIgnoreSandbox     = combine globalIgnoreSandbox
Duncan Coutts's avatar
Duncan Coutts committed
244
  }
245 246 247 248 249 250 251
    where combine field = field a `mappend` field b

globalRepos :: GlobalFlags -> [Repo]
globalRepos globalFlags = remoteRepos ++ localRepos
  where
    remoteRepos =
      [ Repo (Left remote) cacheDir
252
      | remote <- fromNubList $ globalRemoteRepos globalFlags
253 254 255 256
      , let cacheDir = fromFlag (globalCacheDir globalFlags)
                   </> remoteRepoName remote ]
    localRepos =
      [ Repo (Right LocalRepo) local
257
      | local <- fromNubList $ globalLocalRepos globalFlags ]
258 259 260 261

-- ------------------------------------------------------------
-- * Config flags
-- ------------------------------------------------------------
Duncan Coutts's avatar
Duncan Coutts committed
262

263
configureCommand :: CommandUI ConfigFlags
264
configureCommand = (Cabal.configureCommand defaultProgramConfiguration) {
265
    commandDefaultFlags = mempty
Duncan Coutts's avatar
Duncan Coutts committed
266 267
  }

268 269 270
configureOptions ::  ShowOrParseArgs -> [OptionField ConfigFlags]
configureOptions = commandOptions configureCommand

271
filterConfigureFlags :: ConfigFlags -> Version -> ConfigFlags
272
filterConfigureFlags flags cabalLibVersion
273
  | cabalLibVersion >= Version [1,19,2] [] = flags_latest
274 275 276
  | cabalLibVersion <  Version [1,3,10] [] = flags_1_3_10
  | cabalLibVersion <  Version [1,10,0] [] = flags_1_10_0
  | cabalLibVersion <  Version [1,14,0] [] = flags_1_14_0
277
  | cabalLibVersion <  Version [1,18,0] [] = flags_1_18_0
278
  | cabalLibVersion <  Version [1,19,1] [] = flags_1_19_0
279
  | cabalLibVersion <  Version [1,19,2] [] = flags_1_19_1
280
  | otherwise = flags_latest
281
  where
282
    -- Cabal >= 1.19.1 uses '--dependency' and does not need '--constraint'.
283 284
    flags_latest = flags        { configConstraints = [] }

285 286 287 288 289
    -- Cabal < 1.19.2 doesn't know about '--exact-configuration'.
    flags_1_19_1 = flags_latest { configExactConfiguration = NoFlag }
    -- Cabal < 1.19.1 uses '--constraint' instead of '--dependency'.
    flags_1_19_0 = flags_1_19_1 { configDependencies = []
                                , configConstraints  = configConstraints flags }
290
    -- Cabal < 1.18.0 doesn't know about --extra-prog-path and --sysconfdir.
291
    flags_1_18_0 = flags_1_19_0 { configProgramPathExtra = toNubList []
292 293
                                , configInstallDirs = configInstallDirs_1_18_0}
    configInstallDirs_1_18_0 = (configInstallDirs flags) { sysconfdir = NoFlag }
294
    -- Cabal < 1.14.0 doesn't know about '--disable-benchmarks'.
295
    flags_1_14_0 = flags_1_18_0 { configBenchmarks  = NoFlag }
296
    -- Cabal < 1.10.0 doesn't know about '--disable-tests'.
297
    flags_1_10_0 = flags_1_14_0 { configTests       = NoFlag }
298
    -- Cabal < 1.3.10 does not grok the '--constraints' flag.
299
    flags_1_3_10 = flags_1_10_0 { configConstraints = [] }
300

301 302 303 304 305 306 307 308
-- ------------------------------------------------------------
-- * Config extra flags
-- ------------------------------------------------------------

-- | cabal configure takes some extra flags beyond runghc Setup configure
--
data ConfigExFlags = ConfigExFlags {
    configCabalVersion :: Flag Version,
309
    configExConstraints:: [UserConstraint],
310
    configPreferences  :: [Dependency],
311 312
    configSolver       :: Flag PreSolver,
    configAllowNewer   :: Flag AllowNewer
313 314 315
  }

defaultConfigExFlags :: ConfigExFlags
316 317
defaultConfigExFlags = mempty { configSolver     = Flag defaultSolver
                              , configAllowNewer = Flag AllowNewerNone }
318 319 320 321 322

configureExCommand :: CommandUI (ConfigFlags, ConfigExFlags)
configureExCommand = configureCommand {
    commandDefaultFlags = (mempty, defaultConfigExFlags),
    commandOptions      = \showOrParseArgs ->
323
         liftOptions fst setFst
324
         (filter ((`notElem` ["constraint", "dependency", "exact-configuration"])
325
                  . optionName) $ configureOptions  showOrParseArgs)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
      ++ liftOptions snd setSnd (configureExOptions showOrParseArgs)
  }
  where
    setFst a (_,b) = (a,b)
    setSnd b (a,_) = (a,b)

configureExOptions ::  ShowOrParseArgs -> [OptionField ConfigExFlags]
configureExOptions _showOrParseArgs =
  [ option [] ["cabal-lib-version"]
      ("Select which version of the Cabal lib to use to build packages "
      ++ "(useful for testing).")
      configCabalVersion (\v flags -> flags { configCabalVersion = v })
      (reqArg "VERSION" (readP_to_E ("Cannot parse cabal lib version: "++)
                                    (fmap toFlag parse))
                        (map display . flagToList))
341 342 343 344 345 346
  , option [] ["constraint"]
      "Specify constraints on a package (version, installed/source, flags)"
      configExConstraints (\v flags -> flags { configExConstraints = v })
      (reqArg "CONSTRAINT"
              (fmap (\x -> [x]) (ReadE readUserConstraint))
              (map display))
347 348 349 350

  , option [] ["preference"]
      "Specify preferences (soft constraints) on the version of a package"
      configPreferences (\v flags -> flags { configPreferences = v })
351 352 353 354
      (reqArg "CONSTRAINT"
              (readP_to_E (const "dependency expected")
                          (fmap (\x -> [x]) parse))
              (map display))
355 356

  , optionSolver configSolver (\v flags -> flags { configSolver = v })
357 358

  , option [] ["allow-newer"]
cheecheeo's avatar
cheecheeo committed
359
    ("Ignore upper bounds in all dependencies or " ++ allowNewerArgument)
360
    configAllowNewer (\v flags -> flags { configAllowNewer = v})
cheecheeo's avatar
cheecheeo committed
361
    (optArg allowNewerArgument
362 363 364
     (fmap Flag allowNewerParser) (Flag AllowNewerAll)
     allowNewerPrinter)

365
  ]
cheecheeo's avatar
cheecheeo committed
366
  where allowNewerArgument = "DEPS"
367 368 369 370

instance Monoid ConfigExFlags where
  mempty = ConfigExFlags {
    configCabalVersion = mempty,
371
    configExConstraints= mempty,
372
    configPreferences  = mempty,
373 374
    configSolver       = mempty,
    configAllowNewer   = mempty
375 376 377
  }
  mappend a b = ConfigExFlags {
    configCabalVersion = combine configCabalVersion,
378
    configExConstraints= combine configExConstraints,
379
    configPreferences  = combine configPreferences,
380 381
    configSolver       = combine configSolver,
    configAllowNewer   = combine configAllowNewer
382 383 384
  }
    where combine field = field a `mappend` field b

ttuegel's avatar
ttuegel committed
385 386 387 388
-- ------------------------------------------------------------
-- * Build flags
-- ------------------------------------------------------------

389 390 391 392 393 394 395 396 397 398
data SkipAddSourceDepsCheck =
  SkipAddSourceDepsCheck | DontSkipAddSourceDepsCheck
  deriving Eq

data BuildExFlags = BuildExFlags {
  buildOnly     :: Flag SkipAddSourceDepsCheck
}

buildExOptions :: ShowOrParseArgs -> [OptionField BuildExFlags]
buildExOptions _showOrParseArgs =
399
  option [] ["only"]
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  "Don't reinstall add-source dependencies (sandbox-only)"
  buildOnly (\v flags -> flags { buildOnly = v })
  (noArg (Flag SkipAddSourceDepsCheck))

  : []

buildCommand :: CommandUI (BuildFlags, BuildExFlags)
buildCommand = parent {
    commandDefaultFlags = (commandDefaultFlags parent, mempty),
    commandOptions      =
      \showOrParseArgs -> liftOptions fst setFst
                          (commandOptions parent showOrParseArgs)
                          ++
                          liftOptions snd setSnd (buildExOptions showOrParseArgs)
  }
  where
    setFst a (_,b) = (a,b)
    setSnd b (a,_) = (a,b)

    parent = Cabal.buildCommand defaultProgramConfiguration

instance Monoid BuildExFlags where
  mempty = BuildExFlags {
    buildOnly    = mempty
  }
  mappend a b = BuildExFlags {
    buildOnly    = combine buildOnly
  }
    where combine field = field a `mappend` field b

-- ------------------------------------------------------------
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
-- * Repl command
-- ------------------------------------------------------------

replCommand :: CommandUI (ReplFlags, BuildExFlags)
replCommand = parent {
    commandDefaultFlags = (commandDefaultFlags parent, mempty),
    commandOptions      =
      \showOrParseArgs -> liftOptions fst setFst
                          (commandOptions parent showOrParseArgs)
                          ++
                          liftOptions snd setSnd (buildExOptions showOrParseArgs)
  }
  where
    setFst a (_,b) = (a,b)
    setSnd b (a,_) = (a,b)

    parent = Cabal.replCommand defaultProgramConfiguration

449 450 451 452
-- ------------------------------------------------------------
-- * Test command
-- ------------------------------------------------------------

453
testCommand :: CommandUI (TestFlags, BuildFlags, BuildExFlags)
454
testCommand = parent {
455 456
  commandDefaultFlags = (commandDefaultFlags parent,
                         Cabal.defaultBuildFlags, mempty),
457
  commandOptions      =
458
    \showOrParseArgs -> liftOptions get1 set1
459 460
                        (commandOptions parent showOrParseArgs)
                        ++
461 462 463 464
                        liftOptions get2 set2
                        (Cabal.buildOptions progConf showOrParseArgs)
                        ++
                        liftOptions get3 set3 (buildExOptions showOrParseArgs)
465 466
  }
  where
467 468 469
    get1 (a,_,_) = a; set1 a (_,b,c) = (a,b,c)
    get2 (_,b,_) = b; set2 b (a,_,c) = (a,b,c)
    get3 (_,_,c) = c; set3 c (a,b,_) = (a,b,c)
470

471 472
    parent   = Cabal.testCommand
    progConf = defaultProgramConfiguration
473 474 475 476 477

-- ------------------------------------------------------------
-- * Bench command
-- ------------------------------------------------------------

478
benchmarkCommand :: CommandUI (BenchmarkFlags, BuildFlags, BuildExFlags)
479
benchmarkCommand = parent {
480 481
  commandDefaultFlags = (commandDefaultFlags parent,
                         Cabal.defaultBuildFlags, mempty),
482
  commandOptions      =
483
    \showOrParseArgs -> liftOptions get1 set1
484 485
                        (commandOptions parent showOrParseArgs)
                        ++
486 487 488 489
                        liftOptions get2 set2
                        (Cabal.buildOptions progConf showOrParseArgs)
                        ++
                        liftOptions get3 set3 (buildExOptions showOrParseArgs)
ttuegel's avatar
ttuegel committed
490
  }
491
  where
492 493 494
    get1 (a,_,_) = a; set1 a (_,b,c) = (a,b,c)
    get2 (_,b,_) = b; set2 b (a,_,c) = (a,b,c)
    get3 (_,_,c) = c; set3 c (a,b,_) = (a,b,c)
495

496 497
    parent   = Cabal.benchmarkCommand
    progConf = defaultProgramConfiguration
ttuegel's avatar
ttuegel committed
498

499
-- ------------------------------------------------------------
500
-- * Fetch command
501
-- ------------------------------------------------------------
502

503 504 505 506
data FetchFlags = FetchFlags {
--    fetchOutput    :: Flag FilePath,
      fetchDeps      :: Flag Bool,
      fetchDryRun    :: Flag Bool,
507
      fetchSolver           :: Flag PreSolver,
508 509 510
      fetchMaxBackjumps     :: Flag Int,
      fetchReorderGoals     :: Flag Bool,
      fetchIndependentGoals :: Flag Bool,
511
      fetchShadowPkgs       :: Flag Bool,
Andres Löh's avatar
Andres Löh committed
512
      fetchStrongFlags      :: Flag Bool,
513 514 515 516 517 518 519 520
      fetchVerbosity :: Flag Verbosity
    }

defaultFetchFlags :: FetchFlags
defaultFetchFlags = FetchFlags {
--  fetchOutput    = mempty,
    fetchDeps      = toFlag True,
    fetchDryRun    = toFlag False,
521 522
    fetchSolver           = Flag defaultSolver,
    fetchMaxBackjumps     = Flag defaultMaxBackjumps,
523
    fetchReorderGoals     = Flag False,
524
    fetchIndependentGoals = Flag False,
525
    fetchShadowPkgs       = Flag False,
Andres Löh's avatar
Andres Löh committed
526
    fetchStrongFlags      = Flag False,
527 528 529 530
    fetchVerbosity = toFlag normal
   }

fetchCommand :: CommandUI FetchFlags
Duncan Coutts's avatar
Duncan Coutts committed
531 532
fetchCommand = CommandUI {
    commandName         = "fetch",
533
    commandSynopsis     = "Downloads packages for later installation.",
Duncan Coutts's avatar
Duncan Coutts committed
534
    commandDescription  = Nothing,
535
    commandNotes        = Nothing,
refold's avatar
refold committed
536
    commandUsage        = usagePackages "fetch",
537
    commandDefaultFlags = defaultFetchFlags,
538
    commandOptions      = \ showOrParseArgs -> [
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
         optionVerbosity fetchVerbosity (\v flags -> flags { fetchVerbosity = v })

--     , option "o" ["output"]
--         "Put the package(s) somewhere specific rather than the usual cache."
--         fetchOutput (\v flags -> flags { fetchOutput = v })
--         (reqArgFlag "PATH")

       , option [] ["dependencies", "deps"]
           "Resolve and fetch dependencies (default)"
           fetchDeps (\v flags -> flags { fetchDeps = v })
           trueArg

       , option [] ["no-dependencies", "no-deps"]
           "Ignore dependencies"
           fetchDeps (\v flags -> flags { fetchDeps = v })
           falseArg

       , option [] ["dry-run"]
           "Do not install anything, only print what would be installed."
           fetchDryRun (\v flags -> flags { fetchDryRun = v })
           trueArg
560 561 562 563

       ] ++

       optionSolver      fetchSolver           (\v flags -> flags { fetchSolver           = v }) :
564 565
       optionSolverFlags showOrParseArgs
                         fetchMaxBackjumps     (\v flags -> flags { fetchMaxBackjumps     = v })
566 567
                         fetchReorderGoals     (\v flags -> flags { fetchReorderGoals     = v })
                         fetchIndependentGoals (\v flags -> flags { fetchIndependentGoals = v })
568
                         fetchShadowPkgs       (\v flags -> flags { fetchShadowPkgs       = v })
Andres Löh's avatar
Andres Löh committed
569
                         fetchStrongFlags      (\v flags -> flags { fetchStrongFlags      = v })
570

Duncan Coutts's avatar
Duncan Coutts committed
571 572
  }

573 574 575 576 577 578
-- ------------------------------------------------------------
-- * Freeze command
-- ------------------------------------------------------------

data FreezeFlags = FreezeFlags {
      freezeDryRun           :: Flag Bool,
579 580
      freezeTests            :: Flag Bool,
      freezeBenchmarks       :: Flag Bool,
581 582 583 584 585
      freezeSolver           :: Flag PreSolver,
      freezeMaxBackjumps     :: Flag Int,
      freezeReorderGoals     :: Flag Bool,
      freezeIndependentGoals :: Flag Bool,
      freezeShadowPkgs       :: Flag Bool,
Andres Löh's avatar
Andres Löh committed
586
      freezeStrongFlags      :: Flag Bool,
587 588 589 590 591 592
      freezeVerbosity        :: Flag Verbosity
    }

defaultFreezeFlags :: FreezeFlags
defaultFreezeFlags = FreezeFlags {
    freezeDryRun           = toFlag False,
593 594
    freezeTests            = toFlag False,
    freezeBenchmarks       = toFlag False,
595 596
    freezeSolver           = Flag defaultSolver,
    freezeMaxBackjumps     = Flag defaultMaxBackjumps,
597
    freezeReorderGoals     = Flag False,
598 599
    freezeIndependentGoals = Flag False,
    freezeShadowPkgs       = Flag False,
Andres Löh's avatar
Andres Löh committed
600
    freezeStrongFlags      = Flag False,
601 602 603 604 605 606 607 608
    freezeVerbosity        = toFlag normal
   }

freezeCommand :: CommandUI FreezeFlags
freezeCommand = CommandUI {
    commandName         = "freeze",
    commandSynopsis     = "Freeze dependencies.",
    commandDescription  = Nothing,
609
    commandNotes        = Nothing,
610 611 612 613 614 615 616 617 618 619
    commandUsage        = usagePackages "freeze",
    commandDefaultFlags = defaultFreezeFlags,
    commandOptions      = \ showOrParseArgs -> [
         optionVerbosity freezeVerbosity (\v flags -> flags { freezeVerbosity = v })

       , option [] ["dry-run"]
           "Do not freeze anything, only print what would be frozen"
           freezeDryRun (\v flags -> flags { freezeDryRun = v })
           trueArg

620 621 622 623 624 625 626 627 628 629
       , option [] ["tests"]
           "freezing of the dependencies of any tests suites in the package description file."
           freezeTests (\v flags -> flags { freezeTests = v })
           (boolOpt [] [])

       , option [] ["benchmarks"]
           "freezing of the dependencies of any benchmarks suites in the package description file."
           freezeBenchmarks (\v flags -> flags { freezeBenchmarks = v })
           (boolOpt [] [])

630 631 632 633 634 635 636 637
       ] ++

       optionSolver      freezeSolver           (\v flags -> flags { freezeSolver           = v }) :
       optionSolverFlags showOrParseArgs
                         freezeMaxBackjumps     (\v flags -> flags { freezeMaxBackjumps     = v })
                         freezeReorderGoals     (\v flags -> flags { freezeReorderGoals     = v })
                         freezeIndependentGoals (\v flags -> flags { freezeIndependentGoals = v })
                         freezeShadowPkgs       (\v flags -> flags { freezeShadowPkgs       = v })
Andres Löh's avatar
Andres Löh committed
638
                         freezeStrongFlags      (\v flags -> flags { freezeStrongFlags      = v })
639 640 641

  }

642 643 644 645
-- ------------------------------------------------------------
-- * Other commands
-- ------------------------------------------------------------

646
updateCommand  :: CommandUI (Flag Verbosity)
Duncan Coutts's avatar
Duncan Coutts committed
647 648
updateCommand = CommandUI {
    commandName         = "update",
649
    commandSynopsis     = "Updates list of known packages.",
Duncan Coutts's avatar
Duncan Coutts committed
650
    commandDescription  = Nothing,
651
    commandNotes        = Nothing,
refold's avatar
refold committed
652
    commandUsage        = usageFlags "update",
653
    commandDefaultFlags = toFlag normal,
654
    commandOptions      = \_ -> [optionVerbosity id const]
Duncan Coutts's avatar
Duncan Coutts committed
655 656
  }

657
upgradeCommand  :: CommandUI (ConfigFlags, ConfigExFlags, InstallFlags, HaddockFlags)
658
upgradeCommand = configureCommand {
ijones's avatar
ijones committed
659
    commandName         = "upgrade",
660
    commandSynopsis     = "(command disabled, use install instead)",
ijones's avatar
ijones committed
661
    commandDescription  = Nothing,
refold's avatar
refold committed
662
    commandUsage        = usageFlagsOrPackages "upgrade",
663
    commandDefaultFlags = (mempty, mempty, mempty, mempty),
664
    commandOptions      = commandOptions installCommand
ijones's avatar
ijones committed
665 666
  }

Duncan Coutts's avatar
Duncan Coutts committed
667 668 669 670 671 672 673 674 675 676 677
{-
cleanCommand  :: CommandUI ()
cleanCommand = makeCommand name shortDesc longDesc emptyFlags options
  where
    name       = "clean"
    shortDesc  = "Removes downloaded files"
    longDesc   = Nothing
    emptyFlags = ()
    options _  = []
-}

678 679 680
checkCommand  :: CommandUI (Flag Verbosity)
checkCommand = CommandUI {
    commandName         = "check",
681
    commandSynopsis     = "Check the package for common mistakes.",
682
    commandDescription  = Nothing,
683
    commandNotes        = Nothing,
684
    commandUsage        = \pname -> "Usage: " ++ pname ++ " check\n",
685
    commandDefaultFlags = toFlag normal,
686
    commandOptions      = \_ -> []
687 688
  }

689 690 691 692 693
formatCommand  :: CommandUI (Flag Verbosity)
formatCommand = CommandUI {
    commandName         = "format",
    commandSynopsis     = "Reformat the .cabal file using the standard style.",
    commandDescription  = Nothing,
694
    commandNotes        = Nothing,
695 696 697 698 699
    commandUsage        = \pname -> "Usage: " ++ pname ++ " format [FILE]\n",
    commandDefaultFlags = toFlag normal,
    commandOptions      = \_ -> []
  }

700
runCommand :: CommandUI (BuildFlags, BuildExFlags)
refold's avatar
refold committed
701 702 703 704
runCommand = CommandUI {
    commandName         = "run",
    commandSynopsis     = "Runs the compiled executable.",
    commandDescription  = Nothing,
705
    commandNotes        = Nothing,
refold's avatar
refold committed
706
    commandUsage        =
707
      \pname -> "Usage: " ++ pname
708
                ++ " run [FLAGS] [EXECUTABLE] [-- EXECUTABLE_FLAGS]\n",
refold's avatar
refold committed
709
    commandDefaultFlags = mempty,
710 711
    commandOptions      =
      \showOrParseArgs -> liftOptions fst setFst
712
                          (commandOptions parent showOrParseArgs)
713 714 715
                          ++
                          liftOptions snd setSnd
                          (buildExOptions showOrParseArgs)
refold's avatar
refold committed
716 717
  }
  where
718 719 720
    setFst a (_,b) = (a,b)
    setSnd b (a,_) = (a,b)

721
    parent = Cabal.buildCommand defaultProgramConfiguration
refold's avatar
refold committed
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
-- ------------------------------------------------------------
-- * Report flags
-- ------------------------------------------------------------

data ReportFlags = ReportFlags {
    reportUsername  :: Flag Username,
    reportPassword  :: Flag Password,
    reportVerbosity :: Flag Verbosity
  }

defaultReportFlags :: ReportFlags
defaultReportFlags = ReportFlags {
    reportUsername  = mempty,
    reportPassword  = mempty,
    reportVerbosity = toFlag normal
  }

reportCommand :: CommandUI ReportFlags
741 742 743
reportCommand = CommandUI {
    commandName         = "report",
    commandSynopsis     = "Upload build reports to a remote server.",
744 745
    commandDescription  = Nothing,
    commandNotes        = Just $ \_ ->
746
         "You can store your Hackage login in the ~/.cabal/config file\n",
747
    commandUsage        = \pname -> "Usage: " ++ pname ++ " report [FLAGS]\n",
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    commandDefaultFlags = defaultReportFlags,
    commandOptions      = \_ ->
      [optionVerbosity reportVerbosity (\v flags -> flags { reportVerbosity = v })

      ,option ['u'] ["username"]
        "Hackage username."
        reportUsername (\v flags -> flags { reportUsername = v })
        (reqArg' "USERNAME" (toFlag . Username)
                            (flagToList . fmap unUsername))

      ,option ['p'] ["password"]
        "Hackage password."
        reportPassword (\v flags -> flags { reportPassword = v })
        (reqArg' "PASSWORD" (toFlag . Password)
                            (flagToList . fmap unPassword))
      ]
764 765
  }

766 767 768 769 770 771 772 773 774 775 776 777 778
instance Monoid ReportFlags where
  mempty = ReportFlags {
    reportUsername  = mempty,
    reportPassword  = mempty,
    reportVerbosity = mempty
  }
  mappend a b = ReportFlags {
    reportUsername  = combine reportUsername,
    reportPassword  = combine reportPassword,
    reportVerbosity = combine reportVerbosity
  }
    where combine field = field a `mappend` field b

779
-- ------------------------------------------------------------
780
-- * Get flags
781 782
-- ------------------------------------------------------------

783 784 785 786 787 788
data GetFlags = GetFlags {
    getDestDir          :: Flag FilePath,
    getPristine         :: Flag Bool,
    getSourceRepository :: Flag (Maybe RepoKind),
    getVerbosity        :: Flag Verbosity
  }
789

790 791 792 793 794 795
defaultGetFlags :: GetFlags
defaultGetFlags = GetFlags {
    getDestDir          = mempty,
    getPristine         = mempty,
    getSourceRepository = mempty,
    getVerbosity        = toFlag normal
796 797
   }

798 799 800 801
getCommand :: CommandUI GetFlags
getCommand = CommandUI {
    commandName         = "get",
    commandSynopsis     = "Gets a package's source code.",
802 803 804 805 806
    commandDescription  = Just $ \_ ->
          "Creates a local copy of a package's source code. By default it gets "
       ++ "the source\ntarball and unpacks it in a local subdirectory. "
       ++ "Alternatively, with -s it will\nget the code from the source "
       ++ "repository specified by the package.\n",
807
    commandNotes        = Nothing,
808
    commandUsage        = usagePackages "get",
809
    commandDefaultFlags = defaultGetFlags,
810
    commandOptions      = \_ -> [
811
        optionVerbosity getVerbosity (\v flags -> flags { getVerbosity = v })
812 813

       ,option "d" ["destdir"]
refold's avatar
refold committed
814
         "Where to place the package source, defaults to the current directory."
815
         getDestDir (\v flags -> flags { getDestDir = v })
816
         (reqArgFlag "PATH")
817

818
       ,option "s" ["source-repository"]
819
         "Copy the package's source repository (ie git clone, darcs get, etc as appropriate)."
820 821 822 823 824 825
         getSourceRepository (\v flags -> flags { getSourceRepository = v })
        (optArg "[head|this|...]" (readP_to_E (const "invalid source-repository")
                                              (fmap (toFlag . Just) parse))
                                  (Flag Nothing)
                                  (map (fmap show) . flagToList))

826 827 828
       , option [] ["pristine"]
           ("Unpack the original pristine tarball, rather than updating the "
           ++ ".cabal file with the latest revision from the package archive.")
829
           getPristine (\v flags -> flags { getPristine = v })
830
           trueArg
831 832 833
       ]
  }

834 835 836 837 838 839 840
-- 'cabal unpack' is a deprecated alias for 'cabal get'.
unpackCommand :: CommandUI GetFlags
unpackCommand = getCommand {
  commandName  = "unpack",
  commandUsage = usagePackages "unpack"
  }

841
instance Monoid GetFlags where
842 843 844 845 846 847
  mempty = GetFlags {
    getDestDir          = mempty,
    getPristine         = mempty,
    getSourceRepository = mempty,
    getVerbosity        = mempty
    }
848 849 850 851 852
  mappend a b = GetFlags {
    getDestDir          = combine getDestDir,
    getPristine         = combine getPristine,
    getSourceRepository = combine getSourceRepository,
    getVerbosity        = combine getVerbosity
853 854 855
  }
    where combine field = field a `mappend` field b

856 857 858 859 860
-- ------------------------------------------------------------
-- * List flags
-- ------------------------------------------------------------

data ListFlags = ListFlags {
861
    listInstalled    :: Flag Bool,
862
    listSimpleOutput :: Flag Bool,
863 864
    listVerbosity    :: Flag Verbosity,
    listPackageDBs   :: [Maybe PackageDB]
865 866 867 868
  }

defaultListFlags :: ListFlags
defaultListFlags = ListFlags {
869
    listInstalled    = Flag False,
870
    listSimpleOutput = Flag False,
871 872
    listVerbosity    = toFlag normal,
    listPackageDBs   = []
873 874 875 876 877
  }

listCommand  :: CommandUI ListFlags
listCommand = CommandUI {
    commandName         = "list",
878
    commandSynopsis     = "List packages matching a search string.",
879
    commandDescription  = Nothing,
880
    commandNotes        = Nothing,
refold's avatar
refold committed
881
    commandUsage        = usageFlagsOrPackages "list",
882
    commandDefaultFlags = defaultListFlags,
883
    commandOptions      = \_ -> [
884
        optionVerbosity listVerbosity (\v flags -> flags { listVerbosity = v })
885

886
        , option [] ["installed"]
887 888 889 890
            "Only print installed packages"
            listInstalled (\v flags -> flags { listInstalled = v })
            trueArg

891 892 893 894 895
        , option [] ["simple-output"]
            "Print in a easy-to-parse format"
            listSimpleOutput (\v flags -> flags { listSimpleOutput = v })
            trueArg

896 897 898 899 900
        , option "" ["package-db"]
          "Use a given package database. May be a specific file, 'global', 'user' or 'clear'."
          listPackageDBs (\v flags -> flags { listPackageDBs = v })
          (reqArg' "DB" readPackageDbList showPackageDbList)

901 902 903 904
        ]
  }

instance Monoid ListFlags where
905 906 907 908 909 910
  mempty = ListFlags {
    listInstalled    = mempty,
    listSimpleOutput = mempty,
    listVerbosity    = mempty,
    listPackageDBs   = mempty
    }
911
  mappend a b = ListFlags {
912
    listInstalled    = combine listInstalled,
913
    listSimpleOutput = combine listSimpleOutput,
914 915
    listVerbosity    = combine listVerbosity,
    listPackageDBs   = combine listPackageDBs
916 917 918
  }
    where combine field = field a `mappend` field b

919 920 921 922 923
-- ------------------------------------------------------------
-- * Info flags
-- ------------------------------------------------------------

data InfoFlags = InfoFlags {
924 925
    infoVerbosity  :: Flag Verbosity,
    infoPackageDBs :: [Maybe PackageDB]