module CabalGild.Class.MonadWalk where
import qualified System.Directory as Directory
import qualified System.FilePath as FilePath
class (Monad m) => MonadWalk m where
walk :: FilePath -> m [FilePath]
instance MonadWalk IO where
walk :: FilePath -> IO [FilePath]
walk = FilePath -> IO [FilePath]
listDirectoryRecursively
listDirectoryRecursively :: FilePath -> IO [FilePath]
listDirectoryRecursively :: FilePath -> IO [FilePath]
listDirectoryRecursively FilePath
d = do
[FilePath]
es <- FilePath -> IO [FilePath]
Directory.listDirectory FilePath
d
let f :: FilePath -> IO [FilePath]
f FilePath
e = do
let p :: FilePath
p = FilePath -> FilePath -> FilePath
FilePath.combine FilePath
d FilePath
e
Bool
b <- FilePath -> IO Bool
Directory.doesDirectoryExist FilePath
p
if Bool
b
then FilePath -> IO [FilePath]
listDirectoryRecursively FilePath
p
else [FilePath] -> IO [FilePath]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [FilePath
p]
[[FilePath]] -> [FilePath]
forall a. Monoid a => [a] -> a
mconcat ([[FilePath]] -> [FilePath]) -> IO [[FilePath]] -> IO [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FilePath -> IO [FilePath]) -> [FilePath] -> IO [[FilePath]]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse FilePath -> IO [FilePath]
f [FilePath]
es