summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/Compiler/Internal.hs
blob: 16863f81c92b64a8d7182cf302ade74a2169b9e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
--------------------------------------------------------------------------------
-- | Internally used compiler module
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Hakyll.Core.Compiler.Internal
    ( Dependencies
    , DependencyEnvironment (..)
    , CompilerEnvironment (..)
    , Throwing
    , CompilerM (..)
    , Compiler (..)
    , runCompilerJob
    , runCompilerDependencies
    , fromJob
    , fromDependencies
    , fromDependency
    ) where


--------------------------------------------------------------------------------
import           Control.Applicative          (Alternative (..), Applicative,
                                               pure, (<$>), (<*>))
import           Control.Arrow
import           Control.Category             (Category, id, (.))
import           Control.Monad                (liftM2, (<=<))
import           Control.Monad.Error          (ErrorT, catchError, runErrorT,
                                               throwError)
import           Control.Monad.Reader         (Reader, ReaderT, ask, runReader,
                                               runReaderT)
import           Data.Set                     (Set)
import qualified Data.Set                     as S
import           Prelude                      hiding (id, (.))


--------------------------------------------------------------------------------
import           Hakyll.Core.Identifier
import           Hakyll.Core.Logger
import           Hakyll.Core.ResourceProvider
import           Hakyll.Core.Routes
import           Hakyll.Core.Store
import           Hakyll.Core.Util.Arrow


--------------------------------------------------------------------------------
-- | A set of dependencies
type Dependencies = Set (Identifier ())


--------------------------------------------------------------------------------
-- | Environment in which the dependency analyzer runs
data DependencyEnvironment = DependencyEnvironment
    { -- | Target identifier
      dependencyIdentifier :: Identifier ()
    , -- | List of available identifiers we can depend upon
      dependencyUniverse   :: [Identifier ()]
    }


--------------------------------------------------------------------------------
-- | Environment in which a compiler runs
data CompilerEnvironment = CompilerEnvironment
    { -- | Target identifier
      compilerIdentifier       :: Identifier ()
    , -- | Resource provider
      compilerResourceProvider :: ResourceProvider
    , -- | List of all known identifiers
      compilerUniverse         :: [Identifier ()]
    , -- | Site routes
      compilerRoutes           :: Routes
    , -- | Compiler store
      compilerStore            :: Store
    , -- | Flag indicating if the underlying resource was modified
      compilerResourceModified :: Bool
    , -- | Logger
      compilerLogger           :: Logger
    }


--------------------------------------------------------------------------------
-- | A calculation possibly throwing an error
type Throwing a = Either String a


--------------------------------------------------------------------------------
-- | The compiler monad
newtype CompilerM a = CompilerM
    { unCompilerM :: ErrorT String (ReaderT CompilerEnvironment IO) a
    } deriving (Monad, Functor, Applicative)


--------------------------------------------------------------------------------
-- | The compiler arrow
data Compiler a b = Compiler
    { compilerDependencies :: Reader DependencyEnvironment Dependencies
    , compilerJob          :: a -> CompilerM b
    }


