flatten: build nodes relative to cwd

This commit is contained in:
Tom Robinson committed 2010-02-17 14:57:33 -08:00
1 parent 96ee92695c
commit 7f31c0632d
2 files changed
+65 -19

No files matched your search

+2
View File
@@ -378,4 +378,6 @@ exports.includePaths = function()
return global.OBJJ_INCLUDE_PATHS || ["Frameworks", "Frameworks/Debug"];
}
StaticResource.cwd = FILE.cwd();
exports.StaticResource = StaticResource;
+63 -19
View File
@@ -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]+)<script[^>]+Objective-J.js[^>]+>(?:\s*<\/script>)?/,
'$&\n$1<script src = "Application.js" type = "text/javascript"></script>');
if (newIndexHTML === indexHTML) {
print("Unable to automatically add Application.js script tag.");
}
indexHTMLPath.write(newIndexHTML);
}