--------------------------------------------------------------------------------
-- | A module containing various file utility functions
module Hakyll.Core.Util.File
    ( makeDirectories
    , getRecursiveContents
    , removeDirectory
    ) where


--------------------------------------------------------------------------------
import           Control.Monad       (filterM, forM, when)
import           System.Directory    (createDirectoryIfMissing,
                                      doesDirectoryExist, getDirectoryContents,
                                      removeDirectoryRecursive)
import           System.FilePath     (takeDirectory, (</>))


--------------------------------------------------------------------------------
-- | Given a path to a file, try to make the path writable by making
--   all directories on the path.
makeDirectories :: FilePath -> IO ()
makeDirectories :: FilePath -> IO ()
makeDirectories = Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True (FilePath -> IO ()) -> (FilePath -> FilePath) -> FilePath -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> FilePath
takeDirectory


--------------------------------------------------------------------------------
-- | Get all contents of a directory.
getRecursiveContents :: (FilePath -> IO Bool)  -- ^ Ignore this file/directory
                     -> FilePath               -- ^ Directory to search
                     -> IO [FilePath]          -- ^ List of files found
getRecursiveContents :: (FilePath -> IO Bool) -> FilePath -> IO [FilePath]
getRecursiveContents ignore :: FilePath -> IO Bool
ignore top :: FilePath
top = FilePath -> IO [FilePath]
go ""
  where
    isProper :: FilePath -> IO Bool
isProper x :: FilePath
x
        | FilePath
x FilePath -> [FilePath] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [".", ".."] = Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        | Bool
otherwise            = Bool -> Bool
not (Bool -> Bool) -> IO Bool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO Bool
ignore FilePath
x

    go :: FilePath -> IO [FilePath]
go dir :: FilePath
dir     = do
        Bool
dirExists <- FilePath -> IO Bool
doesDirectoryExist (FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
dir)
        if Bool -> Bool
not Bool
dirExists
            then [FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return []
            else do
                [FilePath]
names <- (FilePath -> IO Bool) -> [FilePath] -> IO [FilePath]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM FilePath -> IO Bool
isProper ([FilePath] -> IO [FilePath]) -> IO [FilePath] -> IO [FilePath]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> IO [FilePath]
getDirectoryContents (FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
dir)
                [[FilePath]]
paths <- [FilePath] -> (FilePath -> IO [FilePath]) -> IO [[FilePath]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [FilePath]
names ((FilePath -> IO [FilePath]) -> IO [[FilePath]])
-> (FilePath -> IO [FilePath]) -> IO [[FilePath]]
forall a b. (a -> b) -> a -> b
$ \name :: FilePath
name -> do
                    let rel :: FilePath
rel = FilePath
dir FilePath -> FilePath -> FilePath
</> FilePath
name
                    Bool
isDirectory <- FilePath -> IO Bool
doesDirectoryExist (FilePath
top FilePath -> FilePath -> FilePath
</> FilePath
rel)
                    if Bool
isDirectory
                        then FilePath -> IO [FilePath]
go FilePath
rel
                        else [FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
rel]

                [FilePath] -> IO [FilePath]
forall (m :: * -> *) a. Monad m => a -> m a
return ([FilePath] -> IO [FilePath]) -> [FilePath] -> IO [FilePath]
forall a b. (a -> b) -> a -> b
$ [[FilePath]] -> [FilePath]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[FilePath]]
paths


--------------------------------------------------------------------------------
removeDirectory :: FilePath -> IO ()
removeDirectory :: FilePath -> IO ()
removeDirectory fp :: FilePath
fp = do
    Bool
e <- FilePath -> IO Bool
doesDirectoryExist FilePath
fp
    Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
e (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
removeDirectoryRecursive FilePath
fp