hakyll/src/Hakyll/Main.hs

107 lines
3.9 KiB
Haskell
Raw Normal View History

2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
2011-02-03 10:34:00 +00:00
-- | Module providing the main hakyll function and command-line argument parsing
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
2011-02-03 10:34:00 +00:00
module Hakyll.Main
( hakyll
, hakyllWith
) where
2011-02-03 13:18:09 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
import System.Console.CmdArgs
import qualified System.Console.CmdArgs.Explicit as CA
import System.Environment (getProgName)
import System.IO.Unsafe (unsafePerformIO)
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
import qualified Hakyll.Commands as Commands
import qualified Hakyll.Core.Configuration as Config
import qualified Hakyll.Core.Logger as Logger
2012-11-24 09:56:19 +00:00
import Hakyll.Core.Rules
2011-02-03 10:34:00 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
2011-02-03 10:34:00 +00:00
-- | This usualy is the function with which the user runs the hakyll compiler
2012-11-13 18:03:58 +00:00
hakyll :: Rules a -> IO ()
hakyll = hakyllWith Config.defaultConfiguration
2011-02-03 10:34:00 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
2011-02-03 10:34:00 +00:00
-- | A variant of 'hakyll' which allows the user to specify a custom
-- configuration
hakyllWith :: Config.Configuration -> Rules a -> IO ()
2011-05-27 19:00:59 +00:00
hakyllWith conf rules = do
args' <- cmdArgs hakyllArgs
2012-11-24 09:56:19 +00:00
-- Overwrite conf based on args
let conf' = conf
{ Config.verbosity =
if verbose args' then Logger.Debug else Config.verbosity conf
}
2012-12-29 09:41:05 +00:00
case args' of
Build _ -> Commands.build conf' rules
Check _ -> Commands.check conf'
Clean _ -> Commands.clean conf'
Deploy _ -> Commands.deploy conf'
Help _ -> showHelp
Preview _ p -> Commands.preview conf' rules p
Rebuild _ -> Commands.rebuild conf' rules
Server _ _ -> Commands.server conf' (port args')
2011-02-03 13:18:09 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
2011-02-03 13:18:09 +00:00
-- | Show usage information.
showHelp :: IO ()
showHelp = print $ CA.helpText [] CA.HelpFormatOne $ cmdArgsMode hakyllArgs
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
data HakyllArgs
= Build {verbose :: Bool}
| Check {verbose :: Bool}
| Clean {verbose :: Bool}
| Deploy {verbose :: Bool}
| Help {verbose :: Bool}
| Preview {verbose :: Bool, port :: Int}
| Rebuild {verbose :: Bool}
| Server {verbose :: Bool, port :: Int}
deriving (Data, Typeable, Show)
2011-02-10 19:05:50 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
hakyllArgs :: HakyllArgs
hakyllArgs = modes
[ (Build $ verboseFlag def) &= help "Generate the site"
, (Check $ verboseFlag def) &= help "Validate the site output"
, (Clean $ verboseFlag def) &= help "Clean up and remove cache"
, (Deploy $ verboseFlag def) &= help "Upload/deploy your site"
, (Help $ verboseFlag def) &= help "Show this message" &= auto
, (Preview (verboseFlag def) (portFlag 8000)) &=
help "Start a preview server and autocompile on changes"
, (Rebuild $ verboseFlag def) &= help "Clean and build again"
, (Server (verboseFlag def) (portFlag 8000)) &=
help "Start a preview server"
] &= help "Hakyll static site compiler" &= program progName
2011-02-03 13:18:09 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
verboseFlag :: Data a => a -> a
verboseFlag x = x &= help "Run in verbose mode"
{-# INLINE verboseFlag #-}
2011-06-15 06:53:47 +00:00
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
portFlag :: Data a => a -> a
portFlag x = x &= help "Port to listen on"
{-# INLINE portFlag #-}
2012-11-24 09:56:19 +00:00
--------------------------------------------------------------------------------
-- | This is necessary because not everyone calls their program the same...
progName :: String
progName = unsafePerformIO getProgName
{-# NOINLINE progName #-}