Skip to content

Commit

Permalink
Split off process execution into library (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdone committed Jun 16, 2015
1 parent 1c25585 commit cee427a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
31 changes: 31 additions & 0 deletions src/Stack/Exec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- | Execute commands within the properly configured Stack
-- environment.

module Stack.Exec where

import Control.Monad.Reader
import Path
import Stack.Types
import System.Exit
import qualified System.Process as P
import System.Process.Read

-- | Execute a process within the Stack configured environment.
exec :: (HasConfig r, MonadReader r m, MonadIO m)
=> String -> [String] -> m b
exec cmd args = do
config <- asks getConfig
liftIO $ do
menv <- configEnvOverride config
EnvSettings
{ esIncludeLocals = True
, esIncludeGhcPackagePath = True
}
cmd' <- join $ System.Process.Read.findExecutable menv cmd
let cp = (P.proc (toFilePath cmd') args)
{ P.env = envHelper menv
, P.delegate_ctlc = True
}
(Nothing, Nothing, Nothing, ph) <- P.createProcess cp
ec <- P.waitForProcess ph
exitWith ec
23 changes: 5 additions & 18 deletions src/main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import Stack.Build.Types
import Stack.Config
import Stack.Constants
import qualified Stack.Docker as Docker
import Stack.Exec
import Stack.Fetch
import Stack.GhcPkg (envHelper,getCabalPkgVer)
import Stack.GhcPkg (getCabalPkgVer)
import qualified Stack.PackageIndex
import Stack.Path
import Stack.Setup
Expand All @@ -49,7 +50,6 @@ import System.Environment (getArgs, getProgName)
import System.Exit
import System.FilePath (searchPathSeparator)
import System.IO (stderr)
import qualified System.Process as P
import qualified System.Process.Read

-- | Commandline dispatcher.
Expand Down Expand Up @@ -370,23 +370,10 @@ uploadCmd args0 go = withBuildConfig go ExecStrategy $ do

-- | Execute a command.
execCmd :: (String, [String]) -> GlobalOpts -> IO ()
execCmd (cmd, args) go@GlobalOpts{..} = withBuildConfig go ExecStrategy $ do
config <- asks getConfig
liftIO $ do
menv <- configEnvOverride config
EnvSettings
{ esIncludeLocals = True
, esIncludeGhcPackagePath = True
}
cmd' <- join $ System.Process.Read.findExecutable menv cmd
let cp = (P.proc (toFilePath cmd') args)
{ P.env = envHelper menv
, P.delegate_ctlc = True
}
execCmd (cmd,args) go@GlobalOpts{..} =
withBuildConfig go ExecStrategy $
exec cmd args

(Nothing, Nothing, Nothing, ph) <- P.createProcess cp
ec <- P.waitForProcess ph
exitWith ec

-- | Pull the current Docker image.
dockerPullCmd :: () -> GlobalOpts -> IO ()
Expand Down
1 change: 1 addition & 0 deletions stack.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ library
Stack.Docker
Stack.Docker.GlobalDB
Stack.Fetch
Stack.Exec
Stack.GhcPkg
Stack.Package
Stack.PackageDump
Expand Down

0 comments on commit cee427a

Please sign in to comment.