blob: 7dd266bab6e23791c4d77cc92815501a84b7ba22 (
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
|
--------------------------------------------------------------------------------
module Hakyll.Preview.Poll
( watchUpdates
) where
--------------------------------------------------------------------------------
import Control.Concurrent.MVar (newMVar, putMVar, takeMVar)
import Control.Monad (when)
import Filesystem.Path.CurrentOS (decodeString, encodeString)
import System.Directory (canonicalizePath)
import System.FilePath (pathSeparators)
import System.FSNotify (Event (..), WatchConfig (..),
startManagerConf, watchTree)
--------------------------------------------------------------------------------
import Hakyll.Core.Configuration
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
--------------------------------------------------------------------------------
-- | A thread that watches for updates in a 'providerDirectory' and recompiles
-- a site as soon as any changes occur
watchUpdates :: Configuration -> IO Pattern -> IO ()
watchUpdates conf update = do
let providerDir = decodeString $ providerDirectory conf
lock <- newMVar ()
pattern <- update
fullProviderDir <- canonicalizePath $ providerDirectory conf
manager <- startManagerConf (Debounce 0.1)
let allowed event = do
-- Absolute path of the changed file. This must be inside provider
-- dir, since that's the only dir we're watching.
let path = eventPath event
relative = dropWhile (`elem` pathSeparators) $
drop (length fullProviderDir) path
identifier = fromFilePath relative
shouldIgnore <- shouldIgnoreFile conf path
return $ not shouldIgnore && matches pattern identifier
watchTree manager providerDir (not . isRemove) $ \event -> do
() <- takeMVar lock
allowed' <- allowed event
when allowed' $ update >> return ()
putMVar lock ()
--------------------------------------------------------------------------------
eventPath :: Event -> FilePath
eventPath evt = encodeString $ evtPath evt
where
evtPath (Added p _) = p
evtPath (Modified p _) = p
evtPath (Removed p _) = p
--------------------------------------------------------------------------------
isRemove :: Event -> Bool
isRemove (Removed _ _) = True
isRemove _ = False
|