on d8a3d7e help sign in
86 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.Item
28import Hakyll.Core.Provider
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
36newtype CopyFile = CopyFile FilePath
37 deriving (Binary, Eq, Ord, Show, Typeable)
40--------------------------------------------------------------------------------
41instance Writable CopyFile where
42 write dst (Item _ (CopyFile src)) = copyFile src dst
45--------------------------------------------------------------------------------
46copyFileCompiler :: Compiler (Item CopyFile)
47copyFileCompiler = do
48 identifier <- getUnderlying
49 provider <- compilerProvider <$> compilerAsk
50 makeItem $ CopyFile $ resourceFilePath provider identifier
53--------------------------------------------------------------------------------
54newtype TmpFile = TmpFile FilePath
55 deriving (Typeable)
58--------------------------------------------------------------------------------
59instance Binary TmpFile where
60 put _ = return ()
61 get = error $
62 "Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " ++
63 "this is not possible since these are deleted as soon as possible."
66--------------------------------------------------------------------------------
67instance Writable TmpFile where
68 write dst (Item _ (TmpFile fp)) = renameFile fp dst
71--------------------------------------------------------------------------------
72-- | Create a tmp file
73newTmpFile :: String -- ^ Suffix and extension
74 -> Compiler TmpFile -- ^ Resulting tmp path
75newTmpFile suffix = do
76 path <- mkPath
77 compilerUnsafeIO $ makeDirectories path
78 debugCompiler $ "newTmpFile " ++ path
79 return $ TmpFile path
80 where
81 mkPath = do
82 rand <- compilerUnsafeIO $ randomIO :: Compiler Int
83 tmp <- tmpDirectory . compilerConfig <$> compilerAsk
84 let path = tmp </> Store.hash [show rand] ++ "-" ++ suffix
85 exists <- compilerUnsafeIO $ doesFileExist path
86 if exists then mkPath else return path