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

Directory utilities in "Development.Shake" lifted to `MonadAction` and
well-typed `Path`s.
-}
module Development.Shake.Plus.Directory (
  doesFileExist
, doesDirectoryExist
, getDirectoryFiles
, getDirectoryDirs
, getDirectoryFilesIO
) where

import qualified Development.Shake
import           Development.Shake.Plus.Core
import           Path
import           RIO

-- | Lifted version of `Development.Shake.doesFileExist` using well-typed `Path`s.
doesFileExist :: MonadAction m => Path b File -> m Bool
doesFileExist :: Path b File -> m Bool
doesFileExist = Action Bool -> m Bool
forall (m :: * -> *) a. MonadAction m => Action a -> m a
liftAction (Action Bool -> m Bool)
-> (Path b File -> Action Bool) -> Path b File -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Action Bool
Development.Shake.doesFileExist (FilePath -> Action Bool)
-> (Path b File -> FilePath) -> Path b File -> Action Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path b File -> FilePath
forall b t. Path b t -> FilePath
toFilePath

-- | Lifted version of `Development.Shake.doesDirectoryExist` using well-typed `Path`s.
doesDirectoryExist :: MonadAction m => Path b Dir -> m Bool
doesDirectoryExist :: Path b Dir -> m Bool
doesDirectoryExist = Action Bool -> m Bool
forall (m :: * -> *) a. MonadAction m => Action a -> m a
liftAction (Action Bool -> m Bool)
-> (Path b Dir -> Action Bool) -> Path b Dir -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Action Bool
Development.Shake.doesDirectoryExist (FilePath -> Action Bool)
-> (Path b Dir -> FilePath) -> Path b Dir -> Action Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path b Dir -> FilePath
forall b t. Path b t -> FilePath
toFilePath

-- | Lifted version of `Development.Shake.getDirectoryFiles` using well-typed `Path`s.
getDirectoryFiles :: MonadAction m => Path b Dir -> [FilePattern] -> m [Path Rel File]
getDirectoryFiles :: Path b Dir -> [FilePath] -> m [Path Rel File]
getDirectoryFiles Path b Dir
x [FilePath]
y = Action [Path Rel File] -> m [Path Rel File]
forall (m :: * -> *) a. MonadAction m => Action a -> m a
liftAction (Action [Path Rel File] -> m [Path Rel File])
-> Action [Path Rel File] -> m [Path Rel File]
forall a b. (a -> b) -> a -> b
$ (FilePath -> Action (Path Rel File))
-> [FilePath] -> Action [Path Rel File]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (IO (Path Rel File) -> Action (Path Rel File)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Path Rel File) -> Action (Path Rel File))
-> (FilePath -> IO (Path Rel File))
-> FilePath
-> Action (Path Rel File)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO (Path Rel File)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel File)
parseRelFile) ([FilePath] -> Action [Path Rel File])
-> Action [FilePath] -> Action [Path Rel File]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> [FilePath] -> Action [FilePath]
Development.Shake.getDirectoryFiles (Path b Dir -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path b Dir
x) [FilePath]
y

-- | Lifted version of `Development.Shake.getDirectoryDirs` using well-typed `Path`s.
getDirectoryDirs :: MonadAction m => Path b Dir -> m [Path Rel Dir]
getDirectoryDirs :: Path b Dir -> m [Path Rel Dir]
getDirectoryDirs Path b Dir
x = Action [Path Rel Dir] -> m [Path Rel Dir]
forall (m :: * -> *) a. MonadAction m => Action a -> m a
liftAction (Action [Path Rel Dir] -> m [Path Rel Dir])
-> Action [Path Rel Dir] -> m [Path Rel Dir]
forall a b. (a -> b) -> a -> b
$ (FilePath -> Action (Path Rel Dir))
-> [FilePath] -> Action [Path Rel Dir]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (IO (Path Rel Dir) -> Action (Path Rel Dir)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Path Rel Dir) -> Action (Path Rel Dir))
-> (FilePath -> IO (Path Rel Dir))
-> FilePath
-> Action (Path Rel Dir)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel Dir)
parseRelDir) ([FilePath] -> Action [Path Rel Dir])
-> Action [FilePath] -> Action [Path Rel Dir]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> Action [FilePath]
Development.Shake.getDirectoryDirs (Path b Dir -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path b Dir
x)

-- | Lifted version of `Development.Shake.getDirectoryFilesIO` using well-typed `Path`s.
getDirectoryFilesIO :: MonadIO m => Path b Dir -> [FilePattern] -> m [Path Rel File]
getDirectoryFilesIO :: Path b Dir -> [FilePath] -> m [Path Rel File]
getDirectoryFilesIO Path b Dir
x [FilePath]
y = IO [Path Rel File] -> m [Path Rel File]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Path Rel File] -> m [Path Rel File])
-> IO [Path Rel File] -> m [Path Rel File]
forall a b. (a -> b) -> a -> b
$ (FilePath -> IO (Path Rel File))
-> [FilePath] -> IO [Path Rel File]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (IO (Path Rel File) -> IO (Path Rel File)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Path Rel File) -> IO (Path Rel File))
-> (FilePath -> IO (Path Rel File))
-> FilePath
-> IO (Path Rel File)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO (Path Rel File)
forall (m :: * -> *). MonadThrow m => FilePath -> m (Path Rel File)
parseRelFile) ([FilePath] -> IO [Path Rel File])
-> IO [FilePath] -> IO [Path Rel File]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> [FilePath] -> IO [FilePath]
Development.Shake.getDirectoryFilesIO (Path b Dir -> FilePath
forall b t. Path b t -> FilePath
toFilePath Path b Dir
x) [FilePath]
y