on c8a970d help sign in
90 lines 3.6 kB history raw
1-- | A module containing various file utility functions
2--
3module Hakyll.Core.Util.File
4 ( makeDirectories
5 , getRecursiveContents
6 , isFileObsolete
7 , isFileInternal
8 ) where
10import Control.Applicative ((<$>))
11import System.Time (ClockTime)
12import Control.Monad (forM, filterM)
13import Data.List (isPrefixOf)
14import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
15 , doesFileExist, getModificationTime
16 , getDirectoryContents
17 )
18import System.FilePath ( normalise, takeDirectory, splitPath
19 , dropTrailingPathSeparator, (</>)
20 )
22import Hakyll.Core.Configuration
24-- | Given a path to a file, try to make the path writable by making
25-- all directories on the path.
26--
27makeDirectories :: FilePath -> IO ()
28makeDirectories = createDirectoryIfMissing True . takeDirectory
30-- | Get all contents of a directory. Note that files starting with a dot (.)
31-- will be ignored.
32--
33getRecursiveContents :: Bool -- ^ Include directories?
34 -> FilePath -- ^ Directory to search
35 -> IO [FilePath] -- ^ List of files found
36getRecursiveContents includeDirs topdir = do
37 topdirExists <- doesDirectoryExist topdir
38 if not topdirExists
39 then return []
40 else do
41 names <- filter isProper <$> getDirectoryContents topdir
42 paths <- forM names $ \name -> do
43 let path = normalise $ topdir </> name
44 isDirectory <- doesDirectoryExist path
45 if isDirectory then getRecursiveContents includeDirs path
46 else return [path]
47 return $ if includeDirs then topdir : concat paths
48 else concat paths
49 where
50 isProper = (`notElem` [".", ".."])
52-- | Check if a timestamp is obsolete compared to the timestamps of a number of
53-- files. When they are no files, it is never obsolete.
54--
55isObsolete :: ClockTime -- ^ The time to check.
56 -> [FilePath] -- ^ Dependencies of the cached file.
57 -> IO Bool
58isObsolete _ [] = return False
59isObsolete timeStamp depends = do
60 depends' <- filterM doesFileExist depends
61 dependsModified <- mapM getModificationTime depends'
62 return (timeStamp < maximum dependsModified)
64-- | Check if a file is obsolete, given it's dependencies. When the file does
65-- not exist, it is always obsolete. Other wise, it is obsolete if any of it's
66-- dependencies has a more recent modification time than the file.
67--
68isFileObsolete :: FilePath -- ^ The cached file
69 -> [FilePath] -- ^ Dependencies of the cached file
70 -> IO Bool
71isFileObsolete file depends = do
72 exists <- doesFileExist file
73 if not exists
74 then return True
75 else do timeStamp <- getModificationTime file
76 isObsolete timeStamp depends
78-- | Check if a file is meant for Hakyll internal use, i.e. if it is located in
79-- the destination or store directory
80--
81isFileInternal :: HakyllConfiguration -- ^ Configuration
82 -> FilePath -- ^ File to check
83 -> Bool -- ^ If the given file is internal
84isFileInternal configuration file =
85 any (`isPrefixOf` split file) dirs
86 where
87 split = map dropTrailingPathSeparator . splitPath
88 dirs = map (split . ($ configuration)) [ destinationDirectory
89 , storeDirectory
90 ]