-- |Constructors for 'SysProcConf', Internal
module Polysemy.Process.SysProcConf where

import Path (Abs, File, Path, Rel, toFilePath)
import Path.IO (findExecutable)
import System.Process.Typed (proc, shell)

import Polysemy.Process.Interpreter.SystemProcess (SysProcConf)

-- |Create a 'SysProcConf' from an executable path and a list of arguments.
processConfig :: Path Abs File -> [Text] -> SysProcConf
processConfig :: Path Abs File -> [Text] -> SysProcConf
processConfig Path Abs File
exe [Text]
args =
  FilePath -> [FilePath] -> SysProcConf
proc (Path Abs File -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path Abs File
exe) (Text -> FilePath
forall a. ToString a => a -> FilePath
toString (Text -> FilePath) -> [Text] -> [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text]
args)
{-# inline processConfig #-}

-- |Create a 'SysProcConf' from an shell command line.
shellConfig :: Text -> SysProcConf
shellConfig :: Text -> SysProcConf
shellConfig Text
cmd =
  FilePath -> SysProcConf
shell (Text -> FilePath
forall a. ToString a => a -> FilePath
toString Text
cmd)
{-# inline shellConfig #-}

-- |Create a 'SysProcConf' by looking up an executable in the path, and using the supplied arguments.
which ::
  Member (Embed IO) r =>
  Path Rel File ->
  [Text] ->
  Sem r (Maybe SysProcConf)
which :: forall (r :: [(* -> *) -> * -> *]).
Member (Embed IO) r =>
Path Rel File -> [Text] -> Sem r (Maybe SysProcConf)
which Path Rel File
exeName [Text]
args =
  (Path Abs File -> SysProcConf)
-> Maybe (Path Abs File) -> Maybe SysProcConf
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Path Abs File -> [Text] -> SysProcConf)
-> [Text] -> Path Abs File -> SysProcConf
forall a b c. (a -> b -> c) -> b -> a -> c
flip Path Abs File -> [Text] -> SysProcConf
processConfig [Text]
args) (Maybe (Path Abs File) -> Maybe SysProcConf)
-> (Maybe (Maybe (Path Abs File)) -> Maybe (Path Abs File))
-> Maybe (Maybe (Path Abs File))
-> Maybe SysProcConf
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe (Maybe (Path Abs File)) -> Maybe (Path Abs File)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Maybe (Maybe (Path Abs File)) -> Maybe SysProcConf)
-> Sem r (Maybe (Maybe (Path Abs File)))
-> Sem r (Maybe SysProcConf)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe (Path Abs File)) -> Sem r (Maybe (Maybe (Path Abs File)))
forall (r :: [(* -> *) -> * -> *]) a.
Member (Embed IO) r =>
IO a -> Sem r (Maybe a)
tryIOErrorMaybe (Path Rel File -> IO (Maybe (Path Abs File))
forall (m :: * -> *).
MonadIO m =>
Path Rel File -> m (Maybe (Path Abs File))
findExecutable Path Rel File
exeName)
{-# inline which #-}