hakyll/src/Hakyll/Preview/Poll.hs

94 lines
3.7 KiB
Haskell
Raw Normal View History

2013-05-22 08:20:43 +00:00
{-# LANGUAGE CPP #-}
2013-04-04 09:47:50 +00:00
--------------------------------------------------------------------------------
2012-12-29 08:53:59 +00:00
module Hakyll.Preview.Poll
( 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)
2013-05-22 08:20:43 +00:00
import System.FilePath (pathSeparators, (</>))
2013-04-04 09:47:50 +00:00
import System.FSNotify (Event (..), WatchConfig (..),
startManagerConf, watchTree)
2013-05-22 08:20:43 +00:00
#ifdef mingw32_HOST_OS
import System.IO (IOMode(ReadMode), Handle, openFile, hClose)
import System.IO.Error (isPermissionError)
import Control.Concurrent (threadDelay)
import Control.Exception (IOException, throw, try)
#endif
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
2012-12-29 08:53:59 +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 ()
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-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-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
2013-05-22 08:20:43 +00:00
when allowed' $ update' ((encodeString providerDir) </> (eventPath event))
2013-04-04 09:47:50 +00:00
putMVar lock ()
2013-05-22 08:20:43 +00:00
where
#ifndef mingw32_HOST_OS
update' _ = update >> return ()
#else
update' path = waitOpen path ReadMode (\_ -> update) >> return ()
2013-04-04 09:47:50 +00:00
2013-05-22 08:20:43 +00:00
-- continuously attempts to open the file in between sleep intervals
-- handler is run only once it is able to open the file
waitOpen :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
waitOpen path mode handler = do
res <- try $ openFile path mode :: IO (Either IOException Handle)
case res of
Left ex -> if isPermissionError ex
then do
threadDelay 100000
waitOpen path mode handler
else throw ex
Right h -> do
handled <- handler h
hClose h
return handled
#endif
2013-04-04 09:47:50 +00:00
--------------------------------------------------------------------------------
2013-04-03 10:31:27 +00:00
eventPath :: Event -> FilePath
eventPath evt = encodeString $ evtPath evt
where
2013-04-04 09:47:50 +00:00
evtPath (Added p _) = p
evtPath (Modified p _) = p
2013-04-04 09:47:50 +00:00
evtPath (Removed p _) = p
2013-04-04 09:47:50 +00:00
--------------------------------------------------------------------------------
isRemove :: Event -> Bool
isRemove (Removed _ _) = True
2013-04-04 09:47:50 +00:00
isRemove _ = False