@@ -27,6 +27,7 @@ import {
2727 IAndroidPluginBuildService ,
2828 IPluginBuildOptions ,
2929 IBuildAndroidPluginData ,
30+ IStaleAar ,
3031} from "../definitions/android-plugin-migrator" ;
3132import {
3233 IFileSystem ,
@@ -239,12 +240,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
239240 aarName ,
240241 ) ;
241242
242- if (
243- options . aarName &&
244- options . aarOutputDir &&
245- ( manifestFilePath || androidSourceDirectories . length )
246- ) {
247- this . removeStaleAars ( options , aarName ) ;
243+ if ( options . aarName && options . aarOutputDir ) {
244+ this . removeStaleAars ( options ) ;
248245 }
249246
250247 const shouldBuildAar = await this . shouldBuildAar ( {
@@ -302,20 +299,94 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
302299 }
303300
304301 public hasOutdatedAar ( options : IPluginBuildOptions ) : boolean {
305- const aarOutputDir = this . getAarOutputDir ( options ) ;
306- if (
307- ! this . $fs . exists ( options . platformsAndroidDirPath ) ||
308- ( ! this . getManifest ( options . platformsAndroidDirPath ) &&
309- ! this . getAndroidSourceDirectories ( options . platformsAndroidDirPath )
310- . length )
311- ) {
302+ if ( ! this . $fs . exists ( options . platformsAndroidDirPath ) ) {
312303 return false ;
313304 }
314305
306+ if ( this . getStaleAars ( options ) . length ) {
307+ return true ;
308+ }
309+
310+ return (
311+ ! this . $fs . exists (
312+ path . join (
313+ this . getAarOutputDir ( options ) ,
314+ `${ this . getAarName ( options ) } .aar` ,
315+ ) ,
316+ ) && this . hasSourcesToBuild ( options . platformsAndroidDirPath )
317+ ) ;
318+ }
319+
320+ /**
321+ * Gradle templates add every aar in a plugin's platforms/android to the app,
322+ * so an aar the CLI built for this plugin under another of its candidate
323+ * names duplicates its classes. Only plugins the CLI builds have such
324+ * leftovers:
325+ * - CLIs before unique aar names copied every build to `<short name>.aar`,
326+ * replacing any file the package shipped under that name, so that file is
327+ * always a leftover.
328+ * - `<scope>_<short name>.aar` is a leftover only when the temp project of
329+ * that name was set up for this plugin and still has its build output;
330+ * otherwise the package ships it.
331+ */
332+ public getStaleAars (
333+ options : IPluginBuildOptions ,
334+ dir : string = this . getAarOutputDir ( options ) ,
335+ ) : IStaleAar [ ] {
315336 const aarName = this . getAarName ( options ) ;
337+ const shortName = getShortPluginName ( options . pluginName ) ;
338+ const existing = getCandidateAarNames ( options . pluginName )
339+ . filter ( ( candidate ) => candidate !== aarName )
340+ . map ( ( candidate ) => ( {
341+ candidate,
342+ aarPath : path . join ( dir , `${ candidate } .aar` ) ,
343+ } ) )
344+ . filter ( ( { aarPath } ) => this . $fs . exists ( aarPath ) ) ;
345+ if (
346+ ! existing . length ||
347+ ! this . hasSourcesToBuild ( options . platformsAndroidDirPath )
348+ ) {
349+ return [ ] ;
350+ }
351+
352+ const staleAars : IStaleAar [ ] = [ ] ;
353+ for ( const { candidate, aarPath } of existing ) {
354+ if ( candidate === shortName ) {
355+ staleAars . push ( { aarPath, differs : false } ) ;
356+ continue ;
357+ }
358+
359+ if ( ! options . tempPluginDirPath ) {
360+ continue ;
361+ }
362+
363+ const tempProjectDir = path . join ( options . tempPluginDirPath , candidate ) ;
364+ const previousOutputPath = path . join (
365+ tempProjectDir ,
366+ "build" ,
367+ "outputs" ,
368+ "aar" ,
369+ `${ candidate } -release.aar` ,
370+ ) ;
371+ if (
372+ this . $fs . exists ( previousOutputPath ) &&
373+ this . getTempProjectPluginName ( tempProjectDir ) === options . pluginName
374+ ) {
375+ staleAars . push ( {
376+ aarPath,
377+ previousOutputPath,
378+ differs : ! this . isSameFileContent ( aarPath , previousOutputPath ) ,
379+ } ) ;
380+ }
381+ }
382+
383+ return staleAars ;
384+ }
385+
386+ private hasSourcesToBuild ( platformsAndroidDirPath : string ) : boolean {
316387 return (
317- ! this . $fs . exists ( path . join ( aarOutputDir , ` ${ aarName } .aar` ) ) ||
318- this . getStaleAarPaths ( options , aarName ) . length > 0
388+ ! ! this . getManifest ( platformsAndroidDirPath ) ||
389+ this . getAndroidSourceDirectories ( platformsAndroidDirPath ) . length > 0
319390 ) ;
320391 }
321392
@@ -327,50 +398,48 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
327398 return options . aarOutputDir || options . platformsAndroidDirPath ;
328399 }
329400
330- private getStaleAarPaths (
331- options : IPluginBuildOptions ,
332- aarName : string ,
333- ) : string [ ] {
334- const aarOutputDir = this . getAarOutputDir ( options ) ;
335- return getCandidateAarNames ( options . pluginName )
336- . filter ( ( candidate ) => candidate !== aarName )
337- . map ( ( candidate ) => path . join ( aarOutputDir , `${ candidate } .aar` ) )
338- . filter ( ( candidatePath ) => this . $fs . exists ( candidatePath ) ) ;
339- }
340-
341401 /**
342- * Gradle templates add every aar in a plugin 's platforms/android to the app,
343- * so an aar the CLI built for this plugin under another candidate name would
344- * duplicate its classes .
402+ * The npm name setupGradle substituted into the temp project 's build.gradle.
403+ * Temp projects are keyed by aar name, so another plugin may now build in the
404+ * one of this plugin's old name .
345405 */
346- private removeStaleAars ( options : IPluginBuildOptions , aarName : string ) : void {
347- for ( const staleAarPath of this . getStaleAarPaths ( options , aarName ) ) {
348- if ( options . pluginBuildLocation !== "platforms" ) {
349- const staleName = path . basename ( staleAarPath , ".aar" ) ;
350- const previousOutput = path . join (
351- options . tempPluginDirPath ,
352- staleName ,
353- "build" ,
354- "outputs" ,
355- "aar" ,
356- `${ staleName } -release.aar` ,
406+ private getTempProjectPluginName ( tempProjectDir : string ) : string | null {
407+ try {
408+ const buildGradle = this . $fs . readText (
409+ path . join ( tempProjectDir , "build.gradle" ) ,
410+ ) ;
411+ return (
412+ buildGradle . match ( / p r o j e c t \. e x t \. P L U G I N _ N A M E \s * = \s * " ( [ ^ " ] * ) " / ) ?. [ 1 ] ??
413+ null
414+ ) ;
415+ } catch ( err ) {
416+ return null ;
417+ }
418+ }
419+
420+ private removeStaleAars ( options : IPluginBuildOptions ) : void {
421+ const inMirror = options . pluginBuildLocation === "platforms" ;
422+ for ( const { aarPath, previousOutputPath, differs } of this . getStaleAars (
423+ options ,
424+ ) ) {
425+ // The mirror is CLI-owned, so nothing in it needs the user's decision.
426+ if ( differs && ! inMirror ) {
427+ this . $errors . fail (
428+ `${ aarPath } has a name the CLI built ${ options . pluginName } 's aar under before, but differs from that build's output at ${ previousOutputPath } . If it is an old build it would add duplicate classes to the app. Delete it or run \`ns clean\`.` ,
357429 ) ;
358- if ( ! this . isSameFileContent ( staleAarPath , previousOutput ) ) {
359- this . $errors . fail (
360- `${ staleAarPath } looks like an aar built for ${ options . pluginName } under an older name and would add duplicate classes to the app. Delete it or run \`ns clean\`.` ,
361- ) ;
362- }
430+ }
363431
364- this . $watchIgnoreListService . addFileToIgnoreList ( staleAarPath ) ;
432+ if ( ! inMirror ) {
433+ this . $watchIgnoreListService . addFileToIgnoreList ( aarPath ) ;
365434 }
366435
367- this . $logger . trace ( `Removing stale plugin aar ${ staleAarPath } ` ) ;
368- this . $fs . deleteFile ( staleAarPath ) ;
436+ this . $logger . trace ( `Removing stale plugin aar ${ aarPath } ` ) ;
437+ this . $fs . deleteFile ( aarPath ) ;
369438 }
370439 }
371440
372441 private isSameFileContent ( first : string , second : string ) : boolean {
373- if ( ! this . $fs . exists ( first ) || ! this . $fs . exists ( second ) ) {
442+ if ( this . $fs . getFsStats ( first ) . size !== this . $fs . getFsStats ( second ) . size ) {
374443 return false ;
375444 }
376445
0 commit comments