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
|
/*
* Copyright (C) 2023-2024 Yomitan Authors
* Copyright (C) 2020-2022 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import assert from 'assert';
import childProcess from 'child_process';
import fs from 'fs';
import JSZip from 'jszip';
import {fileURLToPath} from 'node:url';
import path from 'path';
import readline from 'readline';
import {parseArgs} from 'util';
import {buildLibs} from '../build-libs.js';
import {ManifestUtil} from '../manifest-util.js';
import {getAllFiles} from '../util.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* @param {string} directory
* @param {string[]} excludeFiles
* @param {string} outputFileName
* @param {string[]} sevenZipExes
* @param {?import('jszip').OnUpdateCallback} onUpdate
* @param {boolean} dryRun
*/
async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, onUpdate, dryRun) {
try {
fs.unlinkSync(outputFileName);
} catch (e) {
// NOP
}
if (!dryRun) {
for (const exe of sevenZipExes) {
try {
const excludeArguments = excludeFiles.map((excludeFilePath) => `-x!${excludeFilePath}`);
childProcess.execFileSync(
exe,
[
'a',
outputFileName,
'.',
...excludeArguments
],
{
cwd: directory
}
);
return;
} catch (e) {
// NOP
}
}
}
await createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun);
}
/**
* @param {string} directory
* @param {string[]} excludeFiles
* @param {string} outputFileName
* @param {?import('jszip').OnUpdateCallback} onUpdate
* @param {boolean} dryRun
*/
async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun) {
const files = getAllFiles(directory);
removeItemsFromArray(files, excludeFiles);
const zip = new JSZip();
for (const fileName of files) {
zip.file(
fileName.replace(/\\/g, '/'),
fs.readFileSync(path.join(directory, fileName), {encoding: null, flag: 'r'}),
{}
);
}
if (typeof onUpdate !== 'function') {
onUpdate = () => {}; // NOP
}
const data = await zip.generateAsync({
type: 'nodebuffer',
compression: 'DEFLATE',
compressionOptions: {level: 9}
}, onUpdate);
process.stdout.write('\n');
if (!dryRun) {
fs.writeFileSync(outputFileName, data, {encoding: null, flag: 'w'});
}
}
/**
* @param {string[]} array
* @param {string[]} removeItems
*/
function removeItemsFromArray(array, removeItems) {
for (const item of removeItems) {
const index = getIndexOfFilePath(array, item);
if (index >= 0) {
array.splice(index, 1);
}
}
}
/**
* @param {string[]} array
* @param {string} item
* @returns {number}
*/
function getIndexOfFilePath(array, item) {
const pattern = /\\/g;
const separator = '/';
item = item.replace(pattern, separator);
for (let i = 0, ii = array.length; i < ii; ++i) {
if (array[i].replace(pattern, separator) === item) {
return i;
}
}
return -1;
}
/**
* @param {string} buildDir
* @param {string} extDir
* @param {ManifestUtil} manifestUtil
* @param {string[]} variantNames
* @param {string} manifestPath
* @param {boolean} dryRun
* @param {boolean} dryRunBuildZip
* @param {string} yomitanVersion
*/
async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip, yomitanVersion) {
const sevenZipExes = ['7za', '7z'];
// Create build directory
if (!fs.existsSync(buildDir) && !dryRun) {
fs.mkdirSync(buildDir, {recursive: true});
}
const dontLogOnUpdate = !process.stdout.isTTY;
/** @type {import('jszip').OnUpdateCallback} */
const onUpdate = (metadata) => {
if (dontLogOnUpdate) { return; }
let message = `Progress: ${metadata.percent.toFixed(2)}%`;
if (metadata.currentFile) {
message += ` (${metadata.currentFile})`;
}
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
process.stdout.write(message);
};
process.stdout.write(`Version: ${yomitanVersion}...\n`);
for (const variantName of variantNames) {
const variant = manifestUtil.getVariant(variantName);
if (typeof variant === 'undefined' || variant.buildable === false) { continue; }
const {name, fileName, fileCopies} = variant;
let {excludeFiles} = variant;
if (!Array.isArray(excludeFiles)) { excludeFiles = []; }
process.stdout.write(`Building ${name}...\n`);
const modifiedManifest = manifestUtil.getManifest(variant.name);
ensureFilesExist(extDir, excludeFiles);
if (typeof fileName === 'string') {
const fileNameSafe = path.basename(fileName);
const fullFileName = path.join(buildDir, fileNameSafe);
if (!dryRun) {
fs.writeFileSync(manifestPath, ManifestUtil.createManifestString(modifiedManifest).replace('$YOMITAN_VERSION', yomitanVersion));
}
if (!dryRun || dryRunBuildZip) {
await createZip(extDir, excludeFiles, fullFileName, sevenZipExes, onUpdate, dryRun);
}
if (!dryRun && Array.isArray(fileCopies)) {
for (const fileName2 of fileCopies) {
const fileName2Safe = path.basename(fileName2);
fs.copyFileSync(fullFileName, path.join(buildDir, fileName2Safe));
}
}
}
process.stdout.write('\n');
}
}
/**
* @param {string} directory
* @param {string[]} files
*/
function ensureFilesExist(directory, files) {
for (const file of files) {
assert.ok(fs.existsSync(path.join(directory, file)));
}
}
/** */
export async function main() {
/** @type {import('util').ParseArgsConfig['options']} */
const parseArgsConfigOptions = {
all: {
type: 'boolean',
default: false
},
default: {
type: 'boolean',
default: false
},
manifest: {
type: 'string'
},
dryRun: {
type: 'boolean',
default: false
},
dryRunBuildZip: {
type: 'boolean',
default: false
},
version: {
type: 'string',
default: '0.0.0.0'
}
};
const argv = process.argv.slice(2);
const {values: args} = parseArgs({args: argv, options: parseArgsConfigOptions});
const dryRun = /** @type {boolean} */ (args.dryRun);
const dryRunBuildZip = /** @type {boolean} */ (args.dryRunBuildZip);
const yomitanVersion = /** @type {string} */ (args.version);
const manifestUtil = new ManifestUtil();
const rootDir = path.join(dirname, '..', '..');
const extDir = path.join(rootDir, 'ext');
const buildDir = path.join(rootDir, 'builds');
const manifestPath = path.join(extDir, 'manifest.json');
try {
await buildLibs();
const variantNames = /** @type {string[]} */ ((
argv.length === 0 || args.all ?
manifestUtil.getVariants().filter(({buildable}) => buildable !== false).map(({name}) => name) :
[]
));
await build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip, yomitanVersion);
} finally {
// Restore manifest
const manifestName = /** @type {?string} */ ((!args.default && typeof args.manifest !== 'undefined') ? args.manifest : null);
const restoreManifest = manifestUtil.getManifest(manifestName);
process.stdout.write('Restoring manifest...\n');
if (!dryRun) {
fs.writeFileSync(manifestPath, ManifestUtil.createManifestString(restoreManifest).replace('$YOMITAN_VERSION', yomitanVersion));
}
}
}
await main();
|