summaryrefslogtreecommitdiff
path: root/src/Hakyll
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-26 09:38:40 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-26 09:38:40 +0100
commit53d179a7da994bd45eff1363269c6e1cb533dfd7 (patch)
tree1013380980711307a8d25be2514e550c7b7fa192 /src/Hakyll
parent5bc8028696ae8d5aa2c60db87aea3d00f9d7aebd (diff)
downloadhakyll-53d179a7da994bd45eff1363269c6e1cb533dfd7.tar.gz
Add file utility module
Diffstat (limited to 'src/Hakyll')
-rw-r--r--src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs25
-rw-r--r--src/Hakyll/Core/Util/File.hs66
2 files changed, 67 insertions, 24 deletions
diff --git a/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs b/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs
index b682634..442ae9a 100644
--- a/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs
+++ b/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs
@@ -5,13 +5,10 @@ module Hakyll.Core.ResourceProvider.FileResourceProvider
) where
import Control.Applicative ((<$>))
-import Control.Monad (forM)
-
-import System.Directory (doesDirectoryExist, getDirectoryContents)
-import System.FilePath ((</>), normalise)
import Hakyll.Core.ResourceProvider
import Hakyll.Core.Identifier
+import Hakyll.Core.Util.File
-- | Create a filesystem-based 'ResourceProvider'
--
@@ -22,23 +19,3 @@ fileResourceProvider = do
{ resourceList = list
, resourceString = readFile . toFilePath
}
-
--- | Get all contents of a directory. Note that files starting with a dot (.)
--- will be ignored.
---
-getRecursiveContents :: FilePath -> IO [FilePath]
-getRecursiveContents topdir = do
- topdirExists <- doesDirectoryExist topdir
- if topdirExists
- then do names <- getDirectoryContents topdir
- let properNames = filter isProper names
- paths <- forM properNames $ \name -> do
- let path = topdir </> name
- isDirectory <- doesDirectoryExist path
- if isDirectory
- then getRecursiveContents path
- else return [normalise path]
- return (concat paths)
- else return []
- where
- isProper = not . (== '.') . head
diff --git a/src/Hakyll/Core/Util/File.hs b/src/Hakyll/Core/Util/File.hs
new file mode 100644
index 0000000..355eafb
--- /dev/null
+++ b/src/Hakyll/Core/Util/File.hs
@@ -0,0 +1,66 @@
+-- | A module containing various file utility functions
+--
+module Hakyll.Core.Util.File
+ ( makeDirectories
+ , getRecursiveContents
+ , isFileObsolete
+ ) where
+
+import System.FilePath (normalise, takeDirectory, (</>))
+import System.Time (ClockTime)
+import Control.Monad (forM)
+import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
+ , doesFileExist, getModificationTime
+ , getDirectoryContents
+ )
+
+-- | 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
+
+-- | Get all contents of a directory. Note that files starting with a dot (.)
+-- will be ignored.
+--
+getRecursiveContents :: FilePath -> IO [FilePath]
+getRecursiveContents topdir = do
+ topdirExists <- doesDirectoryExist topdir
+ if topdirExists
+ then do names <- getDirectoryContents topdir
+ let properNames = filter isProper names
+ paths <- forM properNames $ \name -> do
+ let path = topdir </> name
+ isDirectory <- doesDirectoryExist path
+ if isDirectory
+ then getRecursiveContents path
+ else return [normalise path]
+ return (concat paths)
+ else return []
+ where
+ isProper = not . (== ".") . take 1
+
+-- | Check if a timestamp is obsolete compared to the timestamps of a number of
+-- files. When they are no files, it is never obsolete.
+--
+isObsolete :: ClockTime -- ^ The time to check.
+ -> [FilePath] -- ^ Dependencies of the cached file.
+ -> IO Bool
+isObsolete _ [] = return False
+isObsolete timeStamp depends = do
+ dependsModified <- mapM getModificationTime depends
+ return (timeStamp < maximum dependsModified)
+
+-- | Check if a file is obsolete, given it's dependencies. When the file does
+-- not exist, it is always obsolete. Other wise, it is obsolete if any of it's
+-- dependencies has a more recent modification time than the file.
+--
+isFileObsolete :: FilePath -- ^ The cached file
+ -> [FilePath] -- ^ Dependencies of the cached file
+ -> IO Bool
+isFileObsolete file depends = do
+ exists <- doesFileExist file
+ if not exists
+ then return True
+ else do timeStamp <- getModificationTime file
+ isObsolete timeStamp depends