@@ -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
@@ -26,8 +28,11 @@ import Hakyll.Core.Runtime
2628import Hakyll.Core.Util.File
2729
2830--------------------------------------------------------------------------------
31+#ifdef WATCH_SERVER
32+import Hakyll.Preview.Poll (watchUpdates)
33+#endif
34+
2935#ifdef PREVIEW_SERVER
30−import Hakyll.Preview.Poll
3136import Hakyll.Preview.Server
3237#endif
3338
@@ -60,18 +65,39 @@ clean conf = do
6065-- | Preview the site
6166preview :: Configuration -> Verbosity -> Rules a -> Int -> IO ()
6267#ifdef PREVIEW_SERVER
63−preview conf verbosity rules port = do
68+preview conf verbosity rules port = do
69+ deprecatedMessage
70+ watch conf verbosity port True rules
71+ where
72+ deprecatedMessage = mapM_ putStrLn [ "The preview command has been deprecated."
73+ , "Use the watch command for recompilation and serving."
74+ ]
75+#else
76+preview _ _ _ _ = previewServerDisabled
77+#endif
78+
79+
80+--------------------------------------------------------------------------------
81+-- | Watch and recompile for changes
82+
83+watch :: Configuration -> Verbosity -> Int -> Bool -> Rules a -> IO ()
84+#ifdef WATCH_SERVER
85+watch conf verbosity port runServer rules = do
6486 watchUpdates conf update
65− server conf port
87+ _ <- forkIO (server')
88+ loop
6689 where
6790 update = do
6891 (_, ruleSet) <- run conf verbosity rules
6992 return $ rulesPattern ruleSet
93+
94+ loop = threadDelay 100000 >> loop
95+
96+ server' = if runServer then server conf port else return ()
7097#else
71−preview _ _ _ _ = previewServerDisabled
98+watch _ _ _ _ _ = watchServerDisabled
7299#endif
73100
74−
75101--------------------------------------------------------------------------------
76102-- | Rebuild the site
77103rebuild :: Configuration -> Verbosity -> Rules a -> IO ExitCode
@@ -111,3 +137,16 @@ previewServerDisabled =
111137 , "Alternatively, use an external tool to serve your site directory."
112138 ]
113139#endif
140+
141+#ifndef WATCH_SERVER
142+watchServerDisabled :: IO ()
143+watchServerDisabled =
144+ mapM_ putStrLn
145+ [ "WATCH SERVER"
146+ , ""
147+ , "The watch server is not enabled in the version of Hakyll. To"
148+ , "enable it, set the flag to True and recompile Hakyll."
149+ , "Alternatively, use an external tool to serve your site directory."
150+ ]
151+#endif
152+