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