From f14f52040a5377dbca229ab34674542148a4868b Mon Sep 17 00:00:00 2001 From: Eric Stolten Date: Tue, 20 Aug 2013 21:59:39 -0400 Subject: Added a watch command that will continually poll for changes and rebuild them. --- src/Hakyll/Commands.hs | 43 +++++++++++++++++++++++++++++++++++++++---- src/Hakyll/Main.hs | 3 +++ 2 files changed, 42 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Hakyll/Commands.hs b/src/Hakyll/Commands.hs index d86fd5c..6e17a29 100644 --- a/src/Hakyll/Commands.hs +++ b/src/Hakyll/Commands.hs @@ -1,4 +1,4 @@ --------------------------------------------------------------------------------- + -------------------------------------------------------------------------------- -- | Implementation of Hakyll commands: build, preview... {-# LANGUAGE CPP #-} module Hakyll.Commands @@ -9,12 +9,14 @@ module Hakyll.Commands , rebuild , server , deploy + , watch ) where -------------------------------------------------------------------------------- import System.Exit (exitWith, ExitCode) import Control.Applicative +import Control.Concurrent -------------------------------------------------------------------------------- import qualified Hakyll.Check as Check @@ -27,7 +29,7 @@ import Hakyll.Core.Util.File -------------------------------------------------------------------------------- #ifdef PREVIEW_SERVER -import Hakyll.Preview.Poll +import Hakyll.Preview.Poll (watchUpdates) import Hakyll.Preview.Server #endif @@ -61,14 +63,36 @@ clean conf = do preview :: Configuration -> Verbosity -> Rules a -> Int -> IO () #ifdef PREVIEW_SERVER preview conf verbosity rules port = do + watch' conf verbosity rules (server conf port) +#else +preview _ _ _ _ = previewServerDisabled +#endif + + +-------------------------------------------------------------------------------- +-- | Watch and recompile for changes +watch :: Configuration -> Verbosity -> Rules a -> IO () +watch conf verbosity rules = watch' conf verbosity rules (return ()) + +watch' :: Configuration -> Verbosity -> Rules a -> IO () -> IO () +#ifdef PREVIEW_SERVER +watch' conf verbosity rules action = do watchUpdates conf update - server conf port + _ <- forkIO (action) + putStrLn "Press to quit watching." + loop where update = do (_, ruleSet) <- run conf verbosity rules return $ rulesPattern ruleSet + + loop = do + line <- getLine + if line == "" + then putStrLn "Quitting..." + else loop #else -preview _ _ _ _ = previewServerDisabled +watch' _ _ _ _ = watchServerDisabled #endif @@ -110,4 +134,15 @@ previewServerDisabled = , "enable it, set the flag to True and recompile Hakyll." , "Alternatively, use an external tool to serve your site directory." ] + +watchServerDisabled :: IO () +watchServerDisabled = + mapM_ putStrLn + [ "WATCH SERVER" + , "" + , "The watch server is not enabled in the version of Hakyll. To" + , "enable it, set the flag to True and recompile Hakyll." + , "Alternatively, use an external tool to serve your site directory." + ] + #endif diff --git a/src/Hakyll/Main.hs b/src/Hakyll/Main.hs index 7e50418..efa6d99 100644 --- a/src/Hakyll/Main.hs +++ b/src/Hakyll/Main.hs @@ -49,6 +49,7 @@ hakyllWith conf rules = do Preview _ p -> Commands.preview conf verbosity' rules p Rebuild _ -> Commands.rebuild conf verbosity' rules >>= exitWith Server _ _ -> Commands.server conf (port args') + Watch _ -> Commands.watch conf verbosity' rules -------------------------------------------------------------------------------- @@ -67,6 +68,7 @@ data HakyllArgs | Preview {verbose :: Bool, port :: Int} | Rebuild {verbose :: Bool} | Server {verbose :: Bool, port :: Int} + | Watch {verbose :: Bool} deriving (Data, Typeable, Show) @@ -84,6 +86,7 @@ hakyllArgs = modes , (Rebuild $ verboseFlag def) &= help "Clean and build again" , (Server (verboseFlag def) (portFlag 8000)) &= help "Start a preview server" + , (Watch (verboseFlag def) &= help "Autocompile on changes") ] &= help "Hakyll static site compiler" &= program progName -- cgit v1.2.3 From f01b64164954603466f746cfc9412c7cfac3b443 Mon Sep 17 00:00:00 2001 From: Eric Stolten Date: Sun, 25 Aug 2013 22:49:50 -0400 Subject: 1. Added the WATCH_SERVER flag that is enabled by default 1. Added an argument to watch, --no-server which will disable the embedded server. 1. Added a deprecation message to the preview mode --- hakyll.cabal | 22 ++++++++++++++++++++++ src/Hakyll/Commands.hs | 39 +++++++++++++++++++++------------------ src/Hakyll/Main.hs | 30 ++++++++++++++++++------------ 3 files changed, 61 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/hakyll.cabal b/hakyll.cabal index b23763a..0e570a7 100644 --- a/hakyll.cabal +++ b/hakyll.cabal @@ -78,6 +78,10 @@ Flag previewServer Description: Include the preview server Default: True +Flag watchServer + Description: Include the watch server + Default: True + Flag checkExternal Description: Include external link checking Default: True @@ -177,6 +181,15 @@ Library Hakyll.Preview.Poll Hakyll.Preview.Server + If flag(watchServer) + Build-depends: + fsnotify >= 0.0.6 && < 0.1, + system-filepath >= 0.4.6 && <= 0.5 + Cpp-options: + -DWATCH_SERVER + Other-modules: + Hakyll.Preview.Poll + If flag(checkExternal) Build-depends: http-conduit >= 1.8 && < 1.10, @@ -254,6 +267,15 @@ Test-suite hakyll-tests Hakyll.Preview.Poll Hakyll.Preview.Server + If flag(watchServer) + Build-depends: + fsnotify >= 0.0.6 && < 0.1, + system-filepath >= 0.4.6 && <= 0.5 + Cpp-options: + -DWATCH_SERVER + Other-modules: + Hakyll.Preview.Poll + If flag(checkExternal) Build-depends: http-conduit >= 1.8 && < 1.10, diff --git a/src/Hakyll/Commands.hs b/src/Hakyll/Commands.hs index 6e17a29..0601909 100644 --- a/src/Hakyll/Commands.hs +++ b/src/Hakyll/Commands.hs @@ -28,8 +28,11 @@ import Hakyll.Core.Runtime import Hakyll.Core.Util.File -------------------------------------------------------------------------------- -#ifdef PREVIEW_SERVER +#ifdef WATCH_SERVER import Hakyll.Preview.Poll (watchUpdates) +#endif + +#ifdef PREVIEW_SERVER import Hakyll.Preview.Server #endif @@ -62,8 +65,9 @@ clean conf = do -- | Preview the site preview :: Configuration -> Verbosity -> Rules a -> Int -> IO () #ifdef PREVIEW_SERVER -preview conf verbosity rules port = do - watch' conf verbosity rules (server conf port) +preview _ _ _ _ = mapM_ putStrLn [ "The preview command has been deprecated." + , "Use the watch command for recompilation and serving." + , "You can disable the server with watch --no-server"] #else preview _ _ _ _ = previewServerDisabled #endif @@ -71,31 +75,28 @@ preview _ _ _ _ = previewServerDisabled -------------------------------------------------------------------------------- -- | Watch and recompile for changes -watch :: Configuration -> Verbosity -> Rules a -> IO () -watch conf verbosity rules = watch' conf verbosity rules (return ()) -watch' :: Configuration -> Verbosity -> Rules a -> IO () -> IO () -#ifdef PREVIEW_SERVER -watch' conf verbosity rules action = do +watch :: Configuration -> Verbosity -> Int -> Bool -> Rules a -> IO () +#ifdef WATCH_SERVER +watch conf verbosity port runServer rules = do watchUpdates conf update - _ <- forkIO (action) - putStrLn "Press to quit watching." + _ <- forkIO (server') loop where update = do (_, ruleSet) <- run conf verbosity rules return $ rulesPattern ruleSet - loop = do - line <- getLine - if line == "" - then putStrLn "Quitting..." - else loop + server' :: IO () + server' = if runServer + then (server conf port) + else (return ()) + + loop = threadDelay 100000 >> loop #else -watch' _ _ _ _ = watchServerDisabled +watch _ _ _ _ _ = watchServerDisabled #endif - -------------------------------------------------------------------------------- -- | Rebuild the site rebuild :: Configuration -> Verbosity -> Rules a -> IO ExitCode @@ -134,7 +135,9 @@ previewServerDisabled = , "enable it, set the flag to True and recompile Hakyll." , "Alternatively, use an external tool to serve your site directory." ] +#endif +#ifndef WATCH_SERVER watchServerDisabled :: IO () watchServerDisabled = mapM_ putStrLn @@ -144,5 +147,5 @@ watchServerDisabled = , "enable it, set the flag to True and recompile Hakyll." , "Alternatively, use an external tool to serve your site directory." ] - #endif + diff --git a/src/Hakyll/Main.hs b/src/Hakyll/Main.hs index efa6d99..b8e0444 100644 --- a/src/Hakyll/Main.hs +++ b/src/Hakyll/Main.hs @@ -41,15 +41,15 @@ hakyllWith conf rules = do if internal_links args' then Check.InternalLinks else Check.All case args' of - Build _ -> Commands.build conf verbosity' rules >>= exitWith - Check _ _ -> Commands.check conf verbosity' check' - Clean _ -> Commands.clean conf - Deploy _ -> Commands.deploy conf >>= exitWith - Help _ -> showHelp - Preview _ p -> Commands.preview conf verbosity' rules p - Rebuild _ -> Commands.rebuild conf verbosity' rules >>= exitWith - Server _ _ -> Commands.server conf (port args') - Watch _ -> Commands.watch conf verbosity' rules + Build _ -> Commands.build conf verbosity' rules >>= exitWith + Check _ _ -> Commands.check conf verbosity' check' + Clean _ -> Commands.clean conf + Deploy _ -> Commands.deploy conf >>= exitWith + Help _ -> showHelp + Preview _ p -> Commands.preview conf verbosity' rules p + Rebuild _ -> Commands.rebuild conf verbosity' rules >>= exitWith + Server _ _ -> Commands.server conf (port args') + Watch _ p s -> Commands.watch conf verbosity' p (not s) rules -------------------------------------------------------------------------------- @@ -68,7 +68,7 @@ data HakyllArgs | Preview {verbose :: Bool, port :: Int} | Rebuild {verbose :: Bool} | Server {verbose :: Bool, port :: Int} - | Watch {verbose :: Bool} + | Watch {verbose :: Bool, port :: Int, no_server :: Bool } deriving (Data, Typeable, Show) @@ -82,11 +82,12 @@ hakyllArgs = modes , (Deploy $ verboseFlag def) &= help "Upload/deploy your site" , (Help $ verboseFlag def) &= help "Show this message" &= auto , (Preview (verboseFlag def) (portFlag 8000)) &= - help "Start a preview server and autocompile on changes" + help "[Deprecated] Please use the watch command" , (Rebuild $ verboseFlag def) &= help "Clean and build again" , (Server (verboseFlag def) (portFlag 8000)) &= help "Start a preview server" - , (Watch (verboseFlag def) &= help "Autocompile on changes") + , (Watch (verboseFlag def) (portFlag 8000) (noServerFlag False) &= + help "Autocompile on changes and start a preview server") ] &= help "Hakyll static site compiler" &= program progName @@ -96,6 +97,11 @@ verboseFlag x = x &= help "Run in verbose mode" {-# INLINE verboseFlag #-} +-------------------------------------------------------------------------------- +noServerFlag :: Data a => a -> a +noServerFlag x = x &= help "Disable the built-in web server" +{-# INLINE noServerFlag #-} + -------------------------------------------------------------------------------- portFlag :: Data a => a -> a portFlag x = x &= help "Port to listen on" -- cgit v1.2.3 From 866a282f19592e9ef4f0b6d3bc44be2dd2b3e08c Mon Sep 17 00:00:00 2001 From: Eric Stolten Date: Wed, 28 Aug 2013 13:11:44 -0400 Subject: * Preview now shows the deprecation message and calls watch. * Details on how to disable the server in the command description. --- src/Hakyll/Commands.hs | 17 +++++++++-------- src/Hakyll/Main.hs | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/Hakyll/Commands.hs b/src/Hakyll/Commands.hs index 0601909..7951f4e 100644 --- a/src/Hakyll/Commands.hs +++ b/src/Hakyll/Commands.hs @@ -65,9 +65,13 @@ clean conf = do -- | Preview the site preview :: Configuration -> Verbosity -> Rules a -> Int -> IO () #ifdef PREVIEW_SERVER -preview _ _ _ _ = mapM_ putStrLn [ "The preview command has been deprecated." - , "Use the watch command for recompilation and serving." - , "You can disable the server with watch --no-server"] +preview conf verbosity rules port = do + deprecatedMessage + watch conf verbosity port True rules + where + deprecatedMessage = mapM_ putStrLn [ "The preview command has been deprecated." + , "Use the watch command for recompilation and serving." + ] #else preview _ _ _ _ = previewServerDisabled #endif @@ -87,12 +91,9 @@ watch conf verbosity port runServer rules = do (_, ruleSet) <- run conf verbosity rules return $ rulesPattern ruleSet - server' :: IO () - server' = if runServer - then (server conf port) - else (return ()) - loop = threadDelay 100000 >> loop + + server' = if runServer then server conf port else return () #else watch _ _ _ _ _ = watchServerDisabled #endif diff --git a/src/Hakyll/Main.hs b/src/Hakyll/Main.hs index b8e0444..4b30939 100644 --- a/src/Hakyll/Main.hs +++ b/src/Hakyll/Main.hs @@ -87,7 +87,7 @@ hakyllArgs = modes , (Server (verboseFlag def) (portFlag 8000)) &= help "Start a preview server" , (Watch (verboseFlag def) (portFlag 8000) (noServerFlag False) &= - help "Autocompile on changes and start a preview server") + help "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server.") ] &= help "Hakyll static site compiler" &= program progName -- cgit v1.2.3