Setup.hs 6.45 KB
Newer Older
1 2
-----------------------------------------------------------------------------
-- |
Don Stewart's avatar
Don Stewart committed
3
-- Module      :  Hackage.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
--
--
-----------------------------------------------------------------------------
Don Stewart's avatar
Don Stewart committed
13
module Hackage.Setup
Duncan Coutts's avatar
Duncan Coutts committed
14 15 16 17 18 19 20 21 22
    ( globalCommand, Cabal.GlobalFlags(..)
    , installCommand --Cabal.InstallFlags(..)
    , listCommand
    , updateCommand
    , infoCommand
    , fetchCommand

    , parsePackageArgs
    , updateConfig
23 24
    ) where

Duncan Coutts's avatar
Duncan Coutts committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import Distribution.Simple.Program (ProgramConfiguration)
import Distribution.Simple.Compiler (PackageDB(..))
import Distribution.Simple.InstallDirs (combineInstallDirs)
import Distribution.Simple.Command
import qualified Distribution.Simple.Setup as Cabal
  (GlobalFlags(..),  {-emptyGlobalFlags,-}   globalCommand,
  ConfigFlags(..),   {-emptyConfigFlags,-}   configureCommand,
{-  CopyFlags(..),     emptyCopyFlags,     copyCommand,
  InstallFlags(..),  emptyInstallFlags,  installCommand,
  HaddockFlags(..),  emptyHaddockFlags,  haddockCommand,
  HscolourFlags(..), emptyHscolourFlags, hscolourCommand,
  BuildFlags(..),    emptyBuildFlags,    buildCommand,
  CleanFlags(..),    emptyCleanFlags,    cleanCommand,
  PFEFlags(..),      emptyPFEFlags,      programaticaCommand,
  MakefileFlags(..), emptyMakefileFlags, makefileCommand,
  RegisterFlags(..), emptyRegisterFlags, registerCommand, unregisterCommand,
  SDistFlags(..),    emptySDistFlags,    sdistCommand,
                                         testCommand-})
import Distribution.Simple.Setup (fromFlagOrDefault, flagToMaybe)
--import System.Console.GetOpt (ArgDescr (..), OptDescr (..))

import Hackage.Types (ConfigFlags(..), UnresolvedDependency(..))
47
import Hackage.Utils (readPToMaybe, parseDependencyOrPackageId)
48

Duncan Coutts's avatar
Duncan Coutts committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
import Control.Monad (MonadPlus(mplus))

-- | This function updates the configuration with the cabal configure flags.
updateConfig :: Cabal.ConfigFlags -> ConfigFlags -> ConfigFlags
updateConfig flags conf = conf {
      configCompiler          = override configCompiler Cabal.configHcFlavor,
      configCompilerPath      = configCompilerPath conf
                        `mplus` flagToMaybe (Cabal.configHcPath flags),
      configHcPkgPath         = configHcPkgPath conf
                        `mplus` flagToMaybe (Cabal.configHcPkg flags),
      configUserInstallDirs   = overrideInstallDirs userInstall
                                  (configUserInstallDirs conf),
      configGlobalInstallDirs = overrideInstallDirs (not userInstall)
                                  (configGlobalInstallDirs conf),
--    configCacheDir          =    :: FilePath,
--    configRepos             =    :: [Repo],
      configVerbose           = override configVerbose Cabal.configVerbose,
      configUserInstall       = userInstall
  }
  where override base over = fromFlagOrDefault (base conf) (over flags)
        overrideInstallDirs False base = base
        overrideInstallDirs True base = combineInstallDirs mplus base
                      (fmap flagToMaybe (Cabal.configInstallDirs flags))
        userInstall = case flagToMaybe $ Cabal.configPackageDB flags of
                        Nothing              -> configUserInstall conf
                        Just (UserPackageDB) -> True
                        Just _               -> False

globalCommand :: CommandUI Cabal.GlobalFlags
globalCommand = Cabal.globalCommand {
79 80 81 82 83 84 85 86 87
    commandDescription = Just $ \pname ->
         "Typical step for installing Cabal packages:\n"
      ++ "  " ++ pname ++ " install [PACKAGES]\n"
      ++ "\nOccasionally you need to update the list of available packages:\n"
      ++ "  " ++ pname ++ " update\n"
      ++ "\nFor more information about a command, try '"
          ++ pname ++ " COMMAND --help'."
      ++ "\nThis program is the command line interface to the Haskell Cabal Infrastructure."
      ++ "\nSee http://www.haskell.org/cabal/ for more information.\n"
Duncan Coutts's avatar
Duncan Coutts committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  }

installCommand :: ProgramConfiguration -> CommandUI Cabal.ConfigFlags
installCommand progConf = (Cabal.configureCommand progConf) {
    commandName     = "install",
    commandSynopsis = "Installs a list of packages.",
    commandUsage       = usagePackages "install"
  }

fetchCommand :: CommandUI ()
fetchCommand = CommandUI {
    commandName         = "fetch",
    commandSynopsis     = "Downloads packages for later installation or study.",
    commandDescription  = Nothing,
    commandUsage        = usagePackages "fetch",
    commandDefaultFlags = (),
    commandOptions      = \_ -> []
  }

listCommand  :: CommandUI ()
listCommand = CommandUI {
    commandName         = "list",
    commandSynopsis     = "List available packages on the server (cached).",
    commandDescription  = Nothing,
    commandUsage        = usagePackages "list",
    commandDefaultFlags = (),
    commandOptions      = \_ -> []
  }

updateCommand  :: CommandUI ()
updateCommand = CommandUI {
    commandName         = "update",
    commandSynopsis     = "Updates list of known packages",
    commandDescription  = Nothing,
    commandUsage        = usagePackages "update",
    commandDefaultFlags = (),
    commandOptions      = \_ -> []
  }

{-
cleanCommand  :: CommandUI ()
cleanCommand = makeCommand name shortDesc longDesc emptyFlags options
  where
    name       = "clean"
    shortDesc  = "Removes downloaded files"
    longDesc   = Nothing
    emptyFlags = ()
    options _  = []
-}

infoCommand  :: CommandUI ()
infoCommand = CommandUI {
    commandName         = "info",
    commandSynopsis     = "Emit some info about dependency resolution",
    commandDescription  = Nothing,
    commandUsage        = usagePackages "info",
    commandDefaultFlags = (),
    commandOptions      = \_ -> []
  }

usagePackages :: String -> String -> String
usagePackages pname name =
     "Usage: " ++ pname ++ " " ++ name ++ " [FLAGS]\n"
  ++ "   or: " ++ pname ++ " " ++ name ++ " [PACKAGES]\n\n"
  ++ "Flags for " ++ name ++ ":"

parsePackageArgs :: [String] -> Either String [UnresolvedDependency]
parsePackageArgs = parsePkgArgs []
  where
    parsePkgArgs ds [] = Right (reverse ds)
    parsePkgArgs ds (arg:args) =
      case readPToMaybe parseDependencyOrPackageId arg of
        Just dep -> let d = UnresolvedDependency {
                              dependency = dep,
                              depFlags   = []
                            }
                     in parsePkgArgs (d:ds) args
        Nothing  -> Left ("Failed to parse package dependency: " ++ show arg)