Skip to content

Commit 96326f4

Browse files
committed
fix(android): rebuild plugin aars per output dir and warn about shipped ones
The aar build record in platforms/tempPlugin is shared by both android.pluginBuildLocation values, so after building into the mirror, switching back to "node_modules" could skip a needed rebuild and leave an aar built from older sources in the package. The record now holds the output dir it was copied to, and a different one means rebuild (also in hasOutdatedAar, so the plugin is re-prepared). Records without it count as builds into the package's platforms/android, as older CLIs did. Packages sometimes publish the aar the CLI built in the author's repo (e.g. platforms/android/core.aar). The CLI replaces or drops it, so warn before building: an aar in the package's platforms/android under the plugin's current aar name or short name, or under a scope-qualified name the CLI built the plugin as, that differs from this project's last build of the plugin. Tests now remove their temp projects.
1 parent 9fd181c commit 96326f4

7 files changed

Lines changed: 470 additions & 47 deletions

‎lib/definitions/android-plugin-migrator.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ interface IAndroidPluginBuildService {
4848
* with aar sources; aars the package ships are not included.
4949
*/
5050
getStaleAars(options: IPluginBuildOptions, dir?: string): IStaleAar[];
51+
/**
52+
* Aars in `platformsAndroidDirPath` under a name the CLI builds or removes
53+
* for this plugin that differ from this project's last build of it, i.e.
54+
* the package ships them. Only meaningful for a plugin with aar sources.
55+
*/
56+
getShippedBuildOutputAars(options: IPluginBuildOptions): string[];
5157
}
5258

5359
interface IStaleAar {

‎lib/services/android-plugin-build-service.ts‎

Lines changed: 131 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ import * as _ from "lodash";
4545
import { resolvePackageJSONPath } from "@rigor789/resolve-package-path";
4646
import { cwd } from "process";
4747

48+
interface IPluginBuildRecord {
49+
/**
50+
* Resolved directory the aar built from `files` was copied to.
51+
*/
52+
aarOutputDir: string;
53+
files: IStringDictionary;
54+
}
55+
4856
export class AndroidPluginBuildService implements IAndroidPluginBuildService {
4957
private get $platformsDataService(): IPlatformsDataService {
5058
return this.$injector.resolve("platformsDataService");
@@ -261,6 +269,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
261269
pluginTempDir,
262270
aarPath: path.join(this.getAarOutputDir(options), `${aarName}.aar`),
263271
fileHashesInfo: pluginSourceFileHashesInfo,
272+
options,
264273
});
265274

266275
if (shouldBuildAar) {
@@ -303,7 +312,10 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
303312
);
304313
}
305314
this.copyAar(aarName, pluginTempDir, options.aarOutputDir);
306-
this.writePluginHashInfo(pluginSourceFileHashesInfo, pluginTempDir);
315+
this.writePluginBuildRecord(pluginTempDir, {
316+
aarOutputDir: path.resolve(this.getAarOutputDir(options)),
317+
files: pluginSourceFileHashesInfo,
318+
});
307319
}
308320

309321
this.removeStaleAars(staleAars, inMirror);
@@ -328,7 +340,26 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
328340
return (
329341
!this.$fs.exists(
330342
path.join(this.getAarOutputDir(options), `${aarName}.aar`),
331-
) || this.getStaleAars(options).length > 0
343+
) ||
344+
this.getStaleAars(options).length > 0 ||
345+
this.isBuildRecordForAnotherOutputDir(options)
346+
);
347+
}
348+
349+
/**
350+
* The temp project is shared by every aarOutputDir the plugin is built into,
351+
* so its record only vouches for the aar in the dir it was last copied to.
352+
*/
353+
private isBuildRecordForAnotherOutputDir(
354+
options: IPluginBuildOptions,
355+
): boolean {
356+
const record = this.readPluginBuildRecord(
357+
path.join(options.tempPluginDirPath, this.getAarName(options)),
358+
options.platformsAndroidDirPath,
359+
);
360+
return (
361+
!!record &&
362+
record.aarOutputDir !== path.resolve(this.getAarOutputDir(options))
332363
);
333364
}
334365

@@ -363,25 +394,76 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
363394
continue;
364395
}
365396

