2010-12-25 17:15:44 +00:00
|
|
|
-- | A Compiler manages targets and dependencies between targets.
|
|
|
|
--
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
module Hakyll.Core.Compiler
|
2010-12-30 16:47:31 +00:00
|
|
|
( Compiler
|
2010-12-29 21:59:38 +00:00
|
|
|
, getIdentifier
|
2010-12-30 20:18:55 +00:00
|
|
|
, getRoute
|
2010-12-29 21:59:38 +00:00
|
|
|
, getResourceString
|
2010-12-25 17:15:44 +00:00
|
|
|
, require
|
2010-12-30 09:02:25 +00:00
|
|
|
, requireAll
|
2010-12-31 11:38:12 +00:00
|
|
|
, cached
|
2010-12-25 17:15:44 +00:00
|
|
|
) where
|
|
|
|
|
2010-12-29 21:59:38 +00:00
|
|
|
import Prelude hiding ((.), id)
|
2010-12-30 16:47:31 +00:00
|
|
|
import Control.Arrow ((>>>))
|
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import Control.Monad.Reader (ask)
|
2010-12-29 21:59:38 +00:00
|
|
|
import Control.Monad.Trans (liftIO)
|
2010-12-30 16:47:31 +00:00
|
|
|
import Control.Category (Category, (.))
|
2010-12-29 21:59:38 +00:00
|
|
|
|
2010-12-28 10:12:45 +00:00
|
|
|
import Data.Binary (Binary)
|
2010-12-29 21:59:38 +00:00
|
|
|
import Data.Typeable (Typeable)
|
2010-12-25 17:15:44 +00:00
|
|
|
|
|
|
|
import Hakyll.Core.Identifier
|
2010-12-29 14:33:22 +00:00
|
|
|
import Hakyll.Core.Identifier.Pattern
|
2010-12-28 10:12:45 +00:00
|
|
|
import Hakyll.Core.CompiledItem
|
|
|
|
import Hakyll.Core.Writable
|
2010-12-29 14:33:22 +00:00
|
|
|
import Hakyll.Core.ResourceProvider
|
2010-12-30 16:47:31 +00:00
|
|
|
import Hakyll.Core.Compiler.Internal
|
2010-12-31 11:38:12 +00:00
|
|
|
import Hakyll.Core.Store
|
2010-12-29 21:59:38 +00:00
|
|
|
|
2010-12-30 20:18:55 +00:00
|
|
|
-- | Get the identifier of the item that is currently being compiled
|
|
|
|
--
|
2010-12-30 09:11:37 +00:00
|
|
|
getIdentifier :: Compiler a Identifier
|
2010-12-30 20:18:55 +00:00
|
|
|
getIdentifier = fromJob $ const $ CompilerM $ compilerIdentifier <$> ask
|
|
|
|
|
|
|
|
-- | Get the route we are using for this item
|
|
|
|
--
|
|
|
|
getRoute :: Compiler a (Maybe FilePath)
|
|
|
|
getRoute = fromJob $ const $ CompilerM $ compilerRoute <$> ask
|
2010-12-29 21:59:38 +00:00
|
|
|
|
2010-12-30 20:18:55 +00:00
|
|
|
-- | Get the resource we are compiling as a string
|
|
|
|
--
|
2010-12-30 09:11:37 +00:00
|
|
|
getResourceString :: Compiler a String
|
2010-12-29 21:59:38 +00:00
|
|
|
getResourceString = getIdentifier >>> getResourceString'
|
|
|
|
where
|
2010-12-30 16:47:31 +00:00
|
|
|
getResourceString' = fromJob $ \id' -> CompilerM $ do
|
2010-12-29 21:59:38 +00:00
|
|
|
provider <- compilerResourceProvider <$> ask
|
|
|
|
liftIO $ resourceString provider id'
|
2010-12-25 17:15:44 +00:00
|
|
|
|
|
|
|
-- | Require another target. Using this function ensures automatic handling of
|
|
|
|
-- dependencies
|
|
|
|
--
|
2010-12-28 10:12:45 +00:00
|
|
|
require :: (Binary a, Typeable a, Writable a)
|
|
|
|
=> Identifier
|
2010-12-30 09:02:25 +00:00
|
|
|
-> (b -> a -> c)
|
2010-12-29 21:59:38 +00:00
|
|
|
-> Compiler b c
|
2010-12-30 09:02:25 +00:00
|
|
|
require identifier f =
|
2010-12-30 16:47:31 +00:00
|
|
|
fromDependencies (const [identifier]) >>> fromJob require'
|
2010-12-29 21:59:38 +00:00
|
|
|
where
|
|
|
|
require' x = CompilerM $ do
|
|
|
|
lookup' <- compilerDependencyLookup <$> ask
|
2010-12-30 09:02:25 +00:00
|
|
|
return $ f x $ unCompiledItem $ lookup' identifier
|
2010-12-25 17:15:44 +00:00
|
|
|
|
2010-12-29 14:33:22 +00:00
|
|
|
-- | Require a number of targets. Using this function ensures automatic handling
|
|
|
|
-- of dependencies
|
|
|
|
--
|
|
|
|
requireAll :: (Binary a, Typeable a, Writable a)
|
|
|
|
=> Pattern
|
2010-12-30 09:02:25 +00:00
|
|
|
-> (b -> [a] -> c)
|
|
|
|
-> Compiler b c
|
|
|
|
requireAll pattern f =
|
2010-12-30 16:47:31 +00:00
|
|
|
fromDependencies getDeps >>> fromJob requireAll'
|
2010-12-30 09:02:25 +00:00
|
|
|
where
|
|
|
|
getDeps = matches pattern . resourceList
|
|
|
|
requireAll' x = CompilerM $ do
|
|
|
|
deps <- getDeps . compilerResourceProvider <$> ask
|
|
|
|
lookup' <- compilerDependencyLookup <$> ask
|
|
|
|
return $ f x $ map (unCompiledItem . lookup') deps
|
2010-12-31 11:38:12 +00:00
|
|
|
|
|
|
|
cached :: (Binary a)
|
|
|
|
=> String
|
|
|
|
-> Compiler () a
|
|
|
|
-> Compiler () a
|
|
|
|
cached name (Compiler d j) = Compiler d $ const $ CompilerM $ do
|
|
|
|
provider <- compilerResourceProvider <$> ask
|
|
|
|
identifier <- compilerIdentifier <$> ask
|
|
|
|
store <- compilerStore <$> ask
|
|
|
|
modified <- liftIO $ resourceModified provider identifier store
|
|
|
|
liftIO $ putStrLn $
|
|
|
|
show identifier ++ ": " ++ if modified then "MODIFIED" else "OK"
|
|
|
|
if modified
|
|
|
|
then do v <- unCompilerM $ j ()
|
|
|
|
liftIO $ storeSet store name identifier v
|
|
|
|
return v
|
|
|
|
else do v <- liftIO $ storeGet store name identifier
|
|
|
|
case v of Just v' -> return v'
|
|
|
|
Nothing -> error'
|
|
|
|
where
|
|
|
|
error' = error "Hakyll.Core.Compiler.cached: Cache corrupt!"
|