11import * as path from "path" ;
2- import * as shell from "shelljs " ;
2+ import { MANIFEST_FILE_NAME , INCLUDE_GRADLE_NAME , ASSETS_DIR , RESOURCES_DIR } from "../constants " ;
33import { Builder , parseString } from "xml2js" ;
4+ import { ILogger } from "log4js" ;
45
56export class AndroidPluginBuildService implements IAndroidPluginBuildService {
67
78 constructor ( private $fs : IFileSystem ,
89 private $childProcess : IChildProcess ,
910 private $hostInfo : IHostInfo ,
10- private $androidToolsInfo : IAndroidToolsInfo ) { }
11+ private $androidToolsInfo : IAndroidToolsInfo ,
12+ private $logger : ILogger ) { }
1113
12- private static ANDROID_MANIFEST_XML = "AndroidManifest.xml" ;
13- private static INCLUDE_GRADLE = "include.gradle" ;
1414 private static MANIFEST_ROOT = {
1515 $ : {
1616 "xmlns:android" : "http://schemas.android.com/apk/res/android"
@@ -19,7 +19,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
1919 private static ANDROID_PLUGIN_GRADLE_TEMPLATE = "../../vendor/gradle-plugin" ;
2020
2121 private getAndroidSourceDirectories ( source : string ) : Array < string > {
22- const directories = [ "res" , "java" , "assets" , "jniLibs" ] ;
22+ const directories = [ RESOURCES_DIR , "java" , ASSETS_DIR , "jniLibs" ] ;
2323 const resultArr : Array < string > = [ ] ;
2424
2525 this . $fs . enumerateFilesInDirectorySync ( source , ( file , stat ) => {
@@ -33,7 +33,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
3333 }
3434
3535 private getManifest ( platformsDir : string ) : string {
36- const manifest = path . join ( platformsDir , AndroidPluginBuildService . ANDROID_MANIFEST_XML ) ;
36+ const manifest = path . join ( platformsDir , MANIFEST_FILE_NAME ) ;
3737 return this . $fs . exists ( manifest ) ? manifest : null ;
3838 }
3939
@@ -42,10 +42,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
4242 }
4343
4444 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 ) ;
4946
5047 let packageName = defaultPackageName ;
5148 // if the manifest file is full-featured and declares settings inside the manifest scope
@@ -67,17 +64,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
6764 newManifest . manifest [ "$" ] [ "package" ] = packageName ;
6865
6966 const xmlBuilder = new Builder ( ) ;
70- newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
67+ const newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
7168
7269 return newManifestContent ;
7370 }
7471
7572 private createManifestContent ( packageName : string ) : string {
76- let newManifestContent ;
7773 const newManifest : any = { manifest : AndroidPluginBuildService . MANIFEST_ROOT } ;
7874 newManifest . manifest [ "$" ] [ "package" ] = packageName ;
7975 const xmlBuilder : any = new Builder ( ) ;
80- newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
76+ const newManifestContent = xmlBuilder . buildObject ( newManifest ) ;
8177
8278 return newManifestContent ;
8379 }
@@ -96,10 +92,6 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
9692 return promise ;
9793 }
9894
99- private copyRecursive ( source : string , destination : string ) : void {
100- shell . cp ( "-R" , source , destination ) ;
101- }
102-
10395 private getIncludeGradleCompileDependenciesScope ( includeGradleFileContent : string ) : Array < string > {
10496 const indexOfDependenciesScope = includeGradleFileContent . indexOf ( "dependencies" ) ;
10597 const result : Array < string > = [ ] ;
@@ -125,23 +117,23 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
125117 private getScope ( scopeName : string , content : string ) : string {
126118 const indexOfScopeName = content . indexOf ( scopeName ) ;
127119 let result = "" ;
128- const OPENING_BRACKET = "{" ;
129- const CLOSING_BRACKET = "}" ;
120+ const openingBracket = "{" ;
121+ const closingBracket = "}" ;
130122 let openBrackets = 0 ;
131123 let foundFirstBracket = false ;
132124
133125 let i = indexOfScopeName ;
134126 while ( i < content . length ) {
135127 const currCharacter = content [ i ] ;
136- if ( currCharacter === OPENING_BRACKET ) {
128+ if ( currCharacter === openingBracket ) {
137129 if ( openBrackets === 0 ) {
138130 foundFirstBracket = true ;
139131 }
140132
141133 openBrackets ++ ;
142134 }
143135
144- if ( currCharacter === CLOSING_BRACKET ) {
136+ if ( currCharacter === closingBracket ) {
145137 openBrackets -- ;
146138 }
147139
@@ -180,7 +172,6 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
180172 // find manifest file
181173 //// prepare manifest file content
182174 const manifestFilePath = this . getManifest ( options . platformsAndroidDirPath ) ;
183- let updatedManifestContent ;
184175 let shouldBuildAar = false ;
185176
186177 // look for AndroidManifest.xml
@@ -197,6 +188,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
197188
198189 // if a manifest OR/AND resource files are present - write files, build plugin
199190 if ( shouldBuildAar ) {
191+ let updatedManifestContent ;
200192 this . $fs . ensureDirectoryExists ( newPluginMainSrcDir ) ;
201193
202194 if ( manifestFilePath ) {
@@ -215,7 +207,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
215207 }
216208
217209 // 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 ) ;
219211 try {
220212 this . $fs . writeFile ( pathToNewAndroidManifest , updatedManifestContent ) ;
221213 } catch ( e ) {
@@ -231,14 +223,14 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
231223 const destination = path . join ( newPluginMainSrcDir , dirName ) ;
232224 this . $fs . ensureDirectoryExists ( destination ) ;
233225
234- this . copyRecursive ( path . join ( dir , "*" ) , destination ) ;
226+ this . $fs . copyFile ( path . join ( dir , "*" ) , destination ) ;
235227 }
236228
237229 // 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 ) ;
239231
240232 // 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 ) ;
242234 if ( this . $fs . exists ( includeGradlePath ) ) {
243235 const includeGradleContent = this . $fs . readText ( includeGradlePath ) ;
244236 const repositoriesAndDependenciesScopes = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
@@ -280,7 +272,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
280272 if ( this . $fs . exists ( pathToBuiltAar ) ) {
281273 try {
282274 if ( options . aarOutputDir ) {
283- this . copyRecursive ( pathToBuiltAar , path . join ( options . aarOutputDir , `${ shortPluginName } .aar` ) ) ;
275+ this . $fs . copyFile ( pathToBuiltAar , path . join ( options . aarOutputDir , `${ shortPluginName } .aar` ) ) ;
284276 }
285277 } catch ( e ) {
286278 throw new Error ( `Failed to copy built aar to destination. ${ e . message } ` ) ;
@@ -302,7 +294,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
302294 public migrateIncludeGradle ( options : IBuildOptions ) : void {
303295 this . validatePlatformsAndroidDirPathOption ( options ) ;
304296
305- const includeGradleFilePath = path . join ( options . platformsAndroidDirPath , AndroidPluginBuildService . INCLUDE_GRADLE ) ;
297+ const includeGradleFilePath = path . join ( options . platformsAndroidDirPath , INCLUDE_GRADLE_NAME ) ;
306298
307299 if ( this . $fs . exists ( includeGradleFilePath ) ) {
308300 let includeGradleFileContent : string ;
@@ -330,11 +322,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
330322 }
331323
332324 if ( ! options . pluginName ) {
333- console . log ( "No plugin name provided, defaulting to 'myPlugin'." ) ;
325+ this . $logger . info ( "No plugin name provided, defaulting to 'myPlugin'." ) ;
334326 }
335327
336328 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" ) ;
338330 }
339331
340332 if ( ! options . tempPluginDirPath ) {
0 commit comments