Exec.hs 2.34 KB
Newer Older
barmston's avatar
barmston committed
1 2 3 4 5 6 7 8 9 10 11 12 13
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Client.Exec
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Implementation of the 'exec' command. Runs an arbitrary executable in an
-- environment suitable for making use of the sandbox.
-----------------------------------------------------------------------------

module Distribution.Client.Exec ( exec
                                ) where

14 15
import Prelude ()
import Distribution.Client.Compat.Prelude
16

Oleg Grenrus's avatar
Oleg Grenrus committed
17
import Distribution.Simple.Compiler    (Compiler)
18 19 20
import Distribution.Simple.Program.Db  (ProgramDb, requireProgram, modifyProgramSearchPath)
import Distribution.Simple.Program.Run (programInvocation, runProgramInvocation)
import Distribution.Simple.Program.Types ( simpleProgram, ConfiguredProgram(..) )
Oleg Grenrus's avatar
Oleg Grenrus committed
21
import Distribution.Simple.Utils       (die')
barmston's avatar
barmston committed
22

Oleg Grenrus's avatar
Oleg Grenrus committed
23
import Distribution.System    (Platform(..))
barmston's avatar
barmston committed
24 25 26 27 28 29 30 31 32 33 34

-- | Execute the given command in the package's environment.
--
-- The given command is executed with GHC configured to use the correct
-- package database and with the sandbox bin directory added to the PATH.
exec :: Verbosity
     -> Compiler
     -> Platform
     -> ProgramDb
     -> [String]
     -> IO ()
Oleg Grenrus's avatar
Oleg Grenrus committed
35
exec verbosity _comp _platform programDb extraArgs =
barmston's avatar
barmston committed
36 37
    case extraArgs of
        (exe:args) -> do
Oleg Grenrus's avatar
Oleg Grenrus committed
38
            program <- requireProgram' verbosity programDb exe
39
            env <- environmentOverrides (programOverrideEnv program)
40 41 42 43
            let invocation = programInvocation
                                 program { programOverrideEnv = env }
                                 args
            runProgramInvocation verbosity invocation
barmston's avatar
barmston committed
44

45
        [] -> die' verbosity "Please specify an executable to run"
46
  where
Oleg Grenrus's avatar
Oleg Grenrus committed
47
    environmentOverrides env = return env
48 49 50 51 52 53 54

-- | Check that a program is configured and available to be run. If
-- a sandbox is available check in the sandbox's directory.
requireProgram' :: Verbosity
                -> ProgramDb
                -> String
                -> IO ConfiguredProgram
Oleg Grenrus's avatar
Oleg Grenrus committed
55
requireProgram' verbosity programDb exe = do
56 57 58 59 60 61 62
    (program, _) <- requireProgram
                        verbosity
                        (simpleProgram exe)
                        updateSearchPath
    return program
  where
    updateSearchPath =
Oleg Grenrus's avatar
Oleg Grenrus committed
63
        flip modifyProgramSearchPath programDb $ \searchPath -> searchPath