3 " build-custom.sh [app_location] [path_to_jsb3]",
6 " - [app_location] can either be an URL to the application's page or a local HTML file's path",
9 " - build-custom.sh /path/to/my/app.html /path/to/my/app/app.jsb3",
10 " - build-custom.sh http://localhost/path/to/my/app.html /path/to/my/app/app.jsb3"
13 if (phantom.args.length === 0 || phantom.args[0] === "--help") {
14 console.log(help.join("\n"));
18 function cleanPath(path) {
19 return path.replace(/\/\.\.\//g, '/');
22 function parseArguments() {
23 var args = { targets: [] },
27 for (i = 0, ln = phantom.args.length; i < ln; i++) {
28 arg = phantom.args[i];
31 if (!arg.match(/^-{1,2}([^-])/i)) {
41 if ((match = arg.match(/^-(\w)$/i)) || (match = arg.match(/^--(\w+)$/i))) {
44 else if (match = arg.match(/^--([\w]+)=(.*)$/i)) {
45 args[match[1]] = match[2];
47 else if (match = arg.match(/^-([\w]+)$/i)) {
48 match[1].split('').forEach(function(a) {
53 args.targets.push(arg);
64 function getRelativePath(from, to) {
68 for (i = 0, ln = from.length; i < ln; i++) {
69 if (from[i] === to[i]) {
77 if (root.length === 0) {
81 from = from.substring(root.length);
82 to = to.substring(root.length);
83 match = to.match(/\//g);
92 for (i = 0; i < ln; i++) {
99 function navigateObject(object, target) {
101 originalTarget = target,
102 expect = function(expected) {
103 if (typeof expected === 'string') {
104 var ln = expected.length;
106 if (target.substring(0, ln) === expected) {
107 target = target.slice(ln);
115 var result = target.match(expected);
117 if (result !== null) {
118 target = target.slice(result[0].length);
124 push = function(property) {
125 if (!ret.hasOwnProperty(property)) {
126 throw new Error("Invalid target property name " + property);
131 name, bracket, dot, quote;
133 while (target.length > 0) {
134 name = expect(/^[\w]+/i);
141 bracket = expect(/^\[/);
143 if (bracket !== null) {
144 quote = expect(/^'|"/);
146 push(expect(new RegExp('^[^\\]' + (quote ? quote[0] : '') + ']+', 'i')));
148 if (quote !== null) {
160 push(expect(/^[\w]+/i));
166 throw new Error("Malformed target: '" + originalTarget + "', failed parsing from: '" + target + "'");
172 var args = parseArguments(),
173 writeTarget = args.target || args.t || 'builds[0].files',
174 verbose = !!args.verbose || !!args.v,
175 appLocation = args.targets[0],
176 jsb3Path = args.targets[1],
177 jsb3Content, jsb3Object, targetObject,
178 path, pathParts, fileName;
181 jsb3Content = phantomfs.readFile(jsb3Path);
182 jsb3Object = JSON.parse(jsb3Content);
184 throw new Error("Failed parsing JSB file: " + jsb3Path + ". Please make sure the file exists and its content is valid");
187 targetObject = navigateObject(jsb3Object, writeTarget);
188 targetObject.length = 0;
190 if (phantom.state.length === 0) {
191 phantom.state = 'build';
192 phantom.open(appLocation);
194 var currentLocation = window.location.href;
196 if (typeof Ext === 'undefined') {
197 console.log("[ERROR] Ext is not defined, please verify that the library is loaded properly on the application's page");
201 Ext.onReady(function() {
202 Ext.Loader.history.forEach(function(item) {
203 path = Ext.Loader.getPath(item);
204 path = getRelativePath(path, currentLocation);
205 pathParts = path.split('/');
206 fileName = pathParts.pop();
207 path = pathParts.join('/');
213 targetObject.push({path: path, name: fileName});
216 jsb3Content = JSON.stringify(jsb3Object, null, 4);
219 console.log(jsb3Content);
222 phantom.fs.writeFile(jsb3Path, jsb3Content);