{-# 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
(CopyFile -> Put)
-> Get CopyFile -> ([CopyFile] -> Put) -> Binary CopyFile
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
$cput :: CopyFile -> Put
put :: CopyFile -> Put
$cget :: Get CopyFile
get :: Get CopyFile
$cputList :: [CopyFile] -> Put
putList :: [CopyFile] -> Put
Binary, CopyFile -> CopyFile -> Bool
(CopyFile -> CopyFile -> Bool)
-> (CopyFile -> CopyFile -> Bool) -> Eq CopyFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CopyFile -> CopyFile -> Bool
== :: CopyFile -> CopyFile -> Bool
$c/= :: CopyFile -> CopyFile -> Bool
/= :: CopyFile -> CopyFile -> Bool
Eq, Eq CopyFile
Eq CopyFile =>
(CopyFile -> CopyFile -> Ordering)
-> (CopyFile -> CopyFile -> Bool)
-> (CopyFile -> CopyFile -> Bool)
-> (CopyFile -> CopyFile -> Bool)
-> (CopyFile -> CopyFile -> Bool)
-> (CopyFile -> CopyFile -> CopyFile)
-> (CopyFile -> CopyFile -> CopyFile)
-> Ord 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
$ccompare :: CopyFile -> CopyFile -> Ordering
compare :: CopyFile -> CopyFile -> Ordering
$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
>= :: CopyFile -> CopyFile -> Bool
$cmax :: CopyFile -> CopyFile -> CopyFile
max :: CopyFile -> CopyFile -> CopyFile
$cmin :: CopyFile -> CopyFile -> CopyFile
min :: CopyFile -> CopyFile -> CopyFile
Ord, Int -> CopyFile -> FilePath -> FilePath
[CopyFile] -> FilePath -> FilePath
CopyFile -> FilePath
(Int -> CopyFile -> FilePath -> FilePath)
-> (CopyFile -> FilePath)
-> ([CopyFile] -> FilePath -> FilePath)
-> Show CopyFile
forall a.
(Int -> a -> FilePath -> FilePath)
-> (a -> FilePath) -> ([a] -> FilePath -> FilePath) -> Show a
$cshowsPrec :: Int -> CopyFile -> FilePath -> FilePath
showsPrec :: Int -> CopyFile -> FilePath -> FilePath
$cshow :: CopyFile -> FilePath
show :: CopyFile -> FilePath
$cshowList :: [CopyFile] -> FilePath -> FilePath
showList :: [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 <- Compiler Identifier
getUnderlying
provider <- compilerProvider <$> compilerAsk
makeItem $ CopyFile $ resourceFilePath provider identifier
newtype TmpFile = TmpFile FilePath
deriving (Typeable)
instance Binary TmpFile where
put :: TmpFile -> Put
put TmpFile
_ = () -> Put
forall a. a -> PutM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
get :: Get TmpFile
get = FilePath -> Get TmpFile
forall a. HasCallStack => FilePath -> a
error (FilePath -> Get TmpFile) -> FilePath -> Get TmpFile
forall a b. (a -> b) -> a -> b
$
FilePath
"Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " FilePath -> FilePath -> FilePath
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
path <- Compiler FilePath
mkPath
compilerUnsafeIO $ makeDirectories path
debugCompiler $ "newTmpFile " ++ path
return $ TmpFile path
where
mkPath :: Compiler FilePath
mkPath = do
rand <- IO Int -> Compiler Int
forall a. IO a -> Compiler a
compilerUnsafeIO (IO Int -> Compiler Int) -> IO Int -> Compiler Int
forall a b. (a -> b) -> a -> b
$ IO Int
forall a (m :: * -> *). (Random a, MonadIO m) => m a
randomIO :: Compiler Int
tmp <- tmpDirectory . compilerConfig <$> compilerAsk
let path = FilePath
tmp FilePath -> FilePath -> FilePath
</> [FilePath] -> FilePath
Store.hash [Int -> FilePath
forall a. Show a => a -> FilePath
show Int
rand] FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"-" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
suffix
exists <- compilerUnsafeIO $ doesFileExist path
if exists then mkPath else return path