packages feed

atelier-typed-process-0.2.13.0 / src / Atelier / Process / Typed.hs

{-# LANGUAGE BlockArguments #-}{-# LANGUAGE DataKinds #-}-- | @typed-process@ lifted to @effectful@'s 'Eff'.---- This module aims to mirror `System.Process.Typed` as closely as possible.module Atelier.Process.Typed    ( -- * Effect      TypedProcess    , run      -- * Types    , ProcessConfig    , StreamSpec    , StreamType (..)    , Process      -- * ProcessConfig      -- ** Smart constructors    , proc    , shell      -- ** Setters    , setStdin    , setStdout    , setStderr    , setWorkingDir    , setWorkingDirInherit    , setEnv    , setEnvInherit    , setCloseFds    , setCreateGroup    , setDelegateCtlc    , setDetachConsole    , setCreateNewConsole    , setNewSession    , setChildGroup    , setChildGroupInherit    , setChildUser    , setChildUserInherit      -- * Stream specs      -- ** Built-in stream specs    , inherit    , nullStream    , closed    , byteStringInput    , byteStringOutput    , createPipe    , useHandleOpen    , useHandleClose      -- ** Create your own stream spec    , mkStreamSpec    , mkPipeStreamSpec      -- * Launch a process    , runProcess    , readProcess    , readProcessStdout    , readProcessStderr    , readProcessInterleaved    , withProcessWait    , withProcessTerm    , startProcess    , stopProcess      -- ** Exception-throwing functions    --    -- The functions ending in underbar (_) are the same as their counterparts    -- without underbar but instead of returning an ExitCode they throw    -- ExitCodeException on failure.    , runProcess_    , readProcess_    , readProcessStdout_    , readProcessStderr_    , readProcessInterleaved_    , withProcessWait_    , withProcessTerm_      -- * Interact with a process      -- ** Process exit code    , waitExitCode    , waitExitCodeSTM    , getExitCode    , getExitCodeSTM    , checkExitCode    , checkExitCodeSTM      -- ** Process ID    , getPid      -- ** Process streams    , getStdin    , getStdout    , getStderr      -- * Exceptions    , ExitCodeException (..)    , exitCodeExceptionWithOutput    , exitCodeExceptionNoOutput    , ByteStringOutputException (..)      -- * Re-exports    , ExitCode (..)    , StdStream (..)    , Pid    , Handle      -- * Unsafe functions    , unsafeProcessHandle    )whereimport Data.ByteString.Lazy (ByteString)import Effectful (Eff, IOE, Limit (..), Persistence (..), (:>))import Effectful.Concurrent.STM (STM)import Effectful.Dispatch.Static (StaticRep, concUnliftIO, getStaticRep, unsafeEff, unsafeEff_)import System.IO (Handle)import System.Process.Typed    ( ByteStringOutputException (..)    , ExitCode (..)    , ExitCodeException (..)    , Pid    , Process    , ProcessConfig    , StdStream (..)    , StreamSpec    , StreamType    , byteStringInput    , byteStringOutput    , checkExitCodeSTM    , closed    , createPipe    , exitCodeExceptionNoOutput    , getExitCodeSTM    , getStderr    , getStdin    , getStdout    , inherit    , nullStream    , proc    , setChildGroup    , setChildGroupInherit    , setChildUser    , setChildUserInherit    , setCloseFds    , setCreateGroup    , setCreateNewConsole    , setDelegateCtlc    , setDetachConsole    , setEnv    , setEnvInherit    , setNewSession    , setStderr    , setStdin    , setStdout    , setWorkingDir    , setWorkingDirInherit    , shell    , unsafeProcessHandle    , useHandleClose    , useHandleOpen    , waitExitCodeSTM    )import Effectful.Process qualified as Effimport System.Process.Typed qualified as TP-- | Effectful's 'Eff.Process' effect clashes with "System.Process.Typed"'s-- 'Process' type, so we define an alias for it.type TypedProcess = Eff.Processrun :: (IOE :> es) => Eff (TypedProcess ': es) a -> Eff es arun = Eff.runProcess-- | Lifted 'System.Process.Typed.mkStreamSpec'.mkStreamSpec    :: (TypedProcess :> es)    => StdStream    -> (ProcessConfig () () () -> Maybe Handle -> Eff es (a, Eff es ()))    -> Eff es (StreamSpec streamType a)mkStreamSpec stream f = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff \env ->        concUnliftIO env Persistent Unlimited \unlift ->            pure $ TP.mkStreamSpec stream \cfg mHandle -> do                (a, cont) <- unlift $ f cfg mHandle                pure (a, unlift cont)-- | Lifted 'System.Process.Typed.mkPipeStreamSpec'.mkPipeStreamSpec    :: (TypedProcess :> es)    => (ProcessConfig () () () -> Handle -> Eff es (a, Eff es ())) -> Eff es (StreamSpec streamType a)mkPipeStreamSpec f = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff \env ->        concUnliftIO env Persistent Unlimited \unlift ->            pure $ TP.mkPipeStreamSpec \cfg mHandle -> do                (a, cont) <- unlift $ f cfg mHandle                pure (a, unlift cont)-- | Lifted 'System.Process.Typed.runProcess'.runProcess :: (TypedProcess :> es) => ProcessConfig stdin stdout stderr -> Eff es ExitCoderunProcess cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.runProcess cfg-- | Lifted 'System.Process.Typed.readProcess'.readProcess    :: (TypedProcess :> es)    => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es (ExitCode, ByteString, ByteString)readProcess cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcess cfg-- | Lifted 'System.Process.Typed.readProcessStdout'.readProcessStdout    :: (TypedProcess :> es) => ProcessConfig stdin stdoutIgnored stderr -> Eff es (ExitCode, ByteString)readProcessStdout cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcessStdout cfg-- | Lifted 'System.Process.Typed.readProcessStderr'.readProcessStderr    :: (TypedProcess :> es) => ProcessConfig stdin stdout stderrIgnored -> Eff es (ExitCode, ByteString)readProcessStderr cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcessStderr cfg-- | Lifted 'System.Process.Typed.readProcessInterleaved'.readProcessInterleaved    :: (TypedProcess :> es)    => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es (ExitCode, ByteString)readProcessInterleaved cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcessInterleaved cfg-- | Lifted 'System.Process.Typed.withProcessWait'.withProcessWait    :: (TypedProcess :> es)    => ProcessConfig stdin stdout stderr    -> (Process stdin stdout stderr -> Eff es a)    -> Eff es awithProcessWait cfg f = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff \env ->        concUnliftIO env Persistent Unlimited \unlift ->            TP.withProcessWait cfg \p -> unlift $ f p-- | Lifted 'System.Process.Typed.withProcessTerm'.withProcessTerm    :: (TypedProcess :> es)    => ProcessConfig stdin stdout stderr    -> (Process stdin stdout stderr -> Eff es a)    -> Eff es awithProcessTerm cfg f = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff \env ->        concUnliftIO env Persistent Unlimited \unlift ->            TP.withProcessTerm cfg \p -> unlift $ f p-- | Lifted 'System.Process.Typed.startProcess'.startProcess    :: (TypedProcess :> es)    => ProcessConfig stdin stdout stderr    -> Eff es (Process stdin stdout stderr)startProcess cfg = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.startProcess cfg-- | Lifted 'System.Process.Typed.stopProcess'.stopProcess    :: (TypedProcess :> es)    => Process stdin stdout stderr    -> Eff es ()stopProcess process = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.stopProcess process-- | Lifted 'System.Process.Typed.runProcess_'.runProcess_ :: (TypedProcess :> es) => ProcessConfig stdin stdout stderr -> Eff es ()runProcess_ cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.runProcess_ cfg-- | Lifted 'System.Process.Typed.readProcess_'.readProcess_    :: (TypedProcess :> es)    => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es (ByteString, ByteString)readProcess_ cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcess_ cfg-- | Lifted 'System.Process.Typed.readProcessStdout_'.readProcessStdout_    :: (TypedProcess :> es) => ProcessConfig stdin stdoutIgnored stderr -> Eff es ByteStringreadProcessStdout_ cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcessStdout_ cfg-- | Lifted 'System.Process.Typed.readProcessStderr_'.readProcessStderr_    :: (TypedProcess :> es) => ProcessConfig stdin stdout stderrIgnored -> Eff es ByteStringreadProcessStderr_ cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcessStderr_ cfg-- | Lifted 'System.Process.Typed.readProcessInterleaved_'.readProcessInterleaved_    :: (TypedProcess :> es)    => ProcessConfig stdin stdoutIgnored stderrIgnored -> Eff es ByteStringreadProcessInterleaved_ cfg = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff_ $ TP.readProcessInterleaved_ cfg-- | Lifted 'System.Process.Typed.withProcessWait_'.withProcessWait_    :: (TypedProcess :> es)    => ProcessConfig stdin stdout stderr    -> (Process stdin stdout stderr -> Eff es a)    -> Eff es awithProcessWait_ cfg f = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff \env ->        concUnliftIO env Persistent Unlimited \unlift ->            TP.withProcessWait_ cfg \p -> unlift $ f p-- | Lifted 'System.Process.Typed.withProcessTerm_'.withProcessTerm_    :: (TypedProcess :> es)    => ProcessConfig stdin stdout stderr    -> (Process stdin stdout stderr -> Eff es a)    -> Eff es awithProcessTerm_ cfg f = do    (_ :: StaticRep TypedProcess) <- getStaticRep    unsafeEff \env ->        concUnliftIO env Persistent Unlimited \unlift ->            TP.withProcessTerm_ cfg \p -> unlift $ f p-- | Lifted 'System.Process.Typed.waitExitCode'.waitExitCode :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es ExitCodewaitExitCode process = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.waitExitCode process-- | Lifted 'System.Process.Typed.getExitCode'.getExitCode :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es (Maybe ExitCode)getExitCode process = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.getExitCode process-- | Lifted 'System.Process.Typed.checkExitCode'.checkExitCode :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es ()checkExitCode process = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.checkExitCode process-- | Lifted 'System.Process.Typed.getPid'.getPid :: (TypedProcess :> es) => Process stdin stdout stderr -> Eff es (Maybe Pid)getPid process = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.getPid process-- | Lifted 'System.Process.Typed.exitCodeExceptionWithOutput'.exitCodeExceptionWithOutput    :: (TypedProcess :> es)    => Process stdin (STM ByteString) (STM ByteString) -> Eff es ExitCodeExceptionexitCodeExceptionWithOutput process = do    _ :: StaticRep TypedProcess <- getStaticRep    unsafeEff_ $ TP.exitCodeExceptionWithOutput process