summaryrefslogtreecommitdiff
path: root/src/Hakyll/Main.hs
blob: 74097f7f8d9ac88772e1bc5dfc4c7ddb276f7396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- | Module providing the main hakyll function and command-line argument parsing
--
module Hakyll.Main
    ( hakyll
    , hakyllWith
    ) where

import Control.Concurrent (forkIO)
import Control.Monad (when, forM_)
import System.Environment (getProgName, getArgs)
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
import qualified Data.Set as S

import Hakyll.Core.Configuration
import Hakyll.Core.Run
import Hakyll.Core.Rules
import Hakyll.Core.Rules.Internal
import Hakyll.Web.Preview.INotify
import Hakyll.Web.Preview.Server

-- | This usualy is the function with which the user runs the hakyll compiler
--
hakyll :: Rules -> IO ()
hakyll = hakyllWith defaultHakyllConfiguration

-- | A variant of 'hakyll' which allows the user to specify a custom
-- configuration
--
hakyllWith :: HakyllConfiguration -> Rules -> IO ()
hakyllWith configuration rules = do
    args <- getArgs
    case args of
        ["build"]      -> build configuration rules
        ["clean"]      -> clean configuration
        ["help"]       -> help
        ["preview"]    -> preview configuration rules 8000
        ["preview", p] -> preview configuration rules (read p)
        ["rebuild"]    -> rebuild configuration rules
        ["server"]     -> server configuration 8000
        ["server", p]  -> server configuration (read p)
        _              -> help

-- | Build the site
--
build :: HakyllConfiguration -> Rules -> IO ()
build configuration rules = do
    _ <- run configuration rules
    return ()

-- | Remove the output directories
--
clean :: HakyllConfiguration -> IO ()
clean configuration = do
    remove $ destinationDirectory configuration
    remove $ storeDirectory configuration
  where
    remove dir = do
        putStrLn $ "Removing " ++ dir ++ "..."
        exists <- doesDirectoryExist dir
        when exists $ removeDirectoryRecursive dir

-- | Show usage information.
--
help :: IO ()
help = do
    name <- getProgName
    mapM_ putStrLn
        [ "ABOUT"
        , ""
        , "This is a Hakyll site generator program. You should always"
        , "run it from the project root directory."
        , ""
        , "USAGE"
        , ""
        , name ++ " build           Generate the site"
        , name ++ " clean           Clean up and remove cache"
        , name ++ " help            Show this message"
        , name ++ " preview [port]  Run a server and autocompile"
        , name ++ " rebuild         Clean up and build again"
        , name ++ " server [port]   Run a local test server"
        ]

-- | Preview the site
--
preview :: HakyllConfiguration -> Rules -> Int -> IO ()
preview configuration rules port = do
    -- Build once, keep the rule set
    ruleSet <- run configuration rules

    -- Debug: show the resources used
    forM_ (S.toList $ rulesResources ruleSet) $ putStrLn . show

    {-
    -- Fork a thread polling for changes
    _ <- forkIO $ previewPoll configuration "." $ build configuration rules
    
    -- Run the server in the main thread
    server configuration port
    -}

-- | Rebuild the site
--
rebuild :: HakyllConfiguration -> Rules -> IO ()
rebuild configuration rules = do
    clean configuration
    build configuration rules

-- | Start a server
--
server :: HakyllConfiguration -> Int -> IO ()
server configuration port = do
    let destination = destinationDirectory configuration
    staticServer destination preServeHook port
  where
    preServeHook _ = return ()