@@ -1,4 +1,4 @@
1−--------------------------------------------------------------------------------
1+ --------------------------------------------------------------------------------
22-- | Implementation of Hakyll commands: build, preview...
33{-# LANGUAGE CPP #-}
44module Hakyll.Commands
@@ -9,12 +9,14 @@ module Hakyll.Commands
99 , rebuild
1010 , server
1111 , deploy
12+ , watch
1213 ) where
1314
1415
1516--------------------------------------------------------------------------------
1617import System.Exit (exitWith, ExitCode)
1718import Control.Applicative
19+import Control.Concurrent
1820
1921--------------------------------------------------------------------------------
2022import qualified Hakyll.Check as Check
@@ -27,7 +29,7 @@ import Hakyll.Core.Util.File
2729
2830--------------------------------------------------------------------------------
2931#ifdef PREVIEW_SERVER
30−import Hakyll.Preview.Poll
32+import Hakyll.Preview.Poll (watchUpdates)
3133import Hakyll.Preview.Server
3234#endif
3335
@@ -61,14 +63,36 @@ clean conf = do
6163preview :: Configuration -> Verbosity -> Rules a -> Int -> IO ()
6264#ifdef PREVIEW_SERVER
6365preview conf verbosity rules port = do
66+ watch' conf verbosity rules (server conf port)
67+#else
68+preview _ _ _ _ = previewServerDisabled
69+#endif
70+
71+
72+--------------------------------------------------------------------------------
73+-- | Watch and recompile for changes
74+watch :: Configuration -> Verbosity -> Rules a -> IO ()
75+watch conf verbosity rules = watch' conf verbosity rules (return ())
76+
77+watch' :: Configuration -> Verbosity -> Rules a -> IO () -> IO ()
78+#ifdef PREVIEW_SERVER
79+watch' conf verbosity rules action = do
6480 watchUpdates conf update
65− server conf port
81+ _ <- forkIO (action)
82+ putStrLn "Press <enter> to quit watching."
83+ loop
6684 where
6785 update = do
6886 (_, ruleSet) <- run conf verbosity rules
6987 return $ rulesPattern ruleSet
88+
89+ loop = do
90+ line <- getLine
91+ if line == ""
92+ then putStrLn "Quitting..."
93+ else loop
7094#else
71−preview _ _ _ _ = previewServerDisabled
95+watch' _ _ _ _ = watchServerDisabled
7296#endif
7397
7498
@@ -110,4 +134,15 @@ previewServerDisabled =
110134 , "enable it, set the flag to True and recompile Hakyll."
111135 , "Alternatively, use an external tool to serve your site directory."
112136 ]
137+
138+watchServerDisabled :: IO ()
139+watchServerDisabled =
140+ mapM_ putStrLn
141+ [ "WATCH SERVER"
142+ , ""
143+ , "The watch server is not enabled in the version of Hakyll. To"
144+ , "enable it, set the flag to True and recompile Hakyll."
145+ , "Alternatively, use an external tool to serve your site directory."
146+ ]
147+
113148#endif