{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Control.Process.Process(
  module Process
, readCreateProcessWithExitCode
, readProcessWithExitCode
, waitForProcess
, getProcessExitCode
, getProcessExitCodeBool
) where

import Control.Applicative ( Applicative(pure) )
import Control.Category ( Category((.)) )
import Control.Exception ( Exception )
import Control.Exitcode
    ( ExitcodeT,
      fromExitCode',
      liftExitcode,
      hoistExitcode,
      tryExitcode,
      _Exitcode1,
      liftTryExitcode )
import Control.Lens ( Identity(runIdentity), set )
import Control.Monad ( Monad((>>=)) )
import Control.Monad.Except ( ExceptT(..) )
import Data.Bifunctor ( Bifunctor(bimap) )
import Data.Bool ( Bool )
import Data.Maybe ( Maybe(..), isJust, maybe )
import Data.String ( String )
import System.FilePath( FilePath )
import System.IO ( IO )
import System.Process as Process(
    createProcess
  , createProcess_
  , shell
  , proc
  , CreateProcess()
  , CmdSpec(..)
  , StdStream(..)
  , ProcessHandle
  , callProcess
  , callCommand
  , spawnProcess
  , readCreateProcess
  , readProcess
  , withCreateProcess
  , cleanupProcess
  , showCommandForUser
  , Pid
  , getPid
  , getCurrentPid
  , terminateProcess
  , interruptProcessGroupOf
  , createPipe
  , createPipeFd
  )
import qualified System.Process as P(readCreateProcessWithExitCode, readProcessWithExitCode, waitForProcess, getProcessExitCode)

readCreateProcessWithExitCode ::
  Exception e' =>
  CreateProcess
  -> String
  -> ExitcodeT (ExceptT e' IO) (String, String) (String, String)
readCreateProcessWithExitCode :: forall e'.
Exception e' =>
CreateProcess
-> String
-> ExitcodeT (ExceptT e' IO) (String, String) (String, String)
readCreateProcessWithExitCode CreateProcess
p String
a =
  forall e' e a.
Exception e' =>
ExitcodeT IO e a -> ExitcodeT (ExceptT e' IO) e a
tryExitcode (forall (f :: * -> *) a e. Functor f => f a -> ExitcodeT f e a
liftExitcode (CreateProcess -> String -> IO (ExitCode, String, String)
P.readCreateProcessWithExitCode CreateProcess
p String
a)) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(ExitCode
x, String
y, String
z) ->
    forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Identity a -> a
runIdentity) (forall s t a b. ASetter s t a b -> b -> s -> t
set forall a a'. Lens (Exitcode1 a) (Exitcode1 a') a a'
_Exitcode1 (String
y, String
z) (ExitCode -> Exitcode0
fromExitCode' ExitCode
x))

readProcessWithExitCode ::
  Exception e' =>
  FilePath
  -> [String]
  -> String
  -> ExitcodeT (ExceptT e' IO) (String, String) (String, String)
readProcessWithExitCode :: forall e'.
Exception e' =>
String
-> [String]
-> String
-> ExitcodeT (ExceptT e' IO) (String, String) (String, String)
readProcessWithExitCode String
p [String]
a String
i =
  forall e' e a.
Exception e' =>
ExitcodeT IO e a -> ExitcodeT (ExceptT e' IO) e a
tryExitcode (forall (f :: * -> *) a e. Functor f => f a -> ExitcodeT f e a
liftExitcode (String -> [String] -> String -> IO (ExitCode, String, String)
P.readProcessWithExitCode String
p [String]
a String
i)) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(ExitCode
x, String
y, String
z) ->
    forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Identity a -> a
runIdentity) (forall s t a b. ASetter s t a b -> b -> s -> t
set forall a a'. Lens (Exitcode1 a) (Exitcode1 a') a a'
_Exitcode1 (String
y, String
z) (ExitCode -> Exitcode0
fromExitCode' ExitCode
x))

waitForProcess ::
  Exception e' =>
  ProcessHandle
  -> ExitcodeT (ExceptT e' IO) () ()
waitForProcess :: forall e'.
Exception e' =>
ProcessHandle -> ExitcodeT (ExceptT e' IO) () ()
waitForProcess ProcessHandle
h =
  forall e' e a.
Exception e' =>
ExitcodeT IO e a -> ExitcodeT (ExceptT e' IO) e a
tryExitcode (forall (f :: * -> *) a e. Functor f => f a -> ExitcodeT f e a
liftExitcode (ProcessHandle -> IO ExitCode
P.waitForProcess ProcessHandle
h)) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ExitCode
x ->
    forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Identity a -> a
runIdentity) (ExitCode -> Exitcode0
fromExitCode' ExitCode
x)

getProcessExitCode ::
  Exception e' =>
  ProcessHandle
  -> ExitcodeT (ExceptT e' IO) (Maybe ()) (Maybe ())
getProcessExitCode :: forall e'.
Exception e' =>
ProcessHandle -> ExitcodeT (ExceptT e' IO) (Maybe ()) (Maybe ())
getProcessExitCode ProcessHandle
h =
  forall e' a e.
Exception e' =>
IO a -> ExitcodeT (ExceptT e' IO) e a
liftTryExitcode (ProcessHandle -> IO (Maybe ExitCode)
P.getProcessExitCode ProcessHandle
h) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing) (forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Identity a -> a
runIdentity) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall s t a b. ASetter s t a b -> b -> s -> t
set forall a a'. Lens (Exitcode1 a) (Exitcode1 a') a a'
_Exitcode1 (forall a. a -> Maybe a
Just ()) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ExitCode -> Exitcode0
fromExitCode')

getProcessExitCodeBool ::
  Exception e' =>
  ProcessHandle
  -> ExitcodeT (ExceptT e' IO) Bool Bool
getProcessExitCodeBool :: forall e'.
Exception e' =>
ProcessHandle -> ExitcodeT (ExceptT e' IO) Bool Bool
getProcessExitCodeBool =
  forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap forall a. Maybe a -> Bool
isJust forall a. Maybe a -> Bool
isJust forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall e'.
Exception e' =>
ProcessHandle -> ExitcodeT (ExceptT e' IO) (Maybe ()) (Maybe ())
getProcessExitCode