hakyll/src/Hakyll/Core/Util/File.hs

58 lines
2.2 KiB
Haskell
Raw Normal View History

2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
2010-12-26 08:38:40 +00:00
-- | A module containing various file utility functions
module Hakyll.Core.Util.File
( makeDirectories
, getRecursiveContents
2013-01-06 17:33:00 +00:00
, removeDirectory
2010-12-26 08:38:40 +00:00
) where
2011-02-10 15:42:26 +00:00
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
2012-11-25 09:45:55 +00:00
import Control.Applicative ((<$>))
import Control.Monad (filterM, forM, when)
2012-11-25 09:45:55 +00:00
import System.Directory (createDirectoryIfMissing,
2013-01-06 17:33:00 +00:00
doesDirectoryExist, getDirectoryContents,
removeDirectoryRecursive)
2012-11-25 09:45:55 +00:00
import System.FilePath (takeDirectory, (</>))
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
2010-12-26 08:38:40 +00:00
-- | Given a path to a file, try to make the path writable by making
-- all directories on the path.
makeDirectories :: FilePath -> IO ()
makeDirectories = createDirectoryIfMissing True . takeDirectory
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
2012-11-08 11:45:26 +00:00
-- | Get all contents of a directory.
getRecursiveContents :: (FilePath -> IO Bool) -- ^ Ignore this file/directory
-> FilePath -- ^ Directory to search
-> IO [FilePath] -- ^ List of files found
2013-02-06 21:40:18 +00:00
getRecursiveContents ignore top = go ""
2010-12-26 08:38:40 +00:00
where
isProper x
| x `elem` [".", ".."] = return False
| otherwise = not <$> ignore x
2013-02-06 21:40:18 +00:00
go dir = do
2012-11-19 13:59:55 +00:00
dirExists <- doesDirectoryExist (top </> dir)
if not dirExists
then return []
else do
names <- filterM isProper =<< getDirectoryContents (top </> dir)
2012-11-19 13:59:55 +00:00
paths <- forM names $ \name -> do
let rel = dir </> name
isDirectory <- doesDirectoryExist (top </> rel)
if isDirectory
then go rel
else return [rel]
return $ concat paths
2013-01-06 17:33:00 +00:00
--------------------------------------------------------------------------------
removeDirectory :: FilePath -> IO ()
removeDirectory fp = do
e <- doesDirectoryExist fp
when e $ removeDirectoryRecursive fp