on 6a88430 help sign in
85 lines 3.2 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 Data.Binary (Binary (..))
15import Data.Typeable (Typeable)
16import System.Directory (copyFile, doesFileExist,
17 renameFile)
18import System.FilePath ((</>))
19import System.Random (randomIO)
22--------------------------------------------------------------------------------
23import Hakyll.Core.Compiler
24import Hakyll.Core.Compiler.Internal
25import Hakyll.Core.Configuration
26import Hakyll.Core.Item
27import Hakyll.Core.Provider
28import qualified Hakyll.Core.Store as Store
29import Hakyll.Core.Util.File
30import Hakyll.Core.Writable
33--------------------------------------------------------------------------------
34-- | This will copy any file directly by using a system call
35newtype CopyFile = CopyFile FilePath
36 deriving (Binary, Eq, Ord, Show, Typeable)
39--------------------------------------------------------------------------------
40instance Writable CopyFile where
41 write dst (Item _ (CopyFile src)) = copyFile src dst
44--------------------------------------------------------------------------------
45copyFileCompiler :: Compiler (Item CopyFile)
46copyFileCompiler = do
47 identifier <- getUnderlying
48 provider <- compilerProvider <$> compilerAsk
49 makeItem $ CopyFile $ resourceFilePath provider identifier
52--------------------------------------------------------------------------------
53newtype TmpFile = TmpFile FilePath
54 deriving (Typeable)
57--------------------------------------------------------------------------------
58instance Binary TmpFile where
59 put _ = return ()
60 get = error $
61 "Hakyll.Core.File.TmpFile: You tried to load a TmpFile, however, " ++
62 "this is not possible since these are deleted as soon as possible."
65--------------------------------------------------------------------------------
66instance Writable TmpFile where
67 write dst (Item _ (TmpFile fp)) = renameFile fp dst
70--------------------------------------------------------------------------------
71-- | Create a tmp file
72newTmpFile :: String -- ^ Suffix and extension
73 -> Compiler TmpFile -- ^ Resulting tmp path
74newTmpFile suffix = do
75 path <- mkPath
76 compilerUnsafeIO $ makeDirectories path
77 debugCompiler $ "newTmpFile " ++ path
78 return $ TmpFile path
79 where
80 mkPath = do
81 rand <- compilerUnsafeIO $ randomIO :: Compiler Int
82 tmp <- tmpDirectory . compilerConfig <$> compilerAsk
83 let path = tmp </> Store.hash [show rand] ++ "-" ++ suffix
84 exists <- compilerUnsafeIO $ doesFileExist path
85 if exists then mkPath else return path