Skip to content

Commit cd61584

Browse files
committed
feat(android): Gradle facts file and opt-in plugin builds in platforms
- `ns prepare android` writes platforms/android/nativescript-gradle.json right after dependencies.json: the project's root, app and App_Resources dirs and, for every native plugin, its package dir, native dir, aar name and location and native dependencies, with paths relative to the file and the hash of dependencies.json. It is only rewritten when its content changes, and is passed as -Pnativescript.factsFile to app and plugin Gradle builds. org.nativescript:gradle-api reads it. - `android.pluginBuildLocation` in nativescript.config: "node_modules" (the default) keeps today's behaviour; "platforms" copies each native plugin's platforms/android and package.json into platforms/android/.ns-plugins, builds its aar and migrates include.gradle there, and points dependencies.json at the copies, so node_modules is never written to. The copy is incremental and keeps upstream mtimes. Aars the CLI built for a plugin under another of its aar names are left out of the copy, and an aar that differs from the CLI's build under that name is left out without failing prepare; prebuilt-only plugins are copied as they are. Any other value fails prepare. Switching back to "node_modules" removes .ns-plugins. - @nativescript/core and its native dependencies are prepared before other plugins, so plugins can compile against core's aar. - A plugin whose copy changed stays marked for a rebuild until its aar build succeeds, and each plugin file is hashed once per prepare.
1 parent 103c36f commit cd61584

23 files changed

Lines changed: 2270 additions & 73 deletions

