module Agda.Utils.IO.Directory
( copyDirContent
)
where
import Control.Monad
import Control.Monad.Writer ( WriterT, execWriterT, tell )
import Control.Monad.Trans ( lift )
import Data.Monoid ( Endo(Endo, appEndo) )
import System.Directory
import System.FilePath
import Data.ByteString as BS
copyDirContent :: FilePath -> FilePath -> IO ()
copyDirContent :: FilePath -> FilePath -> IO ()
copyDirContent FilePath
src FilePath
dest = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CopyDirAction -> IO ()
performAction forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< do
(forall a. Endo a -> a -> a
`appEndo` []) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT (FilePath -> FilePath -> WriterT (Endo [CopyDirAction]) IO ()
copyDirContentDryRun FilePath
src FilePath
dest)
data CopyDirAction
= MkDir FilePath
| CopyFile FilePath FilePath
performAction :: CopyDirAction -> IO ()
performAction :: CopyDirAction -> IO ()
performAction = \case
MkDir FilePath
d -> Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
d
CopyFile FilePath
src FilePath
dest -> FilePath -> FilePath -> IO ()
copyIfChanged FilePath
src FilePath
dest
copyDirContentDryRun :: FilePath -> FilePath -> WriterT (Endo [CopyDirAction]) IO ()
copyDirContentDryRun :: FilePath -> FilePath -> WriterT (Endo [CopyDirAction]) IO ()
copyDirContentDryRun FilePath
src FilePath
dest = do
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell forall a b. (a -> b) -> a -> b
$ forall a. (a -> a) -> Endo a
Endo (FilePath -> CopyDirAction
MkDir FilePath
dest forall a. a -> [a] -> [a]
:)
[FilePath]
chlds <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ FilePath -> IO [FilePath]
getDirectoryContents FilePath
src
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [FilePath]
chlds forall a b. (a -> b) -> a -> b
$ \ FilePath
x -> do
Bool
isDir <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesDirectoryExist (FilePath
src FilePath -> FilePath -> FilePath
</> FilePath
x)
case Bool
isDir of
Bool
_ | FilePath
x forall a. Eq a => a -> a -> Bool
== FilePath
"." Bool -> Bool -> Bool
|| FilePath
x forall a. Eq a => a -> a -> Bool
== FilePath
".." -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Bool
True -> FilePath -> FilePath -> WriterT (Endo [CopyDirAction]) IO ()
copyDirContentDryRun (FilePath
src FilePath -> FilePath -> FilePath
</> FilePath
x) (FilePath
dest FilePath -> FilePath -> FilePath
</> FilePath
x)
Bool
False -> forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell forall a b. (a -> b) -> a -> b
$ forall a. (a -> a) -> Endo a
Endo (FilePath -> FilePath -> CopyDirAction
CopyFile (FilePath
src FilePath -> FilePath -> FilePath
</> FilePath
x) (FilePath
dest FilePath -> FilePath -> FilePath
</> FilePath
x) forall a. a -> [a] -> [a]
:)
copyIfChanged :: FilePath -> FilePath -> IO ()
copyIfChanged :: FilePath -> FilePath -> IO ()
copyIfChanged FilePath
src FilePath
dst = do
Bool
exist <- FilePath -> IO Bool
doesFileExist FilePath
dst
if Bool -> Bool
not Bool
exist then FilePath -> FilePath -> IO ()
copyFile FilePath
src FilePath
dst else do
ByteString
new <- FilePath -> IO ByteString
BS.readFile FilePath
src
ByteString
old <- FilePath -> IO ByteString
BS.readFile FilePath
dst
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString
old forall a. Eq a => a -> a -> Bool
== ByteString
new) forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
copyFile FilePath
src FilePath
dst