1
1
import * as path from "path" ;
2
- import * as shell from "shelljs " ;
2
+ import { MANIFEST_FILE_NAME , INCLUDE_GRADLE_NAME , ASSETS_DIR , RESOURCES_DIR } from "../constants " ;
3
3
import { Builder , parseString } from "xml2js" ;
4
+ import { ILogger } from "log4js" ;
4
5
5
6
export class AndroidPluginBuildService implements IAndroidPluginBuildService {
6
7
7
8
constructor ( private $fs : IFileSystem ,
8
9
private $childProcess : IChildProcess ,
9
10
private $hostInfo : IHostInfo ,
10
- private $androidToolsInfo : IAndroidToolsInfo ) { }
11
+ private $androidToolsInfo : IAndroidToolsInfo ,
12
+ private $logger : ILogger ) { }
11
13
12
- private static ANDROID_MANIFEST_XML = "AndroidManifest.xml" ;
13
- private static INCLUDE_GRADLE = "include.gradle" ;
14
14
private static MANIFEST_ROOT = {
15
15
$ : {
16
16
"xmlns:android" : "http://schemas.android.com/apk/res/android"
@@ -19,7 +19,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
19
19
private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin" ;
20
20
21
21
private getAndroidSourceDirectories ( source : string ) : Array < string > {
22
- const directories = [ "res" , "java" , "assets" , "jniLibs" ] ;
22
+ const directories = [ RESOURCES_DIR , "java" , ASSETS_DIR , "jniLibs" ] ;
23
23
const resultArr : Array < string > = [ ] ;
24
24
25
25
this . $fs . enumerateFilesInDirectorySync ( source , ( file , stat ) => {
@@ -33,7 +33,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
33
33
}
34
34
35
35
private getManifest ( platformsDir : string ) : string {
36
- const manifest = path . join ( platformsDir , AndroidPluginBuildService . ANDROID_MANIFEST_XML ) ;
36
+ const manifest = path . join ( platformsDir , MANIFEST_FILE_NAME ) ;
37
37
return this . $fs . exists ( manifest ) ? manifest : null ;
38
38
}
39
39
@@ -42,10 +42,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
42
42
}
43
43
44
44
private async updateManifestContent ( oldManifestContent : string , defaultPackageName : string ) : Promise < string > {
45
- const content = oldManifestContent ;
46
- let newManifestContent ;
47
-
48
- let xml : any = await this . getXml ( content ) ;
45
+ let xml : any = await this . getXml ( oldManifestContent ) ;
49
46
50
47
let packageName = defaultPackageName ;
51
48
// if the manifest file is full-featured and declares settings inside the manifest scope
@@ -67,17 +64,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
67
64
newManifest . manifest [ "$" ] [ "package" ] = packageName ;
68
65
69
66
const xmlBuilder = new Builder ( ) ;
70
- newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
67
+ const newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
71
68
72
69
return newManifestContent ;
73
70
}
74
71
75
72
private createManifestContent ( packageName : string ) : string {
76
- let newManifestContent ;
77
73
const newManifest : any = { manifest : AndroidPluginBuildService . MANIFEST_ROOT } ;
78
74
newManifest . manifest [ "$" ] [ "package" ] = packageName ;
79
75
const xmlBuilder : any = new Builder ( ) ;
80
- newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
76
+ const newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
81
77
82
78
return newManifestContent ;
83
79
}
@@ -96,10 +92,6 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
96
92
return promise ;
97
93
}
98
94
99
- private copyRecursive ( source : string , destination : string ) : void {
100
- shell . cp ( "-R" , source , destination ) ;
101
- }
102
-
103
95
private getIncludeGradleCompileDependenciesScope ( includeGradleFileContent : string ) : Array < string > {
104
96
const indexOfDependenciesScope = includeGradleFileContent . indexOf ( "dependencies" ) ;
105
97
const result : Array < string > = [ ] ;
@@ -125,23 +117,23 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
125
117
private getScope ( scopeName : string , content : string ) : string {
126
118
const indexOfScopeName = content . indexOf ( scopeName ) ;
127
119
let result = "" ;
128
- const OPENING_BRACKET = "{" ;
129
- const CLOSING_BRACKET = "}" ;
120
+ const openingBracket = "{" ;
121
+ const closingBracket = "}" ;
130
122
let openBrackets = 0 ;
131
123
let foundFirstBracket = false ;
132
124
133
125
let i = indexOfScopeName ;
134
126
while ( i < content . length ) {
135
127
const currCharacter = content [ i ] ;
136
- if ( currCharacter === OPENING_BRACKET ) {
128
+ if ( currCharacter === openingBracket ) {
137
129
if ( openBrackets === 0 ) {
138
130
foundFirstBracket = true ;
139
131
}
140
132
141
133
openBrackets ++ ;
142
134
}
143
135
144
- if ( currCharacter === CLOSING_BRACKET ) {
136
+ if ( currCharacter === closingBracket ) {
145
137
openBrackets -- ;
146
138
}
147
139
@@ -180,7 +172,6 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
180
172
// find manifest file
181
173
//// prepare manifest file content
182
174
const manifestFilePath = this . getManifest ( options . platformsAndroidDirPath ) ;
183
- let updatedManifestContent ;
184
175
let shouldBuildAar = false ;
185
176
186
177
// look for AndroidManifest.xml
@@ -197,6 +188,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
197
188
198
189
// if a manifest OR/AND resource files are present - write files, build plugin
199
190
if ( shouldBuildAar ) {
191
+ let updatedManifestContent ;
200
192
this . $fs . ensureDirectoryExists ( newPluginMainSrcDir ) ;
201
193
202
194
if ( manifestFilePath ) {
@@ -215,7 +207,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
215
207
}
216
208
217
209
// write the AndroidManifest in the temp-dir/plugin-name/src/main
218
- const pathToNewAndroidManifest = path . join ( newPluginMainSrcDir , AndroidPluginBuildService . ANDROID_MANIFEST_XML ) ;
210
+ const pathToNewAndroidManifest = path . join ( newPluginMainSrcDir , MANIFEST_FILE_NAME ) ;
219
211
try {
220
212
this . $fs . writeFile ( pathToNewAndroidManifest , updatedManifestContent ) ;
221
213
} catch ( e ) {
@@ -231,14 +223,14 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
231
223
const destination = path . join ( newPluginMainSrcDir , dirName ) ;
232
224
this . $fs . ensureDirectoryExists ( destination ) ;
233
225
234
- this . copyRecursive ( path . join ( dir , "*" ) , destination ) ;
226
+ this . $fs . copyFile ( path . join ( dir , "*" ) , destination ) ;
235
227
}
236
228
237
229
// copy the preconfigured gradle android library project template to the temporary android library
238
- this . copyRecursive ( path . join ( path . resolve ( path . join ( __dirname , AndroidPluginBuildService . ANDROID_PLUGIN_GRADLE_TEMPLATE ) , "*" ) ) , newPluginDir ) ;
230
+ this . $fs . copyFile ( path . join ( path . resolve ( path . join ( __dirname , AndroidPluginBuildService . ANDROID_PLUGIN_GRADLE_TEMPLATE ) , "*" ) ) , newPluginDir ) ;
239
231
240
232
// 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
241
- const includeGradlePath = path . join ( options . platformsAndroidDirPath , "include.gradle" ) ;
233
+ const includeGradlePath = path . join ( options . platformsAndroidDirPath , INCLUDE_GRADLE_NAME ) ;
242
234
if ( this . $fs . exists ( includeGradlePath ) ) {
243
235
const includeGradleContent = this . $fs . readText ( includeGradlePath ) ;
244
236
const repositoriesAndDependenciesScopes = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
@@ -280,7 +272,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
280
272
if ( this . $fs . exists ( pathToBuiltAar ) ) {
281
273
try {
282
274
if ( options . aarOutputDir ) {
283
- this . copyRecursive ( pathToBuiltAar , path . join ( options . aarOutputDir , `${ shortPluginName } .aar` ) ) ;
275
+ this . $fs . copyFile ( pathToBuiltAar , path . join ( options . aarOutputDir , `${ shortPluginName } .aar` ) ) ;
284
276
}
285
277
} catch ( e ) {
286
278
throw new Error ( `Failed to copy built aar to destination. ${ e . message } ` ) ;
@@ -302,7 +294,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
302
294
public migrateIncludeGradle ( options : IBuildOptions ) : void {
303
295
this . validatePlatformsAndroidDirPathOption ( options ) ;
304
296
305
- const includeGradleFilePath = path . join ( options . platformsAndroidDirPath , AndroidPluginBuildService . INCLUDE_GRADLE ) ;
297
+ const includeGradleFilePath = path . join ( options . platformsAndroidDirPath , INCLUDE_GRADLE_NAME ) ;
306
298
307
299
if ( this . $fs . exists ( includeGradleFilePath ) ) {
308
300
let includeGradleFileContent : string ;
@@ -330,11 +322,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
330
322
}
331
323
332
324
if ( ! options . pluginName ) {
333
- console . log ( "No plugin name provided, defaulting to 'myPlugin'." ) ;
325
+ this . $logger . info ( "No plugin name provided, defaulting to 'myPlugin'." ) ;
334
326
}
335
327
336
328
if ( ! options . aarOutputDir ) {
337
- console . log ( "No aarOutputDir provided, defaulting to the build outputs directory of the plugin" ) ;
329
+ this . $logger . info ( "No aarOutputDir provided, defaulting to the build outputs directory of the plugin" ) ;
338
330
}
339
331
340
332
if ( ! options . tempPluginDirPath ) {
0 commit comments