on 792fcdf help sign in
54 lines 2.2 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 -> Bool) -- ^ Ignore this file/directory
29 -> FilePath -- ^ Directory to search
30 -> IO [FilePath] -- ^ List of files found
31getRecursiveContents ignore top = go ""
32 where
33 isProper x = notElem x [".", ".."] && not (ignore x)
34 go dir = do
35 dirExists <- doesDirectoryExist (top </> dir)
36 if not dirExists
37 then return []
38 else do
39 names <- filter isProper <$> getDirectoryContents (top </> dir)
40 paths <- forM names $ \name -> do
41 let rel = dir </> name
42 isDirectory <- doesDirectoryExist (top </> rel)
43 if isDirectory
44 then go rel
45 else return [rel]
47 return $ concat paths
50--------------------------------------------------------------------------------
51removeDirectory :: FilePath -> IO ()
52removeDirectory fp = do
53 e <- doesDirectoryExist fp
54 when e $ removeDirectoryRecursive fp