summaryrefslogtreecommitdiff
path: root/src/Text/Hakyll.hs
blob: d34c1c854aa29479c4ac7683abaa192d564afa3a (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
module Text.Hakyll
    ( hakyll
    ) where

import Network.Hakyll.SimpleServer (simpleServer)

import System.Environment (getArgs, getProgName)
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)

-- | Main function to run hakyll.
hakyll :: IO () -> IO ()
hakyll buildFunction = do
    args <- getArgs
    case args of []             -> build buildFunction
                 ["clean"]      -> clean
                 ["preview", p] -> build buildFunction >> server (read p)
                 ["preview"]    -> build buildFunction >> server 8000
                 ["server", p]  -> server (read p)
                 ["server"]     -> server 8000
                 _              -> help

-- | Build the site.
build :: IO () -> IO ()
build buildFunction = do putStrLn "Generating..."
                         buildFunction

-- | Clean up directories.
clean :: IO ()
clean = do remove' "_cache"
           remove' "_site"
    where remove' dir = do putStrLn $ "Removing " ++ dir ++ "..."
                           exists <- doesDirectoryExist dir
                           if exists then removeDirectoryRecursive dir
                                     else return ()

-- | Show usage information.
help :: IO ()
help = do
    name <- getProgName
    putStrLn $  "This is a hakyll site generator program. You should always\n"
             ++ "run it from the project root directory.\n"
             ++ "\n"
             ++ "Usage:\n"
             ++ name ++ "                 Generate the site.\n"
             ++ name ++ " clean           Clean up and remove cache.\n"
             ++ name ++ " help            Show this message.\n"
             ++ name ++ " preview [port]  Generate site, then start a server.\n"
             ++ name ++ " server [port]   Run a local test server.\n"

server :: Integer -> IO ()
server p = do simpleServer (fromIntegral $ p) "_site"