mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 08:20:58 -05:00
Implemented class that represents Quilt metadata
This commit is contained in:
parent
0fec2613dd
commit
547d8d486e
2 changed files with 333 additions and 0 deletions
113
src/loaders/quilt/quilt-metadata.ts
Normal file
113
src/loaders/quilt/quilt-metadata.ts
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
import { Dependency } from "@/dependencies";
|
||||||
|
import { MINECRAFT } from "@/games/minecraft";
|
||||||
|
import { LoaderMetadata } from "@/loaders/loader-metadata";
|
||||||
|
import { PlatformType } from "@/platforms";
|
||||||
|
import { $i } from "@/utils/collections";
|
||||||
|
import { asString } from "@/utils/string-utils";
|
||||||
|
import { getQuiltDependencies, normalizeQuiltDependency } from "./quilt-dependency";
|
||||||
|
import { QuiltMetadataCustomPayload, getDependenciesFromQuiltMetadataCustomPayload, getLoadersFromQuiltMetadataCustomPayload, getProjectIdFromQuiltMetadataCustomPayload, getQuiltMetadataCustomPayload } from "./quilt-metadata-custom-payload";
|
||||||
|
import { RawQuiltMetadata } from "./raw-quilt-metadata";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents Quilt mod metadata.
|
||||||
|
*/
|
||||||
|
export class QuiltMetadata implements LoaderMetadata {
|
||||||
|
/**
|
||||||
|
* The raw Quilt metadata used to create this instance.
|
||||||
|
*/
|
||||||
|
private readonly _raw: RawQuiltMetadata;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new {@link QuiltMetadata} instance.
|
||||||
|
*
|
||||||
|
* @param raw - The raw Quilt metadata.
|
||||||
|
*/
|
||||||
|
private constructor(raw: RawQuiltMetadata) {
|
||||||
|
this._raw = raw || {} as RawQuiltMetadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link QuiltMetadata} instance from the given raw metadata.
|
||||||
|
*
|
||||||
|
* @param raw - The raw Quilt metadata.
|
||||||
|
*
|
||||||
|
* @returns A new `QuiltMetadata` instance.
|
||||||
|
*/
|
||||||
|
static from(raw: RawQuiltMetadata): QuiltMetadata {
|
||||||
|
return new QuiltMetadata(raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get id(): string {
|
||||||
|
const id = asString(this._raw.quilt_loader?.id || "");
|
||||||
|
return id.includes(":") ? id.substring(id.indexOf(":") + 1) : id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get name(): string {
|
||||||
|
return asString(this._raw.quilt_loader?.metadata?.name || this.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get version(): string {
|
||||||
|
return asString(this._raw.quilt_loader?.version || "*");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get loaders(): string[] {
|
||||||
|
return getLoadersFromQuiltMetadataCustomPayload(this.customPayload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get gameName(): string {
|
||||||
|
return MINECRAFT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get gameVersions(): string[] {
|
||||||
|
return [...(this.dependencies.find(x => x.id === this.gameName)?.versions || [])];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
get dependencies(): Dependency[] {
|
||||||
|
const baseDependencies = getQuiltDependencies(this._raw).map(normalizeQuiltDependency).filter(x => x);
|
||||||
|
const payloadDependencies = getDependenciesFromQuiltMetadataCustomPayload(this.customPayload);
|
||||||
|
const dependencyMap = $i(baseDependencies).concat(payloadDependencies).filter(x => x).map(x => [x.id, x] as const).toMap();
|
||||||
|
return [...dependencyMap.values()];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The raw Quilt metadata representing this instance.
|
||||||
|
*/
|
||||||
|
get raw(): RawQuiltMetadata {
|
||||||
|
return this._raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The custom payload attached to the Quilt metadata.
|
||||||
|
*/
|
||||||
|
get customPayload(): QuiltMetadataCustomPayload {
|
||||||
|
return getQuiltMetadataCustomPayload(this._raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
getProjectId(platform: PlatformType): string {
|
||||||
|
return getProjectIdFromQuiltMetadataCustomPayload(this.customPayload, platform) || this.id;
|
||||||
|
}
|
||||||
|
}
|
220
tests/unit/loaders/quilt/quilt-metadata.spec.ts
Normal file
220
tests/unit/loaders/quilt/quilt-metadata.spec.ts
Normal file
|
@ -0,0 +1,220 @@
|
||||||
|
import { readFileSync } from "node:fs";
|
||||||
|
import { resolve as resolvePath } from "node:path";
|
||||||
|
import { DependencyType } from "@/dependencies/dependency-type";
|
||||||
|
import { PlatformType } from "@/platforms/platform-type";
|
||||||
|
import { RawQuiltMetadata } from "@/loaders/quilt/raw-quilt-metadata";
|
||||||
|
import { QuiltMetadata } from "@/loaders/quilt/quilt-metadata";
|
||||||
|
|
||||||
|
const RAW_METADATA: RawQuiltMetadata = Object.freeze(JSON.parse(
|
||||||
|
readFileSync(resolvePath(__dirname, "../../../content/quilt/quilt.mod.json"), "utf8")
|
||||||
|
));
|
||||||
|
|
||||||
|
describe("QuiltMetadata", () => {
|
||||||
|
describe("from", () => {
|
||||||
|
test("constructs new QuiltMetadata instance using given raw metadata", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata).toBeInstanceOf(QuiltMetadata);
|
||||||
|
expect(metadata.raw).toBe(RAW_METADATA);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("id", () => {
|
||||||
|
test("returns id of the mod", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.id).toBe("example-mod");
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("name", () => {
|
||||||
|
test("returns name of the mod", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.name).toBe("Example Mod");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("version", () => {
|
||||||
|
test("returns version of the mod", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.version).toBe("0.1.0");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("loaders", () => {
|
||||||
|
test("returns 'quilt' by default", () => {
|
||||||
|
const metadata = QuiltMetadata.from({} as RawQuiltMetadata);
|
||||||
|
|
||||||
|
expect(metadata.loaders).toEqual(["quilt"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns the same value as the 'loaders' field in the custom payload", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.loaders).toEqual(["quilt", "fabric"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("gameName", () => {
|
||||||
|
test("always returns 'minecraft'", () => {
|
||||||
|
expect(QuiltMetadata.from({} as RawQuiltMetadata).gameName).toBe("minecraft");
|
||||||
|
expect(QuiltMetadata.from(RAW_METADATA).gameName).toBe("minecraft");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("gameVersions", () => {
|
||||||
|
test("returns an empty array if no dependencies were specified", () => {
|
||||||
|
const metadata = QuiltMetadata.from({} as RawQuiltMetadata);
|
||||||
|
|
||||||
|
expect(metadata.gameVersions).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns the same value as the 'minecraft' dependency", () => {
|
||||||
|
const metadata = QuiltMetadata.from({ quilt_loader: { depends: [{ id: "minecraft", versions: "1.16.5" }] } } as RawQuiltMetadata);
|
||||||
|
|
||||||
|
expect(metadata.gameVersions).toEqual(["1.16.5"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns the same values as the 'minecraft' dependency", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.gameVersions).toEqual(["1.17", "1.17.1"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("dependencies", () => {
|
||||||
|
test("returns an empty array if no dependencies were specified", () => {
|
||||||
|
const metadata = QuiltMetadata.from({} as RawQuiltMetadata);
|
||||||
|
|
||||||
|
expect(metadata.dependencies).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns dependencies if they were specified", () => {
|
||||||
|
const metadata = QuiltMetadata.from({ quilt_loader: { breaks: RAW_METADATA.quilt_loader.breaks } } as RawQuiltMetadata);
|
||||||
|
|
||||||
|
const dependencies = metadata.dependencies;
|
||||||
|
|
||||||
|
expect(dependencies).toHaveLength(2);
|
||||||
|
expect(dependencies[0]).toMatchObject({ id: "breaking-mod", versions: ["*"], type: DependencyType.INCOMPATIBLE });
|
||||||
|
expect(dependencies[1]).toMatchObject({ id: "conflicting-mod", versions: ["<0.40.0"], type: DependencyType.CONFLICTING });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("special well-known dependency 'quilted_fabric_api' has custom 'qsl' alias", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
const dependencies = metadata.dependencies;
|
||||||
|
|
||||||
|
const quilt = dependencies.find(x => x.id === "quilted_fabric_api");
|
||||||
|
for (const platform of PlatformType.values()) {
|
||||||
|
expect(quilt?.getProjectId(platform)).toBe("qsl");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("regular dependencies have no aliases", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
const dependencies = metadata.dependencies;
|
||||||
|
const regularDependencies = ["included-mod", "conflicting-mod", "breaking-mod"].map(id => dependencies.find(x => x.id === id));
|
||||||
|
|
||||||
|
for (const dependency of regularDependencies) {
|
||||||
|
for (const platform of PlatformType.values()) {
|
||||||
|
expect(dependency?.getProjectId(platform)).toBe(dependency.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("special dependencies ('quilt_loader', 'minecraft', 'java') are ignored by default", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
const dependencies = metadata.dependencies;
|
||||||
|
|
||||||
|
expect(dependencies.find(x => x.id === "quilt_loader")?.isIgnored()).toBe(true);
|
||||||
|
expect(dependencies.find(x => x.id === "minecraft")?.isIgnored()).toBe(true);
|
||||||
|
expect(dependencies.find(x => x.id === "java")?.isIgnored()).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("regular dependencies are not ignored by default", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
const dependencies = metadata.dependencies;
|
||||||
|
|
||||||
|
expect(dependencies.find(x => x.id === "quilted_fabric_api")?.isIgnored()).toBe(false);
|
||||||
|
expect(dependencies.find(x => x.id === "included-mod")?.isIgnored()).toBe(false);
|
||||||
|
expect(dependencies.find(x => x.id === "suggested-mod")?.isIgnored()).toBe(false);
|
||||||
|
expect(dependencies.find(x => x.id === "conflicting-mod")?.isIgnored()).toBe(false);
|
||||||
|
expect(dependencies.find(x => x.id === "breaking-mod")?.isIgnored()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns dependencies merged with the 'dependencies' declaration from the custom payload", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
const dependencies = metadata.dependencies;
|
||||||
|
|
||||||
|
expect(dependencies).toHaveLength(9);
|
||||||
|
expect(dependencies.find(x => x.id === "quilt_loader")).toMatchObject({ versions: [">=0.11.3"], type: DependencyType.REQUIRED });
|
||||||
|
expect(dependencies.find(x => x.id === "quilted_fabric_api")).toMatchObject({ versions: [">=0.40.0"], type: DependencyType.REQUIRED });
|
||||||
|
expect(dependencies.find(x => x.id === "minecraft")).toMatchObject({ versions: ["1.17", "1.17.1"], type: DependencyType.REQUIRED });
|
||||||
|
expect(dependencies.find(x => x.id === "java")).toMatchObject({ versions: [">=16"], type: DependencyType.REQUIRED });
|
||||||
|
expect(dependencies.find(x => x.id === "recommended-mod")).toMatchObject({ versions: ["0.2.0"], type: DependencyType.RECOMMENDED });
|
||||||
|
expect(dependencies.find(x => x.id === "included-mod")).toMatchObject({ versions: ["*"], type: DependencyType.EMBEDDED });
|
||||||
|
expect(dependencies.find(x => x.id === "suggested-mod")).toMatchObject({ versions: ["*"], type: DependencyType.OPTIONAL });
|
||||||
|
expect(dependencies.find(x => x.id === "conflicting-mod")).toMatchObject({ versions: ["<0.40.0"], type: DependencyType.CONFLICTING });
|
||||||
|
expect(dependencies.find(x => x.id === "breaking-mod")).toMatchObject({ versions: ["*"], type: DependencyType.INCOMPATIBLE });
|
||||||
|
|
||||||
|
const merged = dependencies.find(x => x.id === "recommended-mod");
|
||||||
|
expect(merged.getProjectId(PlatformType.MODRINTH)).toBe("AAAA");
|
||||||
|
expect(merged.getProjectId(PlatformType.CURSEFORGE)).toBe("42");
|
||||||
|
expect(merged.getProjectId(PlatformType.GITHUB)).toBe("v0.2.0");
|
||||||
|
expect(merged.isIgnored()).toBe(true);
|
||||||
|
|
||||||
|
const withMetadata = dependencies.find(x => x.id === "suggested-mod");
|
||||||
|
expect(withMetadata.getProjectId(PlatformType.MODRINTH)).toBe("BBBB");
|
||||||
|
expect(withMetadata.getProjectId(PlatformType.CURSEFORGE)).toBe("43");
|
||||||
|
expect(withMetadata.getProjectId(PlatformType.GITHUB)).toBe("v0.3.0");
|
||||||
|
expect(withMetadata.isIgnored()).toBe(false);
|
||||||
|
expect(withMetadata.isIgnored(PlatformType.CURSEFORGE)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("raw", () => {
|
||||||
|
test("returns the raw metadata oject this instance was created from", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.raw).toBe(RAW_METADATA);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("customPayload", () => {
|
||||||
|
test("returns an empty object by default", () => {
|
||||||
|
const metadata = QuiltMetadata.from({} as RawQuiltMetadata);
|
||||||
|
|
||||||
|
expect(metadata.customPayload).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("return the custom payload if it was specified", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.customPayload?.loaders).toEqual(["quilt", "fabric"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getProjectId", () => {
|
||||||
|
test("returns the mod id by default", () => {
|
||||||
|
const metadata = QuiltMetadata.from({ quilt_loader: { id: "example-mod" } } as RawQuiltMetadata);
|
||||||
|
|
||||||
|
for (const platform of PlatformType.values()) {
|
||||||
|
expect(metadata.getProjectId(platform)).toBe("example-mod");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns the same value as one specified in the custom payload", () => {
|
||||||
|
const metadata = QuiltMetadata.from(RAW_METADATA);
|
||||||
|
|
||||||
|
expect(metadata.getProjectId(PlatformType.MODRINTH)).toBe("AANobbMI");
|
||||||
|
expect(metadata.getProjectId(PlatformType.CURSEFORGE)).toBe("394468");
|
||||||
|
expect(metadata.getProjectId(PlatformType.GITHUB)).toBe("mc1.18-0.4.0-alpha5");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue