Skip to content

Commit 8d3ca37

Browse files
committed
fix(android): address review of the facts file and plugin mirror
- Leftover aars: a plugin's old short-name aar is always a CLI leftover (older CLIs overwrote that file on every build); a scope-qualified one only when the temp project was built for this plugin and its output matches. Prebuilt-only plugins never lose a file, in either pluginBuildLocation. - Build @nativescript/core (and its native dependencies) before other plugins. - Keep a plugin marked for rebuild until its aar build succeeds. - Mirror sync removes obsolete paths before copying, so file/directory swaps upstream don't fail. - Facts gain minReaderSchemaVersion. - Reuse the mirror sync's file hashes for the plugin native hashes, so a prepare hashes each plugin file once. - Drop the plugin-build template's dead $rootDir/buildscript.gradle step.
1 parent dce514f commit 8d3ca37

17 files changed

Lines changed: 840 additions & 148 deletions

‎lib/bootstrap.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ injector.requirePublic(
657657
);
658658

659659
injector.require("filesHashService", "./services/files-hash-service");
660+
injector.require("nativeFileHashesMemo", "./services/native-file-hashes-memo");
660661
injector.require("logParserService", "./services/log-parser-service");
661662
injector.require(
662663
"iOSDebuggerPortService",

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,29 @@ interface IAndroidPluginBuildService {
3333
buildAar(options: IPluginBuildOptions): Promise<boolean>;
3434
migrateIncludeGradle(options: IPluginBuildOptions): boolean;
3535
/**
36-
* Whether the plugin has sources to build but `aarOutputDir` lacks its aar
37-
* or still holds one under another of its candidate names.
36+
* Whether `aarOutputDir` lacks the aar of a plugin with sources to build, or
37+
* still holds one of `getStaleAars`.
3838
*/
3939
hasOutdatedAar(options: IPluginBuildOptions): boolean;
40+
/**
41+
* Aars in `dir` (default: `aarOutputDir`) that the CLI built for this plugin
42+
* under another of its candidate aar names. Aars the package ships are not
43+
* included.
44+
*/
45+
getStaleAars(options: IPluginBuildOptions, dir?: string): IStaleAar[];
46+
}
47+
48+
interface IStaleAar {
49+
aarPath: string;
50+
/**
51+
* The CLI's previous build output under that name, when the leftover is
52+
* recognised by it rather than by its name alone.
53+
*/
54+
previousOutputPath?: string;
55+
/**
56+
* The file differs from `previousOutputPath`.
57+
*/
58+
differs: boolean;
4059
}
4160

4261
interface IAndroidPluginMirror {

‎lib/definitions/files-hash-service.d.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface IFilesHashService {
1010
* A map with key file's path and value - file's hash
1111
*/
1212
generateHashesForProject(
13-
platformData: IPlatformData
13+
platformData: IPlatformData,
1414
): Promise<IStringDictionary>;
1515
/**
1616
* Generates hashes for all prepared files (all files from app folder under platforms folder)
@@ -21,15 +21,26 @@ interface IFilesHashService {
2121
*/
2222
saveHashesForProject(
2323
platformData: IPlatformData,
24-
hashesFileDirectory: string
24+
hashesFileDirectory: string,
2525
): Promise<IStringDictionary>;
2626
saveHashes(hashes: IStringDictionary, hashesFileDirectory: string): void;
2727
getChanges(
2828
files: string[],
29-
oldHashes: IStringDictionary
29+
oldHashes: IStringDictionary,
3030
): Promise<IStringDictionary>;
3131
hasChangesInShasums(
3232
oldHashes: IStringDictionary,
33-
newHashes: IStringDictionary
33+
newHashes: IStringDictionary,
3434
): boolean;
3535
}
36+
37+
/**
38+
* sha1 hashes of plugins' native files computed earlier in the same prepare,
39+
* so a later step does not read the same files again. Cleared when a prepare
40+
* starts; each hash is served once.
41+
*/
42+
interface INativeFileHashesMemo {
43+
clear(): void;
44+
remember(filePath: string, sha1: string): void;
45+
take(filePath: string): string | undefined;
46+
}

‎lib/definitions/gradle-facts.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
*/
77
export interface INativeScriptGradleFacts {
88
schemaVersion: number;
9+
/**
10+
* The lowest schemaVersion a reader must understand to use this file.
11+
*/
12+
minReaderSchemaVersion: number;
913
cli?: { version?: string };
1014
runtime?: { name?: string; version?: string };
1115
capabilities?: string[];

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

Lines changed: 118 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
IAndroidPluginBuildService,
2828
IPluginBuildOptions,
2929
IBuildAndroidPluginData,
30+
IStaleAar,
3031
} from "../definitions/android-plugin-migrator";
3132
import {
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(/project\.ext\.PLUGIN_NAME\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

Comments
 (0)