on master help sign in
Merge pull request #177 from stoltene2/master

Adding a watch command

commit94d7281a2c4f21e67e2da09c6029cf60583c37e7 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 28 Aug 2013, 11:48 (13 years ago) parent015c7c5866a282
3 files changed+85−15
hakyll.cabal+22 src/Hakyll/Commands.hs+45−6 src/Hakyll/Main.hs+18−9
hakyll.cabal+22
@@ -78,6 +78,10 @@ Flag previewServer
7878 Description: Include the preview server
7979 Default: True
8080
81+Flag watchServer
82+ Description: Include the watch server
83+ Default: True
84+
8185Flag checkExternal
8286 Description: Include external link checking
8387 Default: True
@@ -177,6 +181,15 @@ Library
177181 Hakyll.Preview.Poll
178182 Hakyll.Preview.Server
179183
184+ If flag(watchServer)
185+ Build-depends:
186+ fsnotify >= 0.0.6 && < 0.1,
187+ system-filepath >= 0.4.6 && <= 0.5
188+ Cpp-options:
189+ -DWATCH_SERVER
190+ Other-modules:
191+ Hakyll.Preview.Poll
192+
180193 If flag(checkExternal)
181194 Build-depends:
182195 http-conduit >= 1.8 && < 1.10,
@@ -254,6 +267,15 @@ Test-suite hakyll-tests
254267 Hakyll.Preview.Poll
255268 Hakyll.Preview.Server
256269
270+ If flag(watchServer)
271+ Build-depends:
272+ fsnotify >= 0.0.6 && < 0.1,
273+ system-filepath >= 0.4.6 && <= 0.5
274+ Cpp-options:
275+ -DWATCH_SERVER
276+ Other-modules:
277+ Hakyll.Preview.Poll
278+
257279 If flag(checkExternal)
258280 Build-depends:
259281 http-conduit >= 1.8 && < 1.10,
src/Hakyll/Commands.hs+45−6
@@ -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+
src/Hakyll/Main.hs+18−9
@@ -41,14 +41,15 @@ hakyllWith conf rules = do
4141 if internal_links args' then Check.InternalLinks else Check.All
4242
4343 case args' of
44− Build _ -> Commands.build conf verbosity' rules >>= exitWith
45− Check _ _ -> Commands.check conf verbosity' check'
46− Clean _ -> Commands.clean conf
47− Deploy _ -> Commands.deploy conf >>= exitWith
48− Help _ -> showHelp
49− Preview _ p -> Commands.preview conf verbosity' rules p
50− Rebuild _ -> Commands.rebuild conf verbosity' rules >>= exitWith
51− Server _ _ -> Commands.server conf (port args')
44+ Build _ -> Commands.build conf verbosity' rules >>= exitWith
45+ Check _ _ -> Commands.check conf verbosity' check'
46+ Clean _ -> Commands.clean conf
47+ Deploy _ -> Commands.deploy conf >>= exitWith
48+ Help _ -> showHelp
49+ Preview _ p -> Commands.preview conf verbosity' rules p
50+ Rebuild _ -> Commands.rebuild conf verbosity' rules >>= exitWith
51+ Server _ _ -> Commands.server conf (port args')
52+ Watch _ p s -> Commands.watch conf verbosity' p (not s) rules
5253
5354
5455--------------------------------------------------------------------------------
@@ -67,6 +68,7 @@ data HakyllArgs
6768 | Preview {verbose :: Bool, port :: Int}
6869 | Rebuild {verbose :: Bool}
6970 | Server {verbose :: Bool, port :: Int}
71+ | Watch {verbose :: Bool, port :: Int, no_server :: Bool }
7072 deriving (Data, Typeable, Show)
7173
7274
@@ -80,10 +82,12 @@ hakyllArgs = modes
8082 , (Deploy $ verboseFlag def) &= help "Upload/deploy your site"
8183 , (Help $ verboseFlag def) &= help "Show this message" &= auto
8284 , (Preview (verboseFlag def) (portFlag 8000)) &=
83− help "Start a preview server and autocompile on changes"
85+ help "[Deprecated] Please use the watch command"
8486 , (Rebuild $ verboseFlag def) &= help "Clean and build again"
8587 , (Server (verboseFlag def) (portFlag 8000)) &=
8688 help "Start a preview server"
89+ , (Watch (verboseFlag def) (portFlag 8000) (noServerFlag False) &=
90+ help "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server.")
8791 ] &= help "Hakyll static site compiler" &= program progName
8892
8993
@@ -93,6 +97,11 @@ verboseFlag x = x &= help "Run in verbose mode"
9397{-# INLINE verboseFlag #-}
9498
9599
100+--------------------------------------------------------------------------------
101+noServerFlag :: Data a => a -> a
102+noServerFlag x = x &= help "Disable the built-in web server"
103+{-# INLINE noServerFlag #-}
104+
96105--------------------------------------------------------------------------------
97106portFlag :: Data a => a -> a
98107portFlag x = x &= help "Port to listen on"