on b287c82 help sign in
hakyll/src/Hakyll/Commands.hs
129 lines 4.1 kB history raw
1--------------------------------------------------------------------------------
2-- | Implementation of Hakyll commands: build, preview...
3{-# LANGUAGE CPP #-}
4module Hakyll.Commands
5 ( build
6 , check
7 , clean
8 , preview
9 , rebuild
10 , server
11 , deploy
12 ) where
15--------------------------------------------------------------------------------
16import System.Exit (ExitCode (ExitSuccess), exitWith)
17import System.Process (system)
20--------------------------------------------------------------------------------
21import qualified Hakyll.Check as Check
22import Hakyll.Core.Configuration
23import Hakyll.Core.Logger (Verbosity)
24import Hakyll.Core.Rules
25import Hakyll.Core.Runtime
26import Hakyll.Core.Util.File
29--------------------------------------------------------------------------------
30#ifdef PREVIEW_SERVER
31import Control.Concurrent (forkIO)
32import qualified Data.Set as S
33import Hakyll.Core.Identifier
34import Hakyll.Core.Rules.Internal
35import Hakyll.Preview.Poll
36import Hakyll.Preview.Server
37#endif
40--------------------------------------------------------------------------------
41-- | Build the site
42build :: Configuration -> Verbosity -> Rules a -> IO ()
43build conf verbosity rules = do
44 _ <- run conf verbosity rules
45 return ()
48--------------------------------------------------------------------------------
49-- | Run the checker and exit
50check :: Configuration -> Verbosity -> Check.Check -> IO ()
51check config verbosity check' = Check.check config verbosity check' >>= exitWith
54--------------------------------------------------------------------------------
55-- | Remove the output directories
56clean :: Configuration -> IO ()
57clean conf = do
58 remove $ destinationDirectory conf
59 remove $ storeDirectory conf
60 remove $ tmpDirectory conf
61 where
62 remove dir = do
63 putStrLn $ "Removing " ++ dir ++ "..."
64 removeDirectory dir
67--------------------------------------------------------------------------------
68-- | Preview the site
69preview :: Configuration -> Verbosity -> Rules a -> Int -> IO ()
70#ifdef PREVIEW_SERVER
71preview conf verbosity rules port = do
72 -- Run the server in a separate thread
73 _ <- forkIO $ server conf port
74 previewPoll conf update
75 where
76 update = do
77 (exitCode, ruleSet) <- run conf verbosity rules
78 case exitCode of
79 ExitSuccess -> return $ map toFilePath $ S.toList $
80 rulesResources ruleSet
81 _ -> exitWith exitCode
82#else
83preview _ _ _ _ = previewServerDisabled
84#endif
87--------------------------------------------------------------------------------
88-- | Rebuild the site
89rebuild :: Configuration -> Verbosity -> Rules a -> IO ()
90rebuild conf verbosity rules = do
91 clean conf
92 build conf verbosity rules
95--------------------------------------------------------------------------------
96-- | Start a server
97server :: Configuration -> Int -> IO ()
98#ifdef PREVIEW_SERVER
99server conf port = do
100 let destination = destinationDirectory conf
101 staticServer destination preServeHook port
102 where
103 preServeHook _ = return ()
104#else
105server _ _ = previewServerDisabled
106#endif
109--------------------------------------------------------------------------------
110-- | Upload the site
111deploy :: Configuration -> IO ()
112deploy conf = do
113 _ <- system $ deployCommand conf
114 return ()
117--------------------------------------------------------------------------------
118-- | Print a warning message about the preview serving not being enabled
119#ifndef PREVIEW_SERVER
120previewServerDisabled :: IO ()
121previewServerDisabled =
122 mapM_ putStrLn
123 [ "PREVIEW SERVER"
124 , ""
125 , "The preview server is not enabled in the version of Hakyll. To"
126 , "enable it, set the flag to True and recompile Hakyll."
127 , "Alternatively, use an external tool to serve your site directory."
128 ]
129#endif