module Hakyll.Main
( hakyll
, hakyllWith
) where
import Control.Monad (when)
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
import System.Environment (getProgName, getArgs)
import System.Process (system)
import Hakyll.Core.Configuration
import Hakyll.Core.Run
import Hakyll.Core.Rules
#ifdef PREVIEW_SERVER
import Control.Applicative ((<$>))
import Control.Concurrent (forkIO)
import qualified Data.Set as S
import Hakyll.Core.Resource
import Hakyll.Core.Rules.Internal
import Hakyll.Web.Preview.Poll
import Hakyll.Web.Preview.Server
#endif
hakyll :: RulesM a -> IO ()
hakyll = hakyllWith defaultHakyllConfiguration
hakyllWith :: HakyllConfiguration -> RulesM a -> IO ()
hakyllWith conf rules = do
args <- getArgs
case args of
["build"] -> build conf rules
["clean"] -> clean conf
["help"] -> help
["preview"] -> preview conf rules 8000
["preview", p] -> preview conf rules (read p)
["rebuild"] -> rebuild conf rules
["server"] -> server conf 8000
["server", p] -> server conf (read p)
["deploy"] -> deploy conf
_ -> help
build :: HakyllConfiguration -> RulesM a -> IO ()
build conf rules = do
_ <- run conf rules
return ()
clean :: HakyllConfiguration -> IO ()
clean conf = do
remove $ destinationDirectory conf
remove $ storeDirectory conf
where
remove dir = do
putStrLn $ "Removing " ++ dir ++ "..."
exists <- doesDirectoryExist dir
when exists $ removeDirectoryRecursive dir
help :: IO ()
help = do
name <- getProgName
mapM_ putStrLn
[ "ABOUT"
, ""
, "This is a Hakyll site generator program. You should always"
, "run it from the project root directory."
, ""
, "USAGE"
, ""
, name ++ " build Generate the site"
, name ++ " clean Clean up and remove cache"
, name ++ " help Show this message"
, name ++ " preview [port] Run a server and autocompile"
, name ++ " rebuild Clean up and build again"
, name ++ " server [port] Run a local test server"
, name ++ " deploy Upload/deploy your site"
, ""
]
#ifndef PREVIEW_SERVER
previewServerDisabled
#endif
preview :: HakyllConfiguration -> RulesM a -> Int -> IO ()
#ifdef PREVIEW_SERVER
preview conf rules port = do
_ <- forkIO $ previewPoll conf update
server conf port
where
update = map unResource . S.toList . rulesResources <$> run conf rules
#else
preview _ _ _ = previewServerDisabled
#endif
rebuild :: HakyllConfiguration -> RulesM a -> IO ()
rebuild conf rules = do
clean conf
build conf rules
server :: HakyllConfiguration -> Int -> IO ()
#ifdef PREVIEW_SERVER
server conf port = do
let destination = destinationDirectory conf
staticServer destination preServeHook port
where
preServeHook _ = return ()
#else
server _ _ = previewServerDisabled
#endif
deploy :: HakyllConfiguration -> IO ()
deploy conf = do
_ <- system $ deployCommand conf
return ()
#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."
]
#endif