forked from GrolauxDonatien/easyOMR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_installer.js
111 lines (96 loc) · 4.19 KB
/
build_installer.js
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
// reference: https://ourcodeworld.com/articles/read/927/how-to-create-a-msi-installer-in-windows-for-an-electron-framework-application
// to run: node build_installer.js
const { MSICreator } = require('electron-wix-msi');
const path = require('path');
const fs = require("fs");
const child_process = require('child_process');
let packages = JSON.parse(fs.readFileSync("package.json"));
let versions = packages.version.split(".");
let last = parseInt(versions[versions.length - 1]);
versions.pop();
versions.push(last + 1);
versions = versions.join(".");
packages.version = versions;
fs.writeFileSync("package.json", JSON.stringify(packages, null, 4));
// turn off debug
function setConstant(fn, constant, value) {
let source = fs.readFileSync(fn, "utf-8");
let idx = source.indexOf(constant);
if (idx != -1) {
idx += constant.length;
while (idx < source.length && (source[idx] == " " || source[idx] == "\n")) idx++;
if (source[idx] == "=") {
idx++;
while (idx < source.length && (source[idx] == " " || source[idx] == "\n")) idx++;
let start = idx;
while (idx < source.length && (source[idx] != ";" && source[idx] != "\n")) idx++;
source = source.substring(0, start) + value + source.substring(idx);
fs.writeFileSync(fn, source);
}
}
}
setConstant('src/main.js', "DEBUG", "false");
setConstant('src/omr.js', "DEBUG", "false");
setConstant('src/main.js', "VERSION", '"' + versions + '"');
console.log("Creating version " + versions);
const APP_NAME = "easyOMR"
const APP_DIR = path.resolve(__dirname, './' + APP_NAME + '-win32-x64');
const OUT_DIR = path.resolve(__dirname, './windows_installer');
const APP_ICON = path.resolve(__dirname, './icon.ico');
const REPACKAGE = true;
const BUILDMSI = true;
if (REPACKAGE) {
// clear things up
if (fs.existsSync(APP_DIR)) fs.rmSync(APP_DIR, { recursive: true, force: true });
if (fs.existsSync(OUT_DIR)) fs.rmSync(OUT_DIR, { recursive: true, force: true });
console.log("Former build directories were deleted");
child_process.execSync("electron-packager . --platform=win32 --arch=x64 --icon=" + APP_ICON + " " + APP_NAME);
console.log("Stuff is packaged");
// remove tensorflow training folder
fs.rmSync(path.resolve(APP_DIR, './resources/app/tensorflow'), { recursive: true, force: true });
// clean up packaged stuff to make installer much smaller
const OPENCV_DIR = path.resolve(APP_DIR, './resources/app/node_modules/opencv-build/opencv/build');
let keep = ["bin", "win-install"];
for (let f of fs.readdirSync(OPENCV_DIR, { withFileTypes: true })) {
if (!f.isDirectory()) continue;
if (keep.indexOf(f.name) == -1) {
fs.rmSync(path.resolve(OPENCV_DIR, "./" + f.name), { recursive: true, force: true })
}
}
const APP_NODE_MODULES = path.resolve(APP_DIR, './resources/app');
child_process.execSync("modclean -n default:safe -r -p " + APP_NODE_MODULES);
// child_process.execSync("modclean -n default:caution -r -p " + APP_NODE_MODULES);
console.log("Opencv stuff is cleaned for smaller package");
}
// Instantiate the MSICreator
const msiCreator = new MSICreator({
appDirectory: APP_DIR,
outputDirectory: OUT_DIR,
appIconPath: APP_ICON,
// Configure metadata
description: 'easyOMR scans Moodle',
exe: APP_NAME,
name: APP_NAME,
manufacturer: 'ICHEC Brussels Management School, Donatien Grolaux',
version: versions,
// Configure installer User Interface
ui: {
chooseDirectory: true
},
});
if (BUILDMSI) {
// 4. Create a .wxs template file
msiCreator.create().then(function () {
// Step 5: Compile the template to a .msi file
msiCreator.compile().then(function () {
// Step 6: rename the .msi to include the version number.
let files = fs.readdirSync(OUT_DIR);
for (let i = 0; i < files.length; i++) {
if (files[i].endsWith(".msi")) {
fs.renameSync(path.join(OUT_DIR, files[i]), path.join(OUT_DIR, files[i].substring(0, files[i].length - 4) + "-" + versions + ".msi"));
}
}
console.log("MSI is ready");
});
});
}