From 7a8b3f6b81c4e3934b423d8d0e748f33e88a6e4a Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sat, 11 Dec 2021 00:12:31 +0300 Subject: [PATCH] Made aliases for special case dependencies like `fabric` --- src/metadata/dependency.ts | 4 ++-- src/metadata/fabric/fabric-mod-metadata.ts | 24 ++++++++++++++++++++-- test/content/fabric.mod.json | 2 +- test/mod-metadata-reader.test.ts | 22 +++++++++++++------- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/metadata/dependency.ts b/src/metadata/dependency.ts index d0ed2a5..0058f50 100644 --- a/src/metadata/dependency.ts +++ b/src/metadata/dependency.ts @@ -11,13 +11,13 @@ interface Dependency { } namespace Dependency { - export function create({ id, version = "*", kind = DependencyKind.Depends, ignore = false }: { id: string, version?: string, kind?: DependencyKind, ignore?: boolean }): Dependency { + export function create({ id, version = "*", kind = DependencyKind.Depends, ignore = false, aliases = null }: { id: string, version?: string, kind?: DependencyKind, ignore?: boolean, aliases?: Map }): Dependency { return { id, version: version ?? "*", kind: kind ?? DependencyKind.Depends, ignore: ignore ?? false, - getProjectSlug: _ => id + getProjectSlug: target => aliases?.has(target) ? aliases.get(target) : id }; } } diff --git a/src/metadata/fabric/fabric-mod-metadata.ts b/src/metadata/fabric/fabric-mod-metadata.ts index 016d52e..0a6b8d5 100644 --- a/src/metadata/fabric/fabric-mod-metadata.ts +++ b/src/metadata/fabric/fabric-mod-metadata.ts @@ -1,3 +1,4 @@ +import action from "../../../package.json"; import ModConfig from "../../metadata/mod-config"; import ModConfigDependency from "../../metadata/mod-config-dependency"; import Dependency from "../../metadata/dependency"; @@ -5,15 +6,34 @@ import DependencyKind from "../../metadata/dependency-kind"; import PublisherTarget from "../../publishing/publisher-target"; const ignoredByDefault = ["minecraft", "java", "fabricloader"]; +const aliases = new Map([ + ["fabric", "fabric-api"] +]); function getDependenciesByKind(config: any, kind: DependencyKind): Dependency[] { const kindName = DependencyKind.toString(kind).toLowerCase(); const dependencies = new Array(); for (const [id, value] of Object.entries(config[kindName] || {})) { const ignore = ignoredByDefault.includes(id); if (typeof value === "string") { - dependencies.push(Dependency.create({ id, kind, version: value, ignore })); + const dependencyAliases = aliases.has(id) ? new Map(PublisherTarget.getValues().map(x => [x, aliases.get(id)])) : null; + dependencies.push(Dependency.create({ id, kind, version: value, ignore, aliases: dependencyAliases })); } else { - dependencies.push(new ModConfigDependency({ ignore, ...value, id, kind })); + const dependencyMetadata = { ignore, ...value, id, kind }; + if (aliases.has(id)) { + if (!dependencyMetadata.custom) { + dependencyMetadata.custom = {}; + } + if (!dependencyMetadata.custom[action.name]) { + dependencyMetadata.custom[action.name] = {}; + } + for (const target of PublisherTarget.getValues()) { + const targetName = PublisherTarget.toString(target).toLowerCase(); + if (typeof dependencyMetadata.custom[action.name][targetName] !== "string") { + dependencyMetadata.custom[action.name][targetName] = aliases.get(id); + } + } + } + dependencies.push(new ModConfigDependency(dependencyMetadata)); } } return dependencies; diff --git a/test/content/fabric.mod.json b/test/content/fabric.mod.json index b6b0d1f..7659725 100644 --- a/test/content/fabric.mod.json +++ b/test/content/fabric.mod.json @@ -65,7 +65,7 @@ "suggested-mod": "*" }, "conflicts": { - "conflicting-mod": "*" + "conflicting-mod": "<0.40.0" }, "breaks": { "breaking-mod": "*" diff --git a/test/mod-metadata-reader.test.ts b/test/mod-metadata-reader.test.ts index 34cff9a..d81c65a 100644 --- a/test/mod-metadata-reader.test.ts +++ b/test/mod-metadata-reader.test.ts @@ -54,14 +54,14 @@ describe("ModMetadataReader.readMetadata", () => { test("dependency info can be read", async () => { const metadata = await ModMetadataReader.readMetadata("example-mod.fabric.jar"); - const fabric = metadata.dependencies.find(x => x.id === "fabric"); - expect(fabric).toBeTruthy(); - expect(fabric.id).toBe("fabric"); - expect(fabric.kind).toBe(DependencyKind.Depends); - expect(fabric.version).toBe(">=0.40.0"); - expect(fabric.ignore).toBe(false); + const conflicting = metadata.dependencies.find(x => x.id === "conflicting-mod"); + expect(conflicting).toBeTruthy(); + expect(conflicting.id).toBe("conflicting-mod"); + expect(conflicting.kind).toBe(DependencyKind.Conflicts); + expect(conflicting.version).toBe("<0.40.0"); + expect(conflicting.ignore).toBe(false); for (const project of PublisherTarget.getValues()) { - expect(fabric.getProjectSlug(project)).toBe(fabric.id); + expect(conflicting.getProjectSlug(project)).toBe(conflicting.id); } }); @@ -84,6 +84,14 @@ describe("ModMetadataReader.readMetadata", () => { expect(metadata.dependencies.find(x => x.id === "java").ignore).toBe(true); expect(metadata.dependencies.find(x => x.id === "fabricloader").ignore).toBe(true); }); + + test("special case dependencies (fabric) are replaced with their aliases", async() => { + const metadata = await ModMetadataReader.readMetadata("example-mod.fabric.jar"); + const fabric = metadata.dependencies.find(x => x.id === "fabric"); + for (const target of PublisherTarget.getValues()) { + expect(fabric.getProjectSlug(target) === "fabric-api"); + } + }); }); describe("Forge", () => {