From 7f31c0632dcb44f702efa06491488fe446c415d8 Mon Sep 17 00:00:00 2001 From: Tom Robinson Date: Wed, 17 Feb 2010 14:24:59 -0800 Subject: [PATCH] flatten: build nodes relative to cwd --- Objective-J/StaticResource.js | 2 + Tools/flatten/flatten | 82 +++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/Objective-J/StaticResource.js b/Objective-J/StaticResource.js index 322e727bf..95d5bd603 100644 --- a/Objective-J/StaticResource.js +++ b/Objective-J/StaticResource.js @@ -378,4 +378,6 @@ exports.includePaths = function() return global.OBJJ_INCLUDE_PATHS || ["Frameworks", "Frameworks/Debug"]; } +StaticResource.cwd = FILE.cwd(); + exports.StaticResource = StaticResource; diff --git a/Tools/flatten/flatten b/Tools/flatten/flatten index a69d630e6..d7eeb74f3 100755 --- a/Tools/flatten/flatten +++ b/Tools/flatten/flatten @@ -24,10 +24,10 @@ function main(args) parser.printUsage(options); return; } - + var rootPath = FILE.path(options.args[0]).join("").absolute(); var outputPath = FILE.path(options.args[1]).join("").absolute(); - + var analyzer = new ObjectiveJRuntimeAnalyzer(rootPath); analyzer.setIncludePaths([rootPath.join("Frameworks")]); @@ -35,52 +35,96 @@ function main(args) analyzer.load(rootPath.join("main.j")); analyzer.finishLoading(); - - var rootNode = analyzer.require("objective-j").StaticResource.root; - print("rootNode="+rootNode); + + var root = analyzer.require("objective-j").StaticResource.root; + + 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); + + var applicationJS = buildApplicationJS(applicationRoot); + + FILE.copyTree(rootPath, outputPath); + + outputPath.join("Application.js").write(applicationJS); + + addApplicationScriptTag(outputPath.join("index.html")); + addApplicationScriptTag(outputPath.join("index-debug.html")); +} + +function buildApplicationJS(applicationRoot) { + print(applicationRoot); var buffer = []; buffer.push("(function(){"); buffer.push("var nodeStack = [];"); + buffer.push("var applicationRoot = StaticResource.root.nodeAtSubPath(StaticResource.cwd, YES);"); buffer.push("var currentNode = null;"); - buffer.push("var newNode;") - serializeStaticResources(buffer, rootNode); + buffer.push("var newNode;"); + serializeStaticResources(buffer, applicationRoot); + buffer.push("console.log(StaticResource.root);"); + buffer.push("console.log(applicationRoot);"); buffer.push("})();"); - + var code = buffer.join("\n"); - + print(code); - rootPath.join("Application.js").write(code); + + return code; } function serializeStaticResources(buffer, node, depth) { depth = depth || 0; - + //print(Array(depth+1).join(" ") + node); - + // not the root node: - if (node.parent()) { + if (depth > 0) { buffer.push("newNode = new StaticResource(" + JSON.stringify(node.name()) + ", currentNode, " + JSON.stringify(node.type()) + ", " + JSON.stringify(node.isResolved()) + ");"); } else { - buffer.push("newNode = StaticResource.root;"); + buffer.push("newNode = applicationRoot;"); } - + if (node.contents()) buffer.push("newNode._contents = " + JSON.stringify(node.contents()) + ";"); - + if (!node.children()) return; - + buffer.push("nodeStack.push(currentNode);"); buffer.push("currentNode = newNode;"); - + for (var name in node.children()) { serializeStaticResources(buffer, node.children()[name], depth+1); } - + buffer.push("currentNode = nodeStack.pop();"); } + +function addApplicationScriptTag(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'); + + if (newIndexHTML === indexHTML) { + print("Unable to automatically add Application.js script tag."); + } + + indexHTMLPath.write(newIndexHTML); +}