{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Hakyll.Core.File
( CopyFile (..)
, copyFileCompiler
, TmpFile (..)
, newTmpFile
) where
import Data.Binary (Binary (..))
import Data.Typeable (Typeable)
#if MIN_VERSION_directory(1,2,6)
import System.Directory (copyFileWithMetadata)
#else
import System.Directory (copyFile)
#endif
import System.Directory (doesFileExist,
renameFile)
import System.FilePath ((</>))
import System.Random (randomIO)
import Hakyll.Core.Compiler
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Configuration
import Hakyll.Core.Item
import Hakyll.Core.Provider
import qualified Hakyll.Core.Store as Store
import Hakyll.Core.Util.File
import Hakyll.Core.Writable
newtype CopyFile = CopyFile FilePath
deriving (Get CopyFile
[CopyFile] -> Put
CopyFile -> Put
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [CopyFile] -> Put
$cputList :: [CopyFile] -> Put
get :: Get CopyFile
$cget :: Get CopyFile
put :: CopyFile -> Put
$cput :: CopyFile -> Put
Binary, CopyFile -> CopyFile -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CopyFile -> CopyFile -> Bool
$c/= :: CopyFile -> CopyFile -> Bool
== :: CopyFile -> CopyFile -> Bool
$c== :: CopyFile -> CopyFile -> Bool
Eq, Eq CopyFile
CopyFile -> CopyFile -> Bool
CopyFile -> CopyFile -> Ordering
CopyFile -> CopyFile -> CopyFile
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CopyFile -> CopyFile -> CopyFile
$cmin :: CopyFile -> CopyFile -> CopyFile
max :: CopyFile -> CopyFile -> CopyFile
$cmax :: CopyFile -> CopyFile -> CopyFile
>= :: CopyFile -> CopyFile -> Bool
$c>= :: CopyFile -> CopyFile -> Bool
> :: CopyFile -> CopyFile -> Bool
$c> :: CopyFile -> CopyFile -> Bool
<= :: CopyFile -> CopyFile -> Bool
$c<= :: CopyFile -> CopyFile -> Bool
< :: CopyFile -> CopyFile -> Bool
$c< :: CopyFile -> CopyFile -> Bool
compare :: CopyFile -> CopyFile -> Ordering
$ccompare :: CopyFile -> CopyFile -> Ordering
Ord, Int -> CopyFile -> FilePath -> FilePath
[CopyFile] -> FilePath -> FilePath
CopyFile -> FilePath
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
showList :: [CopyFile] -> FilePath -> FilePath
$cshowList :: [CopyFile] -> FilePath -> FilePath
show :: CopyFile -> FilePath
$cshow :: CopyFile -> FilePath
showsPrec :: Int -> CopyFile -> FilePath -> FilePath
$cshowsPrec :: Int -> CopyFile -> FilePath -> FilePath
Show, Typeable)
instance Writable CopyFile where
#if MIN_VERSION_directory(1,2,6)
write :: FilePath -> Item CopyFile -> IO ()
write FilePath
dst (Item Identifier
_ (CopyFile FilePath
src)) = FilePath -> FilePath -> IO ()
copyFileWithMetadata FilePath
src FilePath
dst
#else
write dst (Item _ (CopyFile src)) = copyFile src dst
#endif
copyFileCompiler :: Compiler (Item CopyFile)
copyFileCompiler :: Compiler (Item CopyFile)
copyFileCompiler = do
Identifier
identifier <- Compiler Identifier
getUnderlying
Provider
provider <- CompilerRead -> Provider
compilerProvider forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Compiler CompilerRead
compilerAsk
forall a. a -> Compiler (Item a)
makeItem forall a b. (a -> b) -> a -> b
$ FilePath -> CopyFile
CopyFile forall a b. (a -> b) -> a -> b
$ Provider -> Identifier -> FilePath
resourceFilePath Provider
provider Identifier
identifier
newtype TmpFile = TmpFile FilePath
deriving (Typeable)
instance Binary TmpFile where
put :: TmpFile -> Put
put TmpFile
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
get :: Get TmpFile
get = forall a. HasCallStack => FilePath -> a
error forall a b. (a -> b) -> a -> b
$
FilePath
"Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " forall a. [a] -> [a] -> [a]
++
FilePath
"this is not possible since these are deleted as soon as possible."
instance Writable TmpFile where
write :: FilePath -> Item TmpFile -> IO ()
write FilePath
dst (Item Identifier
_ (TmpFile FilePath
fp)) = FilePath -> FilePath -> IO ()
renameFile FilePath
fp FilePath
dst
newTmpFile :: String
-> Compiler TmpFile
newTmpFile :: FilePath -> Compiler TmpFile
newTmpFile FilePath
suffix = do
FilePath
path <- Compiler FilePath
mkPath
forall a. IO a -> Compiler a
compilerUnsafeIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
makeDirectories FilePath
path
FilePath -> Compiler ()
debugCompiler forall a b. (a -> b) -> a -> b
$ FilePath
"newTmpFile " forall a. [a] -> [a] -> [a]
++ FilePath
path
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ FilePath -> TmpFile
TmpFile FilePath
path
where
mkPath :: Compiler FilePath
mkPath = do
Int
rand <- forall a. IO a -> Compiler a
compilerUnsafeIO forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *). (Random a, MonadIO m) => m a
randomIO :: Compiler Int
FilePath
tmp <- Configuration -> FilePath
tmpDirectory forall b c a. (b -> c) -> (a -> b) -> a -> c
. CompilerRead -> Configuration
compilerConfig forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Compiler CompilerRead
compilerAsk
let path :: FilePath
path = FilePath
tmp FilePath -> FilePath -> FilePath
</> [FilePath] -> FilePath
Store.hash [forall a. Show a => a -> FilePath
show Int
rand] forall a. [a] -> [a] -> [a]
++ FilePath
"-" forall a. [a] -> [a] -> [a]
++ FilePath
suffix
Bool
exists <- forall a. IO a -> Compiler a
compilerUnsafeIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO Bool
doesFileExist FilePath
path
if Bool
exists then Compiler FilePath
mkPath else forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
path