366-
const tempProjectDir = path.join(options.tempPluginDirPath, candidate);
367-
const previousOutputPath = path.join(
368-
tempProjectDir,
369-
"build",
370-
"outputs",
371-
"aar",
372-
`${candidate}-release.aar`,
397+
const previousOutputPath = this.getPreviousBuildOutput(
398+
options,
399+
candidate,
373400
);
374-
if (
375-
this.$fs.exists(previousOutputPath) &&
376-
this.getTempProjectPluginName(tempProjectDir) === options.pluginName
377-
) {
401+
if (previousOutputPath) {
378402
staleAars.push({ aarPath, previousOutputPath });
379403
}
380404
}
381405

382406
return staleAars;
383407
}
384408

409+
public getShippedBuildOutputAars(options: IPluginBuildOptions): string[] {
410+
const aarName = this.getAarName(options);
411+
const shortName = getShortPluginName(options.pluginName);
412+
const shippedAars: string[] = [];
413+
for (const candidate of _.uniq([
414+
aarName,
415+
...getCandidateAarNames(options.pluginName),
416+
])) {
417+
const aarPath = path.join(
418+
options.platformsAndroidDirPath,
419+
`${candidate}.aar`,
420+
);
421+
if (!this.$fs.exists(aarPath)) {
422+
continue;
423+
}
424+
425+
const previousOutputPath = this.getPreviousBuildOutput(
426+
options,
427+
candidate,
428+
);
429+
if (
430+
previousOutputPath
431+
? this.isSameFileContent(aarPath, previousOutputPath)
432+
: candidate !== aarName && candidate !== shortName
433+
) {
434+
// Either this project's own build, or a scope-qualified aar the CLI
435+
// never built for the plugin, which stays in the app as the package's.
436+
continue;
437+
}
438+
439+
shippedAars.push(aarPath);
440+
}
441+
442+
return shippedAars;
443+
}
444+
445+
/**
446+
* The aar the CLI last built for this plugin under `name`, if the temp
447+
* project under that name was set up for this plugin.
448+
*/
449+
private getPreviousBuildOutput(
450+
options: IPluginBuildOptions,
451+
name: string,
452+
): string | null {
453+
const tempProjectDir = path.join(options.tempPluginDirPath, name);
454+
const previousOutputPath = path.join(
455+
tempProjectDir,
456+
"build",
457+
"outputs",
458+
"aar",
459+
`${name}-release.aar`,
460+
);
461+
return this.$fs.exists(previousOutputPath) &&
462+
this.getTempProjectPluginName(tempProjectDir) === options.pluginName
463+
? previousOutputPath
464+
: null;
465+
}
466+
385467
/**
386468
* The npm name setupGradle substituted into the temp project's build.gradle.
387469
*/
@@ -455,19 +537,42 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
455537
return this.$filesHashService.generateHashes(pluginNativeDataFiles);
456538
}
457539

