@@ -1,3 +1,5 @@
1+{-# LANGUAGE CPP #-}
2+
13--------------------------------------------------------------------------------
24module Hakyll.Preview.Poll
35 ( watchUpdates
@@ -6,13 +8,22 @@ module Hakyll.Preview.Poll
68
79--------------------------------------------------------------------------------
810import Control.Concurrent.MVar (newMVar, putMVar, takeMVar)
9−import Control.Monad (when)
11+import Control.Monad (when, void)
1012import Filesystem.Path.CurrentOS (decodeString, encodeString)
1113import System.Directory (canonicalizePath)
12−import System.FilePath (pathSeparators)
14+import System.FilePath (pathSeparators, (</>))
1315import System.FSNotify (Event (..), WatchConfig (..),
1416 startManagerConf, watchTree)
1517
18+#ifdef mingw32_HOST_OS
19+import System.IO (IOMode(ReadMode), Handle, openFile,
20+ hClose)
21+import System.IO.Error (isPermissionError)
22+import Control.Concurrent (threadDelay)
23+import Control.Exception (IOException, throw, try)
24+import System.Exit (exitFailure)
25+import System.Directory (doesFileExist)
26+#endif
1627
1728--------------------------------------------------------------------------------
1829import Hakyll.Core.Configuration
@@ -45,9 +56,38 @@ watchUpdates conf update = do
4556 watchTree manager providerDir (not . isRemove) $ \event -> do
4657 () <- takeMVar lock
4758 allowed' <- allowed event
48− when allowed' $ update >> return ()
59+ when allowed' $ update' event (encodeString providerDir)
4960 putMVar lock ()
61+ where
62+#ifndef mingw32_HOST_OS
63+ update' _ _ = void update
64+#else
65+ update' event provider = do
66+ let path = provider </> eventPath event
67+ -- on windows, a 'Modified' event is also sent on file deletion
68+ fileExists <- doesFileExist path
69+
70+ when fileExists . void $ waitOpen path ReadMode (\_ -> update) 10
5071
72+ -- continuously attempts to open the file in between sleep intervals
73+ -- handler is run only once it is able to open the file
74+ waitOpen :: FilePath -> IOMode -> (Handle -> IO r) -> Integer -> IO r
75+ waitOpen _ _ _ 0 = do
76+ putStrLn "[ERROR] Failed to retrieve modified file for regeneration"
77+ exitFailure
78+ waitOpen path mode handler retries = do
79+ res <- try $ openFile path mode :: IO (Either IOException Handle)
80+ case res of
81+ Left ex -> if isPermissionError ex
82+ then do
83+ threadDelay 100000
84+ waitOpen path mode handler (retries - 1)
85+ else throw ex
86+ Right h -> do
87+ handled <- handler h
88+ hClose h
89+ return handled
90+#endif
5191
5292--------------------------------------------------------------------------------
5393eventPath :: Event -> FilePath