Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Strokkur424 committed Oct 25, 2024
1 parent 46f7cf6 commit f5c71bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ of a few ways:
%%_MAJ_MIN_PAT_MC_%% - Major-Minor-Patch Paper Version (E.g. 1.20.4)
%%_MAJ_MIN_VEL_%% - Major Velocity Version (E.g. 3.1.0)
%%_MAJ_MIN_PAT_VEL_%% - Major-Minor-Patch Velocity Version (E.g. 3.1.1-SNAPSHOT)
%%_USERDEV_VER_%% - Latest Paperweight-Userdev Version (E.g. 1.7.3)
%%_MAJ_MIN_PAT_USERDEV_%% - Latest Paperweight-Userdev Version (E.g. 1.7.3)
````

When the major version of the software changes, the docs will still need to have a "snapshot" created to keep documentation
Expand Down
2 changes: 1 addition & 1 deletion docs/paper/dev/getting-started/userdev.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Portal](https://plugins.gradle.org/plugin/io.papermc.paperweight.userdev).
<VersionFormattedCode language={"kotlin"} title={"build.gradle.kts"}>
```
plugins {
id("io.papermc.paperweight.userdev") version "%%_USERDEV_VER_%%"
id("io.papermc.paperweight.userdev") version "%%_MAJ_MIN_PAT_USERDEV_%%"
}
```
</VersionFormattedCode>
Expand Down
7 changes: 5 additions & 2 deletions src/components/versioning/VersionFormattedCode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useRef, useState } from "react";
import CodeBlock from "@docusaurus/theme-classic/lib/theme/CodeBlock";
import { getProjectVersion, VersionType, getUserdevVersion } from "../../util/versionUtils";
import { getProjectVersion, VersionType } from "../../util/versionUtils";
import { useDocsVersion } from "@docusaurus/plugin-content-docs/client";

export default function VersionFormattedCode({
Expand Down Expand Up @@ -47,7 +47,10 @@ export default function VersionFormattedCode({
await getProjectVersion("velocity", versionMeta)
);

code = code.replace(/%%_USERDEV_VER_%%/g, await getUserdevVersion().value());
code = code.replace(
/%%_MAJ_MIN_PAT_USERDEV_%%/g,
await getProjectVersion("userdev", versionMeta)
);

if (mounted.current) {
setFormattedCode({ code, inline });
Expand Down
34 changes: 17 additions & 17 deletions src/util/versionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,32 @@ class ExpiringValue<T> {
}
}

const requestHeaders = { "User-Agent": "PaperMC-Docs" };

const createProjectVersionsValue = (
project: string,
ttl: number = 5 * 60 * 1000
): ExpiringValue<string[]> => {
return new ExpiringValue(ttl, async () => {
const result = await fetch(`https://api.papermc.io/v2/projects/${project}`).then((r) =>
r.json()
);
const result = await fetch(`https://api.papermc.io/v2/projects/${project}`, {
headers: requestHeaders,
}).then((r) => r.json());

return result.versions;
});
};

export type Project = "paper" | "velocity";
const createUserdevVersionsValue = (ttl: number = 5 * 60 * 1000): ExpiringValue<string[]> => {
return new ExpiringValue(ttl, async () => {
const json = await fetch("https://api.github.com/repos/PaperMC/paperweight/tags", {
headers: requestHeaders,
}).then((r) => r.json());

return json.map((e) => e.name.substring(1)).reverse();
});
};

export type Project = "paper" | "velocity" | "userdev";

export enum VersionType {
Major,
Expand All @@ -48,6 +60,7 @@ export enum VersionType {
const projects: Record<Project, ExpiringValue<string[]>> = {
paper: createProjectVersionsValue("paper"),
velocity: createProjectVersionsValue("velocity"),
userdev: createUserdevVersionsValue(),
};

export interface DocusaurusVersion {
Expand Down Expand Up @@ -86,16 +99,3 @@ export const getProjectVersion = async (

return version;
};

export const getUserdevVersion = (ttl: number = 5 * 60 * 1000): ExpiringValue<string> => {
return new ExpiringValue(ttl, async () => {
const response = await fetch("https://api.github.com/repos/PaperMC/paperweight/tags");

if (!response.ok) {
return "<insert_latest_version>";
}

const json = await response.json();
return json[0].name.substring(1);
});
};

0 comments on commit f5c71bb

Please sign in to comment.