hakyll/src/Hakyll/Main.hs

112 lines
3.1 KiB
Haskell
Raw Normal View History

2011-02-03 10:34:00 +00:00
-- | Module providing the main hakyll function and command-line argument parsing
--
module Hakyll.Main
( hakyll
, hakyllWith
) where
2011-05-27 19:00:59 +00:00
import Control.Applicative ((<$>))
2011-02-10 19:05:50 +00:00
import Control.Concurrent (forkIO)
2011-02-18 17:15:52 +00:00
import Control.Monad (when)
2011-02-03 13:18:09 +00:00
import System.Environment (getProgName, getArgs)
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
2011-05-27 19:00:59 +00:00
import qualified Data.Set as S
2011-02-03 13:18:09 +00:00
2011-02-03 10:34:00 +00:00
import Hakyll.Core.Configuration
2011-05-27 19:00:59 +00:00
import Hakyll.Core.Resource
2011-02-03 10:34:00 +00:00
import Hakyll.Core.Run
import Hakyll.Core.Rules
import Hakyll.Core.Rules.Internal
2011-02-23 09:11:55 +00:00
import Hakyll.Web.Preview.Poll
import Hakyll.Web.Preview.Server
2011-02-03 10:34:00 +00:00
-- | This usualy is the function with which the user runs the hakyll compiler
--
2011-05-29 10:30:33 +00:00
hakyll :: RulesM a -> IO ()
2011-02-03 13:18:09 +00:00
hakyll = hakyllWith defaultHakyllConfiguration
2011-02-03 10:34:00 +00:00
-- | A variant of 'hakyll' which allows the user to specify a custom
-- configuration
--
2011-05-29 10:30:33 +00:00
hakyllWith :: HakyllConfiguration -> RulesM a -> IO ()
2011-05-27 19:00:59 +00:00
hakyllWith conf rules = do
2011-02-03 13:18:09 +00:00
args <- getArgs
case args of
2011-05-27 19:00:59 +00:00
["build"] -> build conf rules
["clean"] -> clean conf
2011-02-03 13:18:09 +00:00
["help"] -> help
2011-05-27 19:00:59 +00:00
["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)
2011-02-03 13:18:09 +00:00
_ -> help
-- | Build the site
--
2011-05-29 10:30:33 +00:00
build :: HakyllConfiguration -> RulesM a -> IO ()
2011-05-27 19:00:59 +00:00
build conf rules = do
_ <- run conf rules
return ()
2011-02-03 13:18:09 +00:00
-- | Remove the output directories
--
clean :: HakyllConfiguration -> IO ()
2011-05-27 19:00:59 +00:00
clean conf = do
remove $ destinationDirectory conf
remove $ storeDirectory conf
2011-02-03 13:18:09 +00:00
where
remove dir = do
putStrLn $ "Removing " ++ dir ++ "..."
exists <- doesDirectoryExist dir
when exists $ removeDirectoryRecursive dir
-- | Show usage information.
--
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"
]
2011-02-10 19:05:50 +00:00
-- | Preview the site
--
2011-05-29 10:30:33 +00:00
preview :: HakyllConfiguration -> RulesM a -> Int -> IO ()
2011-05-27 19:00:59 +00:00
preview conf rules port = do
2011-02-10 19:05:50 +00:00
-- Fork a thread polling for changes
2011-05-27 19:00:59 +00:00
_ <- forkIO $ previewPoll conf update
2011-02-10 19:05:50 +00:00
-- Run the server in the main thread
2011-05-27 19:00:59 +00:00
server conf port
where
update = map unResource . S.toList . rulesResources <$> run conf rules
2011-02-10 19:05:50 +00:00
2011-02-03 13:18:09 +00:00
-- | Rebuild the site
--
2011-05-29 10:30:33 +00:00
rebuild :: HakyllConfiguration -> RulesM a -> IO ()
2011-05-27 19:00:59 +00:00
rebuild conf rules = do
clean conf
build conf rules
2011-02-03 13:18:09 +00:00
-- | Start a server
--
server :: HakyllConfiguration -> Int -> IO ()
2011-05-27 19:00:59 +00:00
server conf port = do
let destination = destinationDirectory conf
2011-02-03 13:18:09 +00:00
staticServer destination preServeHook port
where
preServeHook _ = return ()