Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions lib/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,71 @@ function sanitizePluginName(pluginName: string): string {
return pluginName.split("/").pop();
}

function getScopeQualifiedPluginName(pluginName: string): string | null {
const parts = pluginName.split("/");
if (parts.length < 2 || !parts[0].startsWith("@")) {
return null;
}

const scope = parts[0].slice(1).replace(/[^A-Za-z0-9_]/g, "_");
return `${scope}_${getShortPluginName(pluginName)}`;
}

// The aar name doubles as a Java package segment (the `org.nativescript.<name>`
// namespace fallback), which cannot start with a digit.
function withoutLeadingDigit(name: string): string {
return /^[0-9]/.test(name) ? `_${name}` : name;
}

/**
* Maps each plugin name to a file-system and Java-package safe name for its aar
* that is unique among `pluginNames`. A plugin whose short name is not shared
* keeps `getShortPluginName(name)`, so projects without clashes keep their names.
* Throws if two plugins still end up with the same name.
*/
export function getUniqueShortPluginNames(
pluginNames: string[],
): Map<string, string> {
const sortedNames = _.sortBy(_.uniq(pluginNames));
const namesByShortName = _.groupBy(sortedNames, getShortPluginName);
const result = new Map<string, string>();
const owners = new Map<string, string>();

for (const pluginName of sortedNames) {
const shortName = getShortPluginName(pluginName);
const aarName =
namesByShortName[shortName].length === 1
? shortName
: withoutLeadingDigit(
getScopeQualifiedPluginName(pluginName) ?? shortName,
);

const owner = owners.get(aarName);
if (owner !== undefined) {
throw new Error(
`The plugins "${owner}" and "${pluginName}" both need the Android aar name "${aarName}". Remove one of them from the project.`,
);
}

owners.set(aarName, pluginName);
result.set(pluginName, aarName);
}

return result;
}

/**
* Every aar name `getUniqueShortPluginNames` can give this plugin, depending on
* which other plugins are installed.
*/
export function getCandidateAarNames(pluginName: string): string[] {
const shortName = getShortPluginName(pluginName);
return _.uniq([
shortName,
withoutLeadingDigit(getScopeQualifiedPluginName(pluginName) ?? shortName),
]);
}

export async function executeActionByChunks<T>(
initialData: T[] | IDictionary<T>,
chunkSize: number,
Expand Down
125 changes: 125 additions & 0 deletions lib/common/test/unit-tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,4 +1111,129 @@ const test = require("./test");`,
assert.deepStrictEqual(seen.sink, ["kept"]);
});
});

describe("getUniqueShortPluginNames", () => {
const toObject = (names: string[]) =>
Object.fromEntries(helpers.getUniqueShortPluginNames(names));

it("keeps the short name of plugins without a clash", () => {
assert.deepStrictEqual(
toObject([
"@nativescript/core",
"nativescript-plugin",
"@scope/my-plugin",
"9-lives",
]),
{
"@nativescript/core": "core",
"nativescript-plugin": "nativescript_plugin",
"@scope/my-plugin": "my_plugin",
"9-lives": "9_lives",
},
);
});

it("qualifies clashing scoped plugins with their scope", () => {
assert.deepStrictEqual(
toObject(["@foo/plugin-x", "@bar/plugin-x", "@nativescript/core"]),
{
"@foo/plugin-x": "foo_plugin_x",
"@bar/plugin-x": "bar_plugin_x",
"@nativescript/core": "core",
},
);
});

it("lets an unscoped plugin keep its name when it clashes with a scoped one", () => {
assert.deepStrictEqual(toObject(["plugin-x", "@foo/plugin-x"]), {
"plugin-x": "plugin_x",
"@foo/plugin-x": "foo_plugin_x",
});
});

it("replaces characters a scope may contain but a name may not", () => {
assert.deepStrictEqual(toObject(["@my.org~x/plugin", "@a/plugin"]), {
"@my.org~x/plugin": "my_org_x_plugin",
"@a/plugin": "a_plugin",
});
});

it("prefixes clashing names that would start with a digit", () => {
assert.deepStrictEqual(toObject(["@1password/plugin", "@a/plugin"]), {
"@1password/plugin": "_1password_plugin",
"@a/plugin": "a_plugin",
});
assert.deepStrictEqual(toObject(["9-lives", "@a/9-lives"]), {
"9-lives": "_9_lives",
"@a/9-lives": "a_9_lives",
});
});

it("fails when two plugins still share a name", () => {
assert.throws(
() =>
helpers.getUniqueShortPluginNames(["@foo/plugin-x", "@foo/plugin_x"]),
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
);
assert.throws(
() =>
helpers.getUniqueShortPluginNames([
"foo-plugin-x",
"@foo/plugin-x",
"@bar/plugin-x",
]),
/"@foo\/plugin-x" and "foo-plugin-x".*"foo_plugin_x"/,
);
});

it("does not depend on the order of the plugins", () => {
const names = ["@foo/plugin-x", "plugin-x", "@bar/plugin-x", "other"];
const expected = toObject(names);
assert.deepStrictEqual(toObject([...names].reverse()), expected);
assert.deepStrictEqual(
toObject([names[2], names[0], names[3], names[1]]),
expected,
);
assert.throws(
() =>
helpers.getUniqueShortPluginNames(["@foo/plugin_x", "@foo/plugin-x"]),
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
);
});
});

describe("getCandidateAarNames", () => {
it("lists the short and the scope-qualified name of a scoped plugin", () => {
assert.deepStrictEqual(helpers.getCandidateAarNames("@foo/plugin-x"), [
"plugin_x",
"foo_plugin_x",
]);
});

it("lists the short name of an unscoped plugin", () => {
assert.deepStrictEqual(helpers.getCandidateAarNames("plugin-x"), [
"plugin_x",
]);
assert.deepStrictEqual(helpers.getCandidateAarNames("9-lives"), [
"9_lives",
"_9_lives",
]);
});

it("covers every name getUniqueShortPluginNames gives", () => {
const projects = [
["@foo/plugin-x"],
["@foo/plugin-x", "@bar/plugin-x"],
["plugin-x", "@foo/plugin-x"],
["@1a/plugin", "@b/plugin", "plugin", "9-lives", "@c/9-lives"],
];
for (const project of projects) {
for (const [name, aarName] of helpers.getUniqueShortPluginNames(
project,
)) {
assert.include(helpers.getCandidateAarNames(name), aarName);
}
}
});
});
});
10 changes: 10 additions & 0 deletions lib/definitions/android-plugin-migrator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { IProjectDir } from "../common/declarations";

interface IPluginBuildOptions extends IAndroidBuildOptions {
projectDir?: string;
/**
* File name (without extension) of the built aar and of its temp build dir.
* Defaults to the plugin's short name.
*/
aarName?: string;
}

interface IAndroidBuildOptions {
Expand All @@ -17,6 +22,11 @@ interface IAndroidBuildOptions {
interface IAndroidPluginBuildService {
buildAar(options: IPluginBuildOptions): Promise<boolean>;
migrateIncludeGradle(options: IPluginBuildOptions): boolean;
/**
* Whether the plugin has sources to build but `aarOutputDir` lacks its aar
* or still holds one the CLI built under another of its candidate names.
*/
hasOutdatedAar(options: IPluginBuildOptions): boolean;
}

/**
Expand Down
Loading
Loading