Skip to content

Commit

Permalink
Merge pull request #3 from foghina/initandroid
Browse files Browse the repository at this point in the history
react-native init should generate an android project
  • Loading branch information
Felix Oghină committed Jul 22, 2015
2 parents c62f0e5 + 6252031 commit 14afe7f
Show file tree
Hide file tree
Showing 27 changed files with 159 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var yeoman = require('yeoman-generator');
var yosay = require('yosay');

function validatePackageName(name) {
if (!name.match(/^([a-zA-Z_$][a-zA-Z\d_$]*\.)+([a-zA-Z_$][a-zA-Z\d_$]*)$/)) {
Expand All @@ -18,9 +17,7 @@ module.exports = yeoman.generators.NamedBase.extend({
var done = this.async();

// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the ' + chalk.red('ReactNative') + ' Android app generator!'
));
this.log('Generating Android React Native app...');

this.prompt([{
name: 'package',
Expand Down Expand Up @@ -52,16 +49,27 @@ module.exports = yeoman.generators.NamedBase.extend({
'path when prompted.');
return;
}
var templateParams = {
package: this.props.package,
appname: this.props.name,
name: this.name,
sdkdir: this.props.sdkdir
};
this.fs.copyTpl(
this.templatePath(path.join('src', '**')),
this.destinationPath(this.name),
{package: this.props.package, appname: this.props.name, sdkdir: this.props.sdkdir}
this.destinationRoot(),
templateParams
);
this.fs.copy(
this.templatePath(path.join('bin', '**')),
this.destinationPath(this.name)
this.destinationRoot()
);
var javaPath = path.join.apply(null, ['app', 'src', 'main', 'java'].concat(this.props.package.split('.')));
this.fs.copyTpl(
this.templatePath(path.join('package', '**')),
this.destinationPath(javaPath),
templateParams
);
// TODO: create app/src/main/java/com/package/RNActivity.java, update AndroidManifest.xml
},

_error: function(message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package <%= package %>;

import android.os.Bundle;

import com.facebook.react.shell.BaseReactActivity;


public class RNActivity extends BaseReactActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadApp("<%= name %>");
}

@Override
protected String getDefaultBundleName() {
return "index.android.js";
}

@Override
protected String getDefaultMainModule() {
return "<%= name %>";
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.facebook.react'

android {
compileSdkVersion 22
Expand All @@ -22,4 +23,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.facebook.react:react:0.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="<%= package %>">

<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".RNActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.facebook.react:gradleplugin:1.0.+'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -14,6 +16,7 @@ buildscript {

allprojects {
repositories {
mavenLocal()
jcenter()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rootProject.name = '<%= appname %>'

include ':app'
63 changes: 63 additions & 0 deletions local-cli/generator-react-native-android/test/test-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

var path = require('path');
var assert = require('yeoman-generator').assert;
var helpers = require('yeoman-generator').test;
var os = require('os');

describe('react-native-android:app', function () {
before(function (done) {
helpers.run(path.join(__dirname, '..', 'generators', 'app'))
.withArguments(['TestApp'])
.withPrompts({package: 'com.reactnative.test', name: 'Test App', sdkdir: '/path/to/sdk'})
.on('end', done);
});

it('creates files', function () {
assert.file([
'build.gradle',
'gradle.properties',
'gradlew.bat',
'gradlew',
'local.properties',
'settings.gradle',
path.join('app', 'build.gradle'),
path.join('app', 'proguard-rules.pro'),
path.join('app', 'src', 'main', 'AndroidManifest.xml'),
path.join('app', 'src', 'main', 'java', 'com', 'reactnative', 'test', 'RNActivity.java'),
path.join('app', 'src', 'main', 'res', 'mipmap-hdpi', 'ic_launcher.png'),
path.join('app', 'src', 'main', 'res', 'mipmap-mdpi', 'ic_launcher.png'),
path.join('app', 'src', 'main', 'res', 'mipmap-xhdpi', 'ic_launcher.png'),
path.join('app', 'src', 'main', 'res', 'mipmap-xxhdpi', 'ic_launcher.png'),
path.join('app', 'src', 'main', 'res', 'values', 'strings.xml'),
path.join('app', 'src', 'main', 'res', 'values', 'styles.xml'),
path.join('gradle', 'wrapper', 'gradle-wrapper.jar'),
path.join('gradle', 'wrapper', 'gradle-wrapper.properties')
]);
});

it('replaces variables', function() {
assert.fileContent('local.properties', 'sdk.dir=/path/to/sdk');
assert.fileContent(path.join('app', 'build.gradle'), 'applicationId "com.reactnative.test"');
assert.fileContent(
path.join('app', 'src', 'main', 'AndroidManifest.xml'),
'package="com.reactnative.test"'
);
assert.fileContent(
path.join('app', 'src', 'main', 'AndroidManifest.xml'),
'name=".RNActivity"'
);
assert.fileContent(
path.join('app', 'src', 'main', 'java', 'com', 'reactnative', 'test', 'RNActivity.java'),
'package com.reactnative.test;'
);
assert.fileContent(
path.join('app', 'src', 'main', 'java', 'com', 'reactnative', 'test', 'RNActivity.java'),
'loadApp("TestApp");'
);
assert.fileContent(
path.join('app', 'src', 'main', 'res', 'values', 'strings.xml'),
'<string name="app_name">Test App</string>'
);
});
});
22 changes: 22 additions & 0 deletions local-cli/init.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

var fs = require('fs');
var path = require('path');
var utils = require('./generator-utils');

function init(projectDir, appName) {
console.log('Setting up new React Native app in ' + projectDir);
var source = path.resolve(__dirname, '..', 'Examples/SampleApp');

// copy iOS source
utils.walk(source).forEach(function(f) {
f = f.replace(source + '/', ''); // Strip off absolute path
if (f === 'project.xcworkspace' || f.indexOf('.xcodeproj/xcuserdata') !== -1) {
Expand All @@ -28,6 +30,26 @@ function init(projectDir, appName) {
);
});

// generate Android source
fs.mkdirSync(path.join(projectDir, 'android'));
var oldCwd = process.cwd();
process.chdir(path.join(projectDir, 'android'));

var yeoman = require('yeoman-environment');
var env = yeoman.createEnv();
var generatorPath = path.join(
__dirname,
'generator-react-native-android',
'generators',
'app',
'index.js'
);
env.register(generatorPath, 'android:app');
env.run(['android:app', appName], function() {
process.chdir(oldCwd);
});


console.log('Next Steps:');
console.log(' Open ' + path.resolve(projectDir, appName) + '.xcodeproj in Xcode');
console.log(' Hit Run button');
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@
"wordwrap": "^1.0.0",
"worker-farm": "^1.3.1",
"ws": "0.4.31",
"yargs": "1.3.2"
"yargs": "1.3.2",
"yeoman-environment": "^1.2.5",
"yeoman-generator": "^0.20.1"
},
"devDependencies": {
"jest-cli": "facebook/jest#0.5.x",
"babel-eslint": "3.1.5",
"eslint": "0.21.2",
"eslint-plugin-react": "2.3.0"
"eslint-plugin-react": "2.3.0",
"mocha": "*"
}
}

This file was deleted.

This file was deleted.

56 changes: 0 additions & 56 deletions react-native-cli/generator-react-native-android/test/test-app.js

This file was deleted.

18 changes: 0 additions & 18 deletions react-native-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,6 @@ if (cli) {
process.exit(1);
}
break;
case 'android':
if (args[1]) {
var yeoman = require('yeoman-environment');
var env = yeoman.createEnv();
var generatorPath = path.join(
__dirname,
'generator-react-native-android',
'generators',
'app',
'index.js'
);
env.register(generatorPath, 'android:app');
env.run(['android:app', args[1]]);
} else {
console.error('Usage: react-native android <ProjectName>');
process.exit(1);
}
break;
default:
console.error(
'Command `%s` unrecognized. ' +
Expand Down
7 changes: 1 addition & 6 deletions react-native-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
"react-native": "index.js"
},
"dependencies": {
"prompt": "^0.2.14",
"yeoman-environment": "^1.2.5",
"yeoman-generator": "^0.20.1"
},
"devDependencies": {
"mocha": "*"
"prompt": "^0.2.14"
}
}

0 comments on commit 14afe7f

Please sign in to comment.