1
1
import * as path from "path" ;
2
2
import * as shell from "shelljs" ;
3
-
4
- const xml2js = require ( "xml2js" ) ;
3
+ import { Builder , parseString } from "xml2js" ;
5
4
6
5
export class AndroidPluginBuildService implements IAndroidPluginBuildService {
7
6
@@ -17,11 +16,12 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
17
16
"xmlns:android" : "http://schemas.android.com/apk/res/android"
18
17
}
19
18
} ;
20
- private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "gradle-plugin" ;
19
+ private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/ gradle-plugin" ;
21
20
22
21
private getAndroidSourceDirectories ( source : string ) : Array < string > {
23
22
const directories = [ "res" , "java" , "assets" , "jniLibs" ] ;
24
23
const resultArr : Array < string > = [ ] ;
24
+
25
25
this . $fs . enumerateFilesInDirectorySync ( source , ( file , stat ) => {
26
26
if ( stat . isDirectory ( ) && _ . some ( directories , ( element ) => file . endsWith ( element ) ) ) {
27
27
resultArr . push ( file ) ;
@@ -32,11 +32,9 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
32
32
return resultArr ;
33
33
}
34
34
35
- private getManifest ( platformsDir : string ) {
36
- const manifests = this . $fs
37
- . readDirectory ( platformsDir )
38
- . filter ( fileName => fileName === AndroidPluginBuildService . ANDROID_MANIFEST_XML ) ;
39
- return manifests . length > 0 ? path . join ( platformsDir , manifests [ 0 ] ) : null ;
35
+ private getManifest ( platformsDir : string ) : string {
36
+ const manifest = path . join ( platformsDir , AndroidPluginBuildService . ANDROID_MANIFEST_XML ) ;
37
+ return this . $fs . exists ( manifest ) ? manifest : null ;
40
38
}
41
39
42
40
private getShortPluginName ( pluginName : string ) : string {
@@ -68,26 +66,24 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
68
66
69
67
newManifest . manifest [ "$" ] [ "package" ] = packageName ;
70
68
71
- const xmlBuilder = new xml2js . Builder ( ) ;
69
+ const xmlBuilder = new Builder ( ) ;
72
70
newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
73
71
74
72
return newManifestContent ;
75
73
}
76
74
77
- private createManifestContent ( packageName : string ) {
75
+ private createManifestContent ( packageName : string ) : string {
78
76
let newManifestContent ;
79
77
const newManifest : any = { manifest : AndroidPluginBuildService . MANIFEST_ROOT } ;
80
78
newManifest . manifest [ "$" ] [ "package" ] = packageName ;
81
- const xmlBuilder : any = new xml2js . Builder ( ) ;
79
+ const xmlBuilder : any = new Builder ( ) ;
82
80
newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
83
81
84
82
return newManifestContent ;
85
83
}
86
84
87
- private async getXml ( stringContent : string ) {
88
- const parseString = xml2js . parseString ;
89
-
90
- const promise = new Promise ( ( resolve , reject ) =>
85
+ private async getXml ( stringContent : string ) : Promise < any > {
86
+ const promise = new Promise < any > ( ( resolve , reject ) =>
91
87
parseString ( stringContent , ( err : any , result : any ) => {
92
88
if ( err ) {
93
89
reject ( err ) ;
@@ -100,11 +96,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
100
96
return promise ;
101
97
}
102
98
103
- private copyRecursive ( source : string , destination : string ) {
99
+ private copyRecursive ( source : string , destination : string ) : void {
104
100
shell . cp ( "-R" , source , destination ) ;
105
101
}
106
102
107
- private getIncludeGradleCompileDependenciesScope ( includeGradleFileContent : string ) {
103
+ private getIncludeGradleCompileDependenciesScope ( includeGradleFileContent : string ) : Array < string > {
108
104
const indexOfDependenciesScope = includeGradleFileContent . indexOf ( "dependencies" ) ;
109
105
const result : Array < string > = [ ] ;
110
106
@@ -126,7 +122,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
126
122
return result ;
127
123
}
128
124
129
- private getScope ( scopeName : string , content : string ) {
125
+ private getScope ( scopeName : string , content : string ) : string {
130
126
const indexOfScopeName = content . indexOf ( scopeName ) ;
131
127
let result = "" ;
132
128
const OPENING_BRACKET = "{" ;
@@ -206,9 +202,9 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
206
202
if ( manifestFilePath ) {
207
203
let androidManifestContent ;
208
204
try {
209
- androidManifestContent = this . $fs . readFile ( manifestFilePath ) . toString ( ) ;
205
+ androidManifestContent = this . $fs . readText ( manifestFilePath ) ;
210
206
} catch ( err ) {
211
- throw Error (
207
+ throw new Error (
212
208
`Failed to fs.readFileSync the manifest file located at ${ manifestFilePath } `
213
209
) ;
214
210
}
@@ -223,7 +219,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
223
219
try {
224
220
this . $fs . writeFile ( pathToNewAndroidManifest , updatedManifestContent ) ;
225
221
} catch ( e ) {
226
- throw Error ( `Failed to write the updated AndroidManifest in the new location - ${ pathToNewAndroidManifest } ` ) ;
222
+ throw new Error ( `Failed to write the updated AndroidManifest in the new location - ${ pathToNewAndroidManifest } ` ) ;
227
223
}
228
224
229
225
// copy all android sourceset directories to the new temporary plugin dir
@@ -241,18 +237,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
241
237
// copy the preconfigured gradle android library project template to the temporary android library
242
238
this . copyRecursive ( path . join ( path . resolve ( path . join ( __dirname , AndroidPluginBuildService . ANDROID_PLUGIN_GRADLE_TEMPLATE ) , "*" ) ) , newPluginDir ) ;
243
239
244
- // sometimes the AndroidManifest.xml or certain resources in /res may have a compile dependency to a lbirary referenced in include.gradle. Make sure to compile the plugin with a compile dependency to those libraries
240
+ // sometimes the AndroidManifest.xml or certain resources in /res may have a compile dependency to a library referenced in include.gradle. Make sure to compile the plugin with a compile dependency to those libraries
245
241
const includeGradlePath = path . join ( options . platformsAndroidDirPath , "include.gradle" ) ;
246
242
if ( this . $fs . exists ( includeGradlePath ) ) {
247
- const includeGradleContent = this . $fs . readFile ( includeGradlePath )
248
- . toString ( ) ;
243
+ const includeGradleContent = this . $fs . readText ( includeGradlePath ) ;
249
244
const repositoriesAndDependenciesScopes = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
250
245
251
246
// dependencies { } object was found - append dependencies scope
252
247
if ( repositoriesAndDependenciesScopes . length > 0 ) {
253
248
const buildGradlePath = path . join ( newPluginDir , "build.gradle" ) ;
254
- this . $fs . appendFile ( buildGradlePath , "\n" + repositoriesAndDependenciesScopes . join ( "\n" )
255
- ) ;
249
+ this . $fs . appendFile ( buildGradlePath , "\n" + repositoriesAndDependenciesScopes . join ( "\n" ) ) ;
256
250
}
257
251
}
258
252
@@ -277,7 +271,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
277
271
try {
278
272
await this . $childProcess . exec ( localArgs . join ( " " ) , { cwd : newPluginDir } ) ;
279
273
} catch ( err ) {
280
- throw Error ( `Failed to build plugin ${ options . pluginName } : \n${ err } ` ) ;
274
+ throw new Error ( `Failed to build plugin ${ options . pluginName } : \n${ err } ` ) ;
281
275
}
282
276
283
277
const finalAarName = `${ shortPluginName } -release.aar` ;
@@ -289,12 +283,12 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
289
283
this . copyRecursive ( pathToBuiltAar , path . join ( options . aarOutputDir , `${ shortPluginName } .aar` ) ) ;
290
284
}
291
285
} catch ( e ) {
292
- throw Error ( `Failed to copy built aar to destination. ${ e . message } ` ) ;
286
+ throw new Error ( `Failed to copy built aar to destination. ${ e . message } ` ) ;
293
287
}
294
288
295
289
return true ;
296
290
} else {
297
- throw Error ( `No built aar found at ${ pathToBuiltAar } ` ) ;
291
+ throw new Error ( `No built aar found at ${ pathToBuiltAar } ` ) ;
298
292
}
299
293
}
300
294
@@ -315,7 +309,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
315
309
try {
316
310
includeGradleFileContent = this . $fs . readFile ( includeGradleFilePath ) . toString ( ) ;
317
311
} catch ( err ) {
318
- throw Error ( `Failed to fs.readFileSync the include.gradle file located at ${ includeGradleFilePath } ` ) ;
312
+ throw new Error ( `Failed to fs.readFileSync the include.gradle file located at ${ includeGradleFilePath } ` ) ;
319
313
}
320
314
321
315
const productFlavorsScope = this . getScope ( "productFlavors" , includeGradleFileContent ) ;
@@ -325,14 +319,14 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
325
319
this . $fs . writeFile ( includeGradleFilePath , newIncludeGradleFileContent ) ;
326
320
327
321
} catch ( e ) {
328
- throw Error ( `Failed to write the updated include.gradle in - ${ includeGradleFilePath } ` ) ;
322
+ throw new Error ( `Failed to write the updated include.gradle in - ${ includeGradleFilePath } ` ) ;
329
323
}
330
324
}
331
325
}
332
326
333
- private validateOptions ( options : IBuildOptions ) {
327
+ private validateOptions ( options : IBuildOptions ) : void {
334
328
if ( ! options ) {
335
- throw Error ( "Android plugin cannot be built without passing an 'options' object." ) ;
329
+ throw new Error ( "Android plugin cannot be built without passing an 'options' object." ) ;
336
330
}
337
331
338
332
if ( ! options . pluginName ) {
@@ -344,19 +338,19 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
344
338
}
345
339
346
340
if ( ! options . tempPluginDirPath ) {
347
- throw Error ( "Android plugin cannot be built without passing the path to a directory where the temporary project should be built." ) ;
341
+ throw new Error ( "Android plugin cannot be built without passing the path to a directory where the temporary project should be built." ) ;
348
342
}
349
343
350
344
this . validatePlatformsAndroidDirPathOption ( options ) ;
351
345
}
352
346
353
- private validatePlatformsAndroidDirPathOption ( options : IBuildOptions ) {
347
+ private validatePlatformsAndroidDirPathOption ( options : IBuildOptions ) : void {
354
348
if ( ! options ) {
355
- throw Error ( "Android plugin cannot be built without passing an 'options' object." ) ;
349
+ throw new Error ( "Android plugin cannot be built without passing an 'options' object." ) ;
356
350
}
357
351
358
352
if ( ! options . platformsAndroidDirPath ) {
359
- throw Error ( "Android plugin cannot be built without passing the path to the platforms/android dir." ) ;
353
+ throw new Error ( "Android plugin cannot be built without passing the path to the platforms/android dir." ) ;
360
354
}
361
355
}
362
356
0 commit comments