Skip to content

Commit

Permalink
Standardise script so to use platforms in payload.json
Browse files Browse the repository at this point in the history
  • Loading branch information
toebeann committed Mar 16, 2024
1 parent d18a063 commit b94f042
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
49 changes: 30 additions & 19 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,21 @@ const unitySchema = z.object({
libraries: z.string().array().optional(),
});

const platformSchema = z.literal("x86").or(z.literal("x64")).or(
z.literal("unix"),
);
type Platform = z.infer<typeof platformSchema>;
const platformsSchema = z.array(platformSchema);

const REPO = repoSchema.parse(gh(payloadJson.repo));
const BEPINEX_REPO = repoSchema.parse(gh(payloadJson.bepinex));
const PAYLOAD_DIR = "payload";
const DIST_DIR = "dist";
const METADATA_FILE = ".metadata.json";
const BEPINEX_RELEASE_TYPES = ["x86", "x64", "unix"] as const;
const BEPINEX_PLATFORMS = platformsSchema.parse(payloadJson.platforms);
const UNITY = "unity" in payloadJson &&
Object.freeze(unitySchema.parse(payloadJson.unity));

type BepInExReleaseType = typeof BEPINEX_RELEASE_TYPES[number];
type Release = Awaited<
ReturnType<InstanceType<typeof Octokit>["rest"]["repos"]["getRelease"]>
>["data"];
Expand All @@ -74,15 +79,15 @@ const httpErrorSchema = z.object({

const bepinexAssetFilter = (
asset: Asset,
type: BepInExReleaseType,
platform: Platform,
unityMono?: boolean,
) =>
asset.name.toLowerCase().includes(type.toLowerCase()) &&
asset.name.toLowerCase().includes(platform.toLowerCase()) &&
(!unityMono || asset.name.toLowerCase().includes("unitymono"));

const getBepInExAsset = (release: Release, type: BepInExReleaseType) =>
release.assets.find((asset) => bepinexAssetFilter(asset, type, true)) ??
release.assets.find((asset) => bepinexAssetFilter(asset, type));
const getBepInExAsset = (release: Release, platform: Platform) =>
release.assets.find((asset) => bepinexAssetFilter(asset, platform, true)) ??
release.assets.find((asset) => bepinexAssetFilter(asset, platform));

const getVersion = (version: string) => {
const cleaned = clean(version, true);
Expand Down Expand Up @@ -262,25 +267,25 @@ const writeArchiveToDisk = (path: string, archive: JSZip) => {

const getBepInExArchive = async (
release: Release,
type: BepInExReleaseType,
platform: Platform,
octokit: Octokit = new Octokit(),
) => {
let asset: ReturnType<typeof getBepInExAsset>;
try {
asset = getBepInExAsset(release, type);
if (!asset) return { asset, type, success: false };
asset = getBepInExAsset(release, platform);
if (!asset) return { asset, platform, success: false };

const assetBuffer = await downloadAsset(asset, BEPINEX_REPO, octokit);
if (!assetBuffer) return { asset, type, success: false };
if (!assetBuffer) return { asset, platform, success: false };

return {
asset,
type,
platform,
success: true,
archive: await JSZip.loadAsync(assetBuffer),
};
} catch {
return { asset, type, success: false };
return { asset, platform, success: false };
}
};

Expand Down Expand Up @@ -321,7 +326,7 @@ const mergeArchives = async (...archives: JSZip[]) => {
};

if (import.meta.main) {
if (!Deno.env.get("GITHUB_PERSONAL_ACCESS_TOKEN")) {
if (!Deno.env.get("GITHUB_PERSONAL_ACCESS_TOKEN") && Deno.env.get("CI")) {
console.error("GitHub PAT not set.");
Deno.exit(1);
}
Expand Down Expand Up @@ -475,7 +480,9 @@ if (import.meta.main) {

// we have a new release, let's handle it
const archives = await Promise.all([
getBepInExArchive(latestBepInExRelease, "x64", octokit),
...BEPINEX_PLATFORMS.map((platform) =>
getBepInExArchive(latestBepInExRelease, platform, octokit)
),
...latestPayloadReleases.map((release) =>
getPayloadArchive(
release,
Expand Down Expand Up @@ -611,7 +618,7 @@ if (import.meta.main) {
tag_name: `v${version}`,
target_commitish: commit.commit,
name: `v${version}`,
body: `## Release notes
body: `# Payload auto-update
${
(updatedSources ?? []).map((source) => {
Expand All @@ -622,11 +629,15 @@ ${
if (!release || !parsed?.owner || !parsed?.name) return "";
return `<details>
<summary>${parsed.repository}</summary>
<summary>Update ${parsed.repository} to ${release.tag_name}</summary>
${release.name ?? ""} ${release.tag_name} [Link](${release.html_url})
## [Release notes](${release.html_url})
${release.body ?? "No release notes provided."}
${
release.body?.split("\n").map((s) => `> ${s.trimEnd()}`).join(
"\n",
) ?? "No release notes provided."
}
</details>`;
}).join("\n\n")
Expand Down
3 changes: 2 additions & 1 deletion payload.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"version": "1.2.0",
"version": "1.2.1",
"name": "Tobey's BepInEx Pack for Chained Echoes",
"repo": "toebeann/bepinex.chainedechoes",
"bepinex": "bepinex/bepinex",
"platforms": ["x64", "unix"],
"sources": [
"toebeann/Tobey.FileTree",
"toebeann/Tobey.PluginDoctor"
Expand Down

0 comments on commit b94f042

Please sign in to comment.