{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict #-}
module VtUtils.Process
( processSpawnAndWait
) where
import Prelude (Int, IO, ($), (<$>), return)
import Data.Text (Text, unpack)
import Data.Vector (Vector, toList)
import System.Exit (ExitCode(..))
import System.IO (IOMode(WriteMode), withFile)
import System.Process (CreateProcess(..), StdStream(..), proc, waitForProcess, withCreateProcess)
processSpawnAndWait :: Text -> Vector Text -> Text -> IO Int
processSpawnAndWait executable args out = do
code <- withFile (unpack out) WriteMode $ \ha -> do
let argsList = toList (unpack <$> args)
let cpRaw = proc (unpack executable) argsList
let cp = cpRaw
{ std_in = NoStream
, std_out = UseHandle ha
, std_err = UseHandle ha
}
withCreateProcess cp $ \_ _ _ ph ->
waitForProcess ph
case code of
ExitSuccess -> return 0
ExitFailure num -> return num