Skip to content

Commit

Permalink
attempt to fix version sorting (#205)
Browse files Browse the repository at this point in the history
* rearrange version catalogue order
* remove external import utility links
* output errors in worker script
  • Loading branch information
UpcraftLP authored Apr 24, 2024
1 parent 60c4320 commit 0d1352b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
72 changes: 47 additions & 25 deletions functions/api/v1/latest-version-components.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
import { xml2js } from "xml-js";
import * as semverSort from "semver-sort";
import semverSort from "semver-sort";

const META_GAME_VERSIONS = "https://meta.quiltmc.org/v3/versions/game"
const MAVEN = "https://maven.quiltmc.org/repository/release/org/quiltmc/"

async function fetchMetadata(path) {
const metadataUrl = MAVEN + path + "/maven-metadata.xml"
const metadataRequest = await fetch(metadataUrl, { headers: { "User-Agent": "QuiltMC Website API" } })
const metadataUrl = MAVEN + path + "/maven-metadata.xml";
const metadataRequest = await fetch(metadataUrl, { headers: { "User-Agent": "QuiltMC Website API" } });

return xml2js(await metadataRequest.text(), { compact: true, ignoreAttributes: true }).metadata
if (metadataRequest.ok) {
return await metadataRequest.text();
}

throw new Error(`Failed to fetch metadata from ${metadataUrl}`);
}

function getLatestVersion(versions) {
// handle null, undefined, empty array
if (!versions) return null;

return semverSort.asc(versions).toReversed()[0];
}

function getLatestVersionFromMavenMetadataXML(metaXML, latestVersionSelector) {
const json = xml2js(metaXML, { compact: true, ignoreAttributes: true });

const allVersions = json.metadata.versioning.versions.version.map((version) => version._text);

return latestVersionSelector(allVersions);
}

export async function onRequest(context) {
const gameVersionsRequest = await fetch(META_GAME_VERSIONS, { headers: { "User-Agent": "QuiltMC Website API" } })
const gameVersions = await gameVersionsRequest.json()
try {
const gameVersionsRequest = await fetch(META_GAME_VERSIONS, { headers: { "User-Agent": "QuiltMC Website API" } })
const gameVersions = await gameVersionsRequest.json()

const output = {}
const output = {}

const loomVersion = (await fetchMetadata("loom")).versioning.latest._text
const loaderVersion = (await fetchMetadata("quilt-loader")).versioning.latest._text
const loomVersion = await fetchMetadata("loom").then(meta => getLatestVersionFromMavenMetadataXML(meta, getLatestVersion));
const loaderVersion = await fetchMetadata("quilt-loader").then(meta => getLatestVersionFromMavenMetadataXML(meta, getLatestVersion));

const mappingsVersions = (await fetchMetadata("quilt-mappings"))
.versioning.versions.version.map((versioning) => versioning._text)
.sort((a, b) => b.split(".").slice(-1) - a.split(".").slice(-1))
const mappingsVersions = await fetchMetadata("quilt-mappings").then(meta => getLatestVersionFromMavenMetadataXML(meta, versions => {
return versions.sort((a, b) => b.split(".").slice(-1) - a.split(".").slice(-1));
}));

const qfapiVersions = semverSort.asc((await fetchMetadata("quilted-fabric-api/quilted-fabric-api"))
.versioning.versions.version.map((versioning) => versioning._text))
.toReversed() // semverSort.desc chokes on those inputs
const qfapiVersions = await fetchMetadata("quilted-fabric-api/quilted-fabric-api").then(meta => getLatestVersionFromMavenMetadataXML(meta, versions => semverSort.asc(versions).toReversed()));

for (const { version } of gameVersions) {
const mappingsVersion = mappingsVersions.find((versioning) => versioning.split("+")[0] === version) || null
const qfapiVersion = qfapiVersions.find((versioning) => versioning.split("-").at(-1) === version) || null
for (const { version } of gameVersions) {
const mappingsVersion = mappingsVersions.find((versioning) => versioning.split("+")[0] === version) || null
const qfapiVersion = qfapiVersions.find((versioning) => versioning.split("-").at(-1) === version) || null

output[version] = {
"quilt_loader": loaderVersion,
"loom": loomVersion,
"quilt_mappings": mappingsVersion,
"quilted_fabric_api": qfapiVersion
output[version] = {
"quilt_loader": loaderVersion,
"loom": loomVersion,
"quilt_mappings": mappingsVersion,
"quilted_fabric_api": qfapiVersion
}
}
}

return new Response(JSON.stringify(output), { status: 200, headers: { "Content-Type": "text/json" } })
return new Response(JSON.stringify(output), { status: 200, headers: { "Content-Type": "text/json" } });
}
catch (error) {
return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { "Content-Type": "text/json" } });
}
}
5 changes: 1 addition & 4 deletions src/components/setpieces/NavBar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,8 @@ const { style = "is-dark" } = Astro.props as Props;
{t("nav-page-template-mod")}
</NavLink>

