module System.Directory.Extra
( copyDirectoryRecursive
, copyFile'
)
where
import qualified Control.Exception as E
import Distribution.Simple.Utils ( getDirectoryContentsRecursive )
import System.Directory ( copyFile,
createDirectoryIfMissing )
import System.Exit ( ExitCode (ExitFailure), exitWith )
import System.FilePath ( takeDirectory, (</>) )
import System.IO ( hPutStrLn, stderr )
copyDirectoryRecursive :: FilePath
-> FilePath
-> IO ()
copyDirectoryRecursive :: FilePath -> FilePath -> IO ()
copyDirectoryRecursive FilePath
sourceDir FilePath
targetDir =
(SomeException -> IO ()) -> IO () -> IO ()
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
E.handle (FilePath -> FilePath -> SomeException -> IO ()
copyDirectoryRecursiveErrorHandler FilePath
sourceDir FilePath
targetDir) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
[FilePath]
files <- FilePath -> IO [FilePath]
getDirectoryContentsRecursive FilePath
sourceDir
let sourceAndDest :: FilePath -> (FilePath, FilePath)
sourceAndDest FilePath
file = (FilePath
src, FilePath
dest)
where
src :: FilePath
src = FilePath
sourceDir FilePath -> FilePath -> FilePath
</> FilePath
file
dest :: FilePath
dest = FilePath
targetDir FilePath -> FilePath -> FilePath
</> FilePath
file
(FilePath -> IO ()) -> [FilePath] -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((FilePath, FilePath) -> IO ()
copyFile' ((FilePath, FilePath) -> IO ())
-> (FilePath -> (FilePath, FilePath)) -> FilePath -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> (FilePath, FilePath)
sourceAndDest) [FilePath]
files
copyFile' :: (FilePath, FilePath) -> IO ()
copyFile' :: (FilePath, FilePath) -> IO ()
copyFile' (FilePath
origin, FilePath
dest) = do
Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True (FilePath -> FilePath
takeDirectory FilePath
dest)
FilePath -> FilePath -> IO ()
copyFile FilePath
origin FilePath
dest
copyDirectoryRecursiveErrorHandler :: FilePath
-> FilePath
-> E.SomeException
-> IO ()
copyDirectoryRecursiveErrorHandler :: FilePath -> FilePath -> SomeException -> IO ()
copyDirectoryRecursiveErrorHandler FilePath
sourceDir FilePath
targetDir SomeException
_exception = do
Handle -> FilePath -> IO ()
hPutStrLn Handle
stderr (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$
FilePath
"ogma: error: cannot copy " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
sourceDir FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
" to " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
targetDir
ExitCode -> IO ()
forall a. ExitCode -> IO a
exitWith (Int -> ExitCode
ExitFailure Int
1)