module Hakyll.Serve.Main
(
hakyllServe
, hakyllServeWith
, HakyllServeConfiguration(..)
,
hscHakyllConfiguration
, hscServeConfiguration
) where
import Options.Applicative
import System.Environment (getArgs, withArgs)
import Control.Lens (makeLenses)
import Data.Default (Default, def)
import Hakyll (defaultConfiguration, hakyllWith)
import Hakyll.Core.Configuration (Configuration)
import Hakyll.Core.Rules (Rules)
import Options.Applicative.Arrows
import Safe (tailDef)
import Network.Wai.Serve.Main (serve)
import Network.Wai.Serve.Types (ServeConfiguration (..))
data HakyllServeConfiguration = HakyllServeConfiguration
{ _hscHakyllConfiguration :: Configuration
, _hscServeConfiguration :: ServeConfiguration
}
instance Default HakyllServeConfiguration where
def = HakyllServeConfiguration
{ _hscHakyllConfiguration = defaultConfiguration
, _hscServeConfiguration = def
}
data HakyllServeCommand
= HakyllCommand HakyllCommandOptions
| ServeCommand
deriving (Show)
data HakyllCommandOptions = HakyllCommandOptions String Bool Bool Bool
deriving (Show)
data HakyllServeOptions = HakyllServeOptions
{ hsCommand :: HakyllServeCommand
} deriving (Show)
hakyllServeOptions :: Parser HakyllServeOptions
hakyllServeOptions = runA $ proc () -> do
cmd <- (asA . hsubparser) hakyllServeCommand -< ()
A helper -< HakyllServeOptions cmd
hakyllServeCommand :: Mod CommandFields HakyllServeCommand
hakyllServeCommand
= command "hakyll"
(info hakyllCommandOptions (progDesc "Access hakyll commands"))
<> command "serve"
(info (pure ServeCommand) (progDesc "Start a server hosting the site"))
hakyllCommandOptions :: Parser HakyllServeCommand
hakyllCommandOptions = HakyllCommand
<$> (HakyllCommandOptions
<$> strArgument (metavar "SUBCOMMAND")
<*> switch (long "verbose")
<*> switch (long "internal-links")
<*> switch (long "no-server"))
pinfo :: ParserInfo HakyllServeOptions
pinfo = info hakyllServeOptions
$ progDesc "Hakyll static site compiler and server"
hakyllServeWith :: HakyllServeConfiguration -> Rules a -> IO ()
hakyllServeWith conf rules = do
r <- execParser pinfo
case hsCommand r of
HakyllCommand _ -> do
args <- getArgs
withArgs (tailDef [] args)
$ hakyllWith (_hscHakyllConfiguration conf) rules
ServeCommand -> serve (_hscServeConfiguration conf)
hakyllServe :: Rules a -> IO ()
hakyllServe = hakyllServeWith def
makeLenses ''HakyllServeConfiguration