Skip to content

Commit db4549a

Browse files
Adds Support For Generating Maven Package For Android Library (#58)
* Adds Support For Generating Maven Package For Android Library Summary: This adds support for generating the maven package similar to how React Native core does it. This allows a brownfield Android project reference this maven package instead of the project in the node_modules. * Adds config options for author's github, name and email. Updates README * added default values to readme * fixed merge conflict * Makes the library license type command line configurable * Changes authorGithub to githubAccount
1 parent 14ed787 commit db4549a

File tree

5 files changed

+149
-5
lines changed

5 files changed

+149
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Options:
4747
--namespace <namespace> (Windows only!) The namespace for the Windows module
4848
(Default: The name as PascalCase)
4949
--platforms <platforms> Platforms the library will be created for. (comma separated; default: `ios,android,windows`)
50+
--github-account <github_account> The github account where the library is hosted (Default: `github_account`)
51+
--author-name <name> The author's name (Default: `Your Name`)
52+
--author-email <email> The author's email (Default: `yourname@email.com`)
53+
--license <license> The license type of this library (Default: `Apache-2.0`)
5054
```
5155

5256
## Programmatic usage
@@ -69,6 +73,10 @@ createLibrary({
6973
platforms: Array, /* Platforms the library will be created for. (Default: ['ios', 'android', 'windows']) */
7074
packageIdentifier: String, /* (Android only!) The package name for the Android module (Default: com.reactlibrary) */
7175
namespace: String, /* (Windows only!) The namespace for the Windows module (Default: The package identifier as PascalCase, which is `Com.Reactlibrary`) */
76+
githubAccount: String, /* The github account where the library is hosted (Default: `github_account`) */
77+
authorName: String, /* The author's name (Default: `Your Name`) */
78+
authorEmail: String, /* The author's email (Default: `yourname@email.com`) */
79+
license: String, /* The license type of this library (Default: `Apache-2.0`) */
7280
}
7381
```
7482

command.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = {
1414
const namespace = options.namespace;
1515
const platforms = (options.platforms) ? options.platforms.split(',') : options.platforms;
1616
const overridePrefix = options.overridePrefix;
17+
const githubAccount = options.githubAccount;
18+
const authorName = options.authorName;
19+
const authorEmail = options.authorEmail;
20+
const license = options.license;
1721

1822
const beforeCreation = Date.now();
1923
createLibrary({
@@ -24,6 +28,10 @@ module.exports = {
2428
platforms,
2529
namespace,
2630
overridePrefix,
31+
githubAccount,
32+
authorName,
33+
authorEmail,
34+
license
2735
}).then(() => {
2836
console.log(`
2937
${emoji.get('books')} Created library ${name} in \`./${name}\`.
@@ -60,5 +68,21 @@ ${emoji.get('arrow_right')} To get started type \`cd ./${name}\` and run \`npm
6068
command: '--platforms <platforms>',
6169
description: 'Platforms the library will be created for. (comma separated; default: `ios,android,windows`)',
6270
default: 'ios,android,windows',
71+
}, {
72+
command: '--github-account [githubAccount]',
73+
description: 'The github account where the library is hosted (Default: `github_account`)',
74+
default: 'github_account',
75+
}, {
76+
command: '--author-name [authorName]',
77+
description: 'The author\'s name (Default: `Your Name`)',
78+
default: 'Your Name',
79+
}, {
80+
command: '--author-email [authorEmail]',
81+
description: 'The author\'s email (Default: `yourname@email.com`)',
82+
default: 'yourname@email.com',
83+
}, {
84+
command: '--license [license]',
85+
description: 'The license type (Default: `Apache-2.0`)',
86+
default: 'Apache-2.0',
6387
}]
6488
};

lib.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const DEFAULT_MODULE_PREFIX = 'react-native';
1212
const DEFAULT_PACKAGE_IDENTIFIER = 'com.reactlibrary';
1313
const DEFAULT_PLATFORMS = ['android', 'ios', 'windows'];
1414
const DEFAULT_OVERRIDE_PREFIX = false;
15+
const DEFAULT_GITHUB_ACCOUNT = 'github_account'
16+
const DEFAULT_AUTHOR_NAME = 'Your Name'
17+
const DEFAULT_AUTHOR_EMAIL = 'yourname@email.com'
18+
const DEFAULT_LICENSE = 'Apache-2.0'
1519

1620
module.exports = ({
1721
namespace,
@@ -21,6 +25,10 @@ module.exports = ({
2125
packageIdentifier = DEFAULT_PACKAGE_IDENTIFIER,
2226
platforms = DEFAULT_PLATFORMS,
2327
overridePrefix = DEFAULT_OVERRIDE_PREFIX,
28+
githubAccount = DEFAULT_GITHUB_ACCOUNT,
29+
authorName = DEFAULT_AUTHOR_NAME,
30+
authorEmail = DEFAULT_AUTHOR_EMAIL,
31+
license = DEFAULT_LICENSE,
2432
}) => {
2533
if (!overridePrefix) {
2634
if (hasPrefix(name)) {
@@ -64,6 +72,10 @@ module.exports = ({
6472
packageIdentifier,
6573
namespace: namespace || pascalCase(name).split(/(?=[A-Z])/).join('.'),
6674
platforms,
75+
githubAccount,
76+
authorName,
77+
authorEmail,
78+
license,
6779
};
6880

6981
const filename = path.join(name, template.name(args));

templates/android.js

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = platform => [{
22
name: () => `${platform}/build.gradle`,
3-
content: () => `
3+
content: ({ packageIdentifier }) => `
44
buildscript {
55
repositories {
66
jcenter()
@@ -14,6 +14,7 @@ buildscript {
1414
}
1515
1616
apply plugin: 'com.android.library'
17+
apply plugin: 'maven'
1718
1819
android {
1920
compileSdkVersion 23
@@ -43,6 +44,77 @@ repositories {
4344
dependencies {
4445
compile 'com.facebook.react:react-native:+'
4546
}
47+
48+
def configureReactNativePom(def pom) {
49+
def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
50+
51+
pom.project {
52+
name packageJson.title
53+
artifactId packageJson.name
54+
version = packageJson.version
55+
group = "${packageIdentifier}"
56+
description packageJson.description
57+
url packageJson.repository.baseUrl
58+
59+
licenses {
60+
license {
61+
name packageJson.license
62+
url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
63+
distribution 'repo'
64+
}
65+
}
66+
67+
developers {
68+
developer {
69+
id packageJson.author.username
70+
name packageJson.author.name
71+
}
72+
}
73+
}
74+
}
75+
76+
afterEvaluate { project ->
77+
78+
task androidJavadoc(type: Javadoc) {
79+
source = android.sourceSets.main.java.srcDirs
80+
classpath += files(android.bootClasspath)
81+
classpath += files(project.getConfigurations().getByName('compile').asList())
82+
include '**/*.java'
83+
}
84+
85+
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
86+
classifier = 'javadoc'
87+
from androidJavadoc.destinationDir
88+
}
89+
90+
task androidSourcesJar(type: Jar) {
91+
classifier = 'sources'
92+
from android.sourceSets.main.java.srcDirs
93+
include '**/*.java'
94+
}
95+
96+
android.libraryVariants.all { variant ->
97+
def name = variant.name.capitalize()
98+
task "jar$\{name\}"(type: Jar, dependsOn: variant.javaCompile) {
99+
from variant.javaCompile.destinationDir
100+
}
101+
}
102+
103+
artifacts {
104+
archives androidSourcesJar
105+
archives androidJavadocJar
106+
}
107+
108+
task installArchives(type: Upload) {
109+
configuration = configurations.archives
110+
repositories.mavenDeployer {
111+
// Deploy to react-native-event-bridge/maven, ready to publish to npm
112+
repository url: "file://$\{projectDir\}/../android/maven"
113+
114+
configureReactNativePom pom
115+
}
116+
}
117+
}
46118
`,
47119
}, {
48120
name: () => `${platform}/src/main/AndroidManifest.xml`,
@@ -108,4 +180,21 @@ public class ${name}Package implements ReactPackage {
108180
return Collections.emptyList();
109181
}
110182
}`,
111-
}];
183+
}, {
184+
name: () => `${platform}/README.md`,
185+
content: () => `
186+
README
187+
======
188+
189+
If you want to publish the lib as a maven dependency, follow these steps before publishing a new version to npm:
190+
191+
1. Be sure to have the Android [SDK](https://developer.android.com/studio/index.html) and [NDK](https://developer.android.com/ndk/guides/index.html) installed
192+
2. Be sure to have a \`local.properties\` file in this folder that points to the Android SDK and NDK
193+
\`\`\`
194+
ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle
195+
sdk.dir=/Users/{username}/Library/Android/sdk
196+
\`\`\`
197+
3. Delete the \`maven\` folder
198+
4. Run \`sudo ./gradlew installArchives\`
199+
5. Verify that latest set of generated files is in the maven folder with the correct version number
200+
`}];

templates/general.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ${name};
7373
},
7474
}, {
7575
name: () => 'package.json',
76-
content: ({ moduleName, platforms }) => {
76+
content: ({ moduleName, platforms, githubAccount, authorName, authorEmail, license }) => {
7777
let dependencies = `
7878
"react": "16.0.0-alpha.6",
7979
"react-native": "^0.44.1"`;
@@ -85,17 +85,28 @@ ${name};
8585
return `
8686
{
8787
"name": "${moduleName}",
88+
"title": "${moduleName.split('-').map(word => word[0].toUpperCase() + word.substr(1)).join(' ')}",
8889
"version": "1.0.0",
8990
"description": "",
9091
"main": "index.js",
9192
"scripts": {
9293
"test": "echo \\"Error: no test specified\\" && exit 1"
9394
},
95+
"repository": {
96+
"type": "git",
97+
"url": "git+https://github.com/${githubAccount}/${moduleName}.git",
98+
"baseUrl": "https://github.com/${githubAccount}/${moduleName}"
99+
},
94100
"keywords": [
95101
"react-native"
96102
],
97-
"author": "",
98-
"license": "",
103+
"author": {
104+
"name": "${authorName}",
105+
"email": "${authorEmail}"
106+
},
107+
"license": "${license}",
108+
"licenseFilename": "LICENSE",
109+
"readmeFilename": "README.md",
99110
"peerDependencies": {
100111
${dependencies}
101112
},

0 commit comments

Comments
 (0)