summaryrefslogtreecommitdiff
path: root/src-inotify/Hakyll/Web
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2011-03-01 14:50:41 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2011-03-01 14:50:41 +0100
commit90b25105830d6e4b0943ab55f9317bd142533acf (patch)
tree6eefb80a8a84724e70539dd8fa449530f7b17fe0 /src-inotify/Hakyll/Web
parent8ef5a3ed0307be5d34a9564d02af3ed494f8e228 (diff)
parent8b727b994d482d593046f9b01a5c40b97c166d62 (diff)
downloadhakyll-90b25105830d6e4b0943ab55f9317bd142533acf.tar.gz
Merge branch 'hakyll3'
Conflicts: hakyll.cabal src/Text/Hakyll/Tags.hs
Diffstat (limited to 'src-inotify/Hakyll/Web')
-rw-r--r--src-inotify/Hakyll/Web/Preview/Poll.hs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src-inotify/Hakyll/Web/Preview/Poll.hs b/src-inotify/Hakyll/Web/Preview/Poll.hs
new file mode 100644
index 0000000..686f045
--- /dev/null
+++ b/src-inotify/Hakyll/Web/Preview/Poll.hs
@@ -0,0 +1,51 @@
+-- | 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
+ _ <- addWatch inotify [Modify] directory $ \e -> case e of
+ (Modified _ (Just p)) -> ifResource $ directory </> p
+ _ -> return ()
+ return ()