2010-12-26 08:38:40 +00:00
|
|
|
-- | A module containing various file utility functions
|
|
|
|
--
|
|
|
|
module Hakyll.Core.Util.File
|
|
|
|
( makeDirectories
|
|
|
|
, getRecursiveContents
|
2011-02-10 15:42:26 +00:00
|
|
|
, isFileInternal
|
2010-12-26 08:38:40 +00:00
|
|
|
) where
|
|
|
|
|
2011-02-10 17:59:16 +00:00
|
|
|
import Control.Applicative ((<$>))
|
2012-09-24 08:37:33 +00:00
|
|
|
import Control.Monad (forM)
|
2011-02-10 15:42:26 +00:00
|
|
|
import Data.List (isPrefixOf)
|
2010-12-26 08:38:40 +00:00
|
|
|
import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
|
|
|
|
, getDirectoryContents
|
|
|
|
)
|
2011-02-10 15:42:26 +00:00
|
|
|
import System.FilePath ( normalise, takeDirectory, splitPath
|
|
|
|
, dropTrailingPathSeparator, (</>)
|
|
|
|
)
|
|
|
|
|
|
|
|
import Hakyll.Core.Configuration
|
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-08 11:45:26 +00:00
|
|
|
-- | Get all contents of a directory.
|
2011-02-10 17:59:16 +00:00
|
|
|
getRecursiveContents :: Bool -- ^ Include directories?
|
|
|
|
-> FilePath -- ^ Directory to search
|
|
|
|
-> IO [FilePath] -- ^ List of files found
|
|
|
|
getRecursiveContents includeDirs topdir = do
|
2010-12-26 08:38:40 +00:00
|
|
|
topdirExists <- doesDirectoryExist topdir
|
2011-02-10 17:59:16 +00:00
|
|
|
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
|
2010-12-26 08:38:40 +00:00
|
|
|
where
|
2011-04-04 10:16:38 +00:00
|
|
|
isProper = (`notElem` [".", ".."])
|
2010-12-26 08:38:40 +00:00
|
|
|
|
2011-02-10 15:42:26 +00:00
|
|
|
-- | Check if a file is meant for Hakyll internal use, i.e. if it is located in
|
|
|
|
-- the destination or store directory
|
|
|
|
--
|
|
|
|
isFileInternal :: HakyllConfiguration -- ^ Configuration
|
|
|
|
-> FilePath -- ^ File to check
|
|
|
|
-> Bool -- ^ If the given file is internal
|
|
|
|
isFileInternal configuration file =
|
|
|
|
any (`isPrefixOf` split file) dirs
|
|
|
|
where
|
|
|
|
split = map dropTrailingPathSeparator . splitPath
|
|
|
|
dirs = map (split . ($ configuration)) [ destinationDirectory
|
|
|
|
, storeDirectory
|
|
|
|
]
|