-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-plugin-build-service.ts
412 lines (334 loc) · 15.4 KB
/
android-plugin-build-service.ts
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import * as path from "path";
import { MANIFEST_FILE_NAME, INCLUDE_GRADLE_NAME, ASSETS_DIR, RESOURCES_DIR, TNS_ANDROID_RUNTIME_NAME, AndroidBuildDefaults } from "../constants";
import { getShortPluginName, hook } from "../common/helpers";
import { Builder, parseString } from "xml2js";
import { ILogger } from "log4js";
export class AndroidPluginBuildService implements IAndroidPluginBuildService {
/**
* Required for hooks execution to work.
*/
private get $hooksService(): IHooksService {
return this.$injector.resolve("hooksService");
}
private get $platformService(): IPlatformService {
return this.$injector.resolve("platformService");
}
constructor(private $injector: IInjector,
private $fs: IFileSystem,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $androidToolsInfo: IAndroidToolsInfo,
private $logger: ILogger,
private $npm: INodePackageManager,
private $projectDataService: IProjectDataService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $errors: IErrors) { }
private static MANIFEST_ROOT = {
$: {
"xmlns:android": "http://schemas.android.com/apk/res/android"
}
};
private getAndroidSourceDirectories(source: string): Array<string> {
const directories = [RESOURCES_DIR, "java", ASSETS_DIR, "jniLibs"];
const resultArr: Array<string> = [];
this.$fs.enumerateFilesInDirectorySync(source, (file, stat) => {
if (stat.isDirectory() && _.some(directories, (element) => file.endsWith(element))) {
resultArr.push(file);
return true;
}
});
return resultArr;
}
private getManifest(platformsDir: string): string {
const manifest = path.join(platformsDir, MANIFEST_FILE_NAME);
return this.$fs.exists(manifest) ? manifest : null;
}
private async updateManifestContent(oldManifestContent: string, defaultPackageName: string): Promise<string> {
let xml: any = await this.getXml(oldManifestContent);
let packageName = defaultPackageName;
// if the manifest file is full-featured and declares settings inside the manifest scope
if (xml["manifest"]) {
if (xml["manifest"]["$"]["package"]) {
packageName = xml["manifest"]["$"]["package"];
}
// set the xml as the value to iterate over its properties
xml = xml["manifest"];
}
// if the manifest file doesn't have a <manifest> scope, only the first setting will be picked up
const newManifest: any = { manifest: {} };
for (const prop in xml) {
newManifest.manifest[prop] = xml[prop];
}
newManifest.manifest["$"]["package"] = packageName;
const xmlBuilder = new Builder();
const newManifestContent = xmlBuilder.buildObject(newManifest);
return newManifestContent;
}
private createManifestContent(packageName: string): string {
const newManifest: any = { manifest: AndroidPluginBuildService.MANIFEST_ROOT };
newManifest.manifest["$"]["package"] = packageName;
const xmlBuilder: any = new Builder();
const newManifestContent = xmlBuilder.buildObject(newManifest);
return newManifestContent;
}
private async getXml(stringContent: string): Promise<any> {
const promise = new Promise<any>((resolve, reject) =>
parseString(stringContent, (err: any, result: any) => {
if (err) {
reject(err);
} else {
resolve(result);
}
})
);
return promise;
}
private getIncludeGradleCompileDependenciesScope(includeGradleFileContent: string): Array<string> {
const indexOfDependenciesScope = includeGradleFileContent.indexOf("dependencies");
const result: Array<string> = [];
if (indexOfDependenciesScope === -1) {
return result;
}
const indexOfRepositoriesScope = includeGradleFileContent.indexOf("repositories");
let repositoriesScope = "";
if (indexOfRepositoriesScope >= 0) {
repositoriesScope = this.getScope("repositories", includeGradleFileContent);
result.push(repositoriesScope);
}
const dependenciesScope = this.getScope("dependencies", includeGradleFileContent);
result.push(dependenciesScope);
return result;
}
private getScope(scopeName: string, content: string): string {
const indexOfScopeName = content.indexOf(scopeName);
let result = "";
const openingBracket = "{";
const closingBracket = "}";
let openBrackets = 0;
let foundFirstBracket = false;
let i = indexOfScopeName;
while (i < content.length) {
const currCharacter = content[i];
if (currCharacter === openingBracket) {
if (openBrackets === 0) {
foundFirstBracket = true;
}
openBrackets++;
}
if (currCharacter === closingBracket) {
openBrackets--;
}
result += currCharacter;
if (openBrackets === 0 && foundFirstBracket) {
break;
}
i++;
}
return result;
}
/**
* Returns whether the build has completed or not
* @param {Object} options
* @param {string} options.pluginName - The name of the plugin. E.g. 'nativescript-barcodescanner'
* @param {string} options.platformsAndroidDirPath - The path to the 'plugin/src/platforms/android' directory.
* @param {string} options.aarOutputDir - The path where the aar should be copied after a successful build.
* @param {string} options.tempPluginDirPath - The path where the android plugin will be built.
*/
public async buildAar(options: IBuildOptions): Promise<boolean> {
this.validateOptions(options);
const manifestFilePath = this.getManifest(options.platformsAndroidDirPath);
const androidSourceDirectories = this.getAndroidSourceDirectories(options.platformsAndroidDirPath);
const shouldBuildAar = !!manifestFilePath || androidSourceDirectories.length > 0;
if (shouldBuildAar) {
const shortPluginName = getShortPluginName(options.pluginName);
const pluginTempDir = path.join(options.tempPluginDirPath, shortPluginName);
const pluginTempMainSrcDir = path.join(pluginTempDir, "src", "main");
await this.updateManifest(manifestFilePath, pluginTempMainSrcDir, shortPluginName);
this.copySourceSetDirectories(androidSourceDirectories, pluginTempMainSrcDir);
await this.setupGradle(pluginTempDir, options.platformsAndroidDirPath, options.projectDir);
await this.buildPlugin({ pluginDir: pluginTempDir, pluginName: options.pluginName });
this.copyAar(shortPluginName, pluginTempDir, options.aarOutputDir);
}
return shouldBuildAar;
}
private async updateManifest(manifestFilePath: string, pluginTempMainSrcDir: string, shortPluginName: string): Promise<void> {
let updatedManifestContent;
this.$fs.ensureDirectoryExists(pluginTempMainSrcDir);
const defaultPackageName = "org.nativescript." + shortPluginName;
if (manifestFilePath) {
let androidManifestContent;
try {
androidManifestContent = this.$fs.readText(manifestFilePath);
} catch (err) {
this.$errors.failWithoutHelp(`Failed to fs.readFileSync the manifest file located at ${manifestFilePath}. Error is: ${err.toString()}`);
}
updatedManifestContent = await this.updateManifestContent(androidManifestContent, defaultPackageName);
} else {
updatedManifestContent = this.createManifestContent(defaultPackageName);
}
const pathToTempAndroidManifest = path.join(pluginTempMainSrcDir, MANIFEST_FILE_NAME);
try {
this.$fs.writeFile(pathToTempAndroidManifest, updatedManifestContent);
} catch (e) {
this.$errors.failWithoutHelp(`Failed to write the updated AndroidManifest in the new location - ${pathToTempAndroidManifest}. Error is: ${e.toString()}`);
}
}
private copySourceSetDirectories(androidSourceSetDirectories: string[], pluginTempMainSrcDir: string): void {
for (const dir of androidSourceSetDirectories) {
const dirName = path.basename(dir);
const destination = path.join(pluginTempMainSrcDir, dirName);
this.$fs.ensureDirectoryExists(destination);
this.$fs.copyFile(path.join(dir, "*"), destination);
}
}
private async setupGradle(pluginTempDir: string, platformsAndroidDirPath: string, projectDir: string): Promise<void> {
const gradleTemplatePath = path.resolve(path.join(__dirname, "../../vendor/gradle-plugin"));
const allGradleTemplateFiles = path.join(gradleTemplatePath, "*");
const buildGradlePath = path.join(pluginTempDir, "build.gradle");
this.$fs.copyFile(allGradleTemplateFiles, pluginTempDir);
this.addCompileDependencies(platformsAndroidDirPath, buildGradlePath);
const runtimeGradleVersions = await this.getRuntimeGradleVersions(projectDir);
this.replaceGradleVersion(pluginTempDir, runtimeGradleVersions.gradleVersion);
this.replaceGradleAndroidPluginVersion(buildGradlePath, runtimeGradleVersions.gradleAndroidPluginVersion);
}
private async getRuntimeGradleVersions(projectDir: string): Promise<IRuntimeGradleVersions> {
const registryData = await this.$npm.getRegistryPackageData(TNS_ANDROID_RUNTIME_NAME);
let runtimeGradleVersions: IRuntimeGradleVersions = null;
if (projectDir) {
const projectRuntimeVersion = this.$platformService.getCurrentPlatformVersion(
this.$devicePlatformsConstants.Android,
this.$projectDataService.getProjectData(projectDir));
runtimeGradleVersions = this.getGradleVersions(registryData.versions[projectRuntimeVersion]);
this.$logger.trace(`Got gradle versions ${JSON.stringify(runtimeGradleVersions)} from runtime v${projectRuntimeVersion}`);
}
if (!runtimeGradleVersions) {
const latestRuntimeVersion = registryData["dist-tags"].latest;
runtimeGradleVersions = this.getGradleVersions(registryData.versions[latestRuntimeVersion]);
this.$logger.trace(`Got gradle versions ${JSON.stringify(runtimeGradleVersions)} from the latest runtime v${latestRuntimeVersion}`);
}
return runtimeGradleVersions || {};
}
private getGradleVersions(packageData: { gradle: { version: string, android: string }}): IRuntimeGradleVersions {
const packageJsonGradle = packageData && packageData.gradle;
let runtimeVersions: IRuntimeGradleVersions = null;
if (packageJsonGradle && (packageJsonGradle.version || packageJsonGradle.android)) {
runtimeVersions = {};
runtimeVersions.gradleVersion = packageJsonGradle.version;
runtimeVersions.gradleAndroidPluginVersion = packageJsonGradle.android;
}
return runtimeVersions;
}
private replaceGradleVersion(pluginTempDir: string, version: string): void {
const gradleVersion = version || AndroidBuildDefaults.GradleVersion;
const gradleVersionPlaceholder = "{{runtimeGradleVersion}}";
const gradleWrapperPropertiesPath = path.join(pluginTempDir, "gradle", "wrapper", "gradle-wrapper.properties");
this.replaceFileContent(gradleWrapperPropertiesPath, gradleVersionPlaceholder, gradleVersion);
}
private replaceGradleAndroidPluginVersion(buildGradlePath: string, version: string): void {
const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}";
const gradleAndroidPluginVersion = version || AndroidBuildDefaults.GradleAndroidPluginVersion;
this.replaceFileContent(buildGradlePath, gradleAndroidPluginVersionPlaceholder, gradleAndroidPluginVersion);
}
private replaceFileContent(filePath: string, content: string, replacement: string) {
const fileContent = this.$fs.readText(filePath);
const contentRegex = new RegExp(content, "g");
const replacedFileContent = fileContent.replace(contentRegex, replacement);
this.$fs.writeFile(filePath, replacedFileContent);
}
private addCompileDependencies(platformsAndroidDirPath: string, buildGradlePath: string): void {
const includeGradlePath = path.join(platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
if (this.$fs.exists(includeGradlePath)) {
const includeGradleContent = this.$fs.readText(includeGradlePath);
const compileDependencies = this.getIncludeGradleCompileDependenciesScope(includeGradleContent);
if (compileDependencies.length) {
this.$fs.appendFile(buildGradlePath, "\n" + compileDependencies.join("\n"));
}
}
}
private copyAar(shortPluginName: string, pluginTempDir: string, aarOutputDir: string): void {
const finalAarName = `${shortPluginName}-release.aar`;
const pathToBuiltAar = path.join(pluginTempDir, "build", "outputs", "aar", finalAarName);
if (this.$fs.exists(pathToBuiltAar)) {
try {
if (aarOutputDir) {
this.$fs.copyFile(pathToBuiltAar, path.join(aarOutputDir, `${shortPluginName}.aar`));
}
} catch (e) {
this.$errors.failWithoutHelp(`Failed to copy built aar to destination. ${e.message}`);
}
} else {
this.$errors.failWithoutHelp(`No built aar found at ${pathToBuiltAar}`);
}
}
/**
* @param {Object} options
* @param {string} options.platformsAndroidDirPath - The path to the 'plugin/src/platforms/android' directory.
*/
public migrateIncludeGradle(options: IBuildOptions): boolean {
this.validatePlatformsAndroidDirPathOption(options);
const includeGradleFilePath = path.join(options.platformsAndroidDirPath, INCLUDE_GRADLE_NAME);
if (this.$fs.exists(includeGradleFilePath)) {
let includeGradleFileContent: string;
try {
includeGradleFileContent = this.$fs.readFile(includeGradleFilePath).toString();
} catch (err) {
this.$errors.failWithoutHelp(`Failed to fs.readFileSync the include.gradle file located at ${includeGradleFilePath}. Error is: ${err.toString()}`);
}
const productFlavorsScope = this.getScope("productFlavors", includeGradleFileContent);
try {
const newIncludeGradleFileContent = includeGradleFileContent.replace(productFlavorsScope, "");
this.$fs.writeFile(includeGradleFilePath, newIncludeGradleFileContent);
return true;
} catch (e) {
this.$errors.failWithoutHelp(`Failed to write the updated include.gradle ` +
`in - ${includeGradleFilePath}. Error is: ${e.toString()}`);
}
}
return false;
}
@hook("buildAndroidPlugin")
private async buildPlugin(pluginBuildSettings: IBuildAndroidPluginData): Promise<void> {
if (!pluginBuildSettings.androidToolsInfo) {
this.$androidToolsInfo.validateInfo({ showWarningsAsErrors: true, validateTargetSdk: true });
pluginBuildSettings.androidToolsInfo = this.$androidToolsInfo.getToolsInfo();
}
const gradlew = this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew";
const localArgs = [
"-p",
pluginBuildSettings.pluginDir,
"assembleRelease",
`-PcompileSdk=android-${pluginBuildSettings.androidToolsInfo.compileSdkVersion}`,
`-PbuildToolsVersion=${pluginBuildSettings.androidToolsInfo.buildToolsVersion}`,
`-PsupportVersion=${pluginBuildSettings.androidToolsInfo.supportRepositoryVersion}`
];
try {
await this.$childProcess.spawnFromEvent(gradlew, localArgs, "close", { cwd: pluginBuildSettings.pluginDir });
} catch (err) {
this.$errors.failWithoutHelp(`Failed to build plugin ${pluginBuildSettings.pluginName} : \n${err}`);
}
}
private validateOptions(options: IBuildOptions): void {
if (!options) {
this.$errors.failWithoutHelp("Android plugin cannot be built without passing an 'options' object.");
}
if (!options.pluginName) {
this.$logger.info("No plugin name provided, defaulting to 'myPlugin'.");
}
if (!options.aarOutputDir) {
this.$logger.info("No aarOutputDir provided, defaulting to the build outputs directory of the plugin");
}
if (!options.tempPluginDirPath) {
this.$errors.failWithoutHelp("Android plugin cannot be built without passing the path to a directory where the temporary project should be built.");
}
this.validatePlatformsAndroidDirPathOption(options);
}
private validatePlatformsAndroidDirPathOption(options: IBuildOptions): void {
if (!options) {
this.$errors.failWithoutHelp("Android plugin cannot be built without passing an 'options' object.");
}
if (!options.platformsAndroidDirPath) {
this.$errors.failWithoutHelp("Android plugin cannot be built without passing the path to the platforms/android dir.");
}
}
}
$injector.register("androidPluginBuildService", AndroidPluginBuildService);