‎lib/bootstrap.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ injector.require(
4848
"gradleBuildArgsService",
4949
"./services/android/gradle-build-args-service",
5050
);
51+
injector.require(
52+
"androidPluginMirrorService",
53+
"./services/android/android-plugin-mirror-service",
54+
);
5155
injector.require(
5256
"androidBundleToolService",
5357
"./services/android/android-bundle-tool-service",
@@ -653,6 +657,7 @@ injector.requirePublic(
653657
);
654658

655659
injector.require("filesHashService", "./services/files-hash-service");
660+
injector.require("nativeFileHashesMemo", "./services/native-file-hashes-memo");
656661
injector.require("logParserService", "./services/log-parser-service");
657662
injector.require(
658663
"iOSDebuggerPortService",

‎lib/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export const CONFIG_FILE_NAME_DISPLAY = "nativescript.config.(js|ts)";
7373
export const CONFIG_FILE_NAME_JS = "nativescript.config.js";
7474
export const CONFIG_FILE_NAME_TS = "nativescript.config.ts";
7575
export const DEPENDENCIES_JSON_NAME = "dependencies.json";
76+
export const GRADLE_FACTS_FILE_NAME = "nativescript-gradle.json";
77+
export const ANDROID_PLUGIN_MIRRORS_DIR_NAME = ".ns-plugins";
7678
export const APK_EXTENSION_NAME = ".apk";
7779
export const AAB_EXTENSION_NAME = ".aab";
7880
export const APKS_EXTENSION_NAME = ".apks";

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IAndroidToolsInfoData } from "../declarations";
22
import { IProjectDir } from "../common/declarations";
3+
import { AndroidPluginBuildLocation } from "./project";
34

45
interface IPluginBuildOptions extends IAndroidBuildOptions {
56
projectDir?: string;
@@ -8,6 +9,15 @@ interface IPluginBuildOptions extends IAndroidBuildOptions {
89
* Defaults to the plugin's short name.
910
*/
1011
aarName?: string;
12+
/**
13+
* When `"platforms"`, `aarOutputDir` is a CLI-owned copy of the plugin's
14+
* platforms/android. Absent means `"node_modules"`.
15+
*/
16+
pluginBuildLocation?: AndroidPluginBuildLocation;
17+
/**
18+
* The project's nativescript-gradle.json, passed to the plugin's Gradle build when it exists.
19+
*/
20+
gradleFactsFile?: string;
1121
}
1222

1323
interface IAndroidBuildOptions {
@@ -32,6 +42,43 @@ interface IAndroidPluginBuildService {
3242
* or still holds one the CLI built under another of its candidate names.
3343
*/
3444
hasOutdatedAar(options: IPluginBuildOptions): boolean;
45+
/**
46+
* Aars in `dir` (default: `aarOutputDir`) that the CLI built for this plugin
47+
* under another of its candidate aar names. Only meaningful for a plugin
48+
* with aar sources; aars the package ships are not included.
49+
*/
50+
getStaleAars(options: IPluginBuildOptions, dir?: string): IStaleAar[];
51+
}
52+
53+
interface IStaleAar {
54+
aarPath: string;
55+
/**
56+
* The CLI's earlier build output the aar must match to be deleted. Absent
57+
* when the aar is deleted whatever its content.
58+
*/
59+
previousOutputPath?: string;
60+
}
61+
62+
interface IAndroidPluginMirror {
63+
pluginName: string;
64+
/**
65+
* The installed npm package.
66+
*/
67+
packageDir: string;
68+
mirrorDir: string;
69+
/**
70+
* Paths relative to the package's platforms/android that are not copied.
71+
*/
72+
excludedNativeFiles?: string[];
73+
}
74+
75+
interface IAndroidPluginMirrorService {
76+
/**
77+
* Brings `<mirrorsRoot>/<dir>` in line with each plugin's package.json and
78+
* platforms/android, and removes mirrors of plugins not in `mirrors`.
79+
* @returns The names of the plugins whose mirror changed.
80+
*/
81+
syncMirrors(mirrorsRoot: string, mirrors: IAndroidPluginMirror[]): string[];
3582
}
3683

3784
/**
@@ -64,4 +111,14 @@ interface IBuildAndroidPluginData extends Partial<IProjectDir> {
64111
* Optional custom Gradle arguments.
65112
*/
66113
gradleArgs?: string;
114+
115+
/**
116+
* The project's `android.pluginBuildLocation`. Absent means `"node_modules"`.
117+
*/
118+
pluginBuildLocation?: AndroidPluginBuildLocation;
119+
120+
/**
121+
* Absolute path of the project's nativescript-gradle.json, when it exists.
122+
*/
123+
gradleFactsFile?: string;
67124
}

‎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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Contents of platforms/android/nativescript-gradle.json, read by
3+
* org.nativescript:gradle-api. Mirrors gradle-api/schema/nativescript-gradle.schema.json:
4+
* fields are only ever added, and every path is relative to the file's
5+
* directory with forward slashes.
6+
*/
7+
export interface INativeScriptGradleFacts {
8+
schemaVersion: number;
9+
/**
10+
* The lowest schemaVersion a reader must understand to use this file.
11+
*/
12+
minReaderSchemaVersion: number;
13+
cli?: { version?: string };
14+
runtime?: { name?: string; version?: string };
15+
capabilities?: string[];
16+
/**
17+
* Identifies the dependencies.json written together with these facts, so a
18+
* reader can tell when a CLI that does not write facts has replaced it since.
19+
*/
20+
dependenciesJson: { sha256: string };
21+
project: INativeScriptGradleFactsProject;
22+
plugins: INativeScriptGradleFactsPlugin[];
23+
}
24+
25+
export interface INativeScriptGradleFactsProject {
26+
root: string;
27+
appDir: string;
28+
appResourcesDir: string;
29+
}
30+
31+
export interface INativeScriptGradleFactsPlugin {
32+
name: string;
33+
packageDir: string;
34+
nativeDir: string;
35+
aarName: string;
36+
aar: string;
37+
dependencies: string[];
38+
options?: Record<string, unknown>;
39+
}

‎lib/definitions/project.d.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export interface IOSLocalSPMPackage extends IOSSPMPackageBase {
124124

125125
export type IOSSPMPackage = IOSRemoteSPMPackage | IOSLocalSPMPackage;
126126
export type BundlerType = "webpack" | "rspack" | "vite";
127+
export type AndroidPluginBuildLocation = "node_modules" | "platforms";
127128

128129
interface INsConfigIOS extends INsConfigPlaform {
129130
discardUncaughtJsExceptions?: boolean;
@@ -179,6 +180,19 @@ interface INsConfigAndroid extends INsConfigPlaform {
179180
* Custom runtime package name
180181
*/
181182
runtimePackageName?: string;
183+
184+
/**
185+
* Where the CLI builds plugins' native Android code.
186+
*
187+
* - `"node_modules"` (default): each plugin's aar is built into its own
188+
* `platforms/android` folder inside node_modules.
189+
* - `"platforms"`: node_modules is left untouched. Each plugin's Android files
190+
* are copied to `platforms/android/.ns-plugins/` and its aar is built there.
191+
*
192+
* `"platforms"` is opt-in while plugins adopt `org.nativescript:gradle-api`,
193+
* and is expected to become the default in a future major version.
194+
*/
195+
pluginBuildLocation?: AndroidPluginBuildLocation;
182196
}
183197

184198
interface INsConfigHooks {

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

Lines changed: 45 additions & 28 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,
@@ -44,15 +45,6 @@ import * as _ from "lodash";
4445
import { resolvePackageJSONPath } from "@rigor789/resolve-package-path";
4546
import { cwd } from "process";
4647

47-
interface IStaleAar {
48-
aarPath: string;
49-
/**
50-
* The CLI's earlier build output the aar must match to be deleted. Absent
51-
* when the aar is deleted whatever its content.
52-
*/
53-
previousOutputPath?: string;
54-
}
55-
5648
export class AndroidPluginBuildService implements IAndroidPluginBuildService {
5749
private get $platformsDataService(): IPlatformsDataService {
5850
return this.$injector.resolve("platformsDataService");
@@ -243,18 +235,26 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
243235
const hasAarSources = this.hasAarSources(options.platformsAndroidDirPath);
244236
const aarName = this.getAarName(options);
245237
const pluginTempDir = path.join(options.tempPluginDirPath, aarName);
246-
const staleAars =
247-
options.aarName && hasAarSources
248-
? this.getStaleAars(options, aarName)
249-
: [];
238+
const inMirror = options.pluginBuildLocation === "platforms";
239+
const findStaleAars = (dir?: string) =>
240+
options.aarName && hasAarSources ? this.getStaleAars(options, dir) : [];
241+
const staleAars = findStaleAars();
242+
// In a mirror the leftovers are in the copy, but the sources are read from
243+
// the package, whose leftovers the mirror leaves out.
244+
const staleSourceAars = inMirror
245+
? findStaleAars(options.platformsAndroidDirPath)
246+
: staleAars;
250247
const pluginSourceFileHashesInfo = await this.getSourceFilesHashes(
251248
options.platformsAndroidDirPath,
252249
[
253250
path.join(options.platformsAndroidDirPath, `${aarName}.aar`),
254-
...staleAars.map((staleAar) => staleAar.aarPath),
251+
...staleSourceAars.map((staleAar) => staleAar.aarPath),
255252
],
256253
);
257-
this.ensureStaleAarsAreBuildOutputs(options.pluginName, staleAars);
254+
// The mirror is CLI-owned, so nothing in it needs the user's decision.
255+
if (!inMirror) {
256+
this.ensureStaleAarsAreBuildOutputs(options.pluginName, staleAars);
257+
}
258258

259259
const shouldBuildAar = await this.shouldBuildAar({
260260
hasAarSources,
@@ -283,21 +283,30 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
283283
options.pluginName,
284284
aarName,
285285
);
286-
await this.buildPlugin({
286+
const pluginBuildData: IBuildAndroidPluginData = {
287287
gradlePath: options.gradlePath,
288288
gradleArgs: options.gradleArgs,
289289
pluginDir: pluginTempDir,
290290
pluginName: options.pluginName,
291291
projectDir: options.projectDir,
292-
});
293-
this.$watchIgnoreListService.addFileToIgnoreList(
294-
path.join(options.aarOutputDir, `${aarName}.aar`),
295-
);
292+
};
293+
if (inMirror) {
294+
pluginBuildData.pluginBuildLocation = "platforms";
295+
}
296+
if (options.gradleFactsFile && this.$fs.exists(options.gradleFactsFile)) {
297+
pluginBuildData.gradleFactsFile = path.resolve(options.gradleFactsFile);
298+
}
299+
await this.buildPlugin(pluginBuildData);
300+
if (!inMirror) {
301+
this.$watchIgnoreListService.addFileToIgnoreList(
302+
path.join(options.aarOutputDir, `${aarName}.aar`),
303+
);
304+
}
296305
this.copyAar(aarName, pluginTempDir, options.aarOutputDir);
297306
this.writePluginHashInfo(pluginSourceFileHashesInfo, pluginTempDir);
298307
}
299308

300-
this.removeStaleAars(staleAars);
309+
this.removeStaleAars(staleAars, inMirror);
301310

302311
return shouldBuildAar;
303312
}
@@ -319,7 +328,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
319328
return (
320329
!this.$fs.exists(
321330
path.join(this.getAarOutputDir(options), `${aarName}.aar`),
322-
) || this.getStaleAars(options, aarName).length > 0
331+
) || this.getStaleAars(options).length > 0
323332
);
324333
}
325334

@@ -336,15 +345,15 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
336345
* every build. A package may ship a scope-qualified name itself, so that one
337346
* only counts when its temp project was built for this plugin.
338347
*/
339-
private getStaleAars(
348+
public getStaleAars(
340349
options: IPluginBuildOptions,
341-
aarName: string,
350+
dir: string = this.getAarOutputDir(options),
342351
): IStaleAar[] {
343-
const aarOutputDir = this.getAarOutputDir(options);
352+
const aarName = this.getAarName(options);
344353
const shortName = getShortPluginName(options.pluginName);
345354
const staleAars: IStaleAar[] = [];
346355
for (const candidate of getCandidateAarNames(options.pluginName)) {
347-
const aarPath = path.join(aarOutputDir, `${candidate}.aar`);
356+
const aarPath = path.join(dir, `${candidate}.aar`);
348357
if (candidate === aarName || !this.$fs.exists(aarPath)) {
349358
continue;
350359
}
@@ -412,9 +421,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
412421
* duplicate its classes. Only call once the aar under the current name
413422
* exists, so a failed build leaves the plugin with its previous aar.
414423
*/
415-
private removeStaleAars(staleAars: IStaleAar[]): void {
424+
private removeStaleAars(staleAars: IStaleAar[], inMirror: boolean): void {
416425
for (const { aarPath } of staleAars) {
417-
this.$watchIgnoreListService.addFileToIgnoreList(aarPath);
426+
if (!inMirror) {
427+
this.$watchIgnoreListService.addFileToIgnoreList(aarPath);
428+
}
418429
this.$logger.trace(`Removing stale plugin aar ${aarPath}`);
419430
this.$fs.deleteFile(aarPath);
420431
}
@@ -956,6 +967,12 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
956967
`-PappResourcesPath=${this.$projectData.getAppResourcesDirectoryPath()}`,
957968
];
958969

970+
if (pluginBuildSettings.gradleFactsFile) {
971+
localArgs.push(
972+
`-Pnativescript.factsFile=${pluginBuildSettings.gradleFactsFile}`,
973+
);
974+
}
975+
959976
if (pluginBuildSettings.gradleArgs) {
960977
localArgs.push(pluginBuildSettings.gradleArgs);
961978
}

0 commit comments

Comments
 (0)