-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.js
More file actions
437 lines (357 loc) · 12.6 KB
/
main.js
File metadata and controls
437 lines (357 loc) · 12.6 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
const { app, BrowserWindow, ipcMain, ipcRenderer, dialog, nativeImage } = require('electron');
const {Menu} = require('electron');
const exec = require('child_process').exec;
const xmlReader = require('xmlreader');
const path = require('path');
const readFile = require('fs');
// 应用的主窗口
let mainWindow;
// 创建主窗口
function createWindow() {
// 创建浏览器窗口
mainWindow = new BrowserWindow({
title: "安装 aab 程序包",
minHeight: 600,
minWidth: 800,
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
// 主进程中使用
// mainWindow.webContents.openDevTools();
// 加载index.html文件
mainWindow.loadFile("index.html");
console.log('是否是 window 平台:' + isWinOS());
console.log(`平台信息:` + process.platform);
console.log('是否是开发环境:' + app.isPackaged);
console.log(`资源路径:${process.resourcesPath}`)
}
app.on('ready', createWindow)
app.on("window-all-closed", function() {
app.quit();
})
// 处理选择文件
ipcMain.on('open_file_select', function (event, arg) {
var options = {
title: '选择 aab 程序包',
filters: [{ name: 'aab', extensions: ['aab'] }],
properties: ['openFile']
}
dialog.showOpenDialog(win, options)
.then((res) => {
if (res.canceled) {
return
}
const filenames = res.filePaths;
console.log(`选择文件:${filenames}`);
});
});
// 接收渲染进程发送过来的消息,可以通过:on_install_rsp 发送消息回去
ipcMain.on('install_aab', function (event, arg) {
console.log("请求处理安装 aab 安装包: " + arg);
// console.log(getJavaPath());
// 开始 aab 安装处理流程
parseAabContent(event, arg);
});
/**
* 发送消息到 UI 界面进行展示
* @param {Electron.IpcMainEvent} event
* @param {*} msg
*/
function sendMsgToUI(event, msg) {
event.sender.send('on_install_rsp', `[aab 安装]${msg}\n`);
}
function getAssetsPath() {
let assets_path = app.isPackaged ? `${process.resourcesPath}/assets` : `${app.getAppPath()}/assets`;
return assets_path;
}
function getBundletoolJarPath() {
let bundletool_jar_path = `${getJavaPath()} -jar ${getAssetsPath()}/bundletool-all-0.13.4.jar`;
return bundletool_jar_path;
}
function getJavaPath() {
let java_bin_path = `${getAssetsPath()}/java/bin`;
let java_path = isWinOS() ? `${java_bin_path}/java.exe` : `${java_bin_path}/java`;
return java_path;
}
function getAdbPath() {
let adb_path = isWinOS() ? `${getAssetsPath()}/adb.exe` : `${getAssetsPath()}/adb`;
return adb_path;
}
function getInstallTempPath() {
let install_temp_path = `${process.resourcesPath}/install_temp`;
return install_temp_path;
}
/**
* 第一步:解析 aab 文件,用于获取到应用相关的信息
*
* @param {Electron.IpcMainEvent} event
* @param {*} aab_file_path
*/
function parseAabContent(event, aab_file_path) {
sendMsgToUI(event, `1、正在进行 aab 文件解析:${path.basename(aab_file_path)}`);
let bundletool_jar_path = getBundletoolJarPath();
let adb_path = getAdbPath();
let cmd = `${bundletool_jar_path} dump manifest --adb ${adb_path} --bundle=${aab_file_path}`
let workerProcess = exec(cmd, (err, stdout, stderr) => {
if ('' !== stderr) {
let errorMsg = `获取 aab manifest 文件信息出错,错误信息:${stderr}`;
sendMsgToUI(event, errorMsg);
tipsInstallError(errorMsg);
return;
}
xmlReader.read(stdout, function (errors, response) {
if (null !== errors) {
let errorMsg = `解析 aab 的清单内容出错:${errors}`;
log(errorMsg);
sendMsgToUI(event, errorMsg);
tipsInstallError(errorMsg);
return;
}
// 获取到应用的包名
let app_pkg = response.manifest.attributes().package;
// 获取应用的版本名
let app_vname = response.manifest.attributes()['android:versionName'];
// 获取应用的版本号
let app_vcode = response.manifest.attributes()['android:versionCode'];
var aabInfo = new AabInfo(app_pkg, app_vname, app_vcode);
let aabParseRst = `aab 文件解析结果如下👉:\n应用包名:${aabInfo.pkg},应用版本信息:${aabInfo.getAppVersionInfo()}\n`;
log(aabParseRst);
sendMsgToUI(event, aabParseRst);
generateSpecFile(event, aab_file_path, aabInfo);
// log(response.manifest);
// log(response.manifest.application.activity.array[0].attributes()['android:name']);
// log(response.manifest.application.activity.array[0]['intent-filter'] !== null);
})
});
}
/**
* 第二步:生成设备描述文件
* @param {Electron.IpcMainEvent} event
* @param {*} aab_file_path
* @param {AabInfo} aabInfo
*/
function generateSpecFile(event, aab_file_path, aabInfo) {
sendMsgToUI(event, `2、正在生成设备描述文件`);
let install_temp_path = getInstallTempPath();
// bundletool jar 包路径
let bundletool_jar_path = getBundletoolJarPath();
// aab 路径
let adb_path = getAdbPath();
// 设备描述文件路径
let device_spec_file = `${install_temp_path}/device-spec.json`;
// 1、生成连接设备对应的 spec 文件命令
let gen_device_spec_cmd = `${bundletool_jar_path} get-device-spec --adb ${adb_path} --output=${device_spec_file} --overwrite`;
let workerProcess = exec(gen_device_spec_cmd, (errE, stdout) => {
if (null !== errE) {
let errorMsg = `生成设备描述文件出错,错误信息:${errE}`;
sendMsgToUI(event, errorMsg);
tipsInstallError(errorMsg);
return;
}
sendMsgToUI(event, `设备描述信息生成成功,文件路径👉:\n${device_spec_file}\n`);
showDeviceSpecFile(event, aab_file_path, aabInfo, device_spec_file)
});
}
/**
* 第三步:读取生成的设备描述信息
*
* @param {Electron.IpcMainEvent} event
* @param {*} aab_file_path
* @param {AabInfo} aabInfo
* @param {*} device_spec_file
*/
function showDeviceSpecFile(event, aab_file_path, aabInfo, device_spec_file) {
sendMsgToUI(event, `3、读取设备描述信息`);
readFile.readFile(device_spec_file, 'utf-8', function (error, content) {
log(content);
sendMsgToUI(event, `设备描述信息读取成功,内容如下👉:\n${content}\n`);
buildApksFile(event, aab_file_path, aabInfo, device_spec_file);
})
}
/**
* 第四步:根据设备描述文件生成 apks,并安装到连接设备上
*
* @param {Electron.IpcMainEvent} event
* @param {*} aab_file_path
* @param {AabInfo} aabInfo
* @param {*} device_spec_file
*/
function buildApksFile(event, aab_file_path, aabInfo, device_spec_file) {
sendMsgToUI(event, `4、正在使用设备描述信息生成 apks 文件`);
let install_temp_path = getInstallTempPath();
// bundletool jar 包路径
let bundletool_jar_path = getBundletoolJarPath();
// aab 路径
let adb_path = getAdbPath();
// apks 文件路径
let apks_file = `${install_temp_path}/app_bundle.apks`;
// ks 签名文件路径
let ks_file = `${getAssetsPath()}/${aabInfo.getKeystoreName()}`;
log(`拿到的签名文件是:${ks_file}`);
// 获取签名信息
let keyStoreConfig = aabInfo.getKeystoreConfig();
log(`获取到的签名信息:ks_pass=${keyStoreConfig.ks_pass}, alias=${keyStoreConfig.alias}, key_pass=${keyStoreConfig.key_pass}`);
// 根据 spec 文件生成 apks 文件
let gen_apks_cmd = `${bundletool_jar_path} build-apks --adb ${adb_path} --bundle=${aab_file_path} --device-spec=${device_spec_file} --output=${apks_file} --ks=${ks_file} --ks-pass=pass:${keyStoreConfig.ks_pass} --ks-key-alias=${keyStoreConfig.alias} --key-pass=pass:${keyStoreConfig.key_pass} --overwrite`;
let workerProcess = exec(gen_apks_cmd, (errE, stdout) => {
if (null !== errE) {
let errorMsg = `生成 apks 文件出错,错误信息:${errE}`;
sendMsgToUI(event, errorMsg);
tipsInstallError(errorMsg);
return;
}
log(`生成 apks 文件成功,文件路径:${apks_file}`);
sendMsgToUI(event, `生成 apks 文件成功,文件路径👉:\n${apks_file}\n`);
installApkToDevice(event, aabInfo, apks_file);
});
}
/**
* 第五步:将 apks 文件安装到设备中
*
* @param {Electron.IpcMainEvent} event
* @param {AabInfo} aabInfo
* @param {*} apks_file
*/
function installApkToDevice(event, aabInfo, apks_file) {
sendMsgToUI(event, `5、正在将 apks 安装到设备中...`);
// bundletool jar 包路径
let bundletool_jar_path = getBundletoolJarPath();
// aab 路径
let adb_path = getAdbPath();
// 3、安装 apks 到连接设备
let install_apks_cmd = `${bundletool_jar_path} install-apks --adb ${adb_path} --apks=${apks_file}`;
let workerProcess = exec(install_apks_cmd, (errE, stdout) => {
if (null !== errE) {
let errorMsg = `安装 apks 文件到设备时出错,错误信息:${errE}`;
sendMsgToUI(event, errorMsg);
tipsInstallError(errorMsg);
return;
}
sendMsgToUI(event, `已成功将 aab 程序包安装到设备中\n`);
autoStartApplication(event, aabInfo);
});
}
/**
* 第六步:自动启动刚安装完的应用程序
*
* @param {Electron.IpcMainEvent} event
* @param {AabInfo} aabInfo
*/
function autoStartApplication(event, aabInfo) {
sendMsgToUI(event, `6、正在尝试自动启动应用,包名:${aabInfo.pkg}, 应用版本信息:${aabInfo.getAppVersionInfo()}`);
let autoStartActivity = aabInfo.getAutoStartActivity();
if (null == autoStartActivity) {
sendMsgToUI(event, '抱歉无法自动识别打开应用,你可以手动打开应用进行测试');
tipsInstallFinish(false, aabInfo);
return;
}
// aab 路径
let adb_path = getAdbPath();
// 4、启动刚才安装好的应用
let start_app_cmd = `${adb_path} shell am start -n "${aabInfo.getAutoStartActivity()}" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER`;
let workerProcess = exec(start_app_cmd, (errE, stdout) => {
if (null !== errE) {
sendMsgToUI(event, `尝试自动启动应用出错,错误信息:${errE}`);
tipsInstallFinish(false, aabInfo);
return;
}
sendMsgToUI(event, `应用已自动启动,可以开始进行测试验收~~~\n`);
tipsInstallFinish(true, aabInfo);
});
}
/**
* 弹出安装失败的提示框,利于给使用人员强制的提示
*
* @param {*} msg
*/
function tipsInstallError(msg) {
var options = {
type: 'error',
title: '安装出错',
icon: nativeImage.createEmpty(),
message: msg
}
dialog.showMessageBox(options);
}
/**
* 使用强烈的对话框提示已经完成安装
*
* @param {Boolean} is_auto_start
* @param {AabInfo} aabInfo
*/
function tipsInstallFinish(is_auto_start, aabInfo) {
let version_info = `(应用包名:${aabInfo.pkg}, 版本信息:${aabInfo.getAppVersionInfo()})`;
var options = {
type: 'info',
title: '温馨提示',
icon: nativeImage.createEmpty(),
message: is_auto_start ?
`安装成功,应用已自动启动,可以开始进行测试验收~~~\n${version_info}` :
`抱歉无法自动识别打开应用,你可以手动打开应用进行测试\n${version_info}`
}
dialog.showMessageBox(options);
}
// 判断是否是 window 平台
function isWinOS() {
let os_info = process.platform;
if (os_info.startsWith('win')) {
return true;
}
return false;
}
function log(log_str) {
if (log_str == null || log_str == '') {
return;
}
console.log(log_str);
}
// aab 文件信息类
class AabInfo {
constructor(pkg_v, vname_v, vcode_v) {
this.pkg = pkg_v;
this.vname = vname_v;
this.vcode = vcode_v;
}
getAppVersionInfo() {
return `${this.vname}.${this.vcode}`;
}
/**
* 获取签名文件名,放在 assets 目录下
*/
getKeystoreName() {
// 可以针对不同应用使用不同的签名文件
if (this.pkg == 'com.fireantzhang.aabdemo') {
return 'release.jks';
}
return 'release.jks'
}
/**
* 获取签名配置信息
*/
getKeystoreConfig() {
if (this.pkg == 'com.fireantzhang.aabdemo') {
return new KeystoreConfig('fireantzhang', 'fireantzhang', 'fireantzhang');
}
return new KeystoreConfig('fireantzhang', 'fireantzhang', 'fireantzhang');
}
/**
* 获取启动的 activity,TODO:调整成直接从清单文件中读取,不过逻辑有点复杂,暂时未实现
*/
getAutoStartActivity() {
if (this.pkg == 'com.fireantzhang.aabdemo') {
return 'com.fireantzhang.aabdemo/com.fireantzhang.aabdemo.MainActivity';
}
return null;
}
}
// 签名节本信息类
class KeystoreConfig {
constructor(ks_pass_v, alias_v, key_pass_v) {
this.ks_pass = ks_pass_v;
this.alias = alias_v;
this.key_pass = key_pass_v;
}
}