@@ -1,6 +1,7 @@
1--------------------------------------------------------------------------------
1--------------------------------------------------------------------------------
2-- | Module providing the main hakyll function and command-line argument parsing
2-- | Module providing the main hakyll function and command-line argument parsing
3−{-# LANGUAGE CPP #-}
3+{-# LANGUAGE CPP #-}
4+{-# LANGUAGE DeriveDataTypeable #-}
4module Hakyll.Main
5module Hakyll.Main
6 , hakyllWith
7 , hakyllWith
@@ -8,171 +9,98 @@ module Hakyll.Main
10--------------------------------------------------------------------------------
11--------------------------------------------------------------------------------
11−import Control.Monad (when)
12+import System.Console.CmdArgs
12−import System.Directory (doesDirectoryExist,
13+import qualified System.Console.CmdArgs.Explicit as CA
13− removeDirectoryRecursive)
14+import System.Environment (getProgName)
14−import System.Environment (getArgs, getProgName)
15−import System.Exit (exitWith)
15+import System.IO.Unsafe (unsafePerformIO)
16−import System.Process (system)
19--------------------------------------------------------------------------------
18--------------------------------------------------------------------------------
20−import qualified Hakyll.Check as Check
19+import qualified Hakyll.Commands as Commands
21−import Hakyll.Core.Configuration
20+import qualified Hakyll.Core.Configuration as Config
21+import qualified Hakyll.Core.Logger as Logger
22import Hakyll.Core.Rules
22import Hakyll.Core.Rules
23−import Hakyll.Core.Runtime
26−--------------------------------------------------------------------------------
28−import Control.Applicative ((<$>))
29−import Control.Concurrent (forkIO)
30−import qualified Data.Set as S
31−import Hakyll.Core.Identifier
32−import Hakyll.Core.Rules.Internal
33−import Hakyll.Preview.Poll
34−import Hakyll.Preview.Server
38--------------------------------------------------------------------------------
25--------------------------------------------------------------------------------
39-- | This usualy is the function with which the user runs the hakyll compiler
26-- | This usualy is the function with which the user runs the hakyll compiler
40hakyll :: Rules a -> IO ()
27hakyll :: Rules a -> IO ()
41−hakyll = hakyllWith defaultConfiguration
28+hakyll = hakyllWith Config.defaultConfiguration
44--------------------------------------------------------------------------------
31--------------------------------------------------------------------------------
45-- | A variant of 'hakyll' which allows the user to specify a custom
32-- | A variant of 'hakyll' which allows the user to specify a custom
46-- configuration
33-- configuration
47−hakyllWith :: Configuration -> Rules a -> IO ()
34+hakyllWith :: Config.Configuration -> Rules a -> IO ()
48hakyllWith conf rules = do
35hakyllWith conf rules = do
49− args <- getArgs
36+ args' <- cmdArgs hakyllArgs
51− ["build"] -> build conf rules
52− ["check"] -> check conf
53− ["clean"] -> clean conf
55− ["preview"] -> preview conf rules 8000
56− ["preview", p] -> preview conf rules (read p)
57− ["rebuild"] -> rebuild conf rules
58− ["server"] -> server conf 8000
59− ["server", p] -> server conf (read p)
60− ["deploy"] -> deploy conf
64−--------------------------------------------------------------------------------
66−build :: Configuration -> Rules a -> IO ()
72−--------------------------------------------------------------------------------
38+ -- Overwrite conf based on args
73−-- | Run the checker and exit
39+ let conf' = conf
74−check :: Configuration -> IO ()
40+ { Config.verbosity =
75−check config = Check.check config >>= exitWith
41+ if verbose args' then Logger.Debug else Config.verbosity conf
78−--------------------------------------------------------------------------------
45+ Build _ -> Commands.build conf' rules
79−-- | Remove the output directories
46+ Check _ -> Commands.check conf'
80−clean :: Configuration -> IO ()
47+ Clean _ -> Commands.clean conf'
81−clean conf = do
48+ Deploy _ -> Commands.deploy conf'
82− remove $ destinationDirectory conf
49+ Help _ -> showHelp
83− remove $ storeDirectory conf
50+ Preview _ p -> Commands.preview conf' rules p
84− where
51+ Rebuild _ -> Commands.rebuild conf' rules
85− remove dir = do
52+ Server _ _ -> Commands.server conf' (port args')
86− putStrLn $ "Removing " ++ dir ++ "..."
87− exists <- doesDirectoryExist dir
88− when exists $ removeDirectoryRecursive dir
91--------------------------------------------------------------------------------
55--------------------------------------------------------------------------------
92-- | Show usage information.
56-- | Show usage information.
93−help :: IO ()
57+showHelp :: IO ()
94−help = do
58+showHelp = print $ CA.helpText [] CA.HelpFormatOne $ cmdArgsMode hakyllArgs
99− , "This is a Hakyll site generator program. You should always"
100− , "run it from the project root directory."
104− , name ++ " build Generate the site"
105− , name ++ " clean Clean up and remove cache"
106− , name ++ " help Show this message"
107− , name ++ " preview [port] Run a server and autocompile"
108− , name ++ " rebuild Clean up and build again"
109− , name ++ " server [port] Run a local test server"
110− , name ++ " deploy Upload/deploy your site"
114−#ifndef PREVIEW_SERVER
115− previewServerDisabled
119--------------------------------------------------------------------------------
61--------------------------------------------------------------------------------
120−-- | Preview the site
62+data HakyllArgs
121−preview :: Configuration -> Rules a -> Int -> IO ()
63+ = Build {verbose :: Bool}
122−#ifdef PREVIEW_SERVER
64+ | Check {verbose :: Bool}
123−preview conf rules port = do
65+ | Clean {verbose :: Bool}
124− -- Fork a thread polling for changes
66+ | Deploy {verbose :: Bool}
125− _ <- forkIO $ previewPoll conf update
67+ | Help {verbose :: Bool}
126−
68+ | Preview {verbose :: Bool, port :: Int}
127− -- Run the server in the main thread
69+ | Rebuild {verbose :: Bool}
128− server conf port
70+ | Server {verbose :: Bool, port :: Int}
129− where
71+ deriving (Data, Typeable, Show)
130− update = map toFilePath . S.toList . rulesResources <$> run conf rules
132−preview _ _ _ = previewServerDisabled
136--------------------------------------------------------------------------------
74--------------------------------------------------------------------------------
137−-- | Rebuild the site
75+hakyllArgs :: HakyllArgs
138−rebuild :: Configuration -> Rules a -> IO ()
76+hakyllArgs = modes
139−rebuild conf rules = do
77+ [ (Build $ verboseFlag def) &= help "Generate the site"
140− clean conf
78+ , (Check $ verboseFlag def) &= help "Validate the site output"
141− build conf rules
79+ , (Clean $ verboseFlag def) &= help "Clean up and remove cache"
80+ , (Deploy $ verboseFlag def) &= help "Upload/deploy your site"
81+ , (Help $ verboseFlag def) &= help "Show this message" &= auto
82+ , (Preview (verboseFlag def) (portFlag 8000)) &=
83+ help "Start a preview server and autocompile on changes"
84+ , (Rebuild $ verboseFlag def) &= help "Clean and build again"
85+ , (Server (verboseFlag def) (portFlag 8000)) &=
86+ help "Start a preview server"
87+ ] &= help "Hakyll static site compiler" &= program progName
144--------------------------------------------------------------------------------
90--------------------------------------------------------------------------------
145−-- | Start a server
91+verboseFlag :: Data a => a -> a
146−server :: Configuration -> Int -> IO ()
92+verboseFlag x = x &= help "Run in verbose mode"
147−#ifdef PREVIEW_SERVER
93+{-# INLINE verboseFlag #-}
148−server conf port = do
149− let destination = destinationDirectory conf
150− staticServer destination preServeHook port
152− preServeHook _ = return ()
154−server _ _ = previewServerDisabled
158--------------------------------------------------------------------------------
96--------------------------------------------------------------------------------
159−-- | Upload the site
97+portFlag :: Data a => a -> a
160−deploy :: Configuration -> IO ()
98+portFlag x = x &= help "Port to listen on"
161−deploy conf = do
99+{-# INLINE portFlag #-}
162− _ <- system $ deployCommand conf
166--------------------------------------------------------------------------------
102--------------------------------------------------------------------------------
167−-- | Print a warning message about the preview serving not being enabled
103+-- | This is necessary because not everyone calls their program the same...
168−#ifndef PREVIEW_SERVER
104+progName :: String
169−previewServerDisabled :: IO ()
105+progName = unsafePerformIO getProgName
170−previewServerDisabled =
106+{-# NOINLINE progName #-}
174− , "The preview server is not enabled in the version of Hakyll. To"
175− , "enable it, set the flag to True and recompile Hakyll."
176− , "Alternatively, use an external tool to serve your site directory."