2012-12-29 08:53:59 +00:00
|
|
|
module Hakyll.Preview.Poll
|
2011-02-21 12:35:20 +00:00
|
|
|
( previewPoll
|
|
|
|
) where
|
|
|
|
|
2012-12-29 08:53:59 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Control.Concurrent (threadDelay)
|
2013-03-30 14:28:23 +00:00
|
|
|
import Control.Monad (void)
|
|
|
|
import Data.List (isPrefixOf)
|
|
|
|
import Filesystem.Path.CurrentOS (decodeString, encodeString)
|
|
|
|
import System.Directory (canonicalizePath)
|
|
|
|
import System.FSNotify (withManagerConf, watchTree, Event(..), WatchConfig(..))
|
|
|
|
import System.IO.Error (catchIOError)
|
2012-12-29 08:53:59 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Hakyll.Core.Configuration
|
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-30 14:28:23 +00:00
|
|
|
-- | A preview thread that recompiles the site when files change.
|
2012-11-13 14:10:01 +00:00
|
|
|
previewPoll :: Configuration -- ^ Configuration
|
|
|
|
-> IO [FilePath] -- ^ Updating action
|
|
|
|
-> IO () -- ^ Can block forever
|
2013-03-30 14:28:23 +00:00
|
|
|
previewPoll conf update = withManagerConf (Debounce 0.1) monitor
|
2011-02-21 12:35:20 +00:00
|
|
|
where
|
2013-03-30 14:28:23 +00:00
|
|
|
path = decodeString $ providerDirectory conf
|
|
|
|
monitor manager = do
|
|
|
|
_ <- update
|
|
|
|
ignore <- mapM getPath
|
|
|
|
[destinationDirectory, storeDirectory, tmpDirectory]
|
|
|
|
watchTree manager path (predicate ignore) (\_ -> void update)
|
|
|
|
infiniteLoop
|
|
|
|
getPath fn = catchIOError (canonicalizePath $ fn conf)
|
|
|
|
(const $ return $ fn conf)
|
|
|
|
predicate ignore evt
|
|
|
|
| isRemove evt = False
|
|
|
|
| any (flip isPrefixOf $ eventPath evt) ignore == True = False
|
|
|
|
| (ignoreFile conf) (eventPath evt) == True = False
|
|
|
|
| otherwise = True
|
|
|
|
|
|
|
|
infiniteLoop :: IO ()
|
|
|
|
infiniteLoop = do
|
|
|
|
threadDelay maxBound
|
|
|
|
infiniteLoop
|
|
|
|
|
|
|
|
eventPath :: Event -> FilePath
|
|
|
|
eventPath (Added p _) = encodeString p
|
|
|
|
eventPath (Modified p _) = encodeString p
|
|
|
|
eventPath (Removed p _) = encodeString p
|
|
|
|
|
|
|
|
isRemove :: Event -> Bool
|
|
|
|
isRemove (Removed _ _) = True
|
|
|
|
isRemove _ = False
|