on 8a8dade help sign in
44 lines 1.8 kB history raw
1--------------------------------------------------------------------------------
2-- | A module containing various file utility functions
3module Hakyll.Core.Util.File
4 ( makeDirectories
5 , getRecursiveContents
6 ) where
9--------------------------------------------------------------------------------
10import Control.Applicative ((<$>))
11import Control.Monad (forM)
12import System.Directory (createDirectoryIfMissing,
13 doesDirectoryExist, getDirectoryContents)
14import System.FilePath (takeDirectory, (</>))
17--------------------------------------------------------------------------------
18-- | Given a path to a file, try to make the path writable by making
19-- all directories on the path.
20makeDirectories :: FilePath -> IO ()
21makeDirectories = createDirectoryIfMissing True . takeDirectory
24--------------------------------------------------------------------------------
25-- | Get all contents of a directory.
26getRecursiveContents :: FilePath -- ^ Directory to search
27 -> IO [FilePath] -- ^ List of files found
28getRecursiveContents top = go ""
29 where
30 isProper = (`notElem` [".", ".."])
31 go dir = do
32 dirExists <- doesDirectoryExist (top </> dir)
33 if not dirExists
34 then return []
35 else do
36 names <- filter isProper <$> getDirectoryContents (top </> dir)
37 paths <- forM names $ \name -> do
38 let rel = dir </> name
39 isDirectory <- doesDirectoryExist (top </> rel)
40 if isDirectory
41 then go rel
42 else return [rel]
44 return $ concat paths