Skip to content

Commit 7b7624a

Browse files
author
Christopher J. Brody
committed
Merge branch 'dev' of github.com:brodybits/create-react-native-module into android-tools-build-gradle-classpath
2 parents c8959f5 + fb35257 commit 7b7624a

File tree

29 files changed

+1100
-821
lines changed

29 files changed

+1100
-821
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ This tool based on [`react-native-create-library`](https://www.npmjs.com/package
3535
- [issue #29](https://github.com/brodybits/create-react-native-module/issues/29) - View does not work with RN 0.60 on Android (quick patch needed)
3636
- React Native 0.60(+) currently not supported by Expo or react-native-windows
3737
- Out-of-tree platform support
38-
- tvOS platform support - unstable (not tested) (see [issue #95](https://github.com/brodybits/create-react-native-module/issues/95))
38+
- tvOS platform support - unstable with very limited testing, minimum react-native-tvos version is 0.60 (see [issue #95](https://github.com/brodybits/create-react-native-module/issues/95))
3939
- Windows - unstable (not tested, see [issue #23](https://github.com/brodybits/create-react-native-module/issues/23)); now deprecated and may be removed in the near future (see [issue #43](https://github.com/brodybits/create-react-native-module/issues/43))
4040
- for future consideration: macOS (see [issue #94](https://github.com/brodybits/create-react-native-module/issues/94))
4141
- Node.js pre-10 support is deprecated and will be removed in the near future (see [issue #38](https://github.com/brodybits/create-react-native-module/issues/38))

lib/lib.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ const execaDefault = require('execa');
1010
// default fs object
1111
const fsExtra = require('fs-extra');
1212

13+
const jsonfile = require('jsonfile');
14+
1315
// Internal imports:
1416

1517
const normalizedOptions = require('./normalized-options');
1618

17-
// FUTURE TBD remaining module in utils to be factored out:
18-
const { npmAddScriptSync } = require('./utils');
19-
2019
// Imports from templates
2120

2221
const templates = require('../templates');
@@ -46,6 +45,22 @@ const renderTemplateIfValid = (fs, root, template, templateArgs) => {
4645
);
4746
};
4847

48+
// FUTURE TBD make this asynchronous and possibly more functional:
49+
const npmAddScriptSync = (packageJsonPath, script, fs) => {
50+
try {
51+
var packageJson = jsonfile.readFileSync(packageJsonPath, { fs });
52+
if (!packageJson.scripts) packageJson.scripts = {};
53+
packageJson.scripts[script.key] = script.value;
54+
jsonfile.writeFileSync(packageJsonPath, packageJson, { fs, spaces: 2 });
55+
} catch (e) {
56+
if (/ENOENT.*package.json/.test(e.message)) {
57+
throw new Error(`The package.json at path: ${packageJsonPath} does not exist.`);
58+
} else {
59+
throw e;
60+
}
61+
}
62+
};
63+
4964
const generateWithNormalizedOptions = ({
5065
name,
5166
prefix,

lib/utils/index.js

-5
This file was deleted.

lib/utils/npmAddScriptSync.js

-19
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-react-native-module",
3-
"version": "0.10.3-dev",
3+
"version": "0.11.1-dev",
44
"description": "Tool to create a React Native library module or view module with a single command",
55
"bin": "bin/cli.js",
66
"main": "lib/lib.js",

templates/android.js

+45-33
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
module.exports = platform => [{
22
name: () => `${platform}/build.gradle`,
3-
content: ({ packageIdentifier }) => `buildscript {
4-
ext.safeExtGet = {prop, fallback ->
5-
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
6-
}
7-
repositories {
8-
google()
9-
jcenter()
10-
}
3+
content: ({ packageIdentifier }) => `// ${platform}/build.gradle
4+
5+
def safeExtGet(prop, fallback) {
6+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
7+
}
118
12-
dependencies {
13-
// ref:
14-
// * https://github.com/facebook/react-native/blob/0.61-stable/template/android/build.gradle#L15
15-
// * https://mvnrepository.com/artifact/com.android.tools.build/gradle?repo=google
16-
classpath 'com.android.tools.build:gradle:3.5.0'
9+
buildscript {
10+
// The Android Gradle plugin is only required when opening the android folder stand-alone.
11+
// This avoids unnecessary downloads and potential conflicts when the library is included as a
12+
// module dependency in an application project.
13+
if (project == rootProject) {
14+
repositories {
15+
google()
16+
jcenter()
17+
}
18+
dependencies {
19+
// ref:
20+
// * https://github.com/facebook/react-native/blob/0.61-stable/template/android/build.gradle#L15
21+
// * https://mvnrepository.com/artifact/com.android.tools.build/gradle?repo=google
22+
classpath 'com.android.tools.build:gradle:3.5.0'
23+
}
1724
}
1825
}
1926
@@ -29,33 +36,38 @@ def DEFAULT_MIN_SDK_VERSION = 16
2936
def DEFAULT_TARGET_SDK_VERSION = 28
3037
3138
android {
32-
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
33-
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
34-
35-
defaultConfig {
36-
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
37-
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
38-
versionCode 1
39-
versionName "1.0"
40-
}
41-
lintOptions {
42-
abortOnError false
43-
}
39+
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
40+
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
41+
defaultConfig {
42+
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
43+
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
44+
versionCode 1
45+
versionName "1.0"
46+
}
47+
lintOptions {
48+
abortOnError false
49+
}
4450
}
4551
4652
repositories {
53+
mavenLocal()
4754
maven {
4855
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
49-
// Matches recent template from React Native 0.59 / 0.60
50-
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L30
51-
// https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L28
52-
url "$projectDir/../node_modules/react-native/android"
56+
url "$rootDir/../node_modules/react-native/android"
57+
}
58+
maven {
59+
// Android JSC is installed from npm
60+
url "$rootDir/../node_modules/jsc-android/dist"
5361
}
54-
mavenCentral()
62+
google()
63+
jcenter()
5564
}
5665
5766
dependencies {
58-
implementation "com.facebook.react:react-native:\${safeExtGet('reactnativeVersion', '+')}"
67+
// ref:
68+
// https://github.com/facebook/react-native/blob/0.61-stable/template/android/app/build.gradle#L192
69+
//noinspection GradleDynamicVersion
70+
implementation 'com.facebook.react:react-native:+' // From node_modules
5971
}
6072
6173
def configureReactNativePom(def pom) {
@@ -87,7 +99,8 @@ def configureReactNativePom(def pom) {
8799
}
88100
89101
afterEvaluate { project ->
90-
102+
// some Gradle build hooks ref:
103+
// https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
91104
task androidJavadoc(type: Javadoc) {
92105
source = android.sourceSets.main.java.srcDirs
93106
classpath += files(android.bootClasspath)
@@ -123,7 +136,6 @@ afterEvaluate { project ->
123136
repositories.mavenDeployer {
124137
// Deploy to react-native-event-bridge/maven, ready to publish to npm
125138
repository url: "file://\${projectDir}/../android/maven"
126-
127139
configureReactNativePom pom
128140
}
129141
}

tests/integration/cli/create/view/__snapshots__/cli-create-with-view.test.js.snap

+45-33
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,27 @@ sdk.dir=/Users/{username}/Library/Android/sdk
124124
},
125125
Object {
126126
"name": "react-native-integration-view-test-package/android/build.gradle",
127-
"theContent": "buildscript {
128-
ext.safeExtGet = {prop, fallback ->
129-
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
130-
}
131-
repositories {
132-
google()
133-
jcenter()
134-
}
127+
"theContent": "// android/build.gradle
128+
129+
def safeExtGet(prop, fallback) {
130+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
131+
}
135132
136-
dependencies {
137-
// ref:
138-
// * https://github.com/facebook/react-native/blob/0.61-stable/template/android/build.gradle#L15
139-
// * https://mvnrepository.com/artifact/com.android.tools.build/gradle?repo=google
140-
classpath 'com.android.tools.build:gradle:3.5.0'
133+
buildscript {
134+
// The Android Gradle plugin is only required when opening the android folder stand-alone.
135+
// This avoids unnecessary downloads and potential conflicts when the library is included as a
136+
// module dependency in an application project.
137+
if (project == rootProject) {
138+
repositories {
139+
google()
140+
jcenter()
141+
}
142+
dependencies {
143+
// ref:
144+
// * https://github.com/facebook/react-native/blob/0.61-stable/template/android/build.gradle#L15
145+
// * https://mvnrepository.com/artifact/com.android.tools.build/gradle?repo=google
146+
classpath 'com.android.tools.build:gradle:3.5.0'
147+
}
141148
}
142149
}
143150
@@ -153,33 +160,38 @@ def DEFAULT_MIN_SDK_VERSION = 16
153160
def DEFAULT_TARGET_SDK_VERSION = 28
154161
155162
android {
156-
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
157-
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
158-
159-
defaultConfig {
160-
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
161-
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
162-
versionCode 1
163-
versionName \\"1.0\\"
164-
}
165-
lintOptions {
166-
abortOnError false
167-
}
163+
compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
164+
buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
165+
defaultConfig {
166+
minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
167+
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
168+
versionCode 1
169+
versionName \\"1.0\\"
170+
}
171+
lintOptions {
172+
abortOnError false
173+
}
168174
}
169175
170176
repositories {
177+
mavenLocal()
171178
maven {
172179
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
173-
// Matches recent template from React Native 0.59 / 0.60
174-
// https://github.com/facebook/react-native/blob/0.59-stable/template/android/build.gradle#L30
175-
// https://github.com/facebook/react-native/blob/0.60-stable/template/android/build.gradle#L28
176-
url \\"$projectDir/../node_modules/react-native/android\\"
180+
url \\"$rootDir/../node_modules/react-native/android\\"
177181
}
178-
mavenCentral()
182+
maven {
183+
// Android JSC is installed from npm
184+
url \\"$rootDir/../node_modules/jsc-android/dist\\"
185+
}
186+
google()
187+
jcenter()
179188
}
180189
181190
dependencies {
182-
implementation \\"com.facebook.react:react-native:\${safeExtGet('reactnativeVersion', '+')}\\"
191+
// ref:
192+
// https://github.com/facebook/react-native/blob/0.61-stable/template/android/app/build.gradle#L192
193+
//noinspection GradleDynamicVersion
194+
implementation 'com.facebook.react:react-native:+' // From node_modules
183195
}
184196
185197
def configureReactNativePom(def pom) {
@@ -211,7 +223,8 @@ def configureReactNativePom(def pom) {
211223
}
212224
213225
afterEvaluate { project ->
214-
226+
// some Gradle build hooks ref:
227+
// https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
215228
task androidJavadoc(type: Javadoc) {
216229
source = android.sourceSets.main.java.srcDirs
217230
classpath += files(android.bootClasspath)
@@ -247,7 +260,6 @@ afterEvaluate { project ->
247260
repositories.mavenDeployer {
248261
// Deploy to react-native-event-bridge/maven, ready to publish to npm
249262
repository url: \\"file://\${projectDir}/../android/maven\\"
250-
251263
configureReactNativePom pom
252264
}
253265
}

0 commit comments

Comments
 (0)