on d993ebc help sign in
89 lines 3.3 kB history raw
1--------------------------------------------------------------------------------
2-- | Exports simple compilers to just copy files
3{-# LANGUAGE DeriveDataTypeable #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5module Hakyll.Core.File
6 ( CopyFile (..)
7 , copyFileCompiler
8 , TmpFile (..)
9 , newTmpFile
10 ) where
13--------------------------------------------------------------------------------
14import Control.Applicative ((<$>))
15import Data.Binary (Binary (..))
16import Data.Typeable (Typeable)
17import System.Directory (copyFile, doesFileExist,
18 renameFile)
19import System.FilePath ((</>))
20import System.Random (randomIO)
23--------------------------------------------------------------------------------
24import Hakyll.Core.Compiler
25import Hakyll.Core.Compiler.Internal
26import Hakyll.Core.Configuration
27import Hakyll.Core.Identifier
28import Hakyll.Core.Item
29import qualified Hakyll.Core.Store as Store
30import Hakyll.Core.Util.File
31import Hakyll.Core.Writable
34--------------------------------------------------------------------------------
35-- | This will copy any file directly by using a system call
36data CopyFile = CopyFile
37 deriving (Show, Eq, Ord, Typeable)
40--------------------------------------------------------------------------------
41instance Binary CopyFile where
42 put CopyFile = return ()
43 get = return CopyFile
46--------------------------------------------------------------------------------
47instance Writable CopyFile where
48 write dst item = copyFile (toFilePath $ itemIdentifier item) dst
51--------------------------------------------------------------------------------
52copyFileCompiler :: Compiler (Item CopyFile)
53copyFileCompiler = makeItem CopyFile
56--------------------------------------------------------------------------------
57newtype TmpFile = TmpFile FilePath
58 deriving (Typeable)
61--------------------------------------------------------------------------------
62instance Binary TmpFile where
63 put _ = return ()
64 get = error $
65 "Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " ++
66 "this is not possible since these are deleted as soon as possible."
69--------------------------------------------------------------------------------
70instance Writable TmpFile where
71 write dst (Item _ (TmpFile fp)) = renameFile fp dst
74--------------------------------------------------------------------------------
75-- | Create a tmp file
76newTmpFile :: String -- ^ Suffix and extension
77 -> Compiler TmpFile -- ^ Resulting tmp path
78newTmpFile suffix = do
79 path <- mkPath
80 compilerUnsafeIO $ makeDirectories path
81 debugCompiler $ "newTmpFile " ++ path
82 return $ TmpFile path
83 where
84 mkPath = do
85 rand <- compilerUnsafeIO $ randomIO :: Compiler Int
86 tmp <- tmpDirectory . compilerConfig <$> compilerAsk
87 let path = tmp </> Store.hash [show rand] ++ "-" ++ suffix
88 exists <- compilerUnsafeIO $ doesFileExist path
89 if exists then mkPath else return path