module Hakyll.Core.Util.File
( makeDirectories
, getRecursiveContents
, isFileObsolete
, isFileInternal
) where
import Control.Applicative ((<$>))
import System.Time (ClockTime)
import Control.Monad (forM, filterM)
import Data.List (isPrefixOf)
import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
, doesFileExist, getModificationTime
, getDirectoryContents
)
import System.FilePath ( normalise, takeDirectory, splitPath
, dropTrailingPathSeparator, (</>)
)
import Hakyll.Core.Configuration
makeDirectories :: FilePath -> IO ()
makeDirectories = createDirectoryIfMissing True . takeDirectory
getRecursiveContents :: Bool
-> FilePath
-> IO [FilePath]
getRecursiveContents includeDirs topdir = do
topdirExists <- doesDirectoryExist topdir
if not topdirExists
then return []
else do
names <- filter isProper <$> getDirectoryContents topdir
paths <- forM names $ \name -> do
let path = normalise $ topdir </> name
isDirectory <- doesDirectoryExist path
if isDirectory then getRecursiveContents includeDirs path
else return [path]
return $ if includeDirs then topdir : concat paths
else concat paths
where
isProper = (`notElem` [".", ".."])
isObsolete :: ClockTime
-> [FilePath]
-> IO Bool
isObsolete _ [] = return False
isObsolete timeStamp depends = do
depends' <- filterM doesFileExist depends
dependsModified <- mapM getModificationTime depends'
return (timeStamp < maximum dependsModified)
isFileObsolete :: FilePath
-> [FilePath]
-> IO Bool
isFileObsolete file depends = do
exists <- doesFileExist file
if not exists
then return True
else do timeStamp <- getModificationTime file
isObsolete timeStamp depends
isFileInternal :: HakyllConfiguration
-> FilePath
-> Bool
isFileInternal configuration file =
any (`isPrefixOf` split file) dirs
where
split = map dropTrailingPathSeparator . splitPath
dirs = map (split . ($ configuration)) [ destinationDirectory
, storeDirectory
]