-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
267 lines (221 loc) · 7.32 KB
/
main.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
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
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain, dialog} = require('electron')
const path = require('path')
const crypto = require('crypto')
const fs = require('fs')
const git = require('isomorphic-git')
const parseArgs = require('minimist')
function getMD5(path) {
const md5hash = crypto.createHash('md5');
md5hash.update(fs.readFileSync(path));
return md5hash.digest('hex');
}
async function getGitObject(pathr, oid) {
let {blob} = await git.readBlob({
fs,
dir: pathr,
oid: oid
});
return Buffer.from(blob).toString('utf8');
}
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1280,
height: 800,
webPreferences: {
nodeIntegration: false,
enableRemoteModule: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
// and load the index.html of the app.
mainWindow.loadFile('index.html');
// Open the DevTools.
//mainWindow.webContents.openDevTools({mode: 'bottom'})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
//if (process.platform !== 'darwin')
app.quit();
})
const filters = [
{ name : 'Java', extensions : [ 'java' ] },
{ name : 'C/C++', extensions : [ 'c', 'h', 'C', 'H', 'cpp', 'hpp', 'cc', 'hh' ] },
{ name : 'Fortran', extensions : [ 'f', 'F', 'for', 'FOR', 'f90', 'F90', 'h90', 'H90',
'f95', 'F95', 'f03', 'F03', 'f08', 'F08' ] },
{ name : 'Python', extensions : [ 'py' ] },
{ name : 'Verilog', extensions : [ 'v' ] },
{ name: 'All Files', extensions: ['*'] }
]
function error(err) {
console.log("[ERROR] "+err);
}
ipcMain.on('sync-mesg', (event, arg) => {
const args = parseArgs(process.argv);
const cache = args.cache;
const local_cache_name = args.localcachename;
const git_repo_dir = args.git;
if (git_repo_dir) {
const apathr = path.resolve(process.cwd(), git_repo_dir);
if (fs.existsSync(apathr)) {
const path1 = args.path;
var path0 = args.path0;
if (path0) {
} else {
path0 = path1;
}
const oid0 = args.oid0;
const oid1 = args.oid1;
if (path1 && oid0 && oid1) {
var apathd = null;
if (cache) {
apathd = path.resolve(process.cwd(), cache)
} else {
apathd = path.join(app.getPath('home'), '.cca', 'cache');
}
_apathd = path.join(apathd, oid0.substring(0, 2), oid0+'-'+oid1);
if (local_cache_name) {
_apathd = path.join(_apathd, local_cache_name);
}
apathd = path.join(_apathd, 'diff.json');
if (!fs.existsSync(apathd) && !local_cache_name) {
const dents = fs.readdirSync(_apathd, {withFileTypes: true});
for (var i = 0; i < dents.length; i++) {
const dent = dents[i];
if (dent.isDirectory()) {
const dp = path.join(_apathd, dent.name, 'diff.json');
if (fs.existsSync(dp)) {
apathd = dp;
break;
}
}
}
}
if (fs.existsSync(apathd)) {
getGitObject(apathr, oid0).then((blob0) => {
getGitObject(apathr, oid1).then((blob1) => {
global.apath0 = path0;
global.apath1 = path1;
global.apathd = apathd;
global.blob0 = blob0;
global.blob1 = blob1;
event.returnValue = {
apath0: path0,
apath1: path1,
apathd: apathd,
blob0: blob0,
blob1: blob1
};
});
});
} else {
dialog.showErrorBox('Error!', 'cache not found: '+apathd);
app.quit();
}
} else {
dialog.showErrorBox('Error!', 'not found: path1, oid0, or oid1');
app.quit();
}
} else {
dialog.showErrorBox('Error!', 'repository not found: '+apathr);
app.quit();
}
} else {
var file0 = args.file0;
var file1 = args.file1;
if (file0) {
} else {
var r0, r1;
r0 = dialog.showOpenDialogSync(null, {
properties : ['openFile','showHiddenFiles'],
title : 'Select the first source file',
message : 'Select the first source file',
filters : filters
});
if (r0) {
file0 = r0[0];
if (file1) {
} else {
r1 = dialog.showOpenDialogSync(null, {
properties : ['openFile','showHiddenFiles'],
title : 'Select the second source file',
message : 'Select the second source file',
filters : filters
});
if (r1)
file1 = r1[0];
}
} else {
app.quit();
}
}
if (file0 && file1) {
const apath0 = path.resolve(process.cwd(), file0);
if (fs.existsSync(apath0)) {
const apath1 = path.resolve(process.cwd(), file1);
if (fs.existsSync(apath1)) {
var apathd = null;
if (cache) {
apathd = path.resolve(process.cwd(), cache)
} else {
apathd = path.join(app.getPath('home'), '.cca', 'cache');
}
const hash0 = getMD5(apath0);
const hash1 = getMD5(apath1);
_apathd = path.join(apathd, hash0.substring(0, 2), hash0+'-'+hash1);
if (local_cache_name) {
_apathd = path.join(_apathd, local_cache_name);
}
apathd = path.join(_apathd, 'diff.json');
if (!fs.existsSync(apathd) && !local_cache_name) {
const dents = fs.readdirSync(_apathd, {withFileTypes: true});
for (var i = 0; i < dents.length; i++) {
const dent = dents[i];
if (dent.isDirectory()) {
const dp = path.join(_apathd, dent.name, 'diff.json');
if (fs.existsSync(dp)) {
apathd = dp;
break;
}
}
}
}
if (fs.existsSync(apathd)) {
global.apath0 = apath0;
global.apath1 = apath1;
global.apathd = apathd;
event.returnValue = { apath0: apath0, apath1: apath1, apathd: apathd };
} else {
dialog.showErrorBox('Error!', 'cache not found: '+apathd);
app.quit();
}
} else {
dialog.showErrorBox('Error!', 'not found: '+apath1);
app.quit();
}
} else {
dialog.showErrorBox('Error!', 'not found: '+apath0);
app.quit();
}
} else {
app.quit();
}
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.