{- |
   Module     : Development.Shake.Plus.Temp
   License    : MIT
   Stability  : experimental

Temp utilities in "Development.Shake" unlifted to `MonadUnliftAction`.
-}
module Development.Shake.Plus.Temp (
  withTempFile
, withTempDir
, withTempFileWithin
, withTempDirWithin
) where

import qualified Development.Shake
import           Development.Shake.Plus.Core
import           Path
import           RIO                         hiding (withTempFile)

-- | Unlifted version of `Development.Shake.withTempFile` with well-typed `Path`.
withTempFile :: (MonadUnliftAction m, MonadThrow m) => (Path Rel File -> m a) -> m a
withTempFile :: (Path Rel File -> m a) -> m a
withTempFile Path Rel File -> m a
f = ((forall a. m a -> Action a) -> Action a) -> m a
forall (m :: * -> *) b.
MonadUnliftAction m =>
((forall a. m a -> Action a) -> Action b) -> m b
withRunInAction (((forall a. m a -> Action a) -> Action a) -> m a)
-> ((forall a. m a -> Action a) -> Action a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> Action a
run -> (FilePath -> Action a) -> Action a
forall a. (FilePath -> Action a) -> Action a
Development.Shake.withTempFile ((FilePath -> Action a) -> Action a)
-> (FilePath -> Action a) -> Action a
forall a b. (a -> b) -> a -> b
$ m a -> Action a
forall a. m a -> Action a
run (m a -> Action a) -> (FilePath -> m a) -> FilePath -> Action a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path Rel File -> m a
f (Path Rel File -> m a)
-> (FilePath -> m (Path Rel File)) -> FilePath -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< FilePath -> m (Path Rel File)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel File)
parseRelFile)

-- | Unlifted version of `Development.Shake.withTempFile` with well-typed `Path`.
withTempDir :: (MonadUnliftAction m, MonadThrow m) => (Path Rel Dir -> m a) -> m a
withTempDir :: (Path Rel Dir -> m a) -> m a
withTempDir Path Rel Dir -> m a
f = ((forall a. m a -> Action a) -> Action a) -> m a
forall (m :: * -> *) b.
MonadUnliftAction m =>
((forall a. m a -> Action a) -> Action b) -> m b
withRunInAction (((forall a. m a -> Action a) -> Action a) -> m a)
-> ((forall a. m a -> Action a) -> Action a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> Action a
run -> (FilePath -> Action a) -> Action a
forall a. (FilePath -> Action a) -> Action a
Development.Shake.withTempDir ((FilePath -> Action a) -> Action a)
-> (FilePath -> Action a) -> Action a
forall a b. (a -> b) -> a -> b
$ m a -> Action a
forall a. m a -> Action a
run (m a -> Action a) -> (FilePath -> m a) -> FilePath -> Action a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path Rel Dir -> m a
f (Path Rel Dir -> m a)
-> (FilePath -> m (Path Rel Dir)) -> FilePath -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< FilePath -> m (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel Dir)
parseRelDir)

-- | Unlifted version of `Development.Shake.withTempFileWithin` with well-typed `Path`s.
withTempFileWithin :: (MonadUnliftAction m, MonadThrow m) => Path b Dir -> (Path Rel File -> m a) -> m a
withTempFileWithin :: Path b Dir -> (Path Rel File -> m a) -> m a
withTempFileWithin Path b Dir
x Path Rel File -> m a
f = ((forall a. m a -> Action a) -> Action a) -> m a
forall (m :: * -> *) b.
MonadUnliftAction m =>
((forall a. m a -> Action a) -> Action b) -> m b
withRunInAction (((forall a. m a -> Action a) -> Action a) -> m a)
-> ((forall a. m a -> Action a) -> Action a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> Action a
run -> FilePath -> (FilePath -> Action a) -> Action a
forall a. FilePath -> (FilePath -> Action a) -> Action a
Development.Shake.withTempFileWithin (Path b Dir -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path b Dir
x) ((FilePath -> Action a) -> Action a)
-> (FilePath -> Action a) -> Action a
forall a b. (a -> b) -> a -> b
$ m a -> Action a
forall a. m a -> Action a
run (m a -> Action a) -> (FilePath -> m a) -> FilePath -> Action a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path Rel File -> m a
f (Path Rel File -> m a)
-> (FilePath -> m (Path Rel File)) -> FilePath -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< FilePath -> m (Path Rel File)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel File)
parseRelFile)

-- | Unlifted version of `Development.Shake.withTempDirWithin` with well-typed `Path`s.
withTempDirWithin :: (MonadUnliftAction m, MonadThrow m) => Path b Dir -> (Path Rel Dir -> m a) -> m a
withTempDirWithin :: Path b Dir -> (Path Rel Dir -> m a) -> m a
withTempDirWithin Path b Dir
x Path Rel Dir -> m a
f = ((forall a. m a -> Action a) -> Action a) -> m a
forall (m :: * -> *) b.
MonadUnliftAction m =>
((forall a. m a -> Action a) -> Action b) -> m b
withRunInAction (((forall a. m a -> Action a) -> Action a) -> m a)
-> ((forall a. m a -> Action a) -> Action a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> Action a
run -> FilePath -> (FilePath -> Action a) -> Action a
forall a. FilePath -> (FilePath -> Action a) -> Action a
Development.Shake.withTempDirWithin (Path b Dir -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path b Dir
x) ((FilePath -> Action a) -> Action a)
-> (FilePath -> Action a) -> Action a
forall a b. (a -> b) -> a -> b
$ m a -> Action a
forall a. m a -> Action a
run (m a -> Action a) -> (FilePath -> m a) -> FilePath -> Action a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Path Rel Dir -> m a
f (Path Rel Dir -> m a)
-> (FilePath -> m (Path Rel Dir)) -> FilePath -> m a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< FilePath -> m (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel Dir)
parseRelDir)