458-
private writePluginHashInfo(
459-
fileHashesInfo: IStringDictionary,
540+
private writePluginBuildRecord(
460541
pluginTempDir: string,
542+
record: IPluginBuildRecord,
461543
): void {
462544
const buildDataFile = this.getPathToPluginBuildDataFile(pluginTempDir);
463-
this.$fs.writeJson(buildDataFile, fileHashesInfo);
545+
this.$fs.writeJson(buildDataFile, record);
546+
}
547+
548+
/**
549+
* Records written before the output dir was recorded hold only the hashes,
550+
* and CLIs that wrote them always built into the package's platforms/android.
551+
*/
552+
private readPluginBuildRecord(
553+
pluginTempDir: string,
554+
platformsAndroidDirPath: string,
555+
): IPluginBuildRecord | null {
556+
const buildDataFile = this.getPathToPluginBuildDataFile(pluginTempDir);
557+
if (!this.$fs.exists(buildDataFile)) {
558+
return null;
559+
}
560+
561+
const content = this.$fs.readJson(buildDataFile) ?? {};
562+
return typeof content.aarOutputDir === "string" && content.files
563+
? content
564+
: {
565+
aarOutputDir: path.resolve(platformsAndroidDirPath),
566+
files: content,
567+
};
464568
}
465569

466570
private async shouldBuildAar(opts: {
467571
hasAarSources: boolean;
468572
pluginTempDir: string;
469573
aarPath: string;
470574
fileHashesInfo: IStringDictionary;
575+
options: IPluginBuildOptions;
471576
}): Promise<boolean> {
472577
let shouldBuildAar = opts.hasAarSources;
473578

@@ -476,15 +581,18 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
476581
this.$fs.exists(opts.pluginTempDir) &&
477582
this.$fs.exists(opts.aarPath)
478583
) {
479-
const buildDataFile = this.getPathToPluginBuildDataFile(
584+
const record = this.readPluginBuildRecord(
480585
opts.pluginTempDir,
586+
opts.options.platformsAndroidDirPath,
481587
);
482-
if (this.$fs.exists(buildDataFile)) {
483-
const oldHashes = this.$fs.readJson(buildDataFile);
484-
shouldBuildAar = this.$filesHashService.hasChangesInShasums(
485-
oldHashes,
486-
opts.fileHashesInfo,
487-
);
588+
if (record) {
589+
shouldBuildAar =
590+
record.aarOutputDir !==
591+
path.resolve(this.getAarOutputDir(opts.options)) ||
592+
this.$filesHashService.hasChangesInShasums(
593+
record.files,
594+
opts.fileHashesInfo,
595+
);
488596
}
489597
}
490598

‎lib/services/android-project-service.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,57 @@ export class AndroidProjectService
11071107
if (dependencies) {
11081108
dependencies = this.filterUniqueDependencies(dependencies);
11091109
this.setPluginAarNames(projectData, dependencies);
1110+
this.warnAboutShippedBuildOutputs(projectData, dependencies);
11101111
return this.provideDependenciesJson(projectData, dependencies);
11111112
}
11121113
}
11131114

1115+
/**
1116+
* Must run before any plugin is built: a build replaces the temp project
1117+
* the package's aars are compared with, and in "node_modules" mode
1118+
* overwrites the package's aar.
1119+
*/
1120+
private warnAboutShippedBuildOutputs(
1121+
projectData: IProjectData,
1122+
dependencies: IDependencyData[],
1123+
): void {
1124+
const aarNames = this.getPluginAarNames(projectData);
1125+
const tempPluginDirPath = this.getTempPluginDirPath(projectData);
1126+
for (const dep of dependencies) {
1127+
const aarName = aarNames.get(dep.name);
1128+
if (!aarName) {
1129+
continue;
1130+
}
1131+
1132+
const nativeDir = path.join(
1133+
dep.directory,
1134+
constants.PLATFORMS_DIR_NAME,
1135+
AndroidProjectService.ANDROID_PLATFORM_NAME,
1136+
);
1137+
const files = this.$androidPluginBuildService
1138+
.getShippedBuildOutputAars({
1139+
pluginName: dep.name,
1140+
aarName,
1141+
platformsAndroidDirPath: nativeDir,
1142+
aarOutputDir: nativeDir,
1143+
tempPluginDirPath,
1144+
})
1145+
.map(
1146+
(aarPath) =>
1147+
`${constants.PLATFORMS_DIR_NAME}/${AndroidProjectService.ANDROID_PLATFORM_NAME}/${path.basename(aarPath)}`,
1148+
);
1149+
if (files.length === 1) {
1150+
this.$logger.warn(
1151+
`${dep.name} ships ${files[0]}, which looks like an aar the NativeScript CLI builds from the plugin's sources. The CLI replaces it with its own build. Plugin authors: leave it out of the published package (package.json "files" or .npmignore).`,
1152+
);
1153+
} else if (files.length > 1) {
1154+
this.$logger.warn(
1155+
`${dep.name} ships ${files.join(" and ")}, which look like aars the NativeScript CLI builds from the plugin's sources. The CLI replaces them with its own build. Plugin authors: leave them out of the published package (package.json "files" or .npmignore).`,
1156+
);
1157+
}
1158+
}
1159+
}
1160+
11141161
public async handleNativeDependenciesChange(
11151162
projectData: IProjectData,
11161163
opts: IRelease,

0 commit comments

Comments
 (0)