Skip to content

Commit dc19b3d

Browse files
committed
ci: Add a pre-build step to copy the avm2_report asset
1 parent f8dcb10 commit dc19b3d

File tree

5 files changed

+72
-27
lines changed

5 files changed

+72
-27
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7+
"prebuild": "node scripts/fetch-ruffle-report.js",
78
"build": "next build",
89
"start": "next start",
910
"lint": "next lint"

scripts/fetch-ruffle-report.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const { Readable } = require("stream");
4+
const { Octokit } = require("octokit");
5+
const { createTokenAuth } = require("@octokit/auth-token");
6+
7+
const owner = "ruffle-rs";
8+
const repo = "ruffle";
9+
const assetName = "avm2_report.json";
10+
const outputDir = path.resolve(__dirname, "..", "public");
11+
const outputFile = path.join(outputDir, assetName);
12+
13+
function createOctokit() {
14+
if (process.env.GITHUB_TOKEN) {
15+
const auth = createTokenAuth(process.env.GITHUB_TOKEN);
16+
return new Octokit({ authStrategy: auth });
17+
} else {
18+
console.warn(
19+
"Please provide a GitHub Personal Access Token via the GITHUB_TOKEN environment variable.",
20+
);
21+
return new Octokit();
22+
}
23+
}
24+
25+
async function downloadAsset(url, outputPath) {
26+
const res = await fetch(url);
27+
if (!res.ok) throw new Error(`Failed to download asset: ${res.statusText}`);
28+
29+
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
30+
31+
const nodeStream = Readable.from(res.body);
32+
const fileStream = fs.createWriteStream(outputPath);
33+
34+
await new Promise((resolve, reject) => {
35+
nodeStream.pipe(fileStream);
36+
nodeStream.on("error", reject);
37+
fileStream.on("finish", resolve);
38+
});
39+
}
40+
41+
async function downloadAVM2Report() {
42+
try {
43+
const octokit = createOctokit();
44+
45+
const { data: releases } = await octokit.rest.repos.listReleases({
46+
owner,
47+
repo,
48+
request: { next: { revalidate: 1800 } },
49+
per_page: 7,
50+
});
51+
52+
const asset = releases
53+
.flatMap((release) => release.assets)
54+
.find((a) => a.name === assetName);
55+
56+
if (!asset) throw new Error(`No release contains asset "${assetName}"`);
57+
58+
await downloadAsset(asset.browser_download_url, outputFile);
59+
60+
console.log(`Downloaded ${assetName} from latest release to ${outputFile}`);
61+
} catch (err) {
62+
console.error("Error:", err.message);
63+
process.exit(1);
64+
}
65+
}
66+
67+
(async () => {
68+
await downloadAVM2Report();
69+
})();

src/app/compatibility/avm2/report_utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function getReportByNamespace(): Promise<
2727
{ [name: string]: NamespaceStatus } | undefined
2828
> {
2929
let byNamespace: { [name: string]: NamespaceStatus } = {};
30-
const reportReq = await fetch("/compatibility/fetch-report");
30+
const reportReq = await fetch("/avm2_report.json");
3131
const report: AVM2Report = await reportReq.json();
3232
if (!report) {
3333
return;

src/app/compatibility/fetch-report/route.tsx

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/app/compatibility/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function Downloads() {
3939
setAvm1ApiDone(avm1ApiRes);
4040

4141
// Fetch report
42-
const reportReq = await fetch("/compatibility/fetch-report");
42+
const reportReq = await fetch("/avm2_report.json");
4343
const reportRes = await reportReq.json();
4444
if (reportRes) {
4545
const { summary } = reportRes;

0 commit comments

Comments
 (0)