on 877cb21 help sign in
59 lines 2.3 kB history raw
1-- | A module containing various file utility functions
2--
3module Hakyll.Core.Util.File
4 ( makeDirectories
5 , getRecursiveContents
6 , isFileInternal
7 ) where
9import Control.Applicative ((<$>))
10import Control.Monad (forM)
11import Data.List (isPrefixOf)
12import System.Directory ( createDirectoryIfMissing, doesDirectoryExist
13 , getDirectoryContents
14 )
15import System.FilePath ( normalise, takeDirectory, splitPath
16 , dropTrailingPathSeparator, (</>)
17 )
19import Hakyll.Core.Configuration
21-- | Given a path to a file, try to make the path writable by making
22-- all directories on the path.
23--
24makeDirectories :: FilePath -> IO ()
25makeDirectories = createDirectoryIfMissing True . takeDirectory
27-- | Get all contents of a directory.
28getRecursiveContents :: Bool -- ^ Include directories?
29 -> FilePath -- ^ Directory to search
30 -> IO [FilePath] -- ^ List of files found
31getRecursiveContents includeDirs topdir = do
32 topdirExists <- doesDirectoryExist topdir
33 if not topdirExists
34 then return []
35 else do
36 names <- filter isProper <$> getDirectoryContents topdir
37 paths <- forM names $ \name -> do
38 let path = normalise $ topdir </> name
39 isDirectory <- doesDirectoryExist path
40 if isDirectory then getRecursiveContents includeDirs path
41 else return [path]
42 return $ if includeDirs then topdir : concat paths
43 else concat paths
44 where
45 isProper = (`notElem` [".", ".."])
47-- | Check if a file is meant for Hakyll internal use, i.e. if it is located in
48-- the destination or store directory
49--
50isFileInternal :: Configuration -- ^ Configuration
51 -> FilePath -- ^ File to check
52 -> Bool -- ^ If the given file is internal
53isFileInternal configuration file =
54 any (`isPrefixOf` split file) dirs
55 where
56 split = map dropTrailingPathSeparator . splitPath
57 dirs = map (split . ($ configuration)) [ destinationDirectory
58 , storeDirectory
59 ]