<div class="divider">{t("divider-external-links")}</div>
<!-- <div class="divider">{t("divider-external-links")}</div> -->

<NavLink href="https://lambdaurora.dev/tools/import_quilt.html">
{t("nav-page-import-utility")}
</NavLink>
</Dropdown>
</div>

Expand Down
12 changes: 6 additions & 6 deletions src/layouts/LatestVersions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ const props = Astro.props;
const quiltMappings = version.quilt_mappings ? `quilt_mappings = "${version.quilt_mappings}"` : "# Quilt Mappings is currently unavailable for this version";
const quiltedFabricApi = version.quilted_fabric_api ? `quilted_fabric_api = "${version.quilted_fabric_api}"` : "# Quilted Fabric API is currently unavailable for this version";

const content = `[versions]
# Latest versions are available at https://quiltmc.org/en/usage/latest-versions
const content = `# The latest versions are available at https://quiltmc.org/en/usage/latest-versions
[versions]
minecraft = "${e.target.value}"
loom = "${version.loom}"

${quiltMappings}

quilt_loom = "${version.loom}"
quilt_loader = "${version.quilt_loader}"

${quiltedFabricApi}
Expand All @@ -116,7 +116,7 @@ ${quiltedFabricApi}
for (const option of document.querySelectorAll("#version-select option")) {
option.classList.toggle("is-hidden", !option.value.startsWith(gib("version-search").value));
}

if (!gib("version-select").value.startsWith(gib("version-search").value)) {
gib("version-select").value = document.querySelector("#version-select option:not(.is-hidden)").value;
gib("version-select").dispatchEvent(new Event("change"));
Expand All @@ -135,4 +135,4 @@ ${quiltedFabricApi}
});
});
}
</script>
</script>
2 changes: 0 additions & 2 deletions src/locales/en/common.flt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ serial-comma = {$prev}, {$next}
# Common strings
-developer-wiki = Developer Wiki
-forum = Forum
-import-utility = Quilt Import Utility
-help-wanted = Help Wanted
-patch-notes = Patch Notes
-sponsors = Sponsors
Expand Down Expand Up @@ -45,7 +44,6 @@ divider-development = Development
divider-external-links = External Links

nav-page-developer-wiki = {-developer-wiki}
nav-page-import-utility = {-import-utility}
nav-page-maven-repo = Maven Repository
nav-page-help-wanted = {-help-wanted}
nav-page-patch-notes = {-patch-notes}
Expand Down
2 changes: 0 additions & 2 deletions src/locales/zh/common.flt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ serial-comma = {$prev}、{$next}
# Common strings
-developer-wiki = 开发者 Wiki
-forum = 论坛
-import-utility = Quilt 导入工具
-patch-notes = 更新日志
-staff-resources = 内部资源
-template-mod = 模组模板
Expand All @@ -35,7 +34,6 @@ divider-development = 开发资源
divider-external-links = 外部链接

nav-page-developer-wiki = {-developer-wiki}
nav-page-import-utility = {-import-utility}
nav-page-maven-repo = Maven 库
nav-page-help-wanted = {-help-wanted}
nav-page-patch-notes = {-patch-notes}
Expand Down
1 change: 0 additions & 1 deletion src/pages/en/usage/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ so if you have a problem or question that you feel should be covered here, pleas

## Developer Links

- Aurora's [Quilt Import Utility](https://lambdaurora.dev/tools/import_quilt.html) (Versioning info for your Gradle projects)
- [Quilt Developer Wiki](https://wiki.quiltmc.org)
- [Quilt Maven Repository](https://maven.quiltmc.org)
- [Quilt Template Mod](https://github.com/QuiltMC/quilt-template-mod)

1 comment on commit 0d1352b

@Cozy-GitHub
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See preview on Cloudflare Pages: https://24fb3b06.quiltmc-org.pages.dev

Please sign in to comment.