module VCSGui.Common.Process (
exec
) where
import System.Process
import System.Exit
import System.IO (Handle, hFlush, hClose, hGetContents, hPutStr)
import Control.Concurrent
import Control.Monad.Reader
import qualified Control.Exception as Exc
exec :: Maybe FilePath
-> String
-> [String]
-> IO Bool
exec mcwd cmd opts = do
(ec, out, err) <- readProc mcwd cmd opts
case ec of
ExitSuccess -> return $ True
ExitFailure i -> return $ False
readProc :: Maybe FilePath
-> String --command
-> [String]
-> IO (ExitCode, String, String)
readProc mcwd cmd files = do
putStrLn $ "Executing process, mcwd: "++show mcwd++"cmd: "++show cmd++",files: "++show files
(_, Just outh, Just errh, pid) <- createProcess (proc cmd files)
{ std_out = CreatePipe,
std_err = CreatePipe,
cwd = mcwd
}
outMVar <- newEmptyMVar
out <- hGetContents outh
_ <- forkIO $ Exc.evaluate (length out) >> putMVar outMVar ()
err <- hGetContents errh
_ <- forkIO $ Exc.evaluate (length err) >> putMVar outMVar ()
takeMVar outMVar
takeMVar outMVar
hClose outh
hClose errh
ex <- waitForProcess pid
return (ex, out, err)