2015-10-20 16:53:54 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-09-14 14:35:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var chalk = require('chalk');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var yeoman = require('yeoman-generator');
|
|
|
|
|
|
|
|
function validatePackageName(name) {
|
2016-09-09 11:09:20 +00:00
|
|
|
if (!name.match(/^([a-zA-Z][a-zA-Z\d_]*\.)+([a-zA-Z_][a-zA-Z\d_]*)$/)) {
|
2015-09-14 14:35:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = yeoman.generators.NamedBase.extend({
|
|
|
|
constructor: function() {
|
|
|
|
yeoman.generators.NamedBase.apply(this, arguments);
|
|
|
|
|
|
|
|
this.option('package', {
|
|
|
|
desc: 'Package name for the application (com.example.app)',
|
|
|
|
type: String,
|
|
|
|
defaults: 'com.' + this.name.toLowerCase()
|
|
|
|
});
|
2015-10-23 10:35:59 +00:00
|
|
|
this.option('upgrade', {
|
|
|
|
desc: 'Specify an upgrade',
|
|
|
|
type: Boolean,
|
|
|
|
defaults: false
|
|
|
|
});
|
2015-09-14 14:35:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
initializing: function() {
|
|
|
|
if (!validatePackageName(this.options.package)) {
|
|
|
|
throw new Error('Package name ' + this.options.package + ' is invalid');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
writing: function() {
|
|
|
|
var templateParams = {
|
|
|
|
package: this.options.package,
|
|
|
|
name: this.name
|
|
|
|
};
|
2015-10-23 10:35:59 +00:00
|
|
|
if (!this.options.upgrade) {
|
|
|
|
this.fs.copyTpl(
|
|
|
|
this.templatePath(path.join('src', '**')),
|
|
|
|
this.destinationPath('android'),
|
|
|
|
templateParams
|
|
|
|
);
|
|
|
|
this.fs.copy(
|
|
|
|
this.templatePath(path.join('bin', '**')),
|
|
|
|
this.destinationPath('android')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.fs.copyTpl(
|
|
|
|
this.templatePath(path.join('src', '*')),
|
|
|
|
this.destinationPath('android'),
|
|
|
|
templateParams
|
|
|
|
);
|
|
|
|
this.fs.copyTpl(
|
|
|
|
this.templatePath(path.join('src', 'app', '*')),
|
|
|
|
this.destinationPath(path.join('android', 'app')),
|
|
|
|
templateParams
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-14 14:35:58 +00:00
|
|
|
var javaPath = path.join.apply(
|
|
|
|
null,
|
|
|
|
['android', 'app', 'src', 'main', 'java'].concat(this.options.package.split('.'))
|
|
|
|
);
|
|
|
|
this.fs.copyTpl(
|
|
|
|
this.templatePath(path.join('package', '**')),
|
|
|
|
this.destinationPath(javaPath),
|
|
|
|
templateParams
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
end: function() {
|
|
|
|
var projectPath = this.destinationRoot();
|
|
|
|
this.log(chalk.white.bold('To run your app on Android:'));
|
2015-11-03 12:02:49 +00:00
|
|
|
this.log(chalk.white(' Have an Android emulator running (quickest way to get started), or a device connected'));
|
2015-09-14 14:35:58 +00:00
|
|
|
this.log(chalk.white(' cd ' + projectPath));
|
|
|
|
this.log(chalk.white(' react-native run-android'));
|
|
|
|
}
|
|
|
|
});
|