on bc360a3 help sign in
hakyll/src/Hakyll/Core/Configuration.hs
128 lines 4.7 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 Data.Default (Default (..))
12import Data.List (isPrefixOf, isSuffixOf)
13import System.Directory (canonicalizePath)
14import System.Exit (ExitCode)
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 ExitCode
69 , -- | Use an in-memory cache for items. This is faster but uses more
70 -- memory.
71 inMemoryCache :: Bool
72 , -- | Override default port for preview server. Default is 8000.
73 -- One can also override the port as a command line argument:
74 -- ./site preview -p 1234
75 previewPort :: Int
76 }
78--------------------------------------------------------------------------------
79instance Default Configuration where
80 def = defaultConfiguration
82--------------------------------------------------------------------------------
83-- | Default configuration for a hakyll application
84defaultConfiguration :: Configuration
85defaultConfiguration = Configuration
86 { destinationDirectory = "_site"
87 , storeDirectory = "_cache"
88 , tmpDirectory = "_cache/tmp"
89 , providerDirectory = "."
90 , ignoreFile = ignoreFile'
91 , deployCommand = "echo 'No deploy command specified' && exit 1"
92 , deploySite = system . deployCommand
93 , inMemoryCache = True
94 , previewPort = 8000
95 }
96 where
97 ignoreFile' path
98 | "." `isPrefixOf` fileName = True
99 | "#" `isPrefixOf` fileName = True
100 | "~" `isSuffixOf` fileName = True
101 | ".swp" `isSuffixOf` fileName = True
102 | otherwise = False
103 where
104 fileName = takeFileName path
107--------------------------------------------------------------------------------
108-- | Check if a file should be ignored
109shouldIgnoreFile :: Configuration -> FilePath -> IO Bool
110shouldIgnoreFile conf path = orM
111 [ inDir (destinationDirectory conf)
112 , inDir (storeDirectory conf)
113 , inDir (tmpDirectory conf)
114 , return (ignoreFile conf path')
115 ]
116 where
117 path' = normalise path
118 absolute = isAbsolute path
120 inDir dir
121 | absolute = do
122 dir' <- catchIOError (canonicalizePath dir) (const $ return dir)
123 return $ dir' `isPrefixOf` path'
124 | otherwise = return $ dir `isPrefixOf` path'
126 orM :: [IO Bool] -> IO Bool
127 orM [] = return False
128 orM (x : xs) = x >>= \b -> if b then return True else orM xs