From 5abc3d87e234c2f92b6c5481200d1f813ca2ce6f Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 23 Feb 2011 10:11:55 +0100 Subject: Add cabal flag for inotify --- src-inotify/Hakyll/Web/Preview/Poll.hs | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src-inotify/Hakyll/Web/Preview/Poll.hs (limited to 'src-inotify/Hakyll') diff --git a/src-inotify/Hakyll/Web/Preview/Poll.hs b/src-inotify/Hakyll/Web/Preview/Poll.hs new file mode 100644 index 0000000..69370ac --- /dev/null +++ b/src-inotify/Hakyll/Web/Preview/Poll.hs @@ -0,0 +1,52 @@ +-- | Filesystem polling with an inotify backend. Works only on linux. +-- +module Hakyll.Web.Preview.Poll + ( previewPoll + ) where + +import Control.Monad (forM_, when) +import Data.Set (Set) +import qualified Data.Set as S +import System.FilePath (takeDirectory, ()) +import Data.List (isPrefixOf) + +import System.INotify + +import Hakyll.Core.Configuration +import Hakyll.Core.ResourceProvider +import Hakyll.Core.Identifier + +-- | Calls the given callback when the directory tree changes +-- +previewPoll :: HakyllConfiguration -- ^ Configuration + -> Set Resource -- ^ Resources to watch + -> IO () -- ^ Action called when something changes + -> IO () -- ^ Can block forever +previewPoll _ resources callback = do + -- Initialize inotify + inotify <- initINotify + + let -- A set of file paths + paths = S.map (toFilePath . unResource) resources + + -- A list of directories. Run it through a set so we have every + -- directory only once. + directories = S.toList $ S.map (notEmpty . takeDirectory) paths + + -- Problem: we can't add a watcher for "". So we make sure a directory + -- name is not empty + notEmpty "" = "." + notEmpty x = x + + -- Execute the callback when path is known + ifResource path = + let path' = if "./" `isPrefixOf` path then drop 2 path else path + in when (path' `S.member` paths) callback + + -- Add a watcher for every directory + forM_ directories $ \directory -> do + putStrLn $ "Adding watch for " ++ directory + _ <- addWatch inotify [Modify] directory $ \e -> case e of + (Modified _ (Just p)) -> ifResource $ directory p + _ -> return () + return () -- cgit v1.2.3