{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Hakyll.Core.Compiler.Internal
(
Snapshot
, CompilerRead (..)
, CompilerWrite (..)
, CompilerErrors (..)
, CompilerResult (..)
, Compiler (..)
, runCompiler
, compilerResult
, compilerTell
, compilerAsk
, compilerUnsafeIO
, compilerThrow
, compilerNoResult
, compilerCatch
, compilerTry
, compilerErrorMessages
, compilerDebugEntries
, compilerTellDependencies
, compilerTellCacheHits
) where
import Control.Applicative (Alternative (..))
import Control.Exception (SomeException, handle)
import Control.Monad (forM_)
import qualified Control.Monad.Fail as Fail
import Control.Monad.Except (MonadError (..))
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as NonEmpty
#if MIN_VERSION_base(4,9,0)
import Data.Semigroup (Semigroup (..))
#endif
import Data.Set (Set)
import qualified Data.Set as S
import Hakyll.Core.Configuration
import Hakyll.Core.Dependencies
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
import qualified Hakyll.Core.Logger as Logger
import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Routes
import Hakyll.Core.Store
type Snapshot = String
data CompilerRead = CompilerRead
{
compilerConfig :: Configuration
,
compilerUnderlying :: Identifier
,
compilerProvider :: Provider
,
compilerUniverse :: Set Identifier
,
compilerRoutes :: Routes
,
compilerStore :: Store
,
compilerLogger :: Logger.Logger
}
data CompilerWrite = CompilerWrite
{ compilerDependencies :: [Dependency]
, compilerCacheHits :: Int
} deriving (Show)
#if MIN_VERSION_base(4,9,0)
instance Semigroup CompilerWrite where
(<>) (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
CompilerWrite (d1 ++ d2) (h1 + h2)
instance Monoid CompilerWrite where
mempty = CompilerWrite [] 0
mappend = (<>)
#else
instance Monoid CompilerWrite where
mempty = CompilerWrite [] 0
mappend (CompilerWrite d1 h1) (CompilerWrite d2 h2) =
CompilerWrite (d1 ++ d2) (h1 + h2)
#endif
data CompilerErrors a
= CompilationFailure (NonEmpty a)
| CompilationNoResult [a]
deriving Functor
compilerErrorMessages :: CompilerErrors a -> [a]
compilerErrorMessages (CompilationFailure x) = NonEmpty.toList x
compilerErrorMessages (CompilationNoResult x) = x
data CompilerResult a
= CompilerDone a CompilerWrite
| CompilerSnapshot Snapshot (Compiler a)
| CompilerRequire (Identifier, Snapshot) (Compiler a)
| CompilerError (CompilerErrors String)
newtype Compiler a = Compiler
{ unCompiler :: CompilerRead -> IO (CompilerResult a)
}
instance Functor Compiler where
fmap f (Compiler c) = Compiler $ \r -> do
res <- c r
return $ case res of
CompilerDone x w -> CompilerDone (f x) w
CompilerSnapshot s c' -> CompilerSnapshot s (fmap f c')
CompilerRequire i c' -> CompilerRequire i (fmap f c')
CompilerError e -> CompilerError e
{-# INLINE fmap #-}
instance Monad Compiler where
return x = compilerResult $ CompilerDone x mempty
{-# INLINE return #-}
Compiler c >>= f = Compiler $ \r -> do
res <- c r
case res of
CompilerDone x w -> do
res' <- unCompiler (f x) r
return $ case res' of
CompilerDone y w' -> CompilerDone y (w `mappend` w')
CompilerSnapshot s c' -> CompilerSnapshot s $ do
compilerTell w
c'
CompilerRequire i c' -> CompilerRequire i $ do
compilerTell w
c'
CompilerError e -> CompilerError e
CompilerSnapshot s c' -> return $ CompilerSnapshot s (c' >>= f)
CompilerRequire i c' -> return $ CompilerRequire i (c' >>= f)
CompilerError e -> return $ CompilerError e
{-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
fail = Fail.fail
{-# INLINE fail #-}
#endif
instance Fail.MonadFail Compiler where
fail = compilerThrow . return
{-# INLINE fail #-}
instance Applicative Compiler where
pure x = return x
{-# INLINE pure #-}
f <*> x = f >>= \f' -> fmap f' x
{-# INLINE (<*>) #-}
instance MonadMetadata Compiler where
getMetadata = compilerGetMetadata
getMatches = compilerGetMatches
instance MonadError [String] Compiler where
throwError = compilerThrow
catchError c = compilerCatch c . (. compilerErrorMessages)
runCompiler :: Compiler a -> CompilerRead -> IO (CompilerResult a)
runCompiler compiler read' = handle handler $ unCompiler compiler read'
where
handler :: SomeException -> IO (CompilerResult a)
handler e = return $ CompilerError $ CompilationFailure $ show e :| []
instance Alternative Compiler where
empty = compilerNoResult []
x <|> y = x `compilerCatch` (\rx -> y `compilerCatch` (\ry ->
case (rx, ry) of
(CompilationFailure xs, CompilationFailure ys) ->
compilerThrow $ NonEmpty.toList xs ++ NonEmpty.toList ys
(CompilationFailure xs, CompilationNoResult ys) ->
debug ys >> compilerThrow (NonEmpty.toList xs)
(CompilationNoResult xs, CompilationFailure ys) ->
debug xs >> compilerThrow (NonEmpty.toList ys)
(CompilationNoResult xs, CompilationNoResult ys) -> compilerNoResult $ xs ++ ys
))
where
debug = compilerDebugEntries "Hakyll.Core.Compiler.Internal: Alternative fail suppressed"
{-# INLINE (<|>) #-}
compilerResult :: CompilerResult a -> Compiler a
compilerResult x = Compiler $ \_ -> return x
{-# INLINE compilerResult #-}
compilerAsk :: Compiler CompilerRead
compilerAsk = Compiler $ \r -> return $ CompilerDone r mempty
{-# INLINE compilerAsk #-}
compilerTell :: CompilerWrite -> Compiler ()
compilerTell = compilerResult . CompilerDone ()
{-# INLINE compilerTell #-}
compilerUnsafeIO :: IO a -> Compiler a
compilerUnsafeIO io = Compiler $ \_ -> do
x <- io
return $ CompilerDone x mempty
{-# INLINE compilerUnsafeIO #-}
compilerThrow :: [String] -> Compiler a
compilerThrow = compilerResult . CompilerError .
maybe (CompilationNoResult []) CompilationFailure .
NonEmpty.nonEmpty
compilerNoResult :: [String] -> Compiler a
compilerNoResult = compilerResult . CompilerError . CompilationNoResult
compilerTry :: Compiler a -> Compiler (Either (CompilerErrors String) a)
compilerTry (Compiler x) = Compiler $ \r -> do
res <- x r
case res of
CompilerDone res' w -> return (CompilerDone (Right res') w)
CompilerSnapshot s c -> return (CompilerSnapshot s (compilerTry c))
CompilerRequire i c -> return (CompilerRequire i (compilerTry c))
CompilerError e -> return (CompilerDone (Left e) mempty)
{-# INLINE compilerTry #-}
compilerCatch :: Compiler a -> (CompilerErrors String -> Compiler a) -> Compiler a
compilerCatch (Compiler x) f = Compiler $ \r -> do
res <- x r
case res of
CompilerDone res' w -> return (CompilerDone res' w)
CompilerSnapshot s c -> return (CompilerSnapshot s (compilerCatch c f))
CompilerRequire i c -> return (CompilerRequire i (compilerCatch c f))
CompilerError e -> unCompiler (f e) r
{-# INLINE compilerCatch #-}
compilerDebugLog :: [String] -> Compiler ()
compilerDebugLog ms = do
logger <- compilerLogger <$> compilerAsk
compilerUnsafeIO $ forM_ ms $ Logger.debug logger
compilerDebugEntries :: String -> [String] -> Compiler ()
compilerDebugEntries msg = compilerDebugLog . (msg:) . map indent
where
indent = unlines . map (" "++) . lines
compilerTellDependencies :: [Dependency] -> Compiler ()
compilerTellDependencies ds = do
compilerDebugLog $ map (\d ->
"Hakyll.Core.Compiler.Internal: Adding dependency: " ++ show d) ds
compilerTell mempty {compilerDependencies = ds}
{-# INLINE compilerTellDependencies #-}
compilerTellCacheHits :: Int -> Compiler ()
compilerTellCacheHits ch = compilerTell mempty {compilerCacheHits = ch}
{-# INLINE compilerTellCacheHits #-}
compilerGetMetadata :: Identifier -> Compiler Metadata
compilerGetMetadata identifier = do
provider <- compilerProvider <$> compilerAsk
compilerTellDependencies [IdentifierDependency identifier]
compilerUnsafeIO $ resourceMetadata provider identifier
compilerGetMatches :: Pattern -> Compiler [Identifier]
compilerGetMatches pattern = do
universe <- compilerUniverse <$> compilerAsk
let matching = filterMatches pattern $ S.toList universe
set' = S.fromList matching
compilerTellDependencies [PatternDependency pattern set']
return matching