-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathmca-create.js
More file actions
executable file
·447 lines (407 loc) · 13 KB
/
mca-create.js
File metadata and controls
executable file
·447 lines (407 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//usr/bin/env node $0 $*; exit $?
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/**
The top line of this file will allow this script to be run as
a UNIX shell script, as well as being a valid Node.js program.
*/
if (typeof WScript != 'undefined') {
var shell = WScript.CreateObject("WScript.Shell");
try {
// Don't worry about passing along arguments here. It's stricly a double-click convenience.
var ret = shell.Run('cmd /c node "' + WScript.ScriptFullName + '" --pause_on_exit', 1, true);
} catch (e) {
shell.Popup('NodeJS is not installed. Please install it from http://nodejs.org');
ret = 1;
}
WScript.Quit(ret);
}
process.on('uncaughtException', function(e) {
fatal('Uncaught exception: ' + e);
});
var childProcess = require('child_process');
var fs = require('fs');
var path = require('path');
var commandLineFlags = {};
var commandLineArgs = process.argv.slice(2).filter(function(arg) {
if (arg.slice(0, 2) == '--') {
commandLineFlags[arg.slice(2)] = true;
}
return arg.slice(0, 2) != '--';
});
var origDir = process.cwd();
var isWindows = process.platform.slice(0, 3) == 'win';
var eventQueue = [];
var scriptDir = path.dirname(process.argv[1]);
var scriptName = path.basename(process.argv[1]);
var hasAndroidSdk = false;
var hasXcode = false;
function exit(code) {
if (eventQueue) {
eventQueue.length = 0;
}
if (commandLineFlags['pause_on_exit']) {
waitForKey(function() {
process.exit(code);
});
} else {
process.exit(code);
}
}
function fatal(msg) {
console.error(msg);
exit(1);
}
function exec(cmd, onSuccess, opt_onError, opt_silent) {
var onError = opt_onError || function(e) {
fatal('command failed: ' + cmd + '\n' + e);
};
if (!opt_silent) {
console.log('Running: ' + cmd);
}
childProcess.exec(cmd, function(error, stdout, stderr) {
if (error) {
onError(error);
} else {
onSuccess(stdout.trim());
}
});
}
function sudo(cmd, onSuccess, opt_onError, silent) {
if (!isWindows) {
cmd = 'sudo ' + cmd;
}
exec(cmd, onSuccess, opt_onError, silent);
}
function cordovaCmd(args) {
return '"' + process.argv[0] + '" "' + path.join(scriptDir, 'cordova-cli', 'bin', 'cordova') + '" ' + args.join(' ');
}
function chdir(d) {
d = path.resolve(scriptDir, d);
if (process.cwd() != d) {
console.log('Changing directory to: ' + d);
process.chdir(d);
}
}
function copyFile(src, dst, callback) {
var rd = fs.createReadStream(src);
var wr = fs.createWriteStream(dst);
wr.on('error', function(err) {
fatal('Copy file error: ' + err);
});
wr.on('close', callback);
rd.pipe(wr);
}
function recursiveDelete(dirPath) {
if (fs.existsSync(dirPath)) {
console.log('Deleting: ' + dirPath);
helper(dirPath);
}
function helper(dirPath) {
try {
var files = fs.readdirSync(dirPath);
} catch(e) {
return;
}
for (var i = 0; i < files.length; i++) {
var filePath = path.join(dirPath, files[i]);
fs.chmodSync(filePath, '777');
if (fs.statSync(filePath).isFile()) {
fs.unlinkSync(filePath);
} else {
helper(filePath);
}
}
fs.rmdirSync(dirPath);
}
}
function waitForKey(callback) {
console.log('press a key');
function cont(key) {
if (key == '\u0003') {
process.exit(2);
}
process.stdin.removeListener('data', cont);
process.stdin.pause();
callback();
}
process.stdin.resume();
process.stdin.setRawMode(true);
process.stdin.on('data', cont);
}
function pump() {
if (eventQueue.length) {
eventQueue.shift()(pump);
}
}
////////////////////////// INIT LOGIC //////////////////////////
function initRepoMain() {
function checkGit(callback) {
var errMsg = 'git is not installed (or not available on your PATH). Please install it from http://git-scm.com';
exec('git --version', callback, function() {
if (isWindows) {
// See if it's at the default install path.
process.env['PATH'] += ';' + path.join(process.env['ProgramFiles'], 'Git', 'bin');
exec('git --version', callback, function() {
fatal(errMsg);
}, true);
} else {
fatal(errMsg);
}
}, true);
}
function computeGitVersion(callback) {
exec('git describe --tags --long', function(stdout) {
var version = stdout.replace(/^2.5.0-.*?-/, 'dev-');
callback(version);
}, null, true);
}
function checkOutSelf(callback) {
console.log('## Checking Out mobile-chrome-apps');
// If the repo doesn't exist where the script is, then use the CWD as the checkout location.
var requiresClone = true;
// First - try the directory of the script.
if (scriptDir.slice(0, 2) != '\\\\') {
process.chdir(scriptDir);
requiresClone = !fs.existsSync('.git');
}
// Next - try the CWD.
if (requiresClone) {
scriptDir = origDir;
process.chdir(scriptDir);
requiresClone = !fs.existsSync('.git');
}
// Next - see if it exists within the CWD.
if (requiresClone) {
if (fs.existsSync(path.join(origDir, 'mobile-chrome-apps'))) {
scriptDir = path.join(origDir, 'mobile-chrome-apps');
process.chdir(scriptDir);
requiresClone = !fs.existsSync('.git');
}
}
if (requiresClone) {
scriptDir = origDir;
chdir(origDir);
recursiveDelete('mobile-chrome-apps');
exec('git clone "https://github.com/MobileChromeApps/mobile-chrome-apps.git"', function() {
scriptDir = path.join(origDir, 'mobile-chrome-apps');
chdir(scriptDir);
console.log('Successfully cloned mobile-chrome-apps repo. Delegating to checked-out version of ' + scriptName);
// TODO: We should quote the args.
exec('"' + process.argv[0] + '" ' + scriptName + ' ' + process.argv.slice(2).join(' '), function() {
exit(0);
});
});
} else {
callback();
}
}
function checkOutSubModules(callback) {
console.log('## Checking Out SubModules');
chdir(scriptDir);
exec('git pull --rebase', function() {
exec('git submodule init', function() {
exec('git submodule update', function() {
chdir(path.join(scriptDir, 'cordova-cli'));
exec('git submodule init', function() {
exec('git submodule update', callback);
});
});
});
});
}
function buildCordovaJs(callback) {
console.log('## Building cordova-js');
chdir(path.join(scriptDir, 'cordova-js'));
var needsBuildJs = true;
computeGitVersion(function(version) {
if (fs.existsSync('pkg/cordova.ios.js')) {
needsBuildJs = (fs.readFileSync('pkg/cordova.ios.js').toString().indexOf(version) == -1);
}
if (needsBuildJs) {
console.log('CordovaJS needs to be built.');
var packager = require('./cordova-js/build/packager');
packager.generate('ios', version);
packager.generate('android', version);
} else {
console.log('CordovaJS is up-to-date.');
}
callback();
});
}
function initCli(callback) {
console.log('## Setting up cordova-cli');
// TODO: Figure out when this should be re-run (e.g. upon update).
if (fs.existsSync('cordova-cli/node_modules')) {
//console.log('cordova-cli already has its dependencies installed.');
callback();
} else {
process.chdir(path.join(scriptDir, 'cordova-cli'));
exec('npm install', function() {
process.chdir(origDir);
callback();
});
}
}
eventQueue.push(checkGit);
eventQueue.push(checkOutSelf);
eventQueue.push(checkOutSubModules);
eventQueue.push(buildCordovaJs);
eventQueue.push(initCli);
}
function createAppMain(appName) {
if (!/\w+\.\w+\.\w+/.exec(appName)) {
fatal('App Name must follow the pattern: com.company.id');
}
function createApp(callback) {
console.log('## Creating Your Application');
chdir(origDir);
var name = /\w+\.(\w+)$/.exec(appName)[1];
// TODO: add android.
var cmds = [];
if (hasXcode) {
cmds.push(['platform', 'add', 'ios']);
}
if (hasAndroidSdk) {
cmds.push(['platform', 'add', 'android']);
}
['bootstrap', 'common', 'file-chooser', 'fileSystem', 'i18n', 'identity', 'socket', 'storage'].forEach(function(pluginName) {
cmds.push(['plugin', 'add', path.join(scriptDir, 'chrome-cordova', 'plugins', pluginName)]);
});
function runCmd() {
var curCmd = cmds.shift();
if (curCmd) {
console.log(curCmd.join(' '));
exec(cordovaCmd(curCmd), runCmd, undefined, true);
} else {
// Create a script that runs update.js.
if (isWindows) {
fs.writeFileSync('mca_update.bat', '"' + process.argv[0] + '" "' + path.join(scriptDir, scriptName) + '" --update_app');
} else {
fs.writeFileSync('mca_update.sh', '#!/bin/sh\ncd "`dirname "$0"`"\n"' + process.argv[0] + '" "' + path.join(scriptDir, scriptName) + '" --update_app');
fs.chmodSync('mca_update.sh', '777');
}
callback();
}
}
var curCmd = ['create', name, appName, name];
console.log(curCmd.join(' '));
exec(cordovaCmd(curCmd), function() {
chdir(path.join(origDir, name));
runCmd();
}, undefined, true);
}
function setAccessTag(callback) {
console.log('## Setting <access> tag');
var configFilePath = path.join('app', 'config.xml');
if (!fs.existsSync(configFilePath)) {
fatal('Expected file to exist: ' + configFilePath);
}
// Manipulating XML with Regex: See http://stackoverflow.com/a/1732454
var contents = fs.readFileSync(configFilePath, 'utf8');
contents = contents.replace(/(access.*)/,"$1\n <access origin=\"chrome-extension://*\" />");
fs.writeFileSync(configFilePath, contents);
callback();
}
eventQueue.push(createApp);
eventQueue.push(setAccessTag);
eventQueue.push(function(callback) { updateMain(); callback(); });
}
function updateMain() {
var hasAndroid = fs.existsSync(path.join('platforms', 'android'));
var hasIos = fs.existsSync(path.join('platforms', 'ios'));
if (!fs.existsSync('platforms')) {
fatal('No platforms directory found. Please run script from the root of your project.');
}
function runPrepare(callback) {
console.log('## Preparing your project')
exec(cordovaCmd(['prepare']), callback, undefined, true);
}
function assetDirForPlatform(platform) {
if (platform === 'android') {
return path.join('platforms', platform, 'assets','www');
}
return path.join('platforms', platform, 'www');
}
function createAddJsStep(platform) {
return function(callback) {
console.log('## Updating cordova.js for ' + platform);
copyFile(path.join(scriptDir, 'cordova-js', 'pkg', 'cordova.' + platform + '.js'), path.join(assetDirForPlatform(platform), 'cordova.js'), callback);
};
}
eventQueue.push(runPrepare);
if (hasAndroid) {
eventQueue.push(createAddJsStep('android'));
}
if (hasIos) {
eventQueue.push(createAddJsStep('ios'));
}
}
function toolsCheckMain() {
console.log('## Checking that tools are installed');
function checkAndroid(callback) {
exec('android list targets', function() {
hasAndroidSdk = true;
console.log('Android SDK is installed.');
callback();
}, function() {
console.log('Android SDK is not installed.');
callback();
}, true);
}
function checkXcode(callback) {
if (process.platform == 'darwin') {
exec('which xcodebuild', function() {
hasXcode = true;
console.log('Xcode is installed.');
callback();
}, function() {
console.log('Xcode is not installed.');
callback();
}, true);
} else {
callback();
}
}
function checkAtLeastOneTool(callback) {
if (!hasAndroidSdk && !hasXcode) {
if (process.platform == 'darwin') {
fatal('Neither android nor xcodebuild were found on your PATH. Please install at least one of them.');
} else {
fatal('Android SDK was not found on your PATH. Please ensure that the android tool is available.');
}
}
callback();
}
eventQueue.push(checkAndroid);
eventQueue.push(checkXcode);
eventQueue.push(checkAtLeastOneTool);
}
function main() {
toolsCheckMain();
initRepoMain();
if (commandLineFlags['update_app']) {
updateMain();
} else {
var appName = commandLineArgs[0];
if (appName) {
createAppMain(appName);
}
}
pump();
}
main();