Skip to content

Commit 325ce47

Browse files
committed
feat(android): name the opt-in android.pluginBuildLocation
`pluginBuildLocation: "platforms"` builds plugins in platforms/android/.ns-plugins and never writes into node_modules; "node_modules" (the default) keeps the current behaviour and stays as the way back once "platforms" becomes the default. Any other value fails prepare. The plugin build receives -Pnativescript.pluginBuildLocation=platforms.
1 parent 0f9c122 commit 325ce47

11 files changed

Lines changed: 109 additions & 64 deletions

File tree

‎gradle-api/README.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ In that build, **only the `dependencies {}` and `repositories {}` blocks of your
7676
run**: the CLI copies them out of the file. Nothing else in the file runs there, including the
7777
`apply plugin:` line.
7878

79-
On CLI 9.2+ with `android.readOnlyNodeModules` enabled, the CLI loads your `buildscript.gradle` in
79+
On CLI 9.2+ with `android.pluginBuildLocation: "platforms"`, the CLI loads your `buildscript.gradle` in
8080
that build and applies the library for you. On older CLIs it doesn't, so the library isn't
8181
available there. Inside those two blocks, look the extension up instead of assuming it exists, and
8282
keep the old code as the fallback:
@@ -180,7 +180,7 @@ repositories {
180180

181181
## Compatibility
182182

183-
| | CLI 9.1 and older | CLI 9.2+ | CLI 9.2+ with `readOnlyNodeModules` |
183+
| | CLI 9.1 and older | CLI 9.2+ | CLI 9.2+ with `pluginBuildLocation: "platforms"` |
184184
|---|---|---|---|
185185
| Plugin not using the library | Unchanged | Unchanged | Gradle reads its files from a copy in `platforms/android/.ns-plugins/`, and its own `buildscript.gradle` also runs in its own `.aar` build |
186186
| Plugin using the library, app build | Fallback rules | Facts from the CLI | Facts from the CLI |
@@ -194,22 +194,27 @@ Building `platforms/android` from Android Studio works too: CLI 9.2+ leaves the
194194
after `ns prepare android`, so the library has the same answers without the CLI's command-line
195195
flags.
196196

197-
### For app developers: keeping `node_modules` read-only (opt-in)
197+
### For app developers: building plugins in `platforms/android` (opt-in)
198198

199199
```ts
200200
// nativescript.config.ts
201201
export default {
202-
android: { readOnlyNodeModules: true },
202+
android: { pluginBuildLocation: "platforms" },
203203
} satisfies NativeScriptConfig;
204204
```
205205

206-
With this, the CLI never writes into `node_modules`. It copies each native plugin's
206+
`android.pluginBuildLocation` sets where the CLI builds plugins' native Android code. The default,
207+
`"node_modules"`, builds each plugin's `.aar` inside its package, as CLIs up to 9.1 do. With
208+
`"platforms"`, the CLI never writes into `node_modules`. It copies each native plugin's
207209
`platforms/android` and `package.json` into `platforms/android/.ns-plugins/<name>/`, builds plugin
208210
`.aar`s there, and points the Android build at the copies. No runtime change is needed (checked
209211
against the runtime's Gradle templates from 6.5.3 to current), and Android Studio builds use the
210212
copies too. Earlier builds may have left `.aar` files inside `node_modules`;
211213
reinstall `node_modules` once after turning this on to get rid of them.
212214

215+
`"platforms"` is opt-in while plugins adopt this library, and is expected to become the default in a
216+
future major version of the CLI.
217+
213218
## Rules for plugin authors
214219

215220
- Pin the lowest library version you need; no ranges.

‎gradle-api/schema/nativescript-gradle.schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"$ref": "#/$defs/path"
7979
},
8080
"nativeDir": {
81-
"description": "The directory Gradle reads the plugin's Android files from: the package's platforms/android, or the CLI's copy of it when node_modules is kept read-only.",
81+
"description": "The directory Gradle reads the plugin's Android files from: the package's platforms/android, or the CLI's copy of it in platforms/android/.ns-plugins when the app sets android.pluginBuildLocation to \"platforms\".",
8282
"$ref": "#/$defs/path"
8383
},
8484
"aarName": {

‎gradle-api/src/functionalTest/java/org/nativescript/gradle/PluginBuildTest.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ static void requireAndroidSdk() {
5151
assumeTrue(androidHome != null && new File(androidHome).isDirectory(), "ANDROID_HOME is not set");
5252
}
5353

54-
@ParameterizedTest(name = "readOnlyNodeModules={0}")
55-
@ValueSource(booleans = {false, true})
56-
void configuresThePluginBuild(boolean readOnlyNodeModules) throws Exception {
54+
@ParameterizedTest(name = "pluginBuildLocation={0}")
55+
@ValueSource(strings = {"node_modules", "platforms"})
56+
void configuresThePluginBuild(String pluginBuildLocation) throws Exception {
57+
boolean buildsInPlatforms = pluginBuildLocation.equals("platforms");
5758
Path project = tempDir.toRealPath().resolve("project");
5859
Path platformsAndroid = project.resolve("platforms/android");
5960
Fixtures.installCore(project);
@@ -87,8 +88,8 @@ void configuresThePluginBuild(boolean readOnlyNodeModules) throws Exception {
8788
"-PtempBuild=true",
8889
"-PappPath=" + Fixtures.path(project.resolve("app")),
8990
"-PappResourcesPath=" + Fixtures.path(project.resolve("app/App_Resources"))));
90-
if (readOnlyNodeModules) {
91-
arguments.add("-Pnativescript.readOnlyNodeModules=true");
91+
if (buildsInPlatforms) {
92+
arguments.add("-Pnativescript.pluginBuildLocation=platforms");
9293
}
9394
Map<String, String> environment = new HashMap<>(System.getenv());
9495
environment.put("USER_PROJECT_ROOT", Fixtures.path(project));
@@ -103,7 +104,7 @@ void configuresThePluginBuild(boolean readOnlyNodeModules) throws Exception {
103104
.build();
104105
Map<String, String> probes = Fixtures.probes(result);
105106

106-
if (!readOnlyNodeModules) {
107+
if (!buildsInPlatforms) {
107108
// The template only runs the plugin's own buildscript.gradle on request, so the
108109
// library is not on the classpath and the block takes its old CLI branch.
109110
assertEquals("false", probes.get("extension"));

‎gradle-api/src/main/java/org/nativescript/gradle/PluginInfo.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public interface PluginInfo {
2121
String getName();
2222

2323
/**
24-
* The installed npm package. Treat it as read-only: the CLI may keep {@code node_modules}
25-
* read-only and work on a copy of the native files instead.
24+
* The installed npm package. Treat it as read-only: with {@code android.pluginBuildLocation}
25+
* set to {@code "platforms"}, the CLI leaves {@code node_modules} untouched and works on a copy
26+
* of the native files instead.
2627
*
2728
* @return an absolute, normalized directory
2829
* @since API level 1

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

Lines changed: 6 additions & 5 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;
@@ -9,10 +10,10 @@ interface IPluginBuildOptions extends IAndroidBuildOptions {
910
*/
1011
aarName?: string;
1112
/**
12-
* The project keeps node_modules read-only, so `aarOutputDir` is a CLI-owned
13-
* copy of the plugin's platforms/android.
13+
* When `"platforms"`, `aarOutputDir` is a CLI-owned copy of the plugin's
14+
* platforms/android. Absent means `"node_modules"`.
1415
*/
15-
readOnlyNodeModules?: boolean;
16+
pluginBuildLocation?: AndroidPluginBuildLocation;
1617
/**
1718
* The project's nativescript-gradle.json, passed to the plugin's Gradle build when it exists.
1819
*/
@@ -92,9 +93,9 @@ interface IBuildAndroidPluginData extends Partial<IProjectDir> {
9293
gradleArgs?: string;
9394

9495
/**
95-
* The project keeps node_modules read-only (`android.readOnlyNodeModules`).
96+
* The project's `android.pluginBuildLocation`. Absent means `"node_modules"`.
9697
*/
97-
readOnlyNodeModules?: boolean;
98+
pluginBuildLocation?: AndroidPluginBuildLocation;
9899

99100
/**
100101
* Absolute path of the project's nativescript-gradle.json, when it exists.

‎lib/definitions/project.d.ts‎

Lines changed: 11 additions & 3 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;
@@ -181,10 +182,17 @@ interface INsConfigAndroid extends INsConfigPlaform {
181182
runtimePackageName?: string;
182183

183184
/**
184-
* Never write into node_modules: each plugin's Android files are copied to
185-
* `platforms/android/.ns-plugins/` and its aar is built there.
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.
186194
*/
187-
readOnlyNodeModules?: boolean;
195+
pluginBuildLocation?: AndroidPluginBuildLocation;
188196
}
189197

190198
interface INsConfigHooks {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
282282
pluginName: options.pluginName,
283283
projectDir: options.projectDir,
284284
};
285-
if (options.readOnlyNodeModules) {
286-
pluginBuildData.readOnlyNodeModules = true;
285+
if (options.pluginBuildLocation === "platforms") {
286+
pluginBuildData.pluginBuildLocation = "platforms";
287287
}
288288
if (options.gradleFactsFile && this.$fs.exists(options.gradleFactsFile)) {
289289
pluginBuildData.gradleFactsFile = path.resolve(options.gradleFactsFile);
290290
}
291291
await this.buildPlugin(pluginBuildData);
292-
if (!options.readOnlyNodeModules) {
292+
if (options.pluginBuildLocation !== "platforms") {
293293
this.$watchIgnoreListService.addFileToIgnoreList(
294294
path.join(options.aarOutputDir, `${aarName}.aar`),
295295
);
@@ -345,7 +345,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
345345
*/
346346
private removeStaleAars(options: IPluginBuildOptions, aarName: string): void {
347347
for (const staleAarPath of this.getStaleAarPaths(options, aarName)) {
348-
if (!options.readOnlyNodeModules) {
348+
if (options.pluginBuildLocation !== "platforms") {
349349
const staleName = path.basename(staleAarPath, ".aar");
350350
const previousOutput = path.join(
351351
options.tempPluginDirPath,
@@ -922,8 +922,8 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
922922
);
923923
}
924924

925-
if (pluginBuildSettings.readOnlyNodeModules) {
926-
localArgs.push("-Pnativescript.readOnlyNodeModules=true");
925+
if (pluginBuildSettings.pluginBuildLocation === "platforms") {
926+
localArgs.push("-Pnativescript.pluginBuildLocation=platforms");
927927
}
928928

929929
if (pluginBuildSettings.gradleArgs) {

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

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "../common/helpers";
1414
import { performanceLog } from ".././common/decorators";
1515
import {
16+
AndroidPluginBuildLocation,
1617
IProjectData,
1718
IProjectDataService,
1819
IValidatePlatformOutput,
@@ -73,15 +74,15 @@ interface IAndroidPluginLocation {
7374
*/
7475
nativeDir: string;
7576
/**
76-
* Set when node_modules is read-only.
77+
* Set when plugins are built in platforms/android.
7778
*/
7879
mirrorDir?: string;
7980
}
8081

8182
interface IAndroidPluginLayout {
8283
key: string;
8384
platformDir: string;
84-
readOnlyNodeModules: boolean;
85+
pluginBuildLocation: AndroidPluginBuildLocation;
8586
plugins: Map<string, IAndroidPluginLocation>;
8687
/**
8788
* Plugins whose mirror changed since they were last prepared.
@@ -739,9 +740,9 @@ export class AndroidProjectService
739740

740741
const layout = this.getPluginLayout(projectData);
741742
const plugin = layout.plugins.get(pluginData.name);
742-
if (!plugin && layout.readOnlyNodeModules) {
743-
// Gradle only reads the plugins listed in dependencies.json, and with
744-
// read-only node_modules there is nowhere else to build this one to.
743+
if (!plugin && layout.pluginBuildLocation === "platforms") {
744+
// Gradle only reads the plugins listed in dependencies.json, and only
745+
// those get a copy in platforms/android to build into.
745746
this.$logger.trace(
746747
`Skipping ${pluginData.name}: it does not declare Android support in its package.json.`,
747748
);
@@ -805,9 +806,10 @@ export class AndroidProjectService
805806
projectDir: projectData.projectDir,
806807
pluginName: pluginData.name,
807808
platformsAndroidDirPath: pluginPlatformsFolderPath,
808-
aarOutputDir: layout.readOnlyNodeModules
809-
? plugin.nativeDir
810-
: pluginPlatformsFolderPath,
809+
aarOutputDir:
810+
layout.pluginBuildLocation === "platforms"
811+
? plugin.nativeDir
812+
: pluginPlatformsFolderPath,
811813
tempPluginDirPath: path.join(projectData.platformsDir, "tempPlugin"),
812814
gradleFactsFile: getGradleFactsFilePath(layout.platformDir),
813815
};
@@ -816,8 +818,8 @@ export class AndroidProjectService
816818
options.aarName = plugin.aarName;
817819
}
818820

819-
if (layout.readOnlyNodeModules) {
820-
options.readOnlyNodeModules = true;
821+
if (layout.pluginBuildLocation === "platforms") {
822+
options.pluginBuildLocation = "platforms";
821823
}
822824

823825
return options;
@@ -832,15 +834,26 @@ export class AndroidProjectService
832834
);
833835
}
834836

835-
private isReadOnlyNodeModules(projectData: IProjectData): boolean {
836-
return projectData.nsConfig?.android?.readOnlyNodeModules === true;
837+
private getPluginBuildLocation(
838+
projectData: IProjectData,
839+
): AndroidPluginBuildLocation {
840+
const location =
841+
projectData.nsConfig?.android?.pluginBuildLocation ?? "node_modules";
842+
if (location !== "node_modules" && location !== "platforms") {
843+
this.$errors.fail(
844+
'Invalid value %s for android.pluginBuildLocation in nativescript.config: use "node_modules" or "platforms".',
845+
JSON.stringify(location),
846+
);
847+
}
848+
849+
return location;
837850
}
838851

839852
private getPluginLayoutKey(projectData: IProjectData): string {
840853
return JSON.stringify([
841854
projectData.projectDir,
842855
this.getAndroidPlatformDir(projectData),
843-
this.isReadOnlyNodeModules(projectData),
856+
this.getPluginBuildLocation(projectData),
844857
]);
845858
}
846859

@@ -877,7 +890,7 @@ export class AndroidProjectService
877890
nativeDependencyData: IDependencyData[],
878891
): IAndroidPluginLayout {
879892
const platformDir = this.getAndroidPlatformDir(projectData);
880-
const readOnlyNodeModules = this.isReadOnlyNodeModules(projectData);
893+
const pluginBuildLocation = this.getPluginBuildLocation(projectData);
881894
const mirrorsRoot = path.join(
882895
platformDir,
883896
constants.ANDROID_PLUGIN_MIRRORS_DIR_NAME,
@@ -895,9 +908,10 @@ export class AndroidProjectService
895908
const plugins = new Map<string, IAndroidPluginLocation>();
896909
for (const dep of nativeDependencyData) {
897910
const aarName = aarNames.get(dep.name);
898-
const mirrorDir = readOnlyNodeModules
899-
? path.join(mirrorsRoot, aarName)
900-
: undefined;
911+
const mirrorDir =
912+
pluginBuildLocation === "platforms"
913+
? path.join(mirrorsRoot, aarName)
914+
: undefined;
901915
plugins.set(dep.name, {
902916
aarName,
903917
packageDir: dep.directory,
@@ -911,7 +925,7 @@ export class AndroidProjectService
911925
}
912926

913927
let changedMirrors: string[] = [];
914-
if (readOnlyNodeModules) {
928+
if (pluginBuildLocation === "platforms") {
915929
changedMirrors = this.$androidPluginMirrorService.syncMirrors(
916930
mirrorsRoot,
917931
nativeDependencyData.map((dep) => {
@@ -933,7 +947,7 @@ export class AndroidProjectService
933947
return {
934948
key: this.getPluginLayoutKey(projectData),
935949
platformDir,
936-
readOnlyNodeModules,
950+
pluginBuildLocation,
937951
plugins,
938952
changedMirrors: new Set(changedMirrors),
939953
};

0 commit comments

Comments
 (0)