-
Notifications
You must be signed in to change notification settings - Fork 904
/
native_modules.gradle
250 lines (204 loc) · 8.29 KB
/
native_modules.gradle
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
import groovy.json.JsonSlurper
import org.gradle.initialization.DefaultSettings
def generatedFileName = "PackageList.java"
def generatedFileContentsTemplate = """
package com.facebook.react;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays;
import java.util.List;
{{ packageImports }}
public class PackageList {
private ReactNativeHost reactNativeHost;
public PackageList(ReactNativeHost reactNativeHost) {
this.reactNativeHost = reactNativeHost;
}
private ReactNativeHost getReactNativeHost() {
return this.reactNativeHost;
}
private Resources getResources() {
return this.getApplication().getResources();
}
private Application getApplication() {
return this.reactNativeHost.getApplication();
}
private Context getApplicationContext() {
return this.getApplication().getApplicationContext();
}
public List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(){{ packageClassInstances }}
);
}
}
"""
class ReactNativeModules {
private Logger logger
private Project project
private DefaultSettings defaultSettings
private ExtraPropertiesExtension extension
private ArrayList<HashMap<String, String>> reactNativeModules
private static String LOG_PREFIX = ":ReactNative:"
private static String REACT_NATIVE_CLI_BIN = "node_modules${File.separator}@react-native-community${File.separator}cli${File.separator}build${File.separator}index.js"
private static String REACT_NATIVE_CONFIG_CMD = "node ${REACT_NATIVE_CLI_BIN} config"
ReactNativeModules(Logger logger) {
this.logger = logger
}
void applySettingsGradle(DefaultSettings defaultSettings, ExtraPropertiesExtension extraPropertiesExtension) {
this.defaultSettings = defaultSettings
this.extension = extraPropertiesExtension
this.reactNativeModules = this.getReactNativeConfig()
addReactNativeModuleProjects()
}
void applyBuildGradle(Project project, ExtraPropertiesExtension extraPropertiesExtension) {
this.project = project
this.extension = extraPropertiesExtension
this.reactNativeModules = this.getReactNativeConfig()
addReactNativeModuleDependencies()
}
/**
* Include the react native modules android projects and specify their project directory
*/
void addReactNativeModuleProjects() {
reactNativeModules.forEach { reactNativeModule ->
String name = reactNativeModule["name"]
String androidSourceDir = reactNativeModule["androidSourceDir"]
defaultSettings.include(":${name}")
defaultSettings.project(":${name}").projectDir = new File("${androidSourceDir}")
}
}
/**
* Adds the react native modules as dependencies to the users `app` project
*/
void addReactNativeModuleDependencies() {
reactNativeModules.forEach { reactNativeModule ->
def name = reactNativeModule["name"]
project.dependencies {
// TODO(salakar): are other dependency scope methods such as `api` required?
implementation project(path: ":${name}")
}
}
}
/**
* This returns the users project root (e.g. where the node_modules dir is located).
*
* This defaults to up one directory from the root android directory unless the user has defined
* a `ext.reactNativeProjectRoot` extension property
*
* @return
*/
File getReactNativeProjectRoot() {
if (this.extension.has("reactNativeProjectRoot")) {
File rnRoot = File(this.extension.get("reactNativeProjectRoot"))
// allow custom React Native project roots for non-standard directory structures
this.logger.debug("${LOG_PREFIX}Using custom React Native project root path '${rnRoot.toString()}'")
return rnRoot
}
File androidRoot
if (this.project) {
androidRoot = this.project.rootProject.projectDir
} else {
androidRoot = this.defaultSettings.rootProject.projectDir
}
this.logger.debug("${LOG_PREFIX}Using default React Native project root path '${androidRoot.parentFile.toString()}'")
return androidRoot.parentFile
}
/**
* Code-gen a java file with all the detected ReactNativePackage instances automatically added
*
* @param outputDir
* @param generatedFileName
* @param generatedFileContentsTemplate
* @param applicationId
*/
void generatePackagesFile(File outputDir, String generatedFileName, String generatedFileContentsTemplate, String applicationId) {
ArrayList<HashMap<String, String>>[] packages = this.reactNativeModules
String packageImports = ""
String packageClassInstances = ""
if (packages.size() > 0) {
packageImports = "import ${applicationId}.BuildConfig;\n\n"
packageImports = packageImports + packages.collect {
"// ${it.name}\n${it.packageImportPath}"
}.join(';\n')
packageClassInstances = ",\n " + packages.collect { it.packageInstance }.join(',')
}
String generatedFileContents = generatedFileContentsTemplate
.replace("{{ packageImports }}", packageImports)
.replace("{{ packageClassInstances }}", packageClassInstances)
outputDir.mkdirs()
final FileTreeBuilder treeBuilder = new FileTreeBuilder(outputDir)
treeBuilder.file(generatedFileName).newWriter().withWriter { w ->
w << generatedFileContents
}
}
/**
* Runs a process to call the React Native CLI Config command and parses the output
*
* @return ArrayList < HashMap < String , String > >
*/
ArrayList<HashMap<String, String>> getReactNativeConfig() {
if (this.reactNativeModules != null) return this.reactNativeModules
ArrayList<HashMap<String, String>> reactNativeModules = new ArrayList<HashMap<String, String>>()
def cmdProcess
try {
cmdProcess = Runtime.getRuntime().exec(REACT_NATIVE_CONFIG_CMD, null, getReactNativeProjectRoot())
cmdProcess.waitFor()
} catch (Exception exception) {
this.logger.warn("${LOG_PREFIX}${exception.message}")
this.logger.warn("${LOG_PREFIX}Automatic import of native modules failed. (UNKNOWN)")
return reactNativeModules
}
def reactNativeConfigOutput = cmdProcess.in.text
def json = new JsonSlurper().parseText(reactNativeConfigOutput)
def dependencies = json["dependencies"]
dependencies.each { name, value ->
def platformsConfig = value["platforms"];
def androidConfig = platformsConfig["android"]
if (androidConfig != null && androidConfig["sourceDir"] != null) {
this.logger.info("${LOG_PREFIX}Automatically adding native module '${name}'")
HashMap reactNativeModuleConfig = new HashMap<String, String>()
reactNativeModuleConfig.put("name", name)
reactNativeModuleConfig.put("androidSourceDir", androidConfig["sourceDir"])
reactNativeModuleConfig.put("packageInstance", androidConfig["packageInstance"])
reactNativeModuleConfig.put("packageImportPath", androidConfig["packageImportPath"])
this.logger.trace("${LOG_PREFIX}'${name}': ${reactNativeModuleConfig.toMapString()}")
reactNativeModules.add(reactNativeModuleConfig)
} else {
this.logger.info("${LOG_PREFIX}Skipping native module '${name}'")
}
}
return reactNativeModules
}
}
/** -----------------------
* Exported Extensions
* ------------------------ */
def autoModules = new ReactNativeModules(logger)
ext.applyNativeModulesSettingsGradle = { DefaultSettings defaultSettings ->
autoModules.applySettingsGradle(defaultSettings, ext)
}
ext.applyNativeModulesAppBuildGradle = { Project project ->
autoModules.applyBuildGradle(project, ext)
def applicationId
def generatedSrcDir = new File(buildDir, "generated/rncli/src/main/java/com/facebook/react")
// TODO(salakar): not sure if this is the best way of getting the package name (used to import BuildConfig)
project.android.applicationVariants.all { variant ->
applicationId = [variant.mergedFlavor.applicationId, variant.buildType.applicationIdSuffix].findAll().join()
}
task generatePackageList << {
autoModules.generatePackagesFile(generatedSrcDir, generatedFileName, generatedFileContentsTemplate, applicationId)
}
preBuild.dependsOn generatePackageList
android {
sourceSets {
main {
java {
srcDirs += generatedSrcDir
}
}
}
}
}