module Web.Herringbone.Internal.Utils where
import Control.Monad
import Data.Maybe (catMaybes)
import Filesystem.Path.CurrentOS (FilePath, (</>))
import qualified Filesystem.Path.CurrentOS as F
import qualified Filesystem as F
import Prelude hiding (FilePath)
getFilesRecursive :: FilePath -> IO [FilePath]
getFilesRecursive root = do
results <- F.listDirectory root
(files, others) <- partitionM F.isFile results
(dirs, _) <- partitionM F.isDirectory others
subfiles <- mapM getFilesRecursive dirs
return $ files ++ concat subfiles
getFilesRecursiveRelative :: FilePath -> IO [FilePath]
getFilesRecursiveRelative root = do
root' <- F.canonicalizePath (root </> "")
files <- getFilesRecursive root'
let maybes = map (F.stripPrefix root') files
return $ catMaybes maybes
partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a])
partitionM f = foldM g ([], [])
where
g (as, bs) x = do
flag <- f x
return $ if flag
then (x : as, bs)
else (as, x : bs)