Skip to content

Commit 86f148f

Browse files
committed
feat(gradle-api)!: require NativeScript CLI 9.2+
Plugins that use the library now require a project prepared by CLI 9.2 or newer instead of the library reimplementing older CLIs' rules. A missing facts file and one that no longer matches dependencies.json each fail with an actionable message; plugin()/findPlugin() only know the plugins the CLI lists. The README documents a one-line guard for a plugin's own aar build on older CLIs, an optional peer dependency on nativescript >=9.2.0-0, and what plugins need to write (usually nothing) to depend on other plugins.
1 parent 8cf2855 commit 86f148f

22 files changed

Lines changed: 708 additions & 808 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ CLAUDE.local.md
134134

135135
# org.nativescript:gradle-api
136136
gradle-api/build/
137-
gradle-api/.gradle/
137+
gradle-api/**/.gradle/
138138
gradle-api/.kotlin/
139139
gradle-api/samples/**/build/

‎gradle-api/README.md‎

Lines changed: 152 additions & 97 deletions
Large diffs are not rendered by default.

‎gradle-api/gradle.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ POM_ARTIFACT_ID=gradle-api
66
VERSION_NAME=0.1.0-SNAPSHOT
77

88
POM_NAME=NativeScript Gradle API
9-
POM_DESCRIPTION=Gradle API for NativeScript plugins: project paths, installed plugins and CLI capabilities, from the facts the NativeScript CLI writes or from older CLIs' build files.
9+
POM_DESCRIPTION=Gradle API for NativeScript plugins: project paths, installed plugins and CLI capabilities, from the facts NativeScript CLI 9.2 and newer write.
1010
POM_INCEPTION_YEAR=2026
1111
POM_URL=https://github.com/NativeScript/nativescript-cli
1212

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
apply plugin: "org.nativescript.api"
2-
31
// The CLI builds this plugin's aar in a separate project that runs only the next block, not the
4-
// rest of this file. CLI 9.2+ applies the library there; older CLIs may not, so the block falls
5-
// back to what they provide when the extension is missing.
2+
// rest of this file. CLI 9.2+ loads org.nativescript:gradle-api there and applies it; older CLIs
3+
// don't, so the block stops with a clear message instead of failing on an unknown `nativescript`.
64
dependencies {
75
if (project.hasProperty("tempBuild")) {
8-
def ns = project.extensions.findByName("nativescript")
9-
if (ns != null) {
10-
compileOnly ns.plugin("@nativescript/core").artifacts
11-
} else {
12-
compileOnly fileTree(dir: "$USER_PROJECT_ROOT/node_modules/@nativescript/core/platforms/android", include: ["**/*.aar", "**/*.jar"])
6+
if (project.extensions.findByName("nativescript") == null) {
7+
throw new GradleException("@nativescript/gradle-api-sample needs NativeScript CLI 9.2 or newer: npm i -D nativescript@latest")
138
}
9+
compileOnly nativescript.plugin("@nativescript/core").artifacts
1410
}
1511
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"dependenciesJson": {
28-
"description": "The dependencies.json the CLI wrote next to this file, before this file. A CLI that does not write this file still rewrites dependencies.json but leaves this file behind, so readers ignore this file when dependencies.json no longer has this hash.",
28+
"description": "The dependencies.json the CLI wrote next to this file, before this file. A CLI that does not write this file still rewrites dependencies.json but leaves this file behind, so readers report an error, rather than use this file, when dependencies.json is missing or no longer has this hash.",
2929
"type": "object",
3030
"required": ["sha256"],
3131
"properties": {

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

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
*/
2424
class AppBuildTest {
2525
enum Cli {
26-
/** CLI 9.1 and older: no facts file. */
27-
LEGACY,
28-
/** A facts-writing CLI, which passes the file's path. */
26+
/** A CLI before 9.2, which writes no facts file. */
27+
NO_FACTS,
28+
/** CLI 9.2+, which passes the facts file's path. */
2929
FACTS_PROPERTY,
3030
/** platforms/android opened without the CLI, e.g. in Android Studio. */
3131
FACTS_FOUND,
32-
/** An older CLI prepared the project after a facts-writing one: facts left behind. */
32+
/** A CLI before 9.2 prepared the project after a 9.2+ one: facts left behind. */
3333
STALE_FACTS;
3434

3535
boolean usesFacts() {
@@ -60,7 +60,7 @@ void answersFromTheAppBuild(String gradleVersion, Cli cli) throws Exception {
6060
Path pluginB = project.resolve("node_modules/fixture-plugin-b/platforms/android/buildscript.gradle");
6161
Fixtures.write(pluginB, Fixtures.read(pluginB).replace("@LIBRARY_VERSION@", Fixtures.libraryVersion()));
6262
Path core = Fixtures.installCore(project);
63-
Path sample = Fixtures.installSample(project);
63+
Fixtures.installSample(project);
6464
Fixtures.write(project.resolve("package.json"), "{ \"name\": \"fixture-app\" }\n");
6565
Path dependenciesJson = Fixtures.write(platformsAndroid.resolve("dependencies.json"), String.join("\n",
6666
"[",
@@ -74,7 +74,9 @@ void answersFromTheAppBuild(String gradleVersion, Cli cli) throws Exception {
7474
"help", "--offline", "--stacktrace", "--warning-mode=fail",
7575
"--init-script", Fixtures.path(Fixtures.writeInitScript(tempDir))));
7676
Path factsFile = platformsAndroid.resolve("nativescript-gradle.json");
77-
if (cli != Cli.LEGACY) {
77+
if (cli != Cli.NO_FACTS) {
78+
// fixture-plugin-b is left out: an installed package with platforms/android that the CLI
79+
// does not list is not a plugin.
7880
Fixtures.write(factsFile, String.join("\n",
7981
"{",
8082
" \"schemaVersion\": 1,",
@@ -100,22 +102,35 @@ void answersFromTheAppBuild(String gradleVersion, Cli cli) throws Exception {
100102
}
101103
}
102104

103-
BuildResult result = GradleRunner.create()
105+
GradleRunner runner = GradleRunner.create()
104106
.withGradleVersion(gradleVersion)
105107
.withProjectDir(platformsAndroid.toFile())
106108
.withArguments(arguments)
107-
.forwardOutput()
108-
.build();
109+
.forwardOutput();
110+
111+
if (!cli.usesFacts()) {
112+
BuildResult result = runner.buildAndFail();
113+
String error = cli == Cli.NO_FACTS
114+
? "A plugin of this app uses org.nativescript:gradle-api, which needs the project to be prepared by"
115+
+ " NativeScript CLI 9.2 or newer, but there is no facts file at " + Fixtures.path(factsFile)
116+
+ ". Update the project's CLI with `npm i -D nativescript@latest`, then run `ns prepare android`."
117+
: "The NativeScript facts file " + Fixtures.path(factsFile) + " does not match "
118+
+ Fixtures.path(dependenciesJson) + ": a different NativeScript CLI rewrote dependencies.json"
119+
+ " after the facts file was written. Run `ns prepare android` again with the NativeScript CLI"
120+
+ " the project uses.";
121+
assertTrue(result.getOutput().lines().anyMatch(line -> line.equals("> " + error)), result.getOutput());
122+
assertEquals("false", Fixtures.probes(result).get("b.pluginBuild"), "pluginBuild needs no facts");
123+
return;
124+
}
125+
126+
BuildResult result = runner.build();
109127
Map<String, String> probes = Fixtures.probes(result);
110128

111-
boolean facts = cli.usesFacts();
112-
String staleWarning = "Ignoring " + Fixtures.path(factsFile) + ": it does not match " + Fixtures.path(dependenciesJson)
113-
+ ", which a different NativeScript CLI wrote. Using the fallback rules.";
114-
assertEquals(cli == Cli.STALE_FACTS, result.getOutput().contains(staleWarning), result.getOutput());
115-
Path appDir = project.resolve(facts ? "src" : "app");
129+
Path appDir = project.resolve("src");
116130
assertEquals("true", probes.get("typed"), "NativeScript.of returns the extension");
117-
assertEquals("true", probes.get("b.sameExtension"), "a second plugin's include.gradle sees the same extension");
131+
assertEquals("true", probes.get("b.sameExtension"), "include.gradle reaches the extension by its name");
118132
assertEquals("false", probes.get("b.libraryClassVisible"), "include.gradle cannot see the library's classes");
133+
assertEquals("false", probes.get("b.pluginBuild"));
119134
assertEquals(Fixtures.path(project), probes.get("b.projectDir"));
120135
assertEquals("1", probes.get("apiLevel"));
121136
assertEquals("false", probes.get("pluginBuild"));
@@ -124,19 +139,19 @@ void answersFromTheAppBuild(String gradleVersion, Cli cli) throws Exception {
124139
assertEquals(Fixtures.path(project), probes.get("projectDir"));
125140
assertEquals(Fixtures.path(appDir), probes.get("appDir"));
126141
assertEquals(Fixtures.path(appDir.resolve("App_Resources")), probes.get("appResourcesDir"));
127-
assertEquals(String.valueOf(facts), probes.get("has"));
142+
assertEquals("true", probes.get("has"));
128143
assertEquals(Fixtures.path(core), probes.get("core.packageDir"));
129144
assertEquals(Fixtures.path(core.resolve("platforms/android")), probes.get("core.nativeDir"));
130145
assertEquals("inner.aar,widgets-release.aar", probes.get("core.artifacts"));
131-
assertEquals(facts ? "core" : "null", probes.get("core.aarName"));
132-
assertEquals(facts ? Fixtures.path(core.resolve("platforms/android/core.aar")) : "null", probes.get("core.aar"));
133-
assertEquals(facts ? "[flag:true]" : "[:]", probes.get("core.options"));
146+
assertEquals("core", probes.get("core.aarName"));
147+
assertEquals(Fixtures.path(core.resolve("platforms/android/core.aar")), probes.get("core.aar"));
148+
assertEquals("[flag:true]", probes.get("core.options"));
134149
assertEquals(
135-
facts ? Fixtures.path(platformsAndroid.resolve(".ns-plugins/sample/platforms/android"))
136-
: Fixtures.path(sample.resolve("platforms/android")),
150+
Fixtures.path(platformsAndroid.resolve(".ns-plugins/sample/platforms/android")),
137151
probes.get("sample.nativeDir"));
138152
assertEquals(Fixtures.path(project.resolve("node_modules/js-only")), probes.get("jsOnly"));
139153
assertEquals("null", probes.get("jsOnlyPlugin"), "a package without native code is not a plugin");
154+
assertEquals("null", probes.get("unlistedPlugin"), "a package the CLI does not list is not a plugin");
140155
assertEquals("null", probes.get("missingPlugin"));
141156
assertEquals("null", probes.get("missingPackage"));
142157
String error = probes.get("pluginError");

0 commit comments

Comments
 (0)