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(-) 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