on master help sign in
Refine code

This patch includes several smaller changes, namely: 1. We don't use result of `update` function and likely never will, so don't bother generating it. 2. Rename watch function to better reflect what it does. 3. Never exit preview server in case of failed update.

commit259190e30bb9136aef5b06546d47306edc2ad3ee authorSimonas Kazlauskas <git@kazlauskas.me> dateSun 31 Mar 2013, 14:47 (14 years ago) parentbcc0ef8
2 files changed+13−19
src/Hakyll/Commands.hs+4−10 src/Hakyll/Preview/Poll.hs+9−9
src/Hakyll/Commands.hs+4−10
@@ -13,7 +13,7 @@ module Hakyll.Commands
1313
1414
1515--------------------------------------------------------------------------------
16−import System.Exit (ExitCode (ExitSuccess), exitWith)
16+import System.Exit (exitWith)
1717
1818
1919--------------------------------------------------------------------------------
@@ -27,9 +27,6 @@ import Hakyll.Core.Util.File
2727
2828--------------------------------------------------------------------------------
2929#ifdef PREVIEW_SERVER
30−import qualified Data.Set as S
31−import Hakyll.Core.Identifier
32−import Hakyll.Core.Rules.Internal
3330import Hakyll.Preview.Poll
3431import Hakyll.Preview.Server
3532#endif
@@ -67,15 +64,12 @@ clean conf = do
6764preview :: Configuration -> Verbosity -> Rules a -> Int -> IO ()
6865#ifdef PREVIEW_SERVER
6966preview conf verbosity rules port = do
70− previewPoll conf update
67+ watchUpdates conf update
7168 server conf port
7269 where
7370 update = do
74− (exitCode, ruleSet) <- run conf verbosity rules
75− case exitCode of
76− ExitSuccess -> return $ map toFilePath $ S.toList $
77− rulesResources ruleSet
78− _ -> exitWith exitCode
71+ _ <- run conf verbosity rules
72+ return ()
7973#else
8074preview _ _ _ _ = previewServerDisabled
8175#endif
src/Hakyll/Preview/Poll.hs+9−9
@@ -1,9 +1,8 @@
11module Hakyll.Preview.Poll
2− ( previewPoll
2+ ( watchUpdates
33 ) where
44
55--------------------------------------------------------------------------------
6−import Control.Monad (void)
76import Filesystem.Path.CurrentOS (decodeString, encodeString)
87import System.Directory (getCurrentDirectory)
98import System.FilePath (makeRelative)
@@ -16,21 +15,21 @@ import Hakyll.Core.Configuration
1615
1716
1817--------------------------------------------------------------------------------
19−-- | A preview thread that recompiles the site when files change.
20−previewPoll :: Configuration -- ^ Configuration
21− -> IO [FilePath] -- ^ Updating action
22− -> IO () -- ^ Can block forever
23−previewPoll conf update = do
18+-- | A thread that watches for updates in a 'providerDirectory' and recompiles
19+-- a site as soon as any changes occur
20+watchUpdates :: Configuration -> IO () -> IO ()
21+watchUpdates conf update = do
2422 _ <- update
2523 manager <- startManagerConf (Debounce 0.1)
26− wDir <- getCurrentDirectory
27− watchTree manager path (predicate wDir) (\_ -> void update)
24+ workingDirectory <- getCurrentDirectory
25+ watchTree manager path (predicate workingDirectory) $ const update
2826 where
2927 path = decodeString $ providerDirectory conf
3028 predicate wDir evt
3129 | isRemove evt = False
3230 | otherwise = not $ shouldIgnoreFile conf (relativeEventPath wDir evt)
3331
32+
3433relativeEventPath :: FilePath -> Event -> FilePath
3534relativeEventPath b evt = makeRelative b $ encodeString $ evtPath evt
3635 where
@@ -38,6 +37,7 @@ relativeEventPath b evt = makeRelative b $ encodeString $ evtPath evt
3837 evtPath (Modified p _) = p
3938 evtPath (Removed p _) = p
4039
40+
4141isRemove :: Event -> Bool
4242isRemove (Removed _ _) = True
4343isRemove _ = False