Commit c25adc78 authored by Aral Balkan's avatar Aral Balkan

Implemented rudimentary caching for theme changes.

* If a template file is changed, a full build occurs
* If any other theme file is changed, theme is generated but
  content cache is used
* If no theme file has changed, content cache is used
  (as before)
parent 16b85dad
......@@ -62,6 +62,8 @@ class App
dataForSiteCacheDirectory: null
indicesDirectory: null
themeHasNotChanged: true
constructor: ->
# Singleton access.
......@@ -218,10 +220,21 @@ class App
themeCacheCurrentModifiedTimesFile = path.join cacheDirectory, 'current-modified-times', 'themes.json'
themeCachePreviousModifiedTimesFile = path.join cacheDirectory, 'previous-modified-times', 'themes.json'
themeCacheCurrentModifiedTimes = fs.readFileSync themeCacheCurrentModifiedTimesFile, 'utf-8'
themeCachePreviousModifiedTimes = fs.readFileSync themeCachePreviousModifiedTimesFile, 'utf-8'
return !(themeCacheCurrentModifiedTimes == themeCachePreviousModifiedTimes)
themeCacheCurrentModifiedTimes = fs.readJsonSync themeCacheCurrentModifiedTimesFile, 'utf-8'
themeCachePreviousModifiedTimes = fs.readJsonSync themeCachePreviousModifiedTimesFile, 'utf-8'
# Check if there are any template changes (if there are, this will require a full rebuild).
# If there are any changes at all, we need to generate themes
# And if there are no changes, we don’t need to generate themes.
for fileName, lastModifiedDate of themeCachePreviousModifiedTimes
if lastModifiedDate != themeCacheCurrentModifiedTimes[fileName]
# There are changes, generate themes.
@info "\t✓ Change to theme found in #{fileName}, generating theme."
@themeHasNotChanged = false
return true
@info "\t✓ No changes to theme; not generating theme."
return false
generateThemes: =>
......
......@@ -141,12 +141,23 @@ class Blockdown
themeCacheCurrentModifiedTimesFile = path.join cacheDirectory, 'current-modified-times', 'themes.json'
themeCachePreviousModifiedTimesFile = path.join cacheDirectory, 'previous-modified-times', 'themes.json'
themeCacheCurrentModifiedTimes = fs.readFileSync themeCacheCurrentModifiedTimesFile, 'utf-8'
themeCachePreviousModifiedTimes = fs.readFileSync themeCachePreviousModifiedTimesFile, 'utf-8'
if themeCacheCurrentModifiedTimes == themeCachePreviousModifiedTimes
#@info "Decide cache policy: Theme hasn’t changed, use the content cache when possible."
@useContentCache = true
themeCacheCurrentModifiedTimes = fs.readJsonSync themeCacheCurrentModifiedTimesFile, 'utf-8'
themeCachePreviousModifiedTimes = fs.readJsonSync themeCachePreviousModifiedTimesFile, 'utf-8'
# Check if there are any template changes (if there are, this will require a full rebuild),
# otherwise, we can use the cache and we won’t need to re-render the content.
@useContentCache = true
for fileName, lastModifiedDate of themeCachePreviousModifiedTimes
if (fileName.indexOf '/templates/') != -1
# This is a template, check if it has changed
if lastModifiedDate != themeCacheCurrentModifiedTimes[fileName]
# There are changes to a theme template, do not use the cache.
@info "\t✓ Theme template changed (#{fileName}), *not* using cache."
@useContentCache = false
break
# Set up the content cache if we’re using it.
if @useContentCache
contentCacheCurrentModifiedTimesFile = path.join cacheDirectory, 'current-modified-times', 'content.json'
contentCachePreviousModifiedTimesFile = path.join cacheDirectory, 'previous-modified-times', 'content.json'
......@@ -159,13 +170,8 @@ class Blockdown
@contentCurrentModifiedTimes = JSON.parse(_contentCurrentModifiedTimes)
@contentPreviousModifiedTimes = JSON.parse(_contentPreviousModifiedTimes)
console.log "\t✓ Using cache"
else
# @info "Decide cache policy: Theme has changed. Do not use content cache."
@useContentCache = false
console.log "\t✓ No changes to theme templates; using cache."
console.log "\t✓ Not using cache"
#
......@@ -223,9 +229,11 @@ class Blockdown
@copyAssets @contentAssets('site'), app.dataForSiteDirectory
.then =>
Promise.try =>
if @useContentCache # (currently implies that theme hasn’t changed)
if @useContentCache and app.themeHasNotChanged
console.log "\t✓ Theme has not changed, not copying theme assets for site."
return
else
console.log "\t✓ Copying theme assets for site."
@copyAssets @themeForSiteDataAssets(), app.dataForSiteDirectory
.then =>
Promise.try =>
......@@ -280,9 +288,11 @@ class Blockdown
@copyAssets @contentAssets('app'), app.dataForAppDirectory
.then =>
Promise.try =>
if @useContentCache # (currently implies that theme hasn’t changed)
if @useContentCache and app.themeHasNotChanged
console.log "\t✓ Theme has not changed, not copying theme assets for app."
return
else
console.log "\t✓ Copying theme assets for app."
@copyAssets @themeForAppDataAssets(), app.dataForAppDirectory
.then =>
Promise.try =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment