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
59 changes: 59 additions & 0 deletions lib/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,65 @@ function sanitizePluginName(pluginName: string): string {
return pluginName.split("/").pop();
}

// The aar name doubles as a Java package segment (the `org.nativescript.<name>`
// namespace fallback), which cannot start with a digit. Short names are used
// as is, since changing them would rename aars in existing projects.
function getScopeQualifiedAarName(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, "_");
const name = `${scope}_${getShortPluginName(pluginName)}`;
return /^[0-9]/.test(name) ? `_${name}` : name;
}

/**
* Maps each plugin name to a name for its aar that is unique among
* `pluginNames`. A plugin whose short name is not shared, or that is unscoped,
* keeps `getShortPluginName(name)`, so projects without clashes keep their
* names. Throws if two plugins still end up with the same name.
*/
export function getUniqueAarNames(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
: (getScopeQualifiedAarName(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 `getUniqueAarNames` can give this plugin, depending on which
* other plugins are installed.
*/
export function getCandidateAarNames(pluginName: string): string[] {
const scopeQualifiedName = getScopeQualifiedAarName(pluginName);
return [
getShortPluginName(pluginName),
...(scopeQualifiedName ? [scopeQualifiedName] : []),
];
}

export async function executeActionByChunks<T>(
initialData: T[] | IDictionary<T>,
chunkSize: number,
Expand Down
139 changes: 139 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,143 @@ const test = require("./test");`,
assert.deepStrictEqual(seen.sink, ["kept"]);
});
});

describe("getUniqueAarNames", () => {
const toObject = (names: string[]) =>
Object.fromEntries(helpers.getUniqueAarNames(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 scope-qualified 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(["@9a/x", "@b/x"]), {
"@9a/x": "_9a_x",
"@b/x": "b_x",
});
});

it("lets an unscoped plugin starting with a digit keep its name in a clash", () => {
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.getUniqueAarNames(["@foo/plugin-x", "@foo/plugin_x"]),
/"@foo\/plugin-x" and "@foo\/plugin_x"/,
);
assert.throws(
() =>
helpers.getUniqueAarNames([
"foo-plugin-x",
"@foo/plugin-x",
"@bar/plugin-x",
]),
/"@foo\/plugin-x" and "foo-plugin-x".*"foo_plugin_x"/,
);
assert.throws(
() => helpers.getUniqueAarNames(["_9a_x", "@9a/x", "x"]),
/"@9a\/x" and "_9a_x".*"_9a_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.getUniqueAarNames(["@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",
]);
});

it("prefixes a scope-qualified name that would start with a digit", () => {
assert.deepStrictEqual(helpers.getCandidateAarNames("@9a/x"), [
"x",
"_9a_x",
]);
});

it("covers every name getUniqueAarNames 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"],
["@9a/x", "x"],
];
for (const project of projects) {
for (const [name, aarName] of helpers.getUniqueAarNames(project)) {
assert.include(helpers.getCandidateAarNames(name), aarName);
}
}
});
});
});
15 changes: 15 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,16 @@ interface IAndroidBuildOptions {
interface IAndroidPluginBuildService {
buildAar(options: IPluginBuildOptions): Promise<boolean>;
migrateIncludeGradle(options: IPluginBuildOptions): boolean;
/**
* Whether `buildAar` builds an aar from this platforms/android directory,
* i.e. it has an AndroidManifest.xml or Android source directories.
*/
hasAarSources(platformsAndroidDirPath: string): 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