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

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

import Control.Applicative ( Applicative(pure) )
import Control.Category ( Category((.)) )
import Control.Exitcode
    ( ExitcodeT0,
      fromExitCode',
      liftExitcode,
      ExitcodeT1,
      _Exitcode1,
      hoistExitcode )
import Control.Lens ( Identity(runIdentity), set )
import Control.Monad ( Monad((>>=)) )
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)
import Control.Monad.Trans.Maybe ( MaybeT(MaybeT) )

readCreateProcessWithExitCode ::
  CreateProcess
  -> String
  -> ExitcodeT1 IO (String, String)
readCreateProcessWithExitCode :: CreateProcess -> String -> ExitcodeT1 IO (String, String)
readCreateProcessWithExitCode CreateProcess
p String
a =
  IO (ExitCode, String, String)
-> ExitcodeT IO (String, String) (ExitCode, String, String)
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) ExitcodeT IO (String, String) (ExitCode, String, String)
-> ((ExitCode, String, String) -> ExitcodeT1 IO (String, String))
-> ExitcodeT1 IO (String, String)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(ExitCode
x, String
y, String
z) ->
    (forall x. Identity x -> IO x)
-> ExitcodeT Identity (String, String) (String, String)
-> ExitcodeT1 IO (String, String)
forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (x -> IO x
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> IO x) -> (Identity x -> x) -> Identity x -> IO x
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Identity x -> x
forall a. Identity a -> a
runIdentity) (ASetter
  (Exitcode1 ())
  (ExitcodeT Identity (String, String) (String, String))
  ()
  (String, String)
-> (String, String)
-> Exitcode1 ()
-> ExitcodeT Identity (String, String) (String, String)
forall s t a b. ASetter s t a b -> b -> s -> t
set ASetter
  (Exitcode1 ())
  (ExitcodeT Identity (String, String) (String, String))
  ()
  (String, String)
forall a a'. Lens (Exitcode1 a) (Exitcode1 a') a a'
_Exitcode1 (String
y, String
z) (ExitCode -> Exitcode1 ()
fromExitCode' ExitCode
x))

readProcessWithExitCode ::
  FilePath
  -> [String]
  -> String
  -> ExitcodeT1 IO (String, String)
readProcessWithExitCode :: String -> [String] -> String -> ExitcodeT1 IO (String, String)
readProcessWithExitCode String
p [String]
a String
i =
  IO (ExitCode, String, String)
-> ExitcodeT IO (String, String) (ExitCode, String, String)
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) ExitcodeT IO (String, String) (ExitCode, String, String)
-> ((ExitCode, String, String) -> ExitcodeT1 IO (String, String))
-> ExitcodeT1 IO (String, String)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(ExitCode
x, String
y, String
z) ->
    (forall x. Identity x -> IO x)
-> ExitcodeT Identity (String, String) (String, String)
-> ExitcodeT1 IO (String, String)
forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (x -> IO x
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> IO x) -> (Identity x -> x) -> Identity x -> IO x
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Identity x -> x
forall a. Identity a -> a
runIdentity) (ASetter
  (Exitcode1 ())
  (ExitcodeT Identity (String, String) (String, String))
  ()
  (String, String)
-> (String, String)
-> Exitcode1 ()
-> ExitcodeT Identity (String, String) (String, String)
forall s t a b. ASetter s t a b -> b -> s -> t
set ASetter
  (Exitcode1 ())
  (ExitcodeT Identity (String, String) (String, String))
  ()
  (String, String)
forall a a'. Lens (Exitcode1 a) (Exitcode1 a') a a'
_Exitcode1 (String
y, String
z) (ExitCode -> Exitcode1 ()
fromExitCode' ExitCode
x))

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

getProcessExitCode ::
  ProcessHandle
  -> ExitcodeT0 (MaybeT IO)
getProcessExitCode :: ProcessHandle -> ExitcodeT0 (MaybeT IO)
getProcessExitCode ProcessHandle
h =
  MaybeT IO ExitCode -> ExitcodeT (MaybeT IO) () ExitCode
forall (f :: * -> *) a e. Functor f => f a -> ExitcodeT f e a
liftExitcode (IO (Maybe ExitCode) -> MaybeT IO ExitCode
forall (m :: * -> *) a. m (Maybe a) -> MaybeT m a
MaybeT (ProcessHandle -> IO (Maybe ExitCode)
P.getProcessExitCode ProcessHandle
h)) ExitcodeT (MaybeT IO) () ExitCode
-> (ExitCode -> ExitcodeT0 (MaybeT IO)) -> ExitcodeT0 (MaybeT IO)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    (forall x. Identity x -> MaybeT IO x)
-> Exitcode1 () -> ExitcodeT0 (MaybeT IO)
forall (f :: * -> *) (g :: * -> *) e a.
(forall x. f x -> g x) -> ExitcodeT f e a -> ExitcodeT g e a
hoistExitcode (x -> MaybeT IO x
forall (f :: * -> *) a. Applicative f => a -> f a
pure (x -> MaybeT IO x)
-> (Identity x -> x) -> Identity x -> MaybeT IO x
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Identity x -> x
forall a. Identity a -> a
runIdentity) (Exitcode1 () -> ExitcodeT0 (MaybeT IO))
-> (ExitCode -> Exitcode1 ()) -> ExitCode -> ExitcodeT0 (MaybeT IO)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ExitCode -> Exitcode1 ()
fromExitCode'