Files
cappuccino/Tools/flatten/flatten
T

131 lines
3.7 KiB
Plaintext
Executable File

#!/usr/bin/env objj
require("narwhal").ensureEngine("rhino");
@import <Foundation/Foundation.j>
@import <AppKit/AppKit.j>
@import "../press/objj-analysis-tools.j"
var FILE = require("file");
var parser = new (require("args").Parser)();
parser.usage("INPUT_PROJECT OUTPUT_PROJECT");
parser.help("Combine a Cappuccino application into a single JavaScript file.");
parser.helpful();
function main(args)
{
var options = parser.parse(args);
if (options.args.length < 2) {
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")]);
analyzer.setEnvironments(["Browser", "ObjJ"]);
analyzer.load(rootPath.join("main.j"));
analyzer.finishLoading();
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, applicationRoot);
buffer.push("console.log(StaticResource.root);");
buffer.push("console.log(applicationRoot);");
buffer.push("})();");
var code = buffer.join("\n");
print(code);
return code;
}
function serializeStaticResources(buffer, node, depth) {
depth = depth || 0;
//print(Array(depth+1).join(" ") + node);
// not the root node:
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 = 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);
}