forked from goatcorp/dalamud-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci-build.mjs
164 lines (135 loc) · 4.46 KB
/
ci-build.mjs
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
import { Octokit } from '@octokit/rest';
import fs from 'fs';
import { execSync as exec } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
const mainBranchName = 'master';
const versionBranchRegex = /^v[\d\.]+$/;
// shim for __dirname in ESM
const __dirname = path.resolve(path.dirname(fileURLToPath(import.meta.url)));
// get the list of branches for goatcorp/Dalamud
const octokit = new Octokit();
let { data: branches } = await octokit.repos.listBranches({
owner: 'goatcorp',
repo: 'Dalamud',
});
branches = branches.filter(
(branch) =>
branch.name === mainBranchName || branch.name.match(versionBranchRegex),
);
// for each branch, read the file at Dalamud/Dalamud.csproj
// and extract the version from the <DalamudVersion> tag
let versions = {};
for (const branch of branches) {
let { data: file } = await octokit.repos.getContent({
owner: 'goatcorp',
repo: 'Dalamud',
path: 'Dalamud/Dalamud.csproj',
ref: branch.name,
});
if (file.encoding === 'base64') {
file = Buffer.from(file.content, 'base64').toString();
} else {
file = file.content;
}
const versionParts = file
.match(/<DalamudVersion>([\d\.]+)<\/DalamudVersion>/)[1]
.split('.');
const majorVersion = parseInt(versionParts[0]);
versions[branch.name] = majorVersion;
console.log(`${branch.name}: major version: ${majorVersion}`);
}
// sort the versions from newest to oldest major
const sortedVersions = Object.entries(versions).sort((a, b) => b[1] - a[1]);
// write api_versions.json for Docusaurus, with just the branch names
fs.writeFileSync(
'api_versions.json',
JSON.stringify(sortedVersions.map((v) => v[0])),
);
// write dalamud-versions.json for docusaurus.config.js to use
let dalamudVersions = {};
for (const [branch, majorVersion] of sortedVersions) {
let apiLevel = majorVersion;
if (branch === mainBranchName) apiLevel = 8; // todo: remove this after v9 release
let meta = {};
meta = {
label: `${majorVersion}.x (API ${apiLevel})`,
};
if (majorVersion > versions[mainBranchName]) {
meta.banner = 'unreleased';
meta.label = `${meta.label} 🚧`;
} else if (branch === mainBranchName) {
meta.label = `${meta.label} [Current]`;
} else {
meta.banner = 'unmaintained';
meta.label = `${meta.label} [Legacy]`;
}
dalamudVersions[branch] = meta;
}
fs.writeFileSync('dalamud-versions.json', JSON.stringify(dalamudVersions));
console.log('Successfully updated version metadata files.');
// pull, build, and generate metadata for each version
const tempDir = process.env['RUNNER_TEMP'] || process.env['TEMP'] || '/tmp';
const execOptions = { stdio: 'inherit' };
const isWindows = process.platform === 'win32';
for (const branch of Object.keys(versions)) {
console.log(`==> Generating API documentation for branch ${branch}...`);
exec(
`git clone --recursive --depth 1 --branch ${branch} https://github.com/goatcorp/Dalamud Dalamud-${branch}`,
{ cwd: tempDir, ...execOptions },
);
const branchDir = path.join(tempDir, `Dalamud-${branch}`);
// build Dalamud
if (isWindows) {
exec('.\\build.ps1 CompileDalamud -Configuration Release', {
cwd: branchDir,
shell: 'pwsh.exe',
...execOptions,
});
} else {
exec(
'bash ./build.sh CompileDalamud -Configuration Release /p:EnableWindowsTargeting=true',
{
cwd: branchDir,
...execOptions,
},
);
}
// generate metadata
exec('docfx metadata', { cwd: branchDir, ...execOptions });
// execute dfmg
exec('dfmg', {
cwd: branchDir,
env: {
...process.env,
DFMG_CONFIG: path.join(__dirname, 'dfmg-config.yml'),
DFMG_OUTPUT_PATH: path.join(
__dirname,
'api_versioned_docs',
`version-${branch}`,
),
DFMG_YAML_PATH: path.join(branchDir, 'api'),
},
...execOptions,
});
// copy the placeholder sidebar file
fs.copyFileSync(
path.join(
__dirname,
'api_versioned_sidebars',
'version-placeholder-sidebars.json',
),
path.join(
__dirname,
'api_versioned_sidebars',
`version-${branch}-sidebars.json`,
),
);
console.log(`==> Finished generating API documentation for branch ${branch}`);
}
console.log('=> API doc generation complete! 🎉');
// now we can actually build the docs
console.log('=> Running `pnpm run build`...');
exec('pnpm run build', { cwd: __dirname, ...execOptions });
console.log('=> Docs built successfully! 🎉');
process.exit(0); // good ol' escape hatch