on 5dba607 help sign in
hakyll/src/Hakyll/Main.hs
153 lines 4.2 kB history raw
1-- | Module providing the main hakyll function and command-line argument parsing
2--
3{-# LANGUAGE CPP #-}
4module Hakyll.Main
5 ( hakyll
6 , hakyllWith
7 ) where
9import Control.Monad (when)
10import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
11import System.Environment (getProgName, getArgs)
12import System.Process (system)
14import Hakyll.Core.Configuration
15import Hakyll.Core.Run
16import Hakyll.Core.Rules
18#ifdef PREVIEW_SERVER
19import Control.Applicative ((<$>))
20import Control.Concurrent (forkIO)
21import qualified Data.Set as S
23import Hakyll.Core.Resource
24import Hakyll.Core.Rules.Internal
25import Hakyll.Web.Preview.Poll
26import Hakyll.Web.Preview.Server
27#endif
29-- | This usualy is the function with which the user runs the hakyll compiler
30--
31hakyll :: RulesM a -> IO ()
32hakyll = hakyllWith defaultHakyllConfiguration
34-- | A variant of 'hakyll' which allows the user to specify a custom
35-- configuration
36--
37hakyllWith :: HakyllConfiguration -> RulesM a -> IO ()
38hakyllWith conf rules = do
39 args <- getArgs
40 case args of
41 ["build"] -> build conf rules
42 ["clean"] -> clean conf
43 ["help"] -> help
44 ["preview"] -> preview conf rules 8000
45 ["preview", p] -> preview conf rules (read p)
46 ["rebuild"] -> rebuild conf rules
47 ["server"] -> server conf 8000
48 ["server", p] -> server conf (read p)
49 ["deploy"] -> deploy conf
50 _ -> help
52-- | Build the site
53--
54build :: HakyllConfiguration -> RulesM a -> IO ()
55build conf rules = do
56 _ <- run conf rules
57 return ()
59-- | Remove the output directories
60--
61clean :: HakyllConfiguration -> IO ()
62clean conf = do
63 remove $ destinationDirectory conf
64 remove $ storeDirectory conf
65 where
66 remove dir = do
67 putStrLn $ "Removing " ++ dir ++ "..."
68 exists <- doesDirectoryExist dir
69 when exists $ removeDirectoryRecursive dir
71-- | Show usage information.
72--
73help :: IO ()
74help = do
75 name <- getProgName
76 mapM_ putStrLn
77 [ "ABOUT"
78 , ""
79 , "This is a Hakyll site generator program. You should always"
80 , "run it from the project root directory."
81 , ""
82 , "USAGE"
83 , ""
84 , name ++ " build Generate the site"
85 , name ++ " clean Clean up and remove cache"
86 , name ++ " help Show this message"
87 , name ++ " preview [port] Run a server and autocompile"
88 , name ++ " rebuild Clean up and build again"
89 , name ++ " server [port] Run a local test server"
90 , name ++ " deploy Upload/deploy your site"
91 , ""
92 ]
94#ifndef PREVIEW_SERVER
95 previewServerDisabled
96#endif
98-- | Preview the site
99--
100preview :: HakyllConfiguration -> RulesM a -> Int -> IO ()
101#ifdef PREVIEW_SERVER
102preview conf rules port = do
103 -- Fork a thread polling for changes
104 _ <- forkIO $ previewPoll conf update
106 -- Run the server in the main thread
107 server conf port
108 where
109 update = map unResource . S.toList . rulesResources <$> run conf rules
110#else
111preview _ _ _ = previewServerDisabled
112#endif
114-- | Rebuild the site
115--
116rebuild :: HakyllConfiguration -> RulesM a -> IO ()
117rebuild conf rules = do
118 clean conf
119 build conf rules
121-- | Start a server
122--
123server :: HakyllConfiguration -> Int -> IO ()
124#ifdef PREVIEW_SERVER
125server conf port = do
126 let destination = destinationDirectory conf
127 staticServer destination preServeHook port
128 where
129 preServeHook _ = return ()
130#else
131server _ _ = previewServerDisabled
132#endif
134-- | Upload the site
135--
136deploy :: HakyllConfiguration -> IO ()
137deploy conf = do
138 _ <- system $ deployCommand conf
139 return ()
141-- | Print a warning message about the preview serving not being enabled
142--
143#ifndef PREVIEW_SERVER
144previewServerDisabled :: IO ()
145previewServerDisabled =
146 mapM_ putStrLn
147 [ "PREVIEW SERVER"
148 , ""
149 , "The preview server is not enabled in the version of Hakyll. To"
150 , "enable it, set the flag to True and recompile Hakyll."
151 , "Alternatively, use an external tool to serve your site directory."
152 ]
153#endif