--------------------------------------------------------------------------------
instance Functor (Compiler a) where
    fmap f (Compiler d j) = Compiler d $ fmap f . j
    {-# INLINE fmap #-}


--------------------------------------------------------------------------------
instance Applicative (Compiler a) where
    pure = fromJob . const . return
    {-# INLINE pure #-}

    Compiler d1 j1 <*> Compiler d2 j2 =
        Compiler (liftM2 S.union d1 d2) $ \x -> j1 x <*> j2 x
    {-# INLINE (<*>) #-}


--------------------------------------------------------------------------------
instance Alternative (Compiler a) where
    empty = fromJob $ const $ CompilerM $
        throwError "Hakyll.Core.Compiler.Internal: empty alternative"

    Compiler d1 j1 <|> Compiler d2 j2 =
        Compiler (liftM2 S.union d1 d2) $ \x -> CompilerM $
            catchError (unCompilerM $ j1 x) (\_ -> unCompilerM $ j2 x)
    {-# INLINE (<|>) #-}


--------------------------------------------------------------------------------
instance Category Compiler where
    id = Compiler (return S.empty) return
    {-# INLINE id #-}

    Compiler d1 j1 . Compiler d2 j2 =
        Compiler (liftM2 S.union d1 d2) (j1 <=< j2)
    {-# INLINE (.) #-}


--------------------------------------------------------------------------------
instance Arrow Compiler where
    arr f = fromJob (return . f)
    {-# INLINE arr #-}

    first (Compiler d j) = Compiler d $ \(x, y) -> do
        x' <- j x
        return (x', y)
    {-# INLINE first #-}

    second (Compiler d j) = Compiler d $ \(x, y) -> do
        y' <- j y
        return (x, y')
    {-# INLINE second #-}

    Compiler d1 j1 *** Compiler d2 j2 =
        Compiler (liftM2 S.union d1 d2) $ \(x, y) -> do
            x' <- j1 x
            y' <- j2 y
            return (x', y')
    {-# INLINE (***) #-}

    Compiler d1 j1 &&& Compiler d2 j2 =
        Compiler (liftM2 S.union d1 d2) $ \x -> do
            y1 <- j1 x
            y2 <- j2 x
            return (y1, y2)
    {-# INLINE (&&&) #-}


--------------------------------------------------------------------------------
instance ArrowChoice Compiler where
    left (Compiler d j) = Compiler d $ \e -> case e of
        Left l  -> Left  <$> j l
        Right r -> Right <$> return r
    {-# INLINE left #-}

    Compiler d1 j1 ||| Compiler d2 j2 = Compiler (liftM2 S.union d1 d2) $
        \e -> case e of Left x  -> j1 x; Right y -> j2 y
    {-# INLINE (|||) #-}


--------------------------------------------------------------------------------
instance ArrowMap Compiler where
    mapA (Compiler d j) = Compiler d $ mapM j
    {-# INLINE mapA #-}


--------------------------------------------------------------------------------
-- | Run a compiler, yielding the resulting target
runCompilerJob :: Compiler () a     -- ^ Compiler to run
               -> Identifier ()     -- ^ Target identifier
               -> ResourceProvider  -- ^ Resource provider
               -> [Identifier ()]   -- ^ Universe
               -> Routes            -- ^ Route
               -> Store             -- ^ Store
               -> Bool              -- ^ Was the resource modified?
               -> Logger            -- ^ Logger
               -> IO (Throwing a)   -- ^ Result
runCompilerJob compiler id' provider universe route store modified logger =
    runReaderT (runErrorT $ unCompilerM $ compilerJob compiler ()) env
  where
    env = CompilerEnvironment
            { compilerIdentifier       = id'
            , compilerResourceProvider = provider
            , compilerUniverse         = universe
            , compilerRoutes           = route
            , compilerStore            = store
            , compilerResourceModified = modified
            , compilerLogger           = logger
            }


--------------------------------------------------------------------------------
runCompilerDependencies :: Compiler () a
                        -> Identifier ()
                        -> [Identifier ()]
                        -> Dependencies
runCompilerDependencies compiler identifier universe =
    runReader (compilerDependencies compiler) env
  where
    env = DependencyEnvironment
            { dependencyIdentifier = identifier
            , dependencyUniverse   = universe
            }


--------------------------------------------------------------------------------
fromJob :: (a -> CompilerM b) -> Compiler a b
fromJob = Compiler $ return S.empty
{-# INLINE fromJob #-}


--------------------------------------------------------------------------------
fromDependencies :: (Identifier () -> [Identifier ()] -> [Identifier ()])
                 -> Compiler b b
fromDependencies collectDeps = flip Compiler return $ do
    DependencyEnvironment identifier universe <- ask
    return $ S.fromList $ collectDeps identifier universe


--------------------------------------------------------------------------------
-- | Wait until another compiler has finished before running this compiler
fromDependency :: Identifier a -> Compiler b b
fromDependency = fromDependencies . const . const . return . castIdentifier