Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Build system changes (#721)
Browse files Browse the repository at this point in the history
* Refactor manifest.json

* Change end-of-line convention for built plain-text files

* Ignore builds directory

* Mark the "dev" directory as using the node environment

* Create build script

* Register build scripts

* Remove old build script

* Fix 64x64 icon

* Add test to ensure manifest data is updated properly
  • Loading branch information
toasted-nutbread authored Aug 9, 2020
1 parent 14efd8a commit 04d47bf
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 41 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@
}
},
{
"files": ["test/**/*.js"],
"files": [
"test/**/*.js",
"dev/**/*.js"
],
"excludedFiles": ["test/data/html/*.js"],
"parserOptions": {
"ecmaVersion": 8,
Expand Down
8 changes: 7 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
*.handlebars text eol=lf
*.sh text eol=lf
ext/*.handlebars text eol=lf
ext/*.js text eol=lf
ext/*.json text eol=lf
ext/*.css text eol=lf
ext/*.html text eol=lf
ext/*.svg text eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.zip
node_modules
builds/
1 change: 1 addition & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@npm run-script build
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
npm run-script build
7 changes: 0 additions & 7 deletions build_zip.sh

This file was deleted.

214 changes: 214 additions & 0 deletions dev/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* Copyright (C) 2020 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/>.
*/

const fs = require('fs');
const path = require('path');
const readline = require('readline');
const childProcess = require('child_process');
const yomichanTest = require('../test/yomichan-test');


function getAllFiles(directory, relativeTo) {
const results = [];
const directories = [directory];
for (const dir of directories) {
const fileNames = fs.readdirSync(dir);
for (const fileName of fileNames) {
const fullFileName = path.join(dir, fileName);
const relativeFileName = path.relative(relativeTo, fullFileName);
const stats = fs.lstatSync(fullFileName);
if (stats.isFile()) {
results.push(relativeFileName);
} else if (stats.isDirectory()) {
directories.push(fullFileName);
}
}
}
return results;
}

async function createZip(directory, outputFileName, sevenZipExes=[], onUpdate=null) {
for (const exe of sevenZipExes) {
try {
childProcess.execFileSync(
exe,
[
'a',
outputFileName,
'.'
],
{
cwd: directory
}
);
return;
} catch (e) {
// NOP
}
}
return await createJSZip(directory, outputFileName, onUpdate);
}

async function createJSZip(directory, outputFileName, onUpdate) {
const JSZip = yomichanTest.JSZip;
const files = getAllFiles(directory, directory);
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');

fs.writeFileSync(outputFileName, data, {encoding: null, flag: 'w'});
}

function createModifiedManifest(manifest, modifications) {
manifest = JSON.parse(JSON.stringify(manifest));

if (Array.isArray(modifications)) {
for (const modification of modifications) {
const {action, path: path2} = modification;
switch (action) {
case 'set':
{
const value = getObjectProperties(manifest, path2, path2.length - 1);
const last = path2[path2.length - 1];
value[last] = modification.value;
}
break;
case 'replace':
{
const value = getObjectProperties(manifest, path2, path2.length - 1);
const regex = new RegExp(modification.pattern, modification.patternFlags);
const last = path2[path2.length - 1];
let value2 = value[last];
value2 = `${value2}`.replace(regex, modification.replacement);
value[last] = value2;
}
break;
case 'delete':
{
const value = getObjectProperties(manifest, path2, path2.length - 1);
const last = path2[path2.length - 1];
delete value[last];
}
break;
}
}
}

return manifest;
}

function getObjectProperties(object, path2, count) {
for (let i = 0; i < count; ++i) {
object = object[path2[i]];
}
return object;
}

function loadDefaultManifest() {
const {manifest} = loadDefaultManifestAndVariants();
return manifest;
}

function loadDefaultManifestAndVariants() {
const fileName = path.join(__dirname, 'data', 'manifest-variants.json');
const {manifest, variants} = JSON.parse(fs.readFileSync(fileName));
return {manifest, variants};
}

function createManifestString(manifest) {
return JSON.stringify(manifest, null, 4) + '\n';
}


async function main() {
const {manifest, variants} = loadDefaultManifestAndVariants();

const rootDir = path.join(__dirname, '..');
const extDir = path.join(rootDir, 'ext');
const buildDir = path.join(rootDir, 'builds');
const manifestPath = path.join(extDir, 'manifest.json');
const sevenZipExes = ['7za', '7z'];

// Create build directory
if (!fs.existsSync(buildDir)) {
fs.mkdirSync(buildDir, {recursive: true});
}


const onUpdate = (metadata) => {
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);
};

try {
for (const variant of variants) {
const {name, fileName, fileCopies, modifications} = variant;
process.stdout.write(`Building ${name}...\n`);

const fileNameSafe = path.basename(fileName);
const modifiedManifest = createModifiedManifest(manifest, modifications);
const fullFileName = path.join(buildDir, fileNameSafe);
fs.writeFileSync(manifestPath, createManifestString(modifiedManifest));
await createZip(extDir, fullFileName, sevenZipExes, onUpdate);

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');
}
} finally {
// Restore manifest
process.stdout.write('Restoring manifest...\n');
fs.writeFileSync(manifestPath, createManifestString(manifest));
}
}


module.exports = {
loadDefaultManifest,
loadDefaultManifestAndVariants,
createManifestString
};


if (require.main === module) { main(); }
144 changes: 144 additions & 0 deletions dev/data/manifest-variants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
{
"manifest": {
"manifest_version": 2,
"name": "Yomichan",
"version": "20.8.3.0",
"description": "Japanese dictionary with Anki integration",
"author": "Alex Yatskov",
"icons": {
"16": "mixed/img/icon16.png",
"19": "mixed/img/icon19.png",
"32": "mixed/img/icon32.png",
"38": "mixed/img/icon38.png",
"48": "mixed/img/icon48.png",
"64": "mixed/img/icon64.png",
"128": "mixed/img/icon128.png"
},
"browser_action": {
"default_icon": {
"16": "mixed/img/icon16.png",
"19": "mixed/img/icon19.png",
"32": "mixed/img/icon32.png",
"38": "mixed/img/icon38.png",
"48": "mixed/img/icon48.png",
"64": "mixed/img/icon64.png",
"128": "mixed/img/icon128.png"
},
"default_title": "Yomichan",
"default_popup": "bg/context.html"
},
"background": {
"page": "bg/background.html",
"persistent": true
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*",
"file://*/*"
],
"js": [
"mixed/js/core.js",
"mixed/js/yomichan.js",
"mixed/js/comm.js",
"mixed/js/dom.js",
"mixed/js/api.js",
"mixed/js/dynamic-loader.js",
"mixed/js/frame-client.js",
"mixed/js/text-scanner.js",
"fg/js/document.js",
"fg/js/dom-text-scanner.js",
"fg/js/popup.js",
"fg/js/source.js",
"fg/js/popup-factory.js",
"fg/js/frame-offset-forwarder.js",
"fg/js/popup-proxy.js",
"fg/js/frontend.js",
"fg/js/content-script-main.js"
],
"match_about_blank": true,
"all_frames": true
}
],
"minimum_chrome_version": "57.0.0.0",
"options_page": "bg/settings.html",
"options_ui": {
"page": "bg/settings.html",
"open_in_tab": true
},
"permissions": [
"<all_urls>",
"storage",
"clipboardWrite",
"unlimitedStorage",
"nativeMessaging",
"webRequest",
"webRequestBlocking"
],
"optional_permissions": [
"clipboardRead"
],
"commands": {
"toggle": {
"suggested_key": {
"default": "Alt+Delete"
},
"description": "Toggle text scanning"
},
"search": {
"suggested_key": {
"default": "Alt+Insert"
},
"description": "Open search window"
}
},
"web_accessible_resources": [
"fg/float.html"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"applications": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "55.0"
}
}
},
"variants": [
{
"name": "default",
"fileName": "yomichan.zip",
"fileCopies": [
"yomichan.xpi"
]
},
{
"name": "dev",
"fileName": "yomichan-dev.zip",
"fileCopies": [
"yomichan-dev.xpi"
],
"modifications": [
{
"action": "replace",
"path": ["name"],
"pattern": "^.*$",
"patternFlags": "",
"replacement": "$& (development build)"
},
{
"action": "replace",
"path": ["description"],
"pattern": "^(.*)(?:\\.\\s*)?$",
"patternFlags": "",
"replacement": "$1. This is a development build; get the stable version here: https://tinyurl.com/yaatdjmp"
},
{
"action": "set",
"path": ["applications", "gecko", "id"],
"value": "[email protected]"
}
]
}
]
}
Loading

0 comments on commit 04d47bf

Please sign in to comment.