on 5c8eb2a help sign in
56 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.Monad (filterM, forM, when)
12import System.Directory (createDirectoryIfMissing,
13 doesDirectoryExist, getDirectoryContents,
14 removeDirectoryRecursive)
15import System.FilePath (takeDirectory, (</>))
18--------------------------------------------------------------------------------
19-- | Given a path to a file, try to make the path writable by making
20-- all directories on the path.
21makeDirectories :: FilePath -> IO ()
22makeDirectories = createDirectoryIfMissing True . takeDirectory
25--------------------------------------------------------------------------------
26-- | Get all contents of a directory.
27getRecursiveContents :: (FilePath -> IO Bool) -- ^ Ignore this file/directory
28 -> FilePath -- ^ Directory to search
29 -> IO [FilePath] -- ^ List of files found
30getRecursiveContents ignore top = go ""
31 where
32 isProper x
33 | x `elem` [".", ".."] = return False
34 | otherwise = not <$> ignore x
36 go dir = do
37 dirExists <- doesDirectoryExist (top </> dir)
38 if not dirExists
39 then return []
40 else do
41 names <- filterM isProper =<< getDirectoryContents (top </> dir)
42 paths <- forM names $ \name -> do
43 let rel = dir </> name
44 isDirectory <- doesDirectoryExist (top </> rel)
45 if isDirectory
46 then go rel
47 else return [rel]
49 return $ concat paths
52--------------------------------------------------------------------------------
53removeDirectory :: FilePath -> IO ()
54removeDirectory fp = do
55 e <- doesDirectoryExist fp
56 when e $ removeDirectoryRecursive fp