Skip to content

Commit 890f70a

Browse files
committed
feat: add resolveFromPath
1 parent 9ee553d commit 890f70a

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

packages/core/version.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,53 @@ const root = normalize(join(__dirname, "..", "..", "mock"));
66
const tempRoot = normalize(join(__dirname, "..", "..", "temp"));
77

88
describe("Version", () => {
9+
describe("#resolveFromPath", () => {
10+
test("should be able to infer from path", () => {
11+
let path = "org/lwjgl/lwjgl-stb/3.2.1/lwjgl-stb-3.2.1.jar";
12+
let info = LibraryInfo.resolveFromPath(path)
13+
expect(info.path).toEqual(path);
14+
expect(info.groupId).toEqual("org.lwjgl");
15+
expect(info.artifactId).toEqual("lwjgl-stb");
16+
expect(info.version).toEqual("3.2.1");
17+
expect(info.name).toEqual("org.lwjgl:lwjgl-stb:3.2.1");
18+
expect(info.classifier).toEqual("");
19+
expect(info.type).toEqual("jar");
20+
});
21+
test("should be able to infer from path with classifier", () => {
22+
let path = "org/lwjgl/lwjgl-stb/3.2.1/lwjgl-stb-3.2.1-javadoc.jar";
23+
let info = LibraryInfo.resolveFromPath(path)
24+
expect(info.path).toEqual(path);
25+
expect(info.groupId).toEqual("org.lwjgl");
26+
expect(info.artifactId).toEqual("lwjgl-stb");
27+
expect(info.version).toEqual("3.2.1");
28+
expect(info.name).toEqual("org.lwjgl:lwjgl-stb:3.2.1:javadoc");
29+
expect(info.classifier).toEqual("javadoc");
30+
expect(info.type).toEqual("jar");
31+
});
32+
test("should be able to infer from path with other file type", () => {
33+
let path = "org/lwjgl/lwjgl-stb/3.2.1/lwjgl-stb-3.2.1-javadoc.zip";
34+
let info = LibraryInfo.resolveFromPath(path)
35+
expect(info.path).toEqual(path);
36+
expect(info.groupId).toEqual("org.lwjgl");
37+
expect(info.artifactId).toEqual("lwjgl-stb");
38+
expect(info.version).toEqual("3.2.1");
39+
expect(info.name).toEqual("org.lwjgl:lwjgl-stb:3.2.1:javadoc@zip");
40+
expect(info.classifier).toEqual("javadoc");
41+
expect(info.type).toEqual("zip");
42+
});
43+
test("should be able to infer from path with other file type and snapshot", () => {
44+
let path = "org/lwjgl/lwjgl-stb/3.2.1/3.2.1-javadoc.zip";
45+
let info = LibraryInfo.resolveFromPath(path)
46+
expect(info.path).toEqual(path);
47+
expect(info.groupId).toEqual("org.lwjgl");
48+
expect(info.artifactId).toEqual("lwjgl-stb");
49+
expect(info.version).toEqual("3.2.1");
50+
expect(info.name).toEqual("org.lwjgl:lwjgl-stb:3.2.1:javadoc@zip");
51+
expect(info.classifier).toEqual("javadoc");
52+
expect(info.type).toEqual("zip");
53+
expect(info.isSnapshot).toEqual(true);
54+
});
55+
});
956
describe("#getLibraryInfo", () => {
1057
test("should be able to parse normal minecraft library", () => {
1158
const name = "com.mojang:patchy:1.1";

packages/core/version.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MinecraftFolder, MinecraftLocation } from "./folder"
22
import { Platform, currentPlatform } from "./platform";
33
import { readFile, exists } from "./fs";
4+
import { extname } from "path";
45

56
export interface PartialResolvedVersion extends Version {
67
libraries: ResolvedLibrary[];
@@ -88,6 +89,44 @@ export interface LibraryInfo {
8889
}
8990

9091
export namespace LibraryInfo {
92+
export function resolveFromPath(path: string): LibraryInfo {
93+
let parts = path.split("/");
94+
let file = parts[parts.length - 1];
95+
let version = parts[parts.length - 2];
96+
let artifactId = parts[parts.length - 3];
97+
let groupId = parts.slice(0, parts.length - 3).join(".");
98+
99+
let filePrefix = `${artifactId}-${version}`;
100+
let ext = extname(file);
101+
let type = ext.substring(1);
102+
103+
let isSnapshot = file.startsWith(version);
104+
105+
let classifier = file.substring(isSnapshot ? version.length : filePrefix.length, file.length - ext.length);
106+
107+
if (classifier.startsWith("-")) {
108+
classifier = classifier.slice(1);
109+
}
110+
111+
let name = `${groupId}:${artifactId}:${version}`;
112+
if (classifier) {
113+
name += `:${classifier}`;
114+
}
115+
if (type !== "jar") {
116+
name += `@${type}`;
117+
}
118+
119+
return {
120+
type,
121+
groupId,
122+
artifactId,
123+
version,
124+
classifier,
125+
name,
126+
path,
127+
isSnapshot,
128+
}
129+
}
91130
/**
92131
* Get the base info of the library from its name
93132
*
@@ -192,11 +231,11 @@ export namespace Version {
192231
artifact: Artifact;
193232
classifiers: {
194233
[os: string]: Artifact;
195-
},
234+
};
196235
};
197236
rules: Rule[];
198237
extract: {
199-
exclude: string[],
238+
exclude: string[];
200239
};
201240
natives: {
202241
[os: string]: string;
@@ -504,14 +543,16 @@ export namespace Version {
504543
if ("rules" in lib && !checkAllowed(lib.rules, platform)) {
505544
return undefined;
506545
}
546+
// official natives foramt
507547
if ("natives" in lib) {
508548
if (!lib.natives[platform.name]) { return undefined; }
509549
const classifier = (lib.natives[platform.name]).replace("${arch}", platform.arch.substring(1));
510550
const nativeArtifact = lib.downloads.classifiers[classifier];
511551
if (!nativeArtifact) { return undefined; }
512-
return new ResolvedNative(lib.name + ":" + classifier, LibraryInfo.resolve(lib.name + ":" + classifier), lib.downloads.classifiers[classifier], lib.extract ? lib.extract.exclude ? lib.extract.exclude : undefined : undefined);
552+
return new ResolvedNative(lib.name + ":" + classifier, LibraryInfo.resolve(lib.name + ":" + classifier), nativeArtifact, lib.extract ? lib.extract.exclude ? lib.extract.exclude : undefined : undefined);
513553
}
514554
const info = LibraryInfo.resolve(lib.name);
555+
// normal library
515556
if ("downloads" in lib) {
516557
if (!lib.downloads.artifact.url) {
517558
lib.downloads.artifact.url = info.groupId === "net.minecraftforge"

0 commit comments

Comments
 (0)