1
1
import * as path from "path" ;
2
- import { MANIFEST_FILE_NAME , INCLUDE_GRADLE_NAME , ASSETS_DIR , RESOURCES_DIR , TNS_ANDROID_RUNTIME_NAME } from "../constants" ;
2
+ import { MANIFEST_FILE_NAME , INCLUDE_GRADLE_NAME , ASSETS_DIR , RESOURCES_DIR , TNS_ANDROID_RUNTIME_NAME , AndroidBuildDefaults } from "../constants" ;
3
3
import { getShortPluginName , hook } from "../common/helpers" ;
4
4
import { Builder , parseString } from "xml2js" ;
5
5
import { ILogger } from "log4js" ;
@@ -24,7 +24,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
24
24
private $logger : ILogger ,
25
25
private $npm : INodePackageManager ,
26
26
private $projectDataService : IProjectDataService ,
27
- private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ) { }
27
+ private $devicePlatformsConstants : Mobile . IDevicePlatformsConstants ,
28
+ private $errors : IErrors ) { }
28
29
29
30
private static MANIFEST_ROOT = {
30
31
$ : {
@@ -170,16 +171,16 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
170
171
public async buildAar ( options : IBuildOptions ) : Promise < boolean > {
171
172
this . validateOptions ( options ) ;
172
173
const manifestFilePath = this . getManifest ( options . platformsAndroidDirPath ) ;
173
- const androidSourceSetDirectories = this . getAndroidSourceDirectories ( options . platformsAndroidDirPath ) ;
174
- const shouldBuildAar = ! ! manifestFilePath || androidSourceSetDirectories . length > 0 ;
174
+ const androidSourceDirectories = this . getAndroidSourceDirectories ( options . platformsAndroidDirPath ) ;
175
+ const shouldBuildAar = ! ! manifestFilePath || androidSourceDirectories . length > 0 ;
175
176
176
177
if ( shouldBuildAar ) {
177
178
const shortPluginName = getShortPluginName ( options . pluginName ) ;
178
179
const pluginTempDir = path . join ( options . tempPluginDirPath , shortPluginName ) ;
179
180
const pluginTempMainSrcDir = path . join ( pluginTempDir , "src" , "main" ) ;
180
181
181
182
await this . updateManifest ( manifestFilePath , pluginTempMainSrcDir , shortPluginName ) ;
182
- this . copySourceSetDirectories ( androidSourceSetDirectories , pluginTempMainSrcDir ) ;
183
+ this . copySourceSetDirectories ( androidSourceDirectories , pluginTempMainSrcDir ) ;
183
184
await this . setupGradle ( pluginTempDir , options . platformsAndroidDirPath , options . projectDir ) ;
184
185
await this . buildPlugin ( { pluginDir : pluginTempDir , pluginName : options . pluginName } ) ;
185
186
this . copyAar ( shortPluginName , pluginTempDir , options . aarOutputDir ) ;
@@ -197,9 +198,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
197
198
try {
198
199
androidManifestContent = this . $fs . readText ( manifestFilePath ) ;
199
200
} catch ( err ) {
200
- throw new Error (
201
- `Failed to fs.readFileSync the manifest file located at ${ manifestFilePath } `
202
- ) ;
201
+ this . $errors . failWithoutHelp ( `Failed to fs.readFileSync the manifest file located at ${ manifestFilePath } . Error is: ${ err . toString ( ) } ` ) ;
203
202
}
204
203
205
204
updatedManifestContent = await this . updateManifestContent ( androidManifestContent , defaultPackageName ) ;
@@ -211,15 +210,13 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
211
210
try {
212
211
this . $fs . writeFile ( pathToTempAndroidManifest , updatedManifestContent ) ;
213
212
} catch ( e ) {
214
- throw new Error ( `Failed to write the updated AndroidManifest in the new location - ${ pathToTempAndroidManifest } ` ) ;
213
+ this . $errors . failWithoutHelp ( `Failed to write the updated AndroidManifest in the new location - ${ pathToTempAndroidManifest } . Error is: ${ e . toString ( ) } ` ) ;
215
214
}
216
215
}
217
216
218
217
private copySourceSetDirectories ( androidSourceSetDirectories : string [ ] , pluginTempMainSrcDir : string ) : void {
219
218
for ( const dir of androidSourceSetDirectories ) {
220
- const dirNameParts = dir . split ( path . sep ) ;
221
- // get only the last subdirectory of the entire path string. e.g. 'res', 'java', etc.
222
- const dirName = dirNameParts [ dirNameParts . length - 1 ] ;
219
+ const dirName = path . basename ( dir ) ;
223
220
const destination = path . join ( pluginTempMainSrcDir , dirName ) ;
224
221
225
222
this . $fs . ensureDirectoryExists ( destination ) ;
@@ -272,7 +269,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
272
269
}
273
270
274
271
private replaceGradleVersion ( pluginTempDir : string , version : string ) : void {
275
- const gradleVersion = version || "4.4" ;
272
+ const gradleVersion = version || AndroidBuildDefaults . GradleVersion ;
276
273
const gradleVersionPlaceholder = "{{runtimeGradleVersion}}" ;
277
274
const gradleWrapperPropertiesPath = path . join ( pluginTempDir , "gradle" , "wrapper" , "gradle-wrapper.properties" ) ;
278
275
@@ -281,7 +278,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
281
278
282
279
private replaceGradleAndroidPluginVersion ( buildGradlePath : string , version : string ) : void {
283
280
const gradleAndroidPluginVersionPlaceholder = "{{runtimeAndroidPluginVersion}}" ;
284
- const gradleAndroidPluginVersion = version || "3.1.2" ;
281
+ const gradleAndroidPluginVersion = version || AndroidBuildDefaults . GradleAndroidPluginVersion ;
285
282
286
283
this . replaceFileContent ( buildGradlePath , gradleAndroidPluginVersionPlaceholder , gradleAndroidPluginVersion ) ;
287
284
}
@@ -297,10 +294,10 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
297
294
const includeGradlePath = path . join ( platformsAndroidDirPath , INCLUDE_GRADLE_NAME ) ;
298
295
if ( this . $fs . exists ( includeGradlePath ) ) {
299
296
const includeGradleContent = this . $fs . readText ( includeGradlePath ) ;
300
- const repositoriesAndDependenciesScopes = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
297
+ const compileDependencies = this . getIncludeGradleCompileDependenciesScope ( includeGradleContent ) ;
301
298
302
- if ( repositoriesAndDependenciesScopes . length > 0 ) {
303
- this . $fs . appendFile ( buildGradlePath , "\n" + repositoriesAndDependenciesScopes . join ( "\n" ) ) ;
299
+ if ( compileDependencies . length ) {
300
+ this . $fs . appendFile ( buildGradlePath , "\n" + compileDependencies . join ( "\n" ) ) ;
304
301
}
305
302
}
306
303
}
@@ -315,10 +312,10 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
315
312
this . $fs . copyFile ( pathToBuiltAar , path . join ( aarOutputDir , `${ shortPluginName } .aar` ) ) ;
316
313
}
317
314
} catch ( e ) {
318
- throw new Error ( `Failed to copy built aar to destination. ${ e . message } ` ) ;
315
+ this . $errors . failWithoutHelp ( `Failed to copy built aar to destination. ${ e . message } ` ) ;
319
316
}
320
317
} else {
321
- throw new Error ( `No built aar found at ${ pathToBuiltAar } ` ) ;
318
+ this . $errors . failWithoutHelp ( `No built aar found at ${ pathToBuiltAar } ` ) ;
322
319
}
323
320
}
324
321
@@ -336,7 +333,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
336
333
try {
337
334
includeGradleFileContent = this . $fs . readFile ( includeGradleFilePath ) . toString ( ) ;
338
335
} catch ( err ) {
339
- throw new Error ( `Failed to fs.readFileSync the include.gradle file located at ${ includeGradleFilePath } ` ) ;
336
+ this . $errors . failWithoutHelp ( `Failed to fs.readFileSync the include.gradle file located at ${ includeGradleFilePath } . Error is: ${ err . toString ( ) } ` ) ;
340
337
}
341
338
342
339
const productFlavorsScope = this . getScope ( "productFlavors" , includeGradleFileContent ) ;
@@ -347,7 +344,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
347
344
348
345
return true ;
349
346
} catch ( e ) {
350
- throw new Error ( `Failed to write the updated include.gradle in - ${ includeGradleFilePath } ` ) ;
347
+ this . $errors . failWithoutHelp ( `Failed to write the updated include.gradle ` +
348
+ `in - ${ includeGradleFilePath } . Error is: ${ e . toString ( ) } ` ) ;
351
349
}
352
350
}
353
351
@@ -375,13 +373,13 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
375
373
try {
376
374
await this . $childProcess . spawnFromEvent ( gradlew , localArgs , "close" , { cwd : pluginBuildSettings . pluginDir } ) ;
377
375
} catch ( err ) {
378
- throw new Error ( `Failed to build plugin ${ pluginBuildSettings . pluginName } : \n${ err } ` ) ;
376
+ this . $errors . failWithoutHelp ( `Failed to build plugin ${ pluginBuildSettings . pluginName } : \n${ err } ` ) ;
379
377
}
380
378
}
381
379
382
380
private validateOptions ( options : IBuildOptions ) : void {
383
381
if ( ! options ) {
384
- throw new Error ( "Android plugin cannot be built without passing an 'options' object." ) ;
382
+ this . $errors . failWithoutHelp ( "Android plugin cannot be built without passing an 'options' object." ) ;
385
383
}
386
384
387
385
if ( ! options . pluginName ) {
@@ -393,19 +391,19 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
393
391
}
394
392
395
393
if ( ! options . tempPluginDirPath ) {
396
- throw new Error ( "Android plugin cannot be built without passing the path to a directory where the temporary project should be built." ) ;
394
+ this . $errors . failWithoutHelp ( "Android plugin cannot be built without passing the path to a directory where the temporary project should be built." ) ;
397
395
}
398
396
399
397
this . validatePlatformsAndroidDirPathOption ( options ) ;
400
398
}
401
399
402
400
private validatePlatformsAndroidDirPathOption ( options : IBuildOptions ) : void {
403
401
if ( ! options ) {
404
- throw new Error ( "Android plugin cannot be built without passing an 'options' object." ) ;
402
+ this . $errors . failWithoutHelp ( "Android plugin cannot be built without passing an 'options' object." ) ;
405
403
}
406
404
407
405
if ( ! options . platformsAndroidDirPath ) {
408
- throw new Error ( "Android plugin cannot be built without passing the path to the platforms/android dir." ) ;
406
+ this . $errors . failWithoutHelp ( "Android plugin cannot be built without passing the path to the platforms/android dir." ) ;
409
407
}
410
408
}
411
409
0 commit comments