summaryrefslogtreecommitdiff
path: root/dev/bin
diff options
context:
space:
mode:
authorDarius Jahandarie <djahandarie@gmail.com>2023-11-09 13:30:31 +0000
committerGitHub <noreply@github.com>2023-11-09 13:30:31 +0000
commit5c5a167b4792af379cdacf633513cebf20728cd2 (patch)
tree5b6be3620a557d0b9177047003f6d742d9d2a32d /dev/bin
parentb64f51c3b13a46af4dd7f1e43048ac19c781ca7b (diff)
parent0f4d36938fd0d844f548aa5a7f7e7842df8dfb41 (diff)
Merge pull request #307 from themoeway/modernize
Modernize codebase
Diffstat (limited to 'dev/bin')
-rw-r--r--dev/bin/build-libs.js21
-rw-r--r--dev/bin/build.js224
-rw-r--r--dev/bin/dictionary-validate.js40
-rw-r--r--dev/bin/generate-css-json.js29
-rw-r--r--dev/bin/schema-validate.js60
5 files changed, 374 insertions, 0 deletions
diff --git a/dev/bin/build-libs.js b/dev/bin/build-libs.js
new file mode 100644
index 00000000..07d27188
--- /dev/null
+++ b/dev/bin/build-libs.js
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2023 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 {buildLibs} from '../build-libs.js';
+
+buildLibs();
diff --git a/dev/bin/build.js b/dev/bin/build.js
new file mode 100644
index 00000000..282f0414
--- /dev/null
+++ b/dev/bin/build.js
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) 2023 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 path from 'path';
+import readline from 'readline';
+import {fileURLToPath} from 'url';
+import {buildLibs} from '../build-libs.js';
+import {ManifestUtil} from '../manifest-util.js';
+import {getAllFiles, getArgs, testMain} from '../util.js';
+
+const dirname = path.dirname(fileURLToPath(import.meta.url));
+
+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
+ }
+ }
+ }
+ return await createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun);
+}
+
+async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun) {
+ const JSZip = null;
+ 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'});
+ }
+}
+
+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;
+}
+
+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;
+ const onUpdate = (metadata) => {
+ if (dontLogOnUpdate) { return; }
+
+ 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);
+ };
+
+ 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) {
+ if (Array.isArray(fileCopies)) {
+ for (const fileName2 of fileCopies) {
+ const fileName2Safe = path.basename(fileName2);
+ fs.copyFileSync(fullFileName, path.join(buildDir, fileName2Safe));
+ }
+ }
+ }
+ }
+
+ process.stdout.write('\n');
+ }
+}
+
+function ensureFilesExist(directory, files) {
+ for (const file of files) {
+ assert.ok(fs.existsSync(path.join(directory, file)));
+ }
+}
+
+
+export async function main(argv) {
+ const args = getArgs(argv, new Map([
+ ['all', false],
+ ['default', false],
+ ['manifest', null],
+ ['dry-run', false],
+ ['dry-run-build-zip', false],
+ ['yomitan-version', '0.0.0.0'],
+ [null, []]
+ ]));
+
+ const dryRun = args.get('dry-run');
+ const dryRunBuildZip = args.get('dry-run-build-zip');
+ const yomitanVersion = args.get('yomitan-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 = (
+ 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, yomitanVersion);
+ } finally {
+ // Restore manifest
+ const manifestName = (!args.get('default') && args.get('manifest') !== null) ? args.get('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));
+ }
+ }
+}
+
+testMain(main, process.argv.slice(2));
diff --git a/dev/bin/dictionary-validate.js b/dev/bin/dictionary-validate.js
new file mode 100644
index 00000000..78ad5198
--- /dev/null
+++ b/dev/bin/dictionary-validate.js
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2023 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 {testDictionaryFiles} from '../dictionary-validate.js';
+
+async function main() {
+ const dictionaryFileNames = process.argv.slice(2);
+ if (dictionaryFileNames.length === 0) {
+ console.log([
+ 'Usage:',
+ ' node dictionary-validate [--ajv] <dictionary-file-names>...'
+ ].join('\n'));
+ return;
+ }
+
+ let mode = null;
+ if (dictionaryFileNames[0] === '--ajv') {
+ mode = 'ajv';
+ dictionaryFileNames.splice(0, 1);
+ }
+
+ await testDictionaryFiles(mode, dictionaryFileNames);
+}
+
+main();
diff --git a/dev/bin/generate-css-json.js b/dev/bin/generate-css-json.js
new file mode 100644
index 00000000..48b42c65
--- /dev/null
+++ b/dev/bin/generate-css-json.js
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2023 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 fs from 'fs';
+import {formatRulesJson, generateRules, getTargets} from '../generate-css-json.js';
+
+function main() {
+ for (const {cssFile, overridesCssFile, outputPath} of getTargets()) {
+ const json = formatRulesJson(generateRules(cssFile, overridesCssFile));
+ fs.writeFileSync(outputPath, json, {encoding: 'utf8'});
+ }
+}
+
+main();
diff --git a/dev/bin/schema-validate.js b/dev/bin/schema-validate.js
new file mode 100644
index 00000000..86cfebae
--- /dev/null
+++ b/dev/bin/schema-validate.js
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2023 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 fs from 'fs';
+import performance from 'perf_hooks';
+import {createJsonSchema} from '../util.js';
+
+function main() {
+ const args = process.argv.slice(2);
+ if (args.length < 2) {
+ console.log([
+ 'Usage:',
+ ' node schema-validate [--ajv] <schema-file-name> <data-file-names>...'
+ ].join('\n'));
+ return;
+ }
+
+ let mode = null;
+ if (args[0] === '--ajv') {
+ mode = 'ajv';
+ args.splice(0, 1);
+ }
+
+ const schemaSource = fs.readFileSync(args[0], {encoding: 'utf8'});
+ const schema = JSON.parse(schemaSource);
+
+ for (const dataFileName of args.slice(1)) {
+ const start = performance.now();
+ try {
+ console.log(`Validating ${dataFileName}...`);
+ const dataSource = fs.readFileSync(dataFileName, {encoding: 'utf8'});
+ const data = JSON.parse(dataSource);
+ createJsonSchema(mode, schema).validate(data);
+ const end = performance.now();
+ console.log(`No issues detected (${((end - start) / 1000).toFixed(2)}s)`);
+ } catch (e) {
+ const end = performance.now();
+ console.log(`Encountered an error (${((end - start) / 1000).toFixed(2)}s)`);
+ console.warn(e);
+ }
+ }
+}
+
+
+main();