on master help sign in
Add a top-level configuration type
commit26c95402d8048f88c15aad273cca6a2829098d2a authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 3 Feb 2011, 11:34 (16 years ago) parent97ce6cb
3 files changed+76−31
src/Hakyll/Core/Configuration.hs+21 src/Hakyll/Core/Run.hs+34−31 src/Hakyll/Main.hs+21
src/Hakyll/Core/Configuration.hsnew file+21
@@ -0,0 +1,21 @@
1+-- | Exports a datastructure for the top-level hakyll configuration
2+--
3+module Hakyll.Core.Configuration
4+ ( HakyllConfiguration (..)
5+ , defaultHakyllConfiguration
6+ ) where
7+
8+data HakyllConfiguration = HakyllConfiguration
9+ { -- | Directory in which the output written
10+ destinationDirectory :: FilePath
11+ , -- | Directory where hakyll's internal store is kept
12+ storeDirectory :: FilePath
13+ } deriving (Show)
14+
15+-- | Default configuration for a hakyll application
16+--
17+defaultHakyllConfiguration :: HakyllConfiguration
18+defaultHakyllConfiguration = HakyllConfiguration
19+ { destinationDirectory = "_site"
20+ , storeDirectory = "_cache"
21+ }
src/Hakyll/Core/Run.hs+34−31
@@ -1,20 +1,17 @@
11-- | This is the module which binds it all together
22--
33{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4−module Hakyll.Core.Run where
4+module Hakyll.Core.Run
5+ ( run
6+ ) where
57
68import Prelude hiding (reverse)
79import Control.Applicative
810import Control.Monad.Reader
911import Control.Monad.State
10−import Control.Monad.Trans
1112import Control.Arrow ((&&&))
12−import Control.Monad (foldM, forM_, forM, filterM)
13−import Data.Map (Map)
1413import qualified Data.Map as M
1514import Data.Monoid (mempty, mappend)
16−import Data.Typeable (Typeable)
17−import Data.Binary (Binary)
1815import System.FilePath ((</>))
1916import Data.Set (Set)
2017import qualified Data.Set as S
@@ -32,45 +29,49 @@ import Hakyll.Core.DirectedGraph.Dot
3229import Hakyll.Core.DirectedGraph.DependencySolver
3330import Hakyll.Core.Writable
3431import Hakyll.Core.Store
35−import Hakyll.Core.CompiledItem
32+import Hakyll.Core.Configuration
3633
37−hakyll :: Rules -> IO ()
38−hakyll rules = do
39− store <- makeStore "_store"
34+-- | Run all rules needed
35+--
36+run :: HakyllConfiguration -> Rules -> IO ()
37+run configuration rules = do
38+ store <- makeStore $ storeDirectory configuration
4039 provider <- fileResourceProvider
4140 let ruleSet = runRules rules provider
4241 compilers = rulesCompilers ruleSet
4342
4443 -- Extract the reader/state
45− reader = unHakyll $ addNewCompilers [] compilers
44+ reader = unRuntime $ addNewCompilers [] compilers
4645 state' = runReaderT reader $ env ruleSet provider store
4746
4847 evalStateT state' state
4948 where
50− env ruleSet provider store = HakyllEnvironment
51− { hakyllRoute = rulesRoute ruleSet
49+ env ruleSet provider store = RuntimeEnvironment
50+ { hakyllConfiguration = configuration
51+ , hakyllRoute = rulesRoute ruleSet
5252 , hakyllResourceProvider = provider
5353 , hakyllStore = store
5454 }
5555
56− state = HakyllState
56+ state = RuntimeState
5757 { hakyllModified = S.empty
5858 , hakyllGraph = mempty
5959 }
6060
61−data HakyllEnvironment = HakyllEnvironment
62− { hakyllRoute :: Route
61+data RuntimeEnvironment = RuntimeEnvironment
62+ { hakyllConfiguration :: HakyllConfiguration
63+ , hakyllRoute :: Route
6364 , hakyllResourceProvider :: ResourceProvider
6465 , hakyllStore :: Store
6566 }
6667
67−data HakyllState = HakyllState
68+data RuntimeState = RuntimeState
6869 { hakyllModified :: Set Identifier
6970 , hakyllGraph :: DirectedGraph Identifier
7071 }
7172
72−newtype Hakyll a = Hakyll
73− { unHakyll :: ReaderT HakyllEnvironment (StateT HakyllState IO) a
73+newtype Runtime a = Runtime
74+ { unRuntime :: ReaderT RuntimeEnvironment (StateT RuntimeState IO) a
7475 } deriving (Functor, Applicative, Monad)
7576
7677-- | Return a set of modified identifiers
@@ -89,8 +90,8 @@ addNewCompilers :: [(Identifier, Compiler () CompileRule)]
8990 -- ^ Remaining compilers yet to be run
9091 -> [(Identifier, Compiler () CompileRule)]
9192 -- ^ Compilers to add
92− -> Hakyll ()
93−addNewCompilers oldCompilers newCompilers = Hakyll $ do
93+ -> Runtime ()
94+addNewCompilers oldCompilers newCompilers = Runtime $ do
9495 -- Get some information
9596 provider <- hakyllResourceProvider <$> ask
9697 store <- hakyllStore <$> ask
@@ -140,7 +141,7 @@ addNewCompilers oldCompilers newCompilers = Hakyll $ do
140141 modify $ updateState modified' completeGraph
141142
142143 -- Now run the ordered list of compilers
143− unHakyll $ runCompilers orderedCompilers
144+ unRuntime $ runCompilers orderedCompilers
144145 where
145146 -- Add the modified information for the new compilers
146147 updateState modified' graph state = state
@@ -150,10 +151,10 @@ addNewCompilers oldCompilers newCompilers = Hakyll $ do
150151
151152runCompilers :: [(Identifier, Compiler () CompileRule)]
152153 -- ^ Ordered list of compilers
153− -> Hakyll ()
154+ -> Runtime ()
154155 -- ^ No result
155156runCompilers [] = return ()
156−runCompilers ((id', compiler) : compilers) = Hakyll $ do
157+runCompilers ((id', compiler) : compilers) = Runtime $ do
157158 -- Obtain information
158159 route' <- hakyllRoute <$> ask
159160 provider <- hakyllResourceProvider <$> ask
@@ -175,18 +176,20 @@ runCompilers ((id', compiler) : compilers) = Hakyll $ do
175176 CompileRule compiled -> do
176177 case url of
177178 Nothing -> return ()
178− Just r -> liftIO $ do
179− putStrLn $ "Routing " ++ show id' ++ " to " ++ r
180− let path = "_site" </> r
181− makeDirectories path
182− write path compiled
179+ Just r -> do
180+ liftIO $ putStrLn $ "Routing " ++ show id' ++ " to " ++ r
181+ destination <-
182+ destinationDirectory . hakyllConfiguration <$> ask
183+ let path = destination </> r
184+ liftIO $ makeDirectories path
185+ liftIO $ write path compiled
183186
184187 liftIO $ putStrLn ""
185188
186189 -- Continue for the remaining compilers
187− unHakyll $ runCompilers compilers
190+ unRuntime $ runCompilers compilers
188191
189192 -- Metacompiler, slightly more complicated
190193 MetaCompileRule newCompilers ->
191194 -- Actually I was just kidding, it's not hard at all
192− unHakyll $ addNewCompilers compilers newCompilers
195+ unRuntime $ addNewCompilers compilers newCompilers
src/Hakyll/Main.hsnew file+21
@@ -0,0 +1,21 @@
1+-- | Module providing the main hakyll function and command-line argument parsing
2+--
3+module Hakyll.Main
4+ ( hakyll
5+ , hakyllWith
6+ ) where
7+
8+import Hakyll.Core.Configuration
9+import Hakyll.Core.Run
10+import Hakyll.Core.Rules
11+
12+-- | This usualy is the function with which the user runs the hakyll compiler
13+--
14+hakyll :: Rules -> IO ()
15+hakyll = run defaultHakyllConfiguration
16+
17+-- | A variant of 'hakyll' which allows the user to specify a custom
18+-- configuration
19+--
20+hakyllWith :: HakyllConfiguration -> Rules -> IO ()
21+hakyllWith = run