2013-08-21 01:59:39 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2012-12-31 09:25:24 +00:00
|
|
|
-- | Implementation of Hakyll commands: build, preview...
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
module Hakyll.Commands
|
|
|
|
( build
|
|
|
|
, check
|
|
|
|
, clean
|
|
|
|
, preview
|
|
|
|
, rebuild
|
|
|
|
, server
|
|
|
|
, deploy
|
2014-03-04 19:22:35 +00:00
|
|
|
, watch
|
2012-12-31 09:25:24 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2013-05-21 04:17:31 +00:00
|
|
|
import System.Exit (exitWith, ExitCode)
|
2014-03-04 19:22:35 +00:00
|
|
|
import System.IO.Error (catchIOError)
|
2013-05-21 16:01:00 +00:00
|
|
|
import Control.Applicative
|
2014-03-04 19:22:35 +00:00
|
|
|
import Control.Monad (void)
|
2013-08-21 01:59:39 +00:00
|
|
|
import Control.Concurrent
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import qualified Hakyll.Check as Check
|
|
|
|
import Hakyll.Core.Configuration
|
2012-12-31 14:16:14 +00:00
|
|
|
import Hakyll.Core.Logger (Verbosity)
|
2012-12-31 09:25:24 +00:00
|
|
|
import Hakyll.Core.Rules
|
2013-04-04 09:47:50 +00:00
|
|
|
import Hakyll.Core.Rules.Internal
|
2012-12-31 09:25:24 +00:00
|
|
|
import Hakyll.Core.Runtime
|
2013-01-06 17:33:00 +00:00
|
|
|
import Hakyll.Core.Util.File
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2013-08-26 02:49:50 +00:00
|
|
|
#ifdef WATCH_SERVER
|
2013-08-21 01:59:39 +00:00
|
|
|
import Hakyll.Preview.Poll (watchUpdates)
|
2013-08-26 02:49:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PREVIEW_SERVER
|
2012-12-31 09:25:24 +00:00
|
|
|
import Hakyll.Preview.Server
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Build the site
|
2013-05-21 16:01:00 +00:00
|
|
|
build :: Configuration -> Verbosity -> Rules a -> IO ExitCode
|
|
|
|
build conf verbosity rules = fst <$> run conf verbosity rules
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Run the checker and exit
|
2012-12-31 14:32:46 +00:00
|
|
|
check :: Configuration -> Verbosity -> Check.Check -> IO ()
|
|
|
|
check config verbosity check' = Check.check config verbosity check' >>= exitWith
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Remove the output directories
|
|
|
|
clean :: Configuration -> IO ()
|
|
|
|
clean conf = do
|
|
|
|
remove $ destinationDirectory conf
|
|
|
|
remove $ storeDirectory conf
|
2013-01-06 17:33:00 +00:00
|
|
|
remove $ tmpDirectory conf
|
2012-12-31 09:25:24 +00:00
|
|
|
where
|
|
|
|
remove dir = do
|
|
|
|
putStrLn $ "Removing " ++ dir ++ "..."
|
2013-01-06 17:33:00 +00:00
|
|
|
removeDirectory dir
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Preview the site
|
2012-12-31 14:16:14 +00:00
|
|
|
preview :: Configuration -> Verbosity -> Rules a -> Int -> IO ()
|
2012-12-31 09:25:24 +00:00
|
|
|
#ifdef PREVIEW_SERVER
|
2013-08-28 17:11:44 +00:00
|
|
|
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."
|
|
|
|
]
|
2013-08-21 01:59:39 +00:00
|
|
|
#else
|
|
|
|
preview _ _ _ _ = previewServerDisabled
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Watch and recompile for changes
|
|
|
|
|
2013-08-26 02:49:50 +00:00
|
|
|
watch :: Configuration -> Verbosity -> Int -> Bool -> Rules a -> IO ()
|
|
|
|
#ifdef WATCH_SERVER
|
|
|
|
watch conf verbosity port runServer rules = do
|
2014-03-04 19:22:35 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
_ <- forkIO $ watchUpdates conf update
|
|
|
|
#else
|
|
|
|
-- Force windows users to compile with -threaded flag, as otherwise
|
|
|
|
-- thread is blocked indefinitely.
|
|
|
|
catchIOError (void $ forkOS $ watchUpdates conf update) $ do
|
|
|
|
fail $ "Hakyll.Commands.watch: Could not start update watching " ++
|
|
|
|
"thread. Did you compile with -threaded flag?"
|
|
|
|
#endif
|
|
|
|
server'
|
2012-12-31 09:25:24 +00:00
|
|
|
where
|
2012-12-31 14:16:14 +00:00
|
|
|
update = do
|
2013-04-04 09:47:50 +00:00
|
|
|
(_, ruleSet) <- run conf verbosity rules
|
|
|
|
return $ rulesPattern ruleSet
|
2013-08-26 02:49:50 +00:00
|
|
|
loop = threadDelay 100000 >> loop
|
2014-03-04 19:22:35 +00:00
|
|
|
server' = if runServer then server conf port else loop
|
2012-12-31 09:25:24 +00:00
|
|
|
#else
|
2013-08-26 02:49:50 +00:00
|
|
|
watch _ _ _ _ _ = watchServerDisabled
|
2012-12-31 09:25:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Rebuild the site
|
2013-05-21 16:01:00 +00:00
|
|
|
rebuild :: Configuration -> Verbosity -> Rules a -> IO ExitCode
|
2013-05-21 04:17:31 +00:00
|
|
|
rebuild conf verbosity rules =
|
|
|
|
clean conf >> build conf verbosity rules
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Start a server
|
|
|
|
server :: Configuration -> Int -> IO ()
|
|
|
|
#ifdef PREVIEW_SERVER
|
|
|
|
server conf port = do
|
|
|
|
let destination = destinationDirectory conf
|
|
|
|
staticServer destination preServeHook port
|
|
|
|
where
|
|
|
|
preServeHook _ = return ()
|
|
|
|
#else
|
|
|
|
server _ _ = previewServerDisabled
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Upload the site
|
2013-05-22 12:24:26 +00:00
|
|
|
deploy :: Configuration -> IO ExitCode
|
2013-03-24 20:47:05 +00:00
|
|
|
deploy conf = deploySite conf conf
|
2012-12-31 09:25:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Print a warning message about the preview serving not being enabled
|
|
|
|
#ifndef PREVIEW_SERVER
|
|
|
|
previewServerDisabled :: IO ()
|
|
|
|
previewServerDisabled =
|
|
|
|
mapM_ putStrLn
|
|
|
|
[ "PREVIEW SERVER"
|
|
|
|
, ""
|
|
|
|
, "The preview 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."
|
|
|
|
]
|
2013-08-26 02:49:50 +00:00
|
|
|
#endif
|
2013-08-21 01:59:39 +00:00
|
|
|
|
2013-08-26 02:49:50 +00:00
|
|
|
#ifndef WATCH_SERVER
|
2013-08-21 01:59:39 +00:00
|
|
|
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."
|
|
|
|
]
|
2012-12-31 09:25:24 +00:00
|
|
|
#endif
|
2013-08-26 02:49:50 +00:00
|
|
|
|