Skip to content

Commit b0af0fc

Browse files
committed
fix(android): address review of unique aar names
Only names the CLI creates get the leading `_` for a leading digit. An unscoped plugin keeps its exact short name even when it clashes, so `9-lives` next to `@a/9-lives` stays `9_lives` (and its `9_lives.aar` is no longer deleted), while `@9a/x` next to `x` becomes `_9a_x`. Short names are never prefixed, as that would rename aars in existing projects. Two plugins that still end up with the same name keep failing prepare. Aar names are now unique among every package prepare builds an aar for: native Android dependencies plus any package with a `nativescript` key whose platforms/android directory exists, since those share platforms/tempPlugin too. Such packages could otherwise still build in the same temp project as a clashing plugin. dependencies.json and the names in projects without a clash are unchanged. The Android project service gets $nodeModulesDependenciesBuilder through its constructor. getUniqueShortPluginNames is renamed to getUniqueAarNames, and copyAar/updateManifest take an `aarName`.
1 parent 389aa01 commit b0af0fc

5 files changed

Lines changed: 146 additions & 71 deletions

File tree

‎lib/common/helpers.ts‎

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,27 @@ function sanitizePluginName(pluginName: string): string {
125125
return pluginName.split("/").pop();
126126
}
127127

128-
function getScopeQualifiedPluginName(pluginName: string): string | null {
128+
// The aar name doubles as a Java package segment (the `org.nativescript.<name>`
129+
// namespace fallback), which cannot start with a digit. Short names are used
130+
// as is, since changing them would rename aars in existing projects.
131+
function getScopeQualifiedAarName(pluginName: string): string | null {
129132
const parts = pluginName.split("/");
130133
if (parts.length < 2 || !parts[0].startsWith("@")) {
131134
return null;
132135
}
133136

134137
const scope = parts[0].slice(1).replace(/[^A-Za-z0-9_]/g, "_");
135-
return `${scope}_${getShortPluginName(pluginName)}`;
136-
}
137-
138-
// The aar name doubles as a Java package segment (the `org.nativescript.<name>`
139-
// namespace fallback), which cannot start with a digit.
140-
function withoutLeadingDigit(name: string): string {
138+
const name = `${scope}_${getShortPluginName(pluginName)}`;
141139
return /^[0-9]/.test(name) ? `_${name}` : name;
142140
}
143141

144142
/**
145-
* Maps each plugin name to a file-system and Java-package safe name for its aar
146-
* that is unique among `pluginNames`. A plugin whose short name is not shared
147-
* keeps `getShortPluginName(name)`, so projects without clashes keep their names.
148-
* Throws if two plugins still end up with the same name.
143+
* Maps each plugin name to a name for its aar that is unique among
144+
* `pluginNames`. A plugin whose short name is not shared, or that is unscoped,
145+
* keeps `getShortPluginName(name)`, so projects without clashes keep their
146+
* names. Throws if two plugins still end up with the same name.
149147
*/
150-
export function getUniqueShortPluginNames(
151-
pluginNames: string[],
152-
): Map<string, string> {
148+
export function getUniqueAarNames(pluginNames: string[]): Map<string, string> {
153149
const sortedNames = _.sortBy(_.uniq(pluginNames));
154150
const namesByShortName = _.groupBy(sortedNames, getShortPluginName);
155151
const result = new Map<string, string>();
@@ -160,9 +156,7 @@ export function getUniqueShortPluginNames(
160156
const aarName =
161157
namesByShortName[shortName].length === 1
162158
? shortName
163-
: withoutLeadingDigit(
164-
getScopeQualifiedPluginName(pluginName) ?? shortName,
165-
);
159+
: (getScopeQualifiedAarName(pluginName) ?? shortName);
166160

167161
const owner = owners.get(aarName);
168162
if (owner !== undefined) {
@@ -179,15 +173,15 @@ export function getUniqueShortPluginNames(
179173
}
180174

181175
/**
182-
* Every aar name `getUniqueShortPluginNames` can give this plugin, depending on
183-
* which other plugins are installed.
176+
* Every aar name `getUniqueAarNames` can give this plugin, depending on which
177+
* other plugins are installed.
184178
*/
185179
export function getCandidateAarNames(pluginName: string): string[] {
186-
const shortName = getShortPluginName(pluginName);
187-
return _.uniq([
188-
shortName,
189-
withoutLeadingDigit(getScopeQualifiedPluginName(pluginName) ?? shortName),
190-
]);
180+
const scopeQualifiedName = getScopeQualifiedAarName(pluginName);
181+
return [
182+
getShortPluginName(pluginName),
183+
...(scopeQualifiedName ? [scopeQualifiedName] : []),
184+
];
191185
}
192186

193187
export async function executeActionByChunks<T>(

‎lib/common/test/unit-tests/helpers.ts‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,9 @@ const test = require("./test");`,
11121112
});
11131113
});
11141114

1115-
describe("getUniqueShortPluginNames", () => {
1115+
describe("getUniqueAarNames", () => {
11161116
const toObject = (names: string[]) =>
1117-
Object.fromEntries(helpers.getUniqueShortPluginNames(names));
1117+
Object.fromEntries(helpers.getUniqueAarNames(names));
11181118

11191119
it("keeps the short name of plugins without a clash", () => {
11201120
assert.deepStrictEqual(
@@ -1158,32 +1158,42 @@ const test = require("./test");`,
11581158
});
11591159
});
11601160

1161-
it("prefixes clashing names that would start with a digit", () => {
1161+
it("prefixes scope-qualified names that would start with a digit", () => {
11621162
assert.deepStrictEqual(toObject(["@1password/plugin", "@a/plugin"]), {
11631163
"@1password/plugin": "_1password_plugin",
11641164
"@a/plugin": "a_plugin",
11651165
});
1166+
assert.deepStrictEqual(toObject(["@9a/x", "@b/x"]), {
1167+
"@9a/x": "_9a_x",
1168+
"@b/x": "b_x",
1169+
});
1170+
});
1171+
1172+
it("lets an unscoped plugin starting with a digit keep its name in a clash", () => {
11661173
assert.deepStrictEqual(toObject(["9-lives", "@a/9-lives"]), {
1167-
"9-lives": "_9_lives",
1174+
"9-lives": "9_lives",
11681175
"@a/9-lives": "a_9_lives",
11691176
});
11701177
});
11711178

11721179
it("fails when two plugins still share a name", () => {
11731180
assert.throws(
1174-
() =>
1175-
helpers.getUniqueShortPluginNames(["@foo/plugin-x", "@foo/plugin_x"]),
1181+
() => helpers.getUniqueAarNames(["@foo/plugin-x", "@foo/plugin_x"]),
11761182
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
11771183
);
11781184
assert.throws(
11791185
() =>
1180-
helpers.getUniqueShortPluginNames([
1186+
helpers.getUniqueAarNames([
11811187
"foo-plugin-x",
11821188
"@foo/plugin-x",
11831189
"@bar/plugin-x",
11841190
]),
11851191
/"@foo\/plugin-x" and "foo-plugin-x".*"foo_plugin_x"/,
11861192
);
1193+
assert.throws(
1194+
() => helpers.getUniqueAarNames(["_9a_x", "@9a/x", "x"]),
1195+
/"@9a\/x" and "_9a_x".*"_9a_x"/,
1196+
);
11871197
});
11881198

11891199
it("does not depend on the order of the plugins", () => {
@@ -1195,8 +1205,7 @@ const test = require("./test");`,
11951205
expected,
11961206
);
11971207
assert.throws(
1198-
() =>
1199-
helpers.getUniqueShortPluginNames(["@foo/plugin_x", "@foo/plugin-x"]),
1208+
() => helpers.getUniqueAarNames(["@foo/plugin_x", "@foo/plugin-x"]),
12001209
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
12011210
);
12021211
});
@@ -1216,21 +1225,26 @@ const test = require("./test");`,
12161225
]);
12171226
assert.deepStrictEqual(helpers.getCandidateAarNames("9-lives"), [
12181227
"9_lives",
1219-
"_9_lives",
12201228
]);
12211229
});
12221230

1223-
it("covers every name getUniqueShortPluginNames gives", () => {
1231+
it("prefixes a scope-qualified name that would start with a digit", () => {
1232+
assert.deepStrictEqual(helpers.getCandidateAarNames("@9a/x"), [
1233+
"x",
1234+
"_9a_x",
1235+
]);
1236+
});
1237+
1238+
it("covers every name getUniqueAarNames gives", () => {
12241239
const projects = [
12251240
["@foo/plugin-x"],
12261241
["@foo/plugin-x", "@bar/plugin-x"],
12271242
["plugin-x", "@foo/plugin-x"],
12281243
["@1a/plugin", "@b/plugin", "plugin", "9-lives", "@c/9-lives"],
1244+
["@9a/x", "x"],
12291245
];
12301246
for (const project of projects) {
1231-
for (const [name, aarName] of helpers.getUniqueShortPluginNames(
1232-
project,
1233-
)) {
1247+
for (const [name, aarName] of helpers.getUniqueAarNames(project)) {
12341248
assert.include(helpers.getCandidateAarNames(name), aarName);
12351249
}
12361250
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,9 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
327327
}
328328

329329
/**
330-
* Aars in `aarOutputDir` under another of the plugin's candidate names that
331-
* the CLI built. Earlier CLIs copied every built plugin's aar to
332-
* `<short name>.aar`, overwriting any file the package shipped under that
333-
* name, so that file is always a leftover. A scope-qualified name is only
334-
* ever built by this plugin, so that file is a leftover only if the temp
335-
* project for the name was set up for this plugin and has a build output to
336-
* compare it with; otherwise the package ships it.
330+
* `<short name>.aar` is always CLI output, since earlier CLIs overwrote it on
331+
* every build. A package may ship a scope-qualified name itself, so that one
332+
* only counts when its temp project was built for this plugin.
337333
*/
338334
private getStaleAars(
339335
options: IPluginBuildOptions,
@@ -482,11 +478,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
482478
private async updateManifest(
483479
manifestFilePath: string,
484480
pluginTempMainSrcDir: string,
485-
shortPluginName: string,
481+
aarName: string,
486482
): Promise<void> {
487483
let updatedManifestContent;
488484
this.$fs.ensureDirectoryExists(pluginTempMainSrcDir);
489-
const defaultPackageName = "org.nativescript." + shortPluginName;
485+
const defaultPackageName = "org.nativescript." + aarName;
490486
if (manifestFilePath) {
491487
let androidManifestContent;
492488
try {
@@ -851,11 +847,11 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
851847
}
852848

853849
private copyAar(
854-
shortPluginName: string,
850+
aarName: string,
855851
pluginTempDir: string,
856852
aarOutputDir: string,
857853
): void {
858-
const finalAarName = `${shortPluginName}-release.aar`;
854+
const finalAarName = `${aarName}-release.aar`;
859855
const pathToBuiltAar = path.join(
860856
pluginTempDir,
861857
"build",
@@ -869,7 +865,7 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
869865
if (aarOutputDir) {
870866
this.$fs.copyFile(
871867
pathToBuiltAar,
872-
path.join(aarOutputDir, `${shortPluginName}.aar`),
868+
path.join(aarOutputDir, `${aarName}.aar`),
873869
);
874870
}
875871
} catch (e) {

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as semver from "semver";
66
import * as projectServiceBaseLib from "./platform-project-service-base";
77
import { DeviceAndroidDebugBridge } from "../common/mobile/android/device-android-debug-bridge";
88
import { Configurations, LiveSyncPaths } from "../common/constants";
9-
import { getUniqueShortPluginNames, hook } from "../common/helpers";
9+
import { getUniqueAarNames, hook } from "../common/helpers";
1010
import { performanceLog } from ".././common/decorators";
1111
import {
1212
IProjectData,
@@ -156,6 +156,7 @@ export class AndroidProjectService
156156
private $gradleCommandService: IGradleCommandService,
157157
private $gradleBuildService: IGradleBuildService,
158158
private $analyticsService: IAnalyticsService,
159+
private $nodeModulesDependenciesBuilder: INodeModulesDependenciesBuilder,
159160
) {
160161
super($fs, $projectDataService);
161162
}
@@ -764,34 +765,46 @@ export class AndroidProjectService
764765
*/
765766
private getPluginAarNames(projectData: IProjectData): Map<string, string> {
766767
if (this.pluginAarNames?.projectDir !== projectData.projectDir) {
767-
const dependencies = this.$injector
768-
.resolve<INodeModulesDependenciesBuilder>(
769-
"nodeModulesDependenciesBuilder",
770-
)
771-
.getProductionDependencies(
772-
projectData.projectDir,
773-
projectData.ignoredDependencies,
774-
);
775768
this.setPluginAarNames(
776769
projectData,
777-
this.filterUniqueDependencies(dependencies).filter(
778-
AndroidProjectService.isNativeAndroidDependency,
770+
this.filterUniqueDependencies(
771+
this.$nodeModulesDependenciesBuilder.getProductionDependencies(
772+
projectData.projectDir,
773+
projectData.ignoredDependencies,
774+
),
779775
),
780776
);
781777
}
782778

783779
return this.pluginAarNames.names;
784780
}
785781

782+
/**
783+
* Names must be unique among every package preparePluginNativeCode builds,
784+
* not only those in dependencies.json, since they share platforms/tempPlugin.
785+
*/
786786
private setPluginAarNames(
787787
projectData: IProjectData,
788-
nativeDependencyData: IDependencyData[],
788+
dependencies: IDependencyData[],
789789
): void {
790+
const aarPluginNames = dependencies
791+
.filter(
792+
(dep) =>
793+
AndroidProjectService.isNativeAndroidDependency(dep) ||
794+
(dep.nativescript &&
795+
this.$fs.exists(
796+
path.join(
797+
dep.directory,
798+
constants.PLATFORMS_DIR_NAME,
799+
AndroidProjectService.ANDROID_PLATFORM_NAME,
800+
),
801+
)),
802+
)
803+
.map((dep) => dep.name);
804+
790805
let names: Map<string, string>;
791806
try {
792-
names = getUniqueShortPluginNames(
793-
nativeDependencyData.map((dep) => dep.name),
794-
);
807+
names = getUniqueAarNames(aarPluginNames);
795808
} catch (err) {
796809
this.$errors.fail(err.message);
797810
}
@@ -816,6 +829,7 @@ export class AndroidProjectService
816829
): Promise<IDependencyData[]> {
817830
if (dependencies) {
818831
dependencies = this.filterUniqueDependencies(dependencies);
832+
this.setPluginAarNames(projectData, dependencies);
819833
return this.provideDependenciesJson(projectData, dependencies);
820834
}
821835
}
@@ -861,7 +875,6 @@ export class AndroidProjectService
861875
let nativeDependencyData = dependencies.filter(
862876
AndroidProjectService.isNativeAndroidDependency,
863877
);
864-
this.setPluginAarNames(projectData, nativeDependencyData);
865878

866879
let nativeDependencies = nativeDependencyData.map(
867880
({ name, directory, dependencies }) => {

0 commit comments

Comments
 (0)