on master help sign in
Add command-line args
commit5705bb8f88529b4170ffe884c668721abe9fccea authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 3 Feb 2011, 14:18 (16 years ago) parent26c9540
1 file changed+72−2
src/Hakyll/Main.hs+72−2
src/Hakyll/Main.hs+72−2
@@ -5,17 +5,87 @@ module Hakyll.Main
55 , hakyllWith
66 ) where
77
8+import Control.Monad (when)
9+import System.Environment (getProgName, getArgs)
10+import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
11+
812import Hakyll.Core.Configuration
913import Hakyll.Core.Run
1014import Hakyll.Core.Rules
15+import Hakyll.Network.Server
1116
1217-- | This usualy is the function with which the user runs the hakyll compiler
1318--
1419hakyll :: Rules -> IO ()
15−hakyll = run defaultHakyllConfiguration
20+hakyll = hakyllWith defaultHakyllConfiguration
1621
1722-- | A variant of 'hakyll' which allows the user to specify a custom
1823-- configuration
1924--
2025hakyllWith :: HakyllConfiguration -> Rules -> IO ()
21−hakyllWith = run
26+hakyllWith configuration rules = do
27+ args <- getArgs
28+ case args of
29+ ["build"] -> build configuration rules
30+ ["clean"] -> clean configuration
31+ ["help"] -> help
32+ ["preview"] -> putStrLn "Not implemented"
33+ ["preview", p] -> putStrLn "Not implemented"
34+ ["rebuild"] -> rebuild configuration rules
35+ ["server"] -> server configuration 8000
36+ ["server", p] -> server configuration (read p)
37+ _ -> help
38+
39+-- | Build the site
40+--
41+build :: HakyllConfiguration -> Rules -> IO ()
42+build = run
43+
44+-- | Remove the output directories
45+--
46+clean :: HakyllConfiguration -> IO ()
47+clean configuration = do
48+ remove $ destinationDirectory configuration
49+ remove $ storeDirectory configuration
50+ where
51+ remove dir = do
52+ putStrLn $ "Removing " ++ dir ++ "..."
53+ exists <- doesDirectoryExist dir
54+ when exists $ removeDirectoryRecursive dir
55+
56+-- | Show usage information.
57+--
58+help :: IO ()
59+help = do
60+ name <- getProgName
61+ mapM_ putStrLn
62+ [ "ABOUT"
63+ , ""
64+ , "This is a Hakyll site generator program. You should always"
65+ , "run it from the project root directory."
66+ , ""
67+ , "USAGE"
68+ , ""
69+ , name ++ " build Generate the site"
70+ , name ++ " clean Clean up and remove cache"
71+ , name ++ " help Show this message"
72+ , name ++ " preview [port] Run a server and autocompile"
73+ , name ++ " rebuild Clean up and build again"
74+ , name ++ " server [port] Run a local test server"
75+ ]
76+
77+-- | Rebuild the site
78+--
79+rebuild :: HakyllConfiguration -> Rules -> IO ()
80+rebuild configuration rules = do
81+ clean configuration
82+ build configuration rules
83+
84+-- | Start a server
85+--
86+server :: HakyllConfiguration -> Int -> IO ()
87+server configuration port = do
88+ let destination = destinationDirectory configuration
89+ staticServer destination preServeHook port
90+ where
91+ preServeHook _ = return ()