module BuildBox.IO.Directory
( lsFilesIn
, lsDirsIn
, traceFilesFrom)
where
import System.Directory
import Control.Monad
import Control.Monad.Trans
import Data.List
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
lsFilesIn :: MonadIO m => String -> m [String]
lsFilesIn path
= do contents <- liftIO $ getDirectoryContents path
files <- filterM (\p -> liftM not $ liftIO $ doesDirectoryExist p)
$ map (\f -> path ++ "/" ++ f)
$ dropDotPaths contents
return $ sort files
lsDirsIn :: MonadIO m => String -> m [String]
lsDirsIn path
= do
contents <- liftIO $ getDirectoryContents path
dirs <- filterM (liftIO . doesDirectoryExist)
$ map (\f -> path ++ "/" ++ f)
$ dropDotPaths contents
return $ sort dirs
traceFilesFrom :: FilePath -> IO (Seq FilePath)
traceFilesFrom path
= do isDir <- doesDirectoryExist path
isFile <- doesFileExist path
let result
| isDir
= do contents <- liftM dropDotPaths
$ getDirectoryContents path
liftM (join . Seq.fromList)
$ mapM traceFilesFrom
$ map (\f -> path ++ "/" ++ f)
$ contents
| isFile
= return $ Seq.singleton path
| otherwise
= return $ Seq.empty
result
dropDotPaths :: [FilePath] -> [FilePath]
dropDotPaths xx
= filter (\f -> f /= "." && f /= "..") xx