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

68 lines
2.7 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
2011-02-10 15:42:26 +00:00
, isFileInternal
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
--------------------------------------------------------------------------------
import Control.Applicative ((<$>))
import Control.Monad (forM)
import Data.List (isPrefixOf)
import System.Directory (createDirectoryIfMissing,
doesDirectoryExist,
getDirectoryContents)
import System.FilePath (dropTrailingPathSeparator,
splitPath, takeDirectory, (</>))
2010-12-26 08:38:40 +00:00
2012-11-19 13:59:55 +00:00
--------------------------------------------------------------------------------
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-19 13:59:55 +00:00
--------------------------------------------------------------------------------
2012-11-08 11:45:26 +00:00
-- | Get all contents of a directory.
2012-11-19 13:59:55 +00:00
getRecursiveContents :: FilePath -- ^ Directory to search
-> IO [FilePath] -- ^ List of files found
2012-11-19 13:59:55 +00:00
getRecursiveContents top = go ""
2010-12-26 08:38:40 +00:00
where
isProper = (`notElem` [".", ".."])
2012-11-19 13:59:55 +00:00
go dir = do
dirExists <- doesDirectoryExist (top </> dir)
if not dirExists
then return []
else do
names <- filter isProper <$> getDirectoryContents (top </> dir)
paths <- forM names $ \name -> do
let rel = dir </> name
isDirectory <- doesDirectoryExist (top </> rel)
if isDirectory
then go rel
else return [rel]
return $ concat paths
2010-12-26 08:38:40 +00:00
2012-11-19 13:59:55 +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
2012-11-13 14:10:01 +00:00
isFileInternal :: Configuration -- ^ Configuration
-> FilePath -- ^ File to check
2011-02-10 15:42:26 +00:00
-> 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
]