Skip to content

Commit

Permalink
Corrections for 1.20 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
dscalzi committed Jun 25, 2023
1 parent 3ec535d commit 9f6220b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/resolver/forge/adapter/ForgeGradle3.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export class ForgeGradle3Adapter extends ForgeResolver {

const targetLocalPath = join(
libDir,
MavenUtil.mavenComponentsToPath(entry.group, entry.artifact, entry.version, _classifier)
MavenUtil.mavenComponentsAsNormalizedPath(entry.group, entry.artifact, entry.version, _classifier)
)

targetLocations.push(targetLocalPath)
Expand Down
14 changes: 7 additions & 7 deletions src/structure/repo/BaseMavenRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@ export abstract class BaseMavenRepo extends BaseFileStructure {
}

public getArtifactById(mavenIdentifier: string, extension?: string): string {
return resolve(this.containerDirectory, MavenUtil.mavenIdentifierToString(mavenIdentifier, extension))
return resolve(this.containerDirectory, MavenUtil.mavenIdentifierAsPath(mavenIdentifier, extension))
}

public getArtifactByComponents(
group: string, artifact: string, version: string, classifier?: string, extension = 'jar'
): string {
return resolve(this.containerDirectory,
MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension))
MavenUtil.mavenComponentsAsPath(group, artifact, version, classifier, extension))
}

public getArtifactUrlByComponents(
baseURL: string, group: string, artifact: string, version: string, classifier?: string, extension = 'jar'
): string {
return new URL(join(this.relativeRoot,
MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension)), baseURL).toString()
MavenUtil.mavenComponentsAsPath(group, artifact, version, classifier, extension)), baseURL).toString()
}

public async artifactExists(path: string): Promise<boolean> {
return pathExists(path)
}

public async downloadArtifactById(url: string, mavenIdentifier: string, extension?: string): Promise<void> {
return this.downloadArtifactBase(url, MavenUtil.mavenIdentifierToString(mavenIdentifier, extension) as string)
return this.downloadArtifactBase(url, MavenUtil.mavenIdentifierAsPath(mavenIdentifier, extension) as string)
}

public async downloadArtifactByComponents(
url: string, group: string, artifact: string, version: string, classifier?: string, extension?: string
): Promise<void> {
return this.downloadArtifactBase(url,
MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension))
MavenUtil.mavenComponentsAsPath(group, artifact, version, classifier, extension))
}

private async downloadArtifactBase(url: string, relative: string): Promise<void> {
Expand All @@ -75,14 +75,14 @@ export abstract class BaseMavenRepo extends BaseFileStructure {
}

public async headArtifactById(url: string, mavenIdentifier: string, extension?: string): Promise<boolean> {
return this.headArtifactBase(url, MavenUtil.mavenIdentifierToString(mavenIdentifier, extension) as string)
return this.headArtifactBase(url, MavenUtil.mavenIdentifierAsPath(mavenIdentifier, extension) as string)
}

public async headArtifactByComponents(
url: string, group: string, artifact: string, version: string, classifier?: string, extension?: string
): Promise<boolean> {
return this.headArtifactBase(url,
MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension))
MavenUtil.mavenComponentsAsPath(group, artifact, version, classifier, extension))
}

private async headArtifactBase(url: string, relative: string): Promise<boolean> {
Expand Down
67 changes: 39 additions & 28 deletions src/util/MavenUtil.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { normalize } from 'path'
import { URL } from 'url'

export interface MavenComponents {
group: string
artifact: string
version: string
classifier?: string
extension: string
}

export class MavenUtil {

public static readonly ID_REGEX = /(.+):(.+):([^@]+)()(?:@{1}(.+)$)?/
public static readonly ID_REGEX_WITH_CLASSIFIER = /(.+):(.+):(?:([^@]+)(?:-([a-zA-Z]+)))(?:@{1}(.+)$)?/
public static readonly ID_REGEX = /([^@:]+):([^@:]+):?([^@:]+)?:?(?:([^@:]+))?:?(?:@{1}([^@:]+))?/

public static mavenComponentsToIdentifier(
group: string,
Expand All @@ -16,74 +23,78 @@ export class MavenUtil {
return `${group}:${artifact}:${version}${classifier != null ? `:${classifier}` : ''}${extension != null ? `@${extension}` : ''}`
}

public static isMavenIdentifier(id: string): boolean {
return MavenUtil.ID_REGEX.test(id) || MavenUtil.ID_REGEX_WITH_CLASSIFIER.test(id)
public static mavenComponentsToExtensionlessIdentifier(
group: string,
artifact: string,
version: string,
classifier?: string
): string {
return MavenUtil.mavenComponentsToIdentifier(group, artifact, version, classifier)
}

public static getMavenComponents(id: string, extension = 'jar'): {
group: string
public static mavenComponentsToVersionlessIdentifier(
group: string,
artifact: string
version: string
classifier?: string
extension: string
} {
): string {
return `${group}:${artifact}`
}

public static isMavenIdentifier(id: string): boolean {
return MavenUtil.ID_REGEX.test(id)
}

public static getMavenComponents(id: string, extension = 'jar'): MavenComponents {
if (!MavenUtil.isMavenIdentifier(id)) {
throw new Error('Id is not a maven identifier.')
}

let result

if (MavenUtil.ID_REGEX_WITH_CLASSIFIER.test(id)) {
result = MavenUtil.ID_REGEX_WITH_CLASSIFIER.exec(id)
} else {
result = MavenUtil.ID_REGEX.exec(id)
}
const result = MavenUtil.ID_REGEX.exec(id)

if (result != null) {
return {
group: result[1],
artifact: result[2],
version: result[3],
classifier: result[4] || undefined,
classifier: result[4],
extension: result[5] || extension
}
}

throw new Error('Failed to process maven data.')
}

public static mavenIdentifierToString(id: string, extension = 'jar'): string {
public static mavenIdentifierAsPath(id: string, extension = 'jar'): string {
const tmp = MavenUtil.getMavenComponents(id, extension)

return MavenUtil.mavenComponentsToString(
return MavenUtil.mavenComponentsAsPath(
tmp.group, tmp.artifact, tmp.version, tmp.classifier, tmp.extension
)
}

public static mavenComponentsToString(
public static mavenComponentsAsPath(
group: string, artifact: string, version: string, classifier?: string, extension = 'jar'
): string {
return `${group.replace(/\./g, '/').replace(/:/g, '/')}/${artifact}/${version}/${artifact}-${version}${classifier != null ? `-${classifier}` : ''}.${extension}`
return `${group.replace(/\./g, '/')}/${artifact}/${version}/${artifact}-${version}${classifier != null ? `-${classifier}` : ''}.${extension}`
}

public static mavenIdentifierToUrl(id: string, extension = 'jar'): URL {
return new URL(MavenUtil.mavenIdentifierToString(id, extension))
return new URL(MavenUtil.mavenIdentifierAsPath(id, extension))
}

public static mavenComponentsToUrl(
group: string, artifact: string, version: string, classifier?: string, extension = 'jar'
): URL {
return new URL(MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension))
return new URL(MavenUtil.mavenComponentsAsPath(group, artifact, version, classifier, extension))
}

public static mavenIdentifierToPath(id: string, extension = 'jar'): string {
return normalize(MavenUtil.mavenIdentifierToString(id, extension))
return normalize(MavenUtil.mavenIdentifierAsPath(id, extension))
}

public static mavenComponentsToPath(
public static mavenComponentsAsNormalizedPath(
group: string, artifact: string, version: string, classifier?: string, extension = 'jar'
): string {
return normalize(MavenUtil.mavenComponentsToString(group, artifact, version, classifier, extension))
return normalize(MavenUtil.mavenComponentsAsPath(group, artifact, version, classifier, extension))
}

}
}
8 changes: 4 additions & 4 deletions src/util/MinecraftVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ export class MinecraftVersion {

private readonly major: number
private readonly minor: number
private readonly revision: number
private readonly revision: number | undefined

constructor(version: string) {
const res = MinecraftVersion.MINECRAFT_VERSION_REGEX.exec(version)
if(res != null) {
this.major = Number(res[1])
this.minor = Number(res[2])
this.revision = Number(res[3]) ?? undefined
this.revision = res[3] != null ? Number(res[3]) : undefined
} else {
throw new Error(`${version} is not a valid minecraft version!`)
}
Expand All @@ -23,8 +23,8 @@ export class MinecraftVersion {

public getMajor(): number { return this.major }
public getMinor(): number { return this.minor }
public getRevision(): number|undefined { return this.revision }
public getRevision(): number | undefined { return this.revision }

public toString(): string { return `${this.major}.${this.minor}${this.revision? '.'+this.revision:''}`}
public toString(): string { return `${this.major}.${this.minor}${this.revision != null ? '.' + this.revision : ''}`}

}
2 changes: 1 addition & 1 deletion src/util/VersionUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class VersionUtil {
'latest'
]

public static readonly MINECRAFT_VERSION_REGEX = /(\d+).(\d+).?(\d+)?/
public static readonly MINECRAFT_VERSION_REGEX = /(\d+).(\d+).(\d+)?/

public static isVersionAcceptable(version: MinecraftVersion, acceptable: number[]): boolean {
if (version.getMajor() === 1) {
Expand Down

0 comments on commit 9f6220b

Please sign in to comment.