2020-08-09 17:09:06 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2020-2022 Yomichan Authors
|
2020-08-09 17:09:06 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2020-12-19 01:51:22 +00:00
|
|
|
const assert = require('assert');
|
2020-08-09 17:09:06 +00:00
|
|
|
const readline = require('readline');
|
|
|
|
const childProcess = require('child_process');
|
2020-09-19 23:04:28 +00:00
|
|
|
const util = require('./util');
|
2021-05-31 17:24:40 +00:00
|
|
|
const {getAllFiles, getArgs, testMain} = util;
|
|
|
|
const {ManifestUtil} = require('./manifest-util');
|
2020-08-10 01:00:37 +00:00
|
|
|
|
2020-08-09 17:09:06 +00:00
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
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
|
|
|
|
}
|
2020-08-09 17:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 01:51:22 +00:00
|
|
|
return await createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun);
|
2020-08-09 17:09:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun) {
|
2020-08-10 01:00:37 +00:00
|
|
|
const JSZip = util.JSZip;
|
2021-02-15 01:25:41 +00:00
|
|
|
const files = getAllFiles(directory);
|
2020-12-18 21:46:00 +00:00
|
|
|
removeItemsFromArray(files, excludeFiles);
|
2020-08-09 17:09:06 +00:00
|
|
|
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');
|
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
if (!dryRun) {
|
|
|
|
fs.writeFileSync(outputFileName, data, {encoding: null, flag: 'w'});
|
|
|
|
}
|
2020-08-09 17:09:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 21:46:00 +00:00
|
|
|
function removeItemsFromArray(array, removeItems) {
|
|
|
|
for (const item of removeItems) {
|
|
|
|
const index = getIndexOfFilePath(array, item);
|
|
|
|
if (index >= 0) {
|
|
|
|
array.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:24:40 +00:00
|
|
|
async function build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip) {
|
2020-08-09 17:09:06 +00:00
|
|
|
const sevenZipExes = ['7za', '7z'];
|
|
|
|
|
|
|
|
// Create build directory
|
2020-12-19 01:51:22 +00:00
|
|
|
if (!fs.existsSync(buildDir) && !dryRun) {
|
2020-08-09 17:09:06 +00:00
|
|
|
fs.mkdirSync(buildDir, {recursive: true});
|
|
|
|
}
|
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
const dontLogOnUpdate = !process.stdout.isTTY;
|
2020-08-09 17:09:06 +00:00
|
|
|
const onUpdate = (metadata) => {
|
2020-12-19 01:51:22 +00:00
|
|
|
if (dontLogOnUpdate) { return; }
|
|
|
|
|
2020-08-09 17:09:06 +00:00
|
|
|
let message = `Progress: ${metadata.percent.toFixed(2)}%`;
|
|
|
|
if (metadata.currentFile) {
|
|
|
|
message += ` (${metadata.currentFile})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
readline.clearLine(process.stdout);
|
|
|
|
readline.cursorTo(process.stdout, 0);
|
|
|
|
process.stdout.write(message);
|
|
|
|
};
|
|
|
|
|
2020-09-19 00:03:56 +00:00
|
|
|
for (const variantName of variantNames) {
|
2021-05-31 17:24:40 +00:00
|
|
|
const variant = manifestUtil.getVariant(variantName);
|
|
|
|
if (typeof variant === 'undefined' || variant.buildable === false) { continue; }
|
2020-09-19 00:03:56 +00:00
|
|
|
|
|
|
|
const {name, fileName, fileCopies} = variant;
|
2020-12-18 21:46:00 +00:00
|
|
|
let {excludeFiles} = variant;
|
|
|
|
if (!Array.isArray(excludeFiles)) { excludeFiles = []; }
|
|
|
|
|
2020-09-19 00:03:56 +00:00
|
|
|
process.stdout.write(`Building ${name}...\n`);
|
|
|
|
|
2021-05-31 17:24:40 +00:00
|
|
|
const modifiedManifest = manifestUtil.getManifest(variant.name);
|
2020-09-19 00:03:56 +00:00
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
ensureFilesExist(extDir, excludeFiles);
|
2020-09-19 00:03:56 +00:00
|
|
|
|
2022-06-01 21:29:41 +00:00
|
|
|
if (typeof fileName === 'string') {
|
|
|
|
const fileNameSafe = path.basename(fileName);
|
|
|
|
const fullFileName = path.join(buildDir, fileNameSafe);
|
|
|
|
if (!dryRun) {
|
|
|
|
fs.writeFileSync(manifestPath, ManifestUtil.createManifestString(modifiedManifest));
|
|
|
|
}
|
2020-12-19 01:51:22 +00:00
|
|
|
|
2022-06-01 21:29:41 +00:00
|
|
|
if (!dryRun || dryRunBuildZip) {
|
|
|
|
await createZip(extDir, excludeFiles, fullFileName, sevenZipExes, onUpdate, dryRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dryRun) {
|
|
|
|
if (Array.isArray(fileCopies)) {
|
|
|
|
for (const fileName2 of fileCopies) {
|
|
|
|
const fileName2Safe = path.basename(fileName2);
|
|
|
|
fs.copyFileSync(fullFileName, path.join(buildDir, fileName2Safe));
|
|
|
|
}
|
2020-12-19 01:51:22 +00:00
|
|
|
}
|
2020-08-09 17:09:06 +00:00
|
|
|
}
|
2020-09-19 00:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
process.stdout.write('\n');
|
|
|
|
}
|
|
|
|
}
|
2020-08-09 17:09:06 +00:00
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
function ensureFilesExist(directory, files) {
|
|
|
|
for (const file of files) {
|
|
|
|
assert.ok(fs.existsSync(path.join(directory, file)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 00:03:56 +00:00
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
async function main(argv) {
|
2020-09-19 23:04:28 +00:00
|
|
|
const args = getArgs(argv, new Map([
|
2020-09-19 00:03:56 +00:00
|
|
|
['all', false],
|
|
|
|
['default', false],
|
|
|
|
['manifest', null],
|
2020-12-19 01:51:22 +00:00
|
|
|
['dry-run', false],
|
|
|
|
['dry-run-build-zip', false],
|
2020-09-19 00:03:56 +00:00
|
|
|
[null, []]
|
|
|
|
]));
|
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
const dryRun = args.get('dry-run');
|
|
|
|
const dryRunBuildZip = args.get('dry-run-build-zip');
|
|
|
|
|
2021-05-31 17:24:40 +00:00
|
|
|
const manifestUtil = new ManifestUtil();
|
2020-09-19 00:03:56 +00:00
|
|
|
|
|
|
|
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 {
|
2021-05-31 17:24:40 +00:00
|
|
|
const variantNames = (
|
|
|
|
argv.length === 0 || args.get('all') ?
|
|
|
|
manifestUtil.getVariants().filter(({buildable}) => buildable !== false).map(({name}) => name) :
|
|
|
|
args.get(null)
|
|
|
|
);
|
|
|
|
await build(buildDir, extDir, manifestUtil, variantNames, manifestPath, dryRun, dryRunBuildZip);
|
2020-08-09 17:09:06 +00:00
|
|
|
} finally {
|
|
|
|
// Restore manifest
|
2021-05-31 17:24:40 +00:00
|
|
|
const manifestName = (!args.get('default') && args.get('manifest') !== null) ? args.get('manifest') : null;
|
|
|
|
const restoreManifest = manifestUtil.getManifest(manifestName);
|
2020-08-09 17:09:06 +00:00
|
|
|
process.stdout.write('Restoring manifest...\n');
|
2020-12-19 01:51:22 +00:00
|
|
|
if (!dryRun) {
|
2021-05-31 17:24:40 +00:00
|
|
|
fs.writeFileSync(manifestPath, ManifestUtil.createManifestString(restoreManifest));
|
2020-12-19 01:51:22 +00:00
|
|
|
}
|
2020-08-09 17:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-19 01:51:22 +00:00
|
|
|
if (require.main === module) {
|
|
|
|
testMain(main, process.argv.slice(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
main
|
|
|
|
};
|