1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Client.Run
-- Maintainer : cabal-devel@haskell.org
-- Portability : portable
--
-- Implementation of the 'run' command.
-----------------------------------------------------------------------------
module Distribution.Client.Run ( run, splitRunArgs )
where
import Distribution.Client.Utils (tryCanonicalizePath)
import Distribution.PackageDescription (Executable (..),
PackageDescription (..))
import Distribution.Simple.Build.PathsModule (pkgPathEnvVar)
import Distribution.Simple.BuildPaths (exeExtension)
import Distribution.Simple.LocalBuildInfo (LocalBuildInfo (..))
import Distribution.Simple.Utils (die, rawSystemExitWithEnv)
import Distribution.Verbosity (Verbosity)
import Data.Functor ((<$>))
import Data.List (find)
import System.Directory (getCurrentDirectory)
import Distribution.Compat.Environment (getEnvironment)
import System.FilePath ((<.>), (</>))
-- | Return the executable to run and any extra arguments that should be
-- forwarded to it.
splitRunArgs :: LocalBuildInfo -> [String] -> IO (Executable, [String])
splitRunArgs lbi args =
case exes of
[] -> die "Couldn't find any executables."
[exe] -> case args of
[] -> return (exe, [])
(x:xs) | x == exeName exe -> return (exe, xs)
| otherwise -> return (exe, args)
_ -> case args of
[] -> die $ "This package contains multiple executables. "
++ "You must pass the executable name as the first argument "
++ "to 'cabal run'."
(x:xs) -> case find (\exe -> exeName exe == x) exes of
Nothing -> die $ "No executable named '" ++ x ++ "'."
Just exe -> return (exe, xs)
where
pkg_descr = localPkgDescr lbi
exes = executables pkg_descr
-- | Run a given executable.
run :: Verbosity -> LocalBuildInfo -> Executable -> [String] -> IO ()
run verbosity lbi exe exeArgs = do
curDir <- getCurrentDirectory
let buildPref = buildDir lbi
pkg_descr = localPkgDescr lbi
dataDirEnvVar = (pkgPathEnvVar pkg_descr "datadir",
curDir </> dataDir pkg_descr)
path <- tryCanonicalizePath $
buildPref </> exeName exe </> (exeName exe <.> exeExtension)
env <- (dataDirEnvVar:) <$> getEnvironment
rawSystemExitWithEnv verbosity path exeArgs env
|