-
Notifications
You must be signed in to change notification settings - Fork 64
/
deploy.ts
484 lines (405 loc) · 14.8 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as https from 'https';
import * as path from 'path';
import ModSourceUtils from './modSourceUtils';
import { Feed } from 'feed';
import showdown from 'showdown';
// Inspired by https://gist.github.com/ktheory/df3440b01d4b9d3197180d5254d7fb65
async function fetchJson(url: string) {
return new Promise<any>((resolve, reject) => {
const req = https.request(url,
{ headers: { 'User-Agent': 'nodejs' } },
(res) => {
let body = '';
res.on('data', (chunk) => (body += chunk.toString()));
res.on('error', reject);
res.on('end', () => {
if (res.statusCode && res.statusCode >= 200 && res.statusCode <= 299) {
resolve(JSON.parse(body));
} else {
reject('Request failed. status: ' + res.statusCode + ', body: ' + body);
}
});
});
req.on('error', reject);
req.end();
});
}
// https://stackoverflow.com/a/53593328
function JSONstringifyOrder(obj: any, space: number) {
const allKeys = new Set<string>();
JSON.stringify(obj, (key, value) => {
allKeys.add(key);
return value;
});
return JSON.stringify(obj, Array.from(allKeys).sort(), space);
}
function gitExec(args: string[]) {
const result = child_process.spawnSync('git', args, { encoding: 'utf8' });
if (result.status !== 0) {
throw new Error('git ' + args.join(' ') + ' failed with status ' + result.status + ' and stderr ' + result.stderr);
}
if (result.stderr) {
console.warn('git ' + args.join(' ') + ' produced stderr ' + result.stderr);
}
return result.stdout;
}
function getModCreatedTime(modId: string) {
const time = parseInt(gitExec([
'log',
'--diff-filter=A',
'--format=%ct',
'-1',
'--',
`mods/${modId}.wh.cpp`,
]), 10);
if (isNaN(time)) {
throw new Error(`Can't get created time for ${modId}`);
}
return time * 1000;
}
function getModModifiedTime(modId: string) {
const time = parseInt(gitExec([
'log',
'--format=%ct',
'-1',
'--',
`mods/${modId}.wh.cpp`,
]), 10);
if (isNaN(time)) {
throw new Error(`Can't get modified time for ${modId}`);
}
return time * 1000;
}
function findCachedMod(modId: string, version: string, arch: string) {
const lastDeployPath = process.env.WINDHAWK_MODS_LAST_DEPLOY_PATH;
if (!lastDeployPath) {
throw new Error('WINDHAWK_MODS_LAST_DEPLOY_PATH is not set');
}
const modFile = path.join(lastDeployPath, 'mods', modId, `${version}_${arch}.dll`);
return fs.existsSync(modFile) ? modFile : null;
}
function compileMod(modFilePath: string, output32FilePath: string, output64FilePath: string) {
const windhawkPath = process.env.WINDHAWK_PATH;
if (!windhawkPath) {
throw new Error('WINDHAWK_PATH is not set');
}
const result = child_process.spawnSync('py', [
'scripts/compile_mod.py',
'-w',
windhawkPath,
'-f',
modFilePath,
'-o32',
output32FilePath,
'-o64',
output64FilePath,
], { encoding: 'utf8', stdio: 'inherit' });
if (result.status !== 0) {
throw new Error('Compiling ' + modFilePath + ' failed with status ' + result.status);
}
}
function generateModData(modId: string, changelogPath: string, modDir: string) {
if (!fs.existsSync(modDir)) {
fs.mkdirSync(modDir);
}
let changelog = '';
const versions: {
version: string;
timestamp: number;
prerelease?: boolean;
}[] = [];
let sawReleaseVersion = false;
const modSourceUtils = new ModSourceUtils('mods');
const commits = gitExec([
'rev-list',
'HEAD',
'--',
`mods/${modId}.wh.cpp`,
]).trim().split('\n');
const lastCommit = commits[commits.length - 1];
for (const commit of commits) {
const modFile = gitExec([
'show',
`${commit}:mods/${modId}.wh.cpp`,
]);
const metadata = modSourceUtils.extractMetadata(modFile, 'en-US');
if (!metadata.version) {
throw new Error(`Mod ${modId} has no version in commit ${commit}`);
}
const prerelease = metadata.version.includes('-');
if (prerelease && sawReleaseVersion) {
continue;
}
const modVersionFilePath = path.join(modDir, `${metadata.version}.wh.cpp`);
if (fs.existsSync(modVersionFilePath)) {
throw new Error(`Mod ${modId} has duplicate version ${metadata.version} in commit ${commit}`);
}
fs.writeFileSync(modVersionFilePath, modFile);
if (!prerelease && !sawReleaseVersion) {
// Override root file with the latest release version.
fs.copyFileSync(path.join('mods', `${modId}.wh.cpp`), modVersionFilePath);
sawReleaseVersion = true;
}
const modVersionCompiled32FilePath = path.join(modDir, `${metadata.version}_32.dll`);
const modVersionCompiled64FilePath = path.join(modDir, `${metadata.version}_64.dll`);
const cachedMod32Path = findCachedMod(modId, metadata.version, '32');
const cachedMod64Path = findCachedMod(modId, metadata.version, '64');
if (cachedMod32Path || cachedMod64Path) {
const modHas32 = metadata.architecture?.includes('x86') ?? true;
const modHas64 = metadata.architecture?.includes('x86-64') ?? true;
if (modHas32 != !!cachedMod32Path || modHas64 != !!cachedMod64Path) {
throw new Error(`Mod ${modId} architecture mismatch`);
}
if (cachedMod32Path) {
fs.copyFileSync(cachedMod32Path, modVersionCompiled32FilePath);
}
if (cachedMod64Path) {
fs.copyFileSync(cachedMod64Path, modVersionCompiled64FilePath);
}
} else {
compileMod(modVersionFilePath, modVersionCompiled32FilePath, modVersionCompiled64FilePath);
}
const commitTime = parseInt(gitExec([
'log',
'--format=%ct',
'-1',
commit,
]), 10);
const commitFormattedDate = new Date(commitTime * 1000)
.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
const modVersionUrl = `https://github.com/ramensoftware/windhawk-mods/blob/${commit}/mods/${modId}.wh.cpp`;
changelog += `## ${metadata.version} ([${commitFormattedDate}](${modVersionUrl}))\n\n`;
if (commit !== lastCommit) {
const commitMessage = gitExec([
'log',
'-1',
'--pretty=format:%B',
commit,
]);
const changelogItem = getModChangelogTextForVersion(modId, metadata.version, commitMessage);
changelog += `${changelogItem}\n\n`;
} else {
changelog += 'Initial release.\n';
}
versions.unshift({
version: metadata.version,
timestamp: commitTime,
...(prerelease ? { prerelease: true } : {}),
});
}
fs.writeFileSync(changelogPath, changelog);
const versionsPath = path.join(modDir, 'versions.json');
fs.writeFileSync(versionsPath, JSON.stringify(versions));
}
function generateModsData() {
const changelogDir = 'changelogs';
if (!fs.existsSync(changelogDir)) {
fs.mkdirSync(changelogDir);
}
const modsSourceDir = fs.opendirSync('mods');
try {
let modsSourceDirEntry: fs.Dirent | null;
while ((modsSourceDirEntry = modsSourceDir.readSync()) !== null) {
if (modsSourceDirEntry.isFile() && modsSourceDirEntry.name.endsWith('.wh.cpp')) {
const modId = modsSourceDirEntry.name.slice(0, -'.wh.cpp'.length);
const changelogPath = path.join(changelogDir, `${modId}.md`);
const modDir = path.join('mods', modId);
generateModData(modId, changelogPath, modDir);
}
}
} finally {
modsSourceDir.closeSync();
}
}
function enrichCatalog(catalog: Record<string, any>, enrichment: any, modTimes: any) {
const app = {
version: enrichment.app.version,
};
const mods: Record<string, any> = {};
for (const [id, metadata] of Object.entries(catalog)) {
const { id: idFromMetadata, ...rest } = metadata;
if (id !== idFromMetadata) {
throw new Error(`Expected ${id} === ${idFromMetadata}`);
}
modTimes[id] = modTimes[id] || {
published: getModCreatedTime(id),
updated: getModModifiedTime(id),
};
mods[id] = {
metadata: rest,
details: {
published: modTimes[id].published,
updated: modTimes[id].updated,
defaultSorting: 0,
rating: 0,
users: 0,
ratingUsers: 0,
...enrichment.mods[id]?.details,
},
};
if (enrichment.mods[id]?.featured) {
mods[id].featured = true;
}
}
return {
app,
mods,
};
}
async function generateModCatalogs() {
const enrichmentUrl = 'https://update.windhawk.net/mods_catalog_enrichment.json';
const enrichment = await fetchJson(enrichmentUrl);
const translateFilesUrl = 'https://api.github.com/repos/ramensoftware/windhawk-translate/contents';
const translateFiles = await fetchJson(translateFilesUrl);
const modSourceUtils = new ModSourceUtils('mods');
const modTimes = {};
const catalog = modSourceUtils.getMetadataOfMods('en-US');
const catalogEnriched = enrichCatalog(catalog, enrichment, modTimes);
fs.writeFileSync('catalog.json', JSONstringifyOrder(catalogEnriched, 4));
const catalogsDir = 'catalogs';
if (!fs.existsSync(catalogsDir)) {
fs.mkdirSync(catalogsDir);
}
for (const translateFile of translateFiles) {
const translateFileName = translateFile.name;
if (!translateFileName.endsWith('.yml')) {
continue;
}
const language = translateFileName.slice(0, -'.yml'.length);
const catalog = modSourceUtils.getMetadataOfMods(language);
const catalogEnriched = enrichCatalog(catalog, enrichment, modTimes);
fs.writeFileSync(path.join(catalogsDir, `${language}.json`), JSONstringifyOrder(catalogEnriched, 4));
}
}
function getModChangelogTextForVersion(modId: string, modVersion: string, commitMessage: string) {
const overridePath = path.join('changelog_override', modId, `${modVersion}.md`);
if (fs.existsSync(overridePath)) {
return fs.readFileSync(overridePath, 'utf8').trim();
}
let messageTrimmed = commitMessage.trim();
if (messageTrimmed.includes('\n')) {
// Remove first line.
return messageTrimmed.replace(/^.* \(#\d+\)\n\n/, '').trim();
} else {
// Only remove trailing PR number if it's the only line.
return messageTrimmed.replace(/ \(#\d+\)$/, '').trim();
}
}
function generateRssFeed() {
type FeedItem = {
commit: string;
title: string;
content: string;
url: string;
date: Date;
author: {
name?: string;
link?: string;
};
};
let feedItems: FeedItem[] = [];
const modSourceUtils = new ModSourceUtils('mods');
const commits = gitExec([
'rev-list',
'HEAD',
]).trim().split('\n');
for (const commit of commits) {
const changedFiles = gitExec([
'show',
'--name-status',
'--pretty=format:',
commit,
]).trim().split('\n');
if (changedFiles.length !== 1) {
continue;
}
const [changeType, filePath] = changedFiles[0].split('\t');
if (changeType !== 'A' && changeType !== 'M') {
continue;
}
const match = filePath.match(/^mods\/(.+)\.wh\.cpp$/);
if (!match) {
continue;
}
const modId = match[1];
const modFile = gitExec([
'show',
`${commit}:mods/${modId}.wh.cpp`,
]);
const metadata = modSourceUtils.extractMetadata(modFile, 'en-US');
if (!metadata.version) {
throw new Error(`Mod ${modId} has no version in commit ${commit}`);
}
const commitTime = parseInt(gitExec([
'log',
'--format=%ct',
'-1',
commit,
]), 10);
let content = '';
if (changeType === 'M') {
const commitMessage = gitExec([
'log',
'-1',
'--pretty=format:%B',
commit,
]);
content = getModChangelogTextForVersion(modId, metadata.version, commitMessage);
} else {
content = modSourceUtils.extractReadme(modFile) || 'Initial release.';
}
feedItems.push({
commit,
title: `${metadata.name} ${metadata.version}`,
content,
url: `https://windhawk.net/mods/${modId}`,
date: new Date(commitTime * 1000),
author: {
name: metadata.author,
link: metadata.github,
},
});
if (feedItems.length >= 20) {
break;
}
}
const feed = new Feed({
title: 'Windhawk Mod Updates',
description: 'Updates in the official collection of Windhawk mods',
id: 'https://windhawk.net/',
link: 'https://windhawk.net/',
favicon: 'https://windhawk.net/favicon.ico',
copyright: 'Ramen Software',
updated: feedItems[0].date,
});
const showdownConverter = new showdown.Converter();
const markdownToHtml = (markdown: string) => {
// Showdown doesn't support trailing backslashes as newlines. Use double
// spaces instead. https://github.com/showdownjs/showdown/issues/394
markdown = markdown.replace(/\\\n/g, ' \n');
return showdownConverter.makeHtml(markdown);
}
for (const feedItem of feedItems) {
feed.addItem({
title: feedItem.title,
id: feedItem.url + '#' + feedItem.commit,
link: feedItem.url,
content: markdownToHtml(feedItem.content),
date: feedItem.date,
author: [feedItem.author],
});
}
return feed.atom1();
}
async function main() {
generateModsData();
await generateModCatalogs();
fs.writeFileSync('updates.atom', generateRssFeed());
const srcPath = 'public';
for (const file of fs.readdirSync(srcPath, { withFileTypes: true })) {
fs.renameSync(path.join(srcPath, file.name), file.name);
}
}
main();