on 2ef7fee help sign in
hakyll/src/Hakyll/Core/Configuration.hs
123 lines 4.5 kB history raw
1--------------------------------------------------------------------------------
2-- | Exports a datastructure for the top-level hakyll configuration
3module Hakyll.Core.Configuration
4 ( Configuration (..)
5 , shouldIgnoreFile
6 , defaultConfiguration
7 ) where
10--------------------------------------------------------------------------------
11import Control.Monad (void)
12import Data.Default (Default (..))
13import Data.List (isPrefixOf, isSuffixOf)
14import System.Directory (canonicalizePath)
15import System.FilePath (isAbsolute, normalise, takeFileName)
16import System.IO.Error (catchIOError)
17import System.Process (system)
20--------------------------------------------------------------------------------
21data Configuration = Configuration
22 { -- | Directory in which the output written
23 destinationDirectory :: FilePath
24 , -- | Directory where hakyll's internal store is kept
25 storeDirectory :: FilePath
26 , -- | Directory in which some temporary files will be kept
27 tmpDirectory :: FilePath
28 , -- | Directory where hakyll finds the files to compile. This is @.@ by
29 -- default.
30 providerDirectory :: FilePath
31 , -- | Function to determine ignored files
32 --
33 -- In 'defaultConfiguration', the following files are ignored:
34 --
35 -- * files starting with a @.@
36 --
37 -- * files starting with a @#@
38 --
39 -- * files ending with a @~@
40 --
41 -- * files ending with @.swp@
42 --
43 -- Note that the files in 'destinationDirectory' and 'storeDirectory' will
44 -- also be ignored. Note that this is the configuration parameter, if you
45 -- want to use the test, you should use 'shouldIgnoreFile'.
46 --
47 ignoreFile :: FilePath -> Bool
48 , -- | Here, you can plug in a system command to upload/deploy your site.
49 --
50 -- Example:
51 --
52 -- > rsync -ave 'ssh -p 2217' _site jaspervdj@jaspervdj.be:hakyll
53 --
54 -- You can execute this by using
55 --
56 -- > ./site deploy
57 --
58 deployCommand :: String
59 , -- | Function to deploy the site from Haskell.
60 --
61 -- By default, this command executes the shell command stored in
62 -- 'deployCommand'. If you override it, 'deployCommand' will not
63 -- be used implicitely.
64 --
65 -- The 'Configuration' object is passed as a parameter to this
66 -- function.
67 --
68 deploySite :: Configuration -> IO ()
69 , -- | Use an in-memory cache for items. This is faster but uses more
70 -- memory.
71 inMemoryCache :: Bool
72 }
74--------------------------------------------------------------------------------
75instance Default Configuration where
76 def = defaultConfiguration
78--------------------------------------------------------------------------------
79-- | Default configuration for a hakyll application
80defaultConfiguration :: Configuration
81defaultConfiguration = Configuration
82 { destinationDirectory = "_site"
83 , storeDirectory = "_cache"
84 , tmpDirectory = "_cache/tmp"
85 , providerDirectory = "."
86 , ignoreFile = ignoreFile'
87 , deployCommand = "echo 'No deploy command specified'"
88 , deploySite = void . system . deployCommand
89 , inMemoryCache = True
90 }
91 where
92 ignoreFile' path
93 | "." `isPrefixOf` fileName = True
94 | "#" `isPrefixOf` fileName = True
95 | "~" `isSuffixOf` fileName = True
96 | ".swp" `isSuffixOf` fileName = True
97 | otherwise = False
98 where
99 fileName = takeFileName path
102--------------------------------------------------------------------------------
103-- | Check if a file should be ignored
104shouldIgnoreFile :: Configuration -> FilePath -> IO Bool
105shouldIgnoreFile conf path = orM
106 [ inDir (destinationDirectory conf)
107 , inDir (storeDirectory conf)
108 , inDir (tmpDirectory conf)
109 , return (ignoreFile conf path')
110 ]
111 where
112 path' = normalise path
113 absolute = isAbsolute path
115 inDir dir
116 | absolute = do
117 dir' <- catchIOError (canonicalizePath dir) (const $ return dir)
118 return $ dir' `isPrefixOf` path'
119 | otherwise = return $ dir `isPrefixOf` path'
121 orM :: [IO Bool] -> IO Bool
122 orM [] = return False
123 orM (x : xs) = x >>= \b -> if b then return True else orM xs