Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / build / build-custom-jsb3.js
1 var help =  [
2 "Usage:",
3 "   build-custom.sh [app_location] [path_to_jsb3]",
4 "",
5 "Notes:",
6 "   - [app_location] can either be an URL to the application's page or a local HTML file's path",
7 "",
8 "Examples",
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"
11 ];
12
13 if (phantom.args.length === 0 || phantom.args[0] === "--help") {
14     console.log(help.join("\n"));
15     phantom.exit();
16 }
17
18 function cleanPath(path) {
19     return path.replace(/\/\.\.\//g, '/');
20 }
21
22 function parseArguments() {
23     var args = { targets: [] },
24         key = null,
25         i, ln, arg, match;
26
27     for (i = 0, ln = phantom.args.length; i < ln; i++) {
28         arg = phantom.args[i];
29
30         if (key !== null) {
31             if (!arg.match(/^-{1,2}([^-])/i)) {
32                 args[key] = arg;
33                 key = null;
34                 continue;
35             }
36
37             args[key] = true;
38             key = null;
39         }
40
41         if ((match = arg.match(/^-(\w)$/i)) || (match = arg.match(/^--(\w+)$/i))) {
42             key = match[1];
43         }
44         else if (match = arg.match(/^--([\w]+)=(.*)$/i)) {
45             args[match[1]] = match[2];
46         }
47         else if (match = arg.match(/^-([\w]+)$/i)) {
48             match[1].split('').forEach(function(a) {
49                 args[a] = true;
50             });
51         }
52         else {
53             args.targets.push(arg);
54         }
55     }
56
57     if (key !== null) {
58         args[key] = true;
59     }
60
61     return args;
62 }
63
64 function getRelativePath(from, to) {
65     var root = '',
66         i, ln, match;
67
68     for (i = 0, ln = from.length; i < ln; i++) {
69         if (from[i] === to[i]) {
70             root += from[i];
71         }
72         else {
73             break;
74         }
75     }
76
77     if (root.length === 0) {
78         return from;
79     }
80
81     from = from.substring(root.length);
82     to = to.substring(root.length);
83     match = to.match(/\//g);
84
85     if (!match) {
86         ln = 0;
87     }
88     else {
89         ln = match.length;
90     }
91
92     for (i = 0; i < ln; i++) {
93         from = '../' + from;
94     }
95
96     return from;
97 }
98
99 function navigateObject(object, target) {
100     var ret = object,
101         originalTarget =  target,
102         expect = function(expected) {
103             if (typeof expected === 'string') {
104                 var ln = expected.length;
105
106                 if (target.substring(0, ln) === expected) {
107                     target = target.slice(ln);
108
109                     return expected;
110                 }
111
112                 return null;
113             }
114
115             var result = target.match(expected);
116
117             if (result !== null) {
118                 target = target.slice(result[0].length);
119                 return result[0];
120             }
121
122             return null;
123         },
124         push = function(property) {
125             if (!ret.hasOwnProperty(property)) {
126                 throw new Error("Invalid target property name " + property);
127             }
128
129             ret = ret[property];
130         },
131         name, bracket, dot, quote;
132
133     while (target.length > 0) {
134         name = expect(/^[\w]+/i);
135
136         if (name !== null) {
137             push(name);
138             continue;
139         }
140         else {
141             bracket = expect(/^\[/);
142
143             if (bracket !== null) {
144                 quote = expect(/^'|"/);
145
146                 push(expect(new RegExp('^[^\\]' + (quote ? quote[0] : '') + ']+', 'i')));
147
148                 if (quote !== null) {
149                     expect(quote[0]);
150                 }
151
152                 expect(/^\]/);
153
154                 continue;
155             }
156             else {
157                 dot = expect(/^\./);
158
159                 if (dot !== null) {
160                     push(expect(/^[\w]+/i));
161                     continue;
162                 }
163             }
164         }
165
166         throw new Error("Malformed target: '" + originalTarget + "', failed parsing from: '" + target + "'");
167     }
168
169     return ret;
170 }
171
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;
179
180 try {
181     jsb3Content = phantomfs.readFile(jsb3Path);
182     jsb3Object = JSON.parse(jsb3Content);
183 } catch (e) {
184     throw new Error("Failed parsing JSB file: " + jsb3Path + ". Please make sure the file exists and its content is valid");
185 }
186
187 targetObject = navigateObject(jsb3Object, writeTarget);
188 targetObject.length = 0;
189
190 if (phantom.state.length === 0) {
191   phantom.state = 'build';
192   phantom.open(appLocation);
193 } else {
194     var currentLocation = window.location.href;
195
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");
198         phantom.exit();
199     }
200
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('/');
208
209             if (path !== '') {
210                 path += '/';
211             }
212
213             targetObject.push({path: path, name: fileName});
214         });
215
216         jsb3Content = JSON.stringify(jsb3Object, null, 4);
217
218         if (verbose) {
219             console.log(jsb3Content);
220         }
221
222         phantom.fs.writeFile(jsb3Path, jsb3Content);
223         phantom.exit();
224     });
225 }
226
227
228