forked from cappuccino/cappuccino
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automated HTML5 cache manifest generation. Disabled by default.
- Loading branch information
Tom Robinson
committed
Apr 8, 2010
1 parent
aabb1fa
commit 9f50f08
Showing
3 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
var FILE = require("file"); | ||
var MD5 = require("md5"); | ||
|
||
var FileList = require("jake").FileList, | ||
var BundleTask = require("objective-j/jake/bundletask").BundleTask; | ||
|
||
exports.generateManifest = function(productPath, indexFilePath) | ||
{ | ||
indexFilePath = indexFilePath || FILE.join(productPath, "index.html"); | ||
|
||
if (!FILE.isFile(indexFilePath)) { | ||
print("Warning: Skipping cache manifest generation, no index file at "+indexFilePath); | ||
return; | ||
} | ||
|
||
var index = FILE.read(indexFilePath, { charset : "UTF-8" }); | ||
|
||
var manifestName = "app.manifest"; | ||
var manifestPath = FILE.join(productPath, manifestName); | ||
var manifestAttribute = 'manifest="'+manifestName+'"'; | ||
|
||
print("Generating cache manifest: " + manifestPath); | ||
|
||
var manifestOut = FILE.open(manifestPath, "w", { charset : "UTF-8" }); | ||
manifestOut.print("CACHE MANIFEST"); | ||
manifestOut.print(""); | ||
manifestOut.print("CACHE:"); | ||
|
||
var list = new FileList(FILE.join(productPath, "**", "*")); | ||
list.exclude(manifestPath); | ||
list.exclude("**/.DS_Store", "**/.htaccess"); | ||
list.exclude("**/LICENSE"); | ||
list.exclude("**/MHTML*"); | ||
list.exclude("**/CommonJS.environment/*"); | ||
|
||
// FIXME: bleh. heuristic for whether index file includes debug frameworks | ||
if (index.indexOf('"Frameworks/Debug"') < 0) | ||
list.exclude("**/Frameworks/Debug/*"); | ||
|
||
list.forEach(function(path) { | ||
if (FILE.isFile(path)) { | ||
// FIXME: check the actual sprited images file | ||
if (BundleTask.isSpritable(path)) | ||
return; | ||
|
||
// include hash of each file in comments to expire when any file changes | ||
var hash = MD5.hash(FILE.read(path, "b")).decodeToString("base16"); | ||
manifestOut.print("# " + hash); | ||
manifestOut.print(FILE.relative(productPath, path)); | ||
} | ||
}); | ||
|
||
manifestOut.print(""); | ||
manifestOut.print("NETWORK:"); | ||
manifestOut.print("*"); | ||
manifestOut.close(); | ||
|
||
// Insert "manifest" attribute in <html> tag of index file | ||
var matchTag = index.match(/<html[^>]*>/i); | ||
if (matchTag) { | ||
var htmlTag = matchTag[0]; | ||
var newHTMLTag = null; | ||
|
||
var matchAttr = htmlTag.match(/manifest\s*=\s*"([^"]*)"/i); | ||
if (matchAttr) { | ||
if (matchAttr[1] !== manifestName) { | ||
newHTMLTag = htmlTag.replace(matchAttr[0], manifestAttribute); | ||
} | ||
} else { | ||
newHTMLTag = htmlTag.replace(/>$/, " "+manifestAttribute+">"); | ||
} | ||
|
||
if (newHTMLTag) { | ||
print("Replacing html tag: \n " + htmlTag + "\nwith:\n " + newHTMLTag); | ||
var newIndex = index.replace(htmlTag, newHTMLTag); | ||
if (newIndex === index) { | ||
print("Warning: No change!"); | ||
} else { | ||
FILE.write(indexFilePath, newIndex, { charset : "UTF-8" }); | ||
} | ||
} | ||
} | ||
else { | ||
print("Warning: Couldn't find <html> tag in "+indexFilePath); | ||
} | ||
|
||
// Add Content-Type "text/cache-manifest" for manifest file to .htaccess | ||
// This allows manifests to work out of the box on Apache (if htaccess overrides are allowed) | ||
var htaccessPath = FILE.join(productPath, ".htaccess"); | ||
var htaccess = FILE.isFile(htaccessPath) ? FILE.read(htaccessPath, { charset : "UTF-8" }) : ""; | ||
|
||
var htaccessOut = FILE.open(htaccessPath, "w", { charset : "UTF-8" }); | ||
htaccessOut.print(htaccess); | ||
|
||
var openTag = "<Files "+manifestName+">"; | ||
if (htaccess.indexOf(openTag) < 0) { | ||
htaccessOut.print(""); | ||
htaccessOut.print(openTag); | ||
htaccessOut.print("\tHeader set Content-Type text/cache-manifest"); | ||
htaccessOut.print("</Files>"); | ||
} | ||
htaccessOut.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters