From f3633c050b8a1d006371e9d52f6239429d8e6865 Mon Sep 17 00:00:00 2001 From: Tom Robinson Date: Tue, 23 Feb 2010 15:41:52 -0800 Subject: [PATCH] Add OBJJ_AUTO_BOOTSTRAP option to disable auto-bootstrapping. Fixes flatten race condition. --- Objective-J/Bootstrap.js | 25 +++++++++++++++++-------- Tools/flatten/flatten | 35 ++++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/Objective-J/Bootstrap.js b/Objective-J/Bootstrap.js index 0bda2073c..abd530ff7 100644 --- a/Objective-J/Bootstrap.js +++ b/Objective-J/Bootstrap.js @@ -26,15 +26,19 @@ var cwd = FILE.cwd(), StaticResource.root = rootResource; #ifdef BROWSER -if (rootResource.isResolved()) + +exports.bootstrap = function() { - rootResource.nodeAtSubPath(FILE.dirname(cwd), YES); - resolveCWD(); -} -else -{ - rootResource.resolve(); - rootResource.addEventListener("resolve", resolveCWD); + if (rootResource.isResolved()) + { + rootResource.nodeAtSubPath(FILE.dirname(cwd), YES); + resolveCWD(); + } + else + { + rootResource.resolve(); + rootResource.addEventListener("resolve", resolveCWD); + } } function resolveCWD() @@ -76,4 +80,9 @@ afterDocumentLoad(function() { documentLoaded = YES; }); + + +if (typeof OBJJ_AUTO_BOOTSTRAP === "undefined" || OBJJ_AUTO_BOOTSTRAP) + exports.bootstrap(); + #endif diff --git a/Tools/flatten/flatten b/Tools/flatten/flatten index c6b24eb9e..9854df81a 100755 --- a/Tools/flatten/flatten +++ b/Tools/flatten/flatten @@ -39,19 +39,15 @@ function main(args) var root = flattener.require("objective-j").StaticResource.root; + // FIXME: shouldn't have to do this manually var components = rootPath.split("/").slice(1, -1); - print(components) var node = root; while (components.length) { - print(node._path); node = node._children[components.shift()]; } applicationRoot = node; - print("applicationRoot._children="+Object.keys(applicationRoot._children)); - print("rootPath="+rootPath); - print("applicationRoot="+applicationRoot.toString()); - print("root="+root); + print(applicationRoot.toString()); var applicationJS = flattener.buildApplicationJS(applicationRoot); @@ -59,8 +55,8 @@ function main(args) outputPath.join("Application.js").write(applicationJS); - addApplicationScriptTag(outputPath.join("index.html")); - addApplicationScriptTag(outputPath.join("index-debug.html")); + rewriteMainHTML(outputPath.join("index.html")); + rewriteMainHTML(outputPath.join("index-debug.html")); } // ObjectiveJFlattener inherits from ObjectiveJRuntimeAnalyzer @@ -95,6 +91,7 @@ ObjectiveJFlattener.prototype.buildApplicationJS = function(applicationRoot) { buffer.push("console.log(ObjectiveJ.StaticResource.root);"); buffer.push("console.log(applicationRoot);"); + buffer.push("ObjectiveJ.bootstrap();"); buffer.push("})();"); return buffer.join("\n"); @@ -150,16 +147,24 @@ ObjectiveJFlattener.prototype.serializeStaticResources = function(node, depth) { this.resourceBuffer.push("currentNode = nodeStack.pop();"); } -function addApplicationScriptTag(indexHTMLPath) { +function rewriteMainHTML(indexHTMLPath) { var indexHTML = indexHTMLPath.read(); - // attempt to append the Application.js script tag after - var newIndexHTML = indexHTML.replace(/([ \t]+)]+Objective-J.js[^>]+>(?:\s*<\/script>)?/, - '$&\n$1'); + // "$1" is the matching indentation + var scriptTagsBefore = '$1'; + var scriptTagsAfter = '$1'; + + // attempt to find Objective-J script tag and add ours + var newIndexHTML = indexHTML.replace(/([ \t]+)]+Objective-J\.js[^>]+>(?:\s*<\/script>)?/, + scriptTagsBefore+'\n$&\n'+scriptTagsAfter); if (newIndexHTML === indexHTML) { - print("Unable to automatically add Application.js script tag."); + stream.print("\0yellow(Warning: Unable to automatically modify "+indexHTMLPath + "\0)"); + stream.print("\nAdd the following before the Objective-J script tag:"); + stream.print(scriptTagsBefore.replace(/\$1/g, " ")); + stream.print("\nAdd the following after the Objective-J script tag:"); + stream.print(scriptTagsAfter.replace(/\$1/g, " ")); + } else { + indexHTMLPath.write(newIndexHTML); } - - indexHTMLPath.write(newIndexHTML); }