on master help sign in
Added autocompile mode to replace preview mode.
commit6611e86ac5db5790b271f0b3e41376a0affdbb80 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 12 Feb 2010, 12:01 (17 years ago) parent732fdc9
6 files changed+47−13
hakyll.cabal+1 src/Text/Hakyll.hs+25−4 src/Text/Hakyll/File.hs+17−7 src/Text/Hakyll/Hakyll.hs+2 src/Text/Hakyll/Internal/Cache.hs+1−1 src/Text/Hakyll/Render.hs+1−1
hakyll.cabal+1
@@ -33,6 +33,7 @@ library
3333 network >= 2,
3434 mtl >= 1.1,
3535 old-locale >= 1,
36+ old-time >= 1,
3637 time >= 1,
3738 binary >= 0.5,
3839 QuickCheck >= 2
src/Text/Hakyll.hs+25−4
@@ -13,14 +13,17 @@ module Text.Hakyll
1313 , hakyllWithConfiguration
1414 ) where
1515
16−import Control.Monad.Reader (runReaderT, liftIO)
16+import Control.Monad.Reader (runReaderT, liftIO, ask)
17+import Control.Concurrent (forkIO, threadDelay)
1718import Control.Monad (when)
1819import qualified Data.Map as M
1920import System.Environment (getArgs, getProgName)
2021import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
22+import System.Time (getClockTime)
2123
2224import Network.Hakyll.SimpleServer (simpleServer)
2325import Text.Hakyll.Hakyll
26+import Text.Hakyll.File
2427
2528-- | The default hakyll configuration.
2629defaultHakyllConfiguration :: HakyllConfiguration
@@ -29,6 +32,7 @@ defaultHakyllConfiguration = HakyllConfiguration
2932 , siteDirectory = "_site"
3033 , cacheDirectory = "_cache"
3134 , enableIndexUrl = False
35+ , previewPollDelay = 1000000
3236 }
3337
3438-- | Main function to run Hakyll with the default configuration.
@@ -41,8 +45,8 @@ hakyllWithConfiguration configuration buildFunction = do
4145 args <- getArgs
4246 let f = case args of ["build"] -> buildFunction
4347 ["clean"] -> clean
44− ["preview", p] -> buildFunction >> server (read p)
45− ["preview"] -> buildFunction >> server 8000
48+ ["preview", p] -> preview buildFunction (read p)
49+ ["preview"] -> preview buildFunction 8000
4650 ["server", p] -> server (read p)
4751 ["server"] -> server 8000
4852 _ -> help
@@ -57,6 +61,23 @@ clean = do askHakyll siteDirectory >>= remove'
5761 exists <- doesDirectoryExist dir
5862 when exists $ removeDirectoryRecursive dir
5963
64+-- | Autocompile mode.
65+preview :: Hakyll () -> Integer -> Hakyll ()
66+preview buildFunction port = do
67+ buildFunction
68+ _ <- startServer
69+ liftIO getClockTime >>= run
70+ where
71+ startServer = do configuration <- ask
72+ liftIO $ forkIO $ runReaderT (server port) configuration
73+ run time = do delay <- askHakyll previewPollDelay
74+ liftIO $ threadDelay delay
75+ contents <- getRecursiveContents "."
76+ valid <- isMoreRecent time contents
77+ if valid then run time
78+ else do buildFunction
79+ liftIO getClockTime >>= run
80+
6081-- | Show usage information.
6182help :: Hakyll ()
6283help = liftIO $ do
@@ -68,7 +89,7 @@ help = liftIO $ do
6889 ++ name ++ " build Generate the site.\n"
6990 ++ name ++ " clean Clean up and remove cache.\n"
7091 ++ name ++ " help Show this message.\n"
71− ++ name ++ " preview [port] Generate site, then start a server.\n"
92+ ++ name ++ " preview [port] Run a server and autocompile.\n"
7293 ++ name ++ " server [port] Run a local test server.\n"
7394
7495-- | Start a server at the given port number.
src/Text/Hakyll/File.hs+17−7
@@ -10,12 +10,14 @@ module Text.Hakyll.File
1010 , getRecursiveContents
1111 , sortByBaseName
1212 , havingExtension
13− , isMoreRecent
1413 , directory
14+ , isMoreRecent
15+ , isFileMoreRecent
1516 ) where
1617
1718import System.Directory
1819import System.FilePath
20+import System.Time (ClockTime)
1921import Control.Monad
2022import Data.List (isPrefixOf, sortBy)
2123import Control.Monad.Reader (liftIO)
@@ -144,14 +146,22 @@ havingExtension extension = filter ((==) extension . takeExtension)
144146directory :: (FilePath -> Hakyll ()) -> FilePath -> Hakyll ()
145147directory action dir = getRecursiveContents dir >>= mapM_ action
146148
147−-- | Check if a file is newer then a number of given files.
148−isMoreRecent :: FilePath -- ^ The cached file.
149+-- | Check if a timestamp is newer then a number of given files.
150+isMoreRecent :: ClockTime -- ^ The time to check.
149151 -> [FilePath] -- ^ Dependencies of the cached file.
150152 -> Hakyll Bool
151−isMoreRecent file depends = do
153+isMoreRecent _ [] = return True
154+isMoreRecent timeStamp depends = do
155+ dependsModified <- liftIO $ mapM getModificationTime depends
156+ return (timeStamp >= maximum dependsModified)
157+
158+-- | Check if a file is newer then a number of given files.
159+isFileMoreRecent :: FilePath -- ^ The cached file.
160+ -> [FilePath] -- ^ Dependencies of the cached file.
161+ -> Hakyll Bool
162+isFileMoreRecent file depends = do
152163 exists <- liftIO $ doesFileExist file
153164 if not exists
154165 then return False
155− else do dependsModified <- liftIO $ mapM getModificationTime depends
156− fileModified <- liftIO $ getModificationTime file
157− return (fileModified >= maximum dependsModified)
166+ else do timeStamp <- liftIO $ getModificationTime file
167+ isMoreRecent timeStamp depends
src/Text/Hakyll/Hakyll.hs+2
@@ -21,6 +21,8 @@ data HakyllConfiguration = HakyllConfiguration
2121 cacheDirectory :: FilePath
2222 , -- | Enable index links.
2323 enableIndexUrl :: Bool
24+ , -- | Delay between polls in preview mode.
25+ previewPollDelay :: Int
2426 }
2527
2628-- | Our custom monad stack.
src/Text/Hakyll/Internal/Cache.hs+1−1
@@ -27,4 +27,4 @@ getFromCache = liftIO . decodeFile <=< toCache
2727
2828-- | Check if a file in the cache is more recent than a number of other files.
2929isCacheMoreRecent :: FilePath -> [FilePath] -> Hakyll Bool
30−isCacheMoreRecent file depends = toCache file >>= flip isMoreRecent depends
30+isCacheMoreRecent file depends = toCache file >>= flip isFileMoreRecent depends
src/Text/Hakyll/Render.hs+1−1
@@ -32,7 +32,7 @@ depends :: FilePath -- ^ File to be rendered or created.
3232 -> Hakyll ()
3333depends file dependencies action = do
3434 destination <- toDestination file
35− valid <- isMoreRecent destination dependencies
35+ valid <- isFileMoreRecent destination dependencies
3636 unless valid action
3737
3838-- | Render to a Page.