2013-04-04 09:47:50 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2012-12-29 08:53:59 +00:00
|
|
|
module Hakyll.Preview.Poll
|
2013-03-31 11:47:47 +00:00
|
|
|
( watchUpdates
|
2011-02-21 12:35:20 +00:00
|
|
|
) where
|
|
|
|
|
2013-04-04 09:47:50 +00:00
|
|
|
|
2012-12-29 08:53:59 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2013-04-04 09:47:50 +00:00
|
|
|
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)
|
|
|
|
|
2012-12-29 08:53:59 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Hakyll.Core.Configuration
|
2013-04-04 09:47:50 +00:00
|
|
|
import Hakyll.Core.Identifier
|
|
|
|
import Hakyll.Core.Identifier.Pattern
|
2011-02-21 12:35:20 +00:00
|
|
|
|
2013-03-30 14:28:23 +00:00
|
|
|
|
2012-12-29 08:53:59 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2013-03-31 11:47:47 +00:00
|
|
|
-- | A thread that watches for updates in a 'providerDirectory' and recompiles
|
|
|
|
-- a site as soon as any changes occur
|
2013-04-04 09:47:50 +00:00
|
|
|
watchUpdates :: Configuration -> IO Pattern -> IO ()
|
2013-03-31 11:47:47 +00:00
|
|
|
watchUpdates conf update = do
|
2013-04-04 09:47:50 +00:00
|
|
|
let providerDir = decodeString $ providerDirectory conf
|
|
|
|
lock <- newMVar ()
|
|
|
|
pattern <- update
|
|
|
|
fullProviderDir <- canonicalizePath $ providerDirectory conf
|
|
|
|
manager <- startManagerConf (Debounce 0.1)
|
2013-03-30 14:28:23 +00:00
|
|
|
|
2013-04-04 09:47:50 +00:00
|
|
|
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
|
2013-03-31 11:47:47 +00:00
|
|
|
|
2013-04-04 09:47:50 +00:00
|
|
|
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 ()
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2013-04-03 10:31:27 +00:00
|
|
|
eventPath :: Event -> FilePath
|
|
|
|
eventPath evt = encodeString $ evtPath evt
|
2013-03-30 16:38:31 +00:00
|
|
|
where
|
2013-04-04 09:47:50 +00:00
|
|
|
evtPath (Added p _) = p
|
2013-03-30 16:38:31 +00:00
|
|
|
evtPath (Modified p _) = p
|
2013-04-04 09:47:50 +00:00
|
|
|
evtPath (Removed p _) = p
|
2013-03-30 14:28:23 +00:00
|
|
|
|
2013-03-31 11:47:47 +00:00
|
|
|
|
2013-04-04 09:47:50 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2013-03-30 14:28:23 +00:00
|
|
|
isRemove :: Event -> Bool
|
|
|
|
isRemove (Removed _ _) = True
|
2013-04-04 09:47:50 +00:00
|
|
|
isRemove _ = False
|