hakyll/src/Hakyll/Preview/Poll.hs

44 lines
1.6 KiB
Haskell
Raw Normal View History

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.Monad (void)
import Filesystem.Path.CurrentOS (decodeString, encodeString)
import System.Directory (getCurrentDirectory)
import System.FilePath (makeRelative)
2013-03-30 15:24:20 +00:00
import System.FSNotify (startManagerConf, watchTree,
Event(..), WatchConfig(..))
2012-12-29 08:53:59 +00:00
--------------------------------------------------------------------------------
import Hakyll.Core.Configuration
2011-02-21 12:35:20 +00:00
2012-12-29 08:53:59 +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 16:07:16 +00:00
previewPoll conf update = do
_ <- update
manager <- startManagerConf (Debounce 0.1)
wDir <- getCurrentDirectory
watchTree manager path (predicate wDir) (\_ -> void update)
2011-02-21 12:35:20 +00:00
where
path = decodeString $ providerDirectory conf
predicate wDir evt
| isRemove evt = False
| otherwise = not $ shouldIgnoreFile conf (relativeEventPath wDir evt)
relativeEventPath :: FilePath -> Event -> FilePath
relativeEventPath b evt = makeRelative b $ encodeString $ evtPath evt
where
evtPath (Added p _) = p
evtPath (Modified p _) = p
evtPath (Removed p _) = p
isRemove :: Event -> Bool
isRemove (Removed _ _) = True
isRemove _ = False