修改安装进度;解决安装文件log重复监听

This commit is contained in:
dinglili 2024-01-18 17:08:33 +08:00 committed by wangpenglong
parent 3bdfc815e5
commit 8ea50378d9
5 changed files with 250 additions and 134 deletions

View File

@ -1339,6 +1339,9 @@ export class CodeApplication extends Disposable {
let extMap: Record<string, string> = {};
let depList: string[] = [];
let extList: string[] = [];
let installFsWatcher: fs.FSWatcher;
let extlogFsWatcher: fs.FSWatcher;
let pkglogFsWatcher: fs.FSWatcher;
// let scriptList: {}[] = [];
//electron生命周期 监听did-finish-load事件
this.win.webContents.on('did-finish-load', async function () {
@ -1368,6 +1371,7 @@ export class CodeApplication extends Disposable {
ipcMain.removeAllListeners('kylinide.installWizard.showErrorBox');
ipcMain.removeAllListeners('kylinide.installWizard.init');
ipcMain.removeAllListeners('kylinide.installWizard.justDownLoad');
ipcMain.removeAllListeners('kylinide.installWizard.removeFileLister');
//读取selectfile.json中selecdir的值如果值不同则删除当前的时间戳目录
if (updateConfigDir) {
console.log("updateConfig:" + updateConfigDir)
@ -1644,6 +1648,8 @@ export class CodeApplication extends Disposable {
}
}
//检查软件包安装进度
let offset = 0;
let resultIndex = 0;
ipcMain.on('kylinide.installWizard.checkInstall', async (event, args) => {
console.log("kylinide.installWizard.checkInstall");
if (startInstallDepWatch === 1) return;
@ -1653,7 +1659,7 @@ export class CodeApplication extends Disposable {
fs.mkdirSync(installConfig, { recursive: true });
fs.writeFileSync(logFile0, '');
}
//监听install pkg输出信息
fs.watch(logFile0, (eventType, filename) => {
if (eventType === 'change') {
// 获取文件的当前大小
@ -1696,100 +1702,161 @@ export class CodeApplication extends Disposable {
});
//检测ext 安装信息文件变化
extlogFsWatcher =
fs.watch(extLogFile0, (eventType, filename) => {
if (eventType === 'change') {
// 获取文件的当前大小
const stats = fs.statSync(extLogFile0);
const currentSize = stats.size;
fs.watch(extLogFile0, (eventType, filename) => {
if (eventType === 'change') {
// 获取文件的当前大小
const stats = fs.statSync(extLogFile0);
const currentSize = stats.size;
// 计算更新部分的大小
const updateSize = currentSize - lastExtLogPosition;
if (updateSize <= 0) return;
// 读取更新部分的内容
const buffer = Buffer.alloc(updateSize);
const fileDescriptor = fs.openSync(extLogFile0, 'r');
fs.readSync(fileDescriptor, buffer, 0, updateSize, lastExtLogPosition);
fs.closeSync(fileDescriptor);
// 计算更新部分的大小
const updateSize = currentSize - lastExtLogPosition;
if (updateSize <= 0) return;
// 读取更新部分的内容
const buffer = Buffer.alloc(updateSize);
const fileDescriptor = fs.openSync(extLogFile0, 'r');
fs.readSync(fileDescriptor, buffer, 0, updateSize, lastExtLogPosition);
fs.closeSync(fileDescriptor);
// 将更新部分的内容转换为字符串并输出
const updatedContent = buffer.toString('utf8');
// 将更新部分的内容转换为字符串并输出
const updatedContent = buffer.toString('utf8');
// 更新上一次读取的位置
lastExtLogPosition = currentSize;
const installMsg = {
type: "extInstallLogPre",
data: updatedContent
};
// 更新上一次读取的位置
lastExtLogPosition = currentSize;
const installMsg = {
type: "extInstallLogPre",
data: updatedContent
};
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", installMsg)
}
});
let sendindex = 0;
let succ = 0;
// 监测 resultFile 文件的变化
fs.watch(resultFile, (eventType, filename) => {
if (filename) {
// 创建可读流
const stream = fs.createReadStream(resultFile, 'utf8');
let data = '';
const installMsg = {
type: "depStartInit",
depName: depList[sendindex],
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", installMsg);
}
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", installMsg);
});
// let sendindex = 0;
// let succ = 0;
// 监测 resultFile 文件的变化
// fs.watch(resultFile, (eventType, filename) => {
// if (filename) {
// // 创建可读流
// const stream = fs.createReadStream(resultFile, 'utf8');
// let data = '';
// stream.on('data', chunk => {
// data += chunk;
// // 在这里可以对文件的内容进行处理
// const lines: string[] = []; // 存储每行内容的列表
// const linesArray = data.split('\n');
// linesArray.forEach(line => {
// if (line)
// lines.push(line);
// });
// let installMsg;
// if (lines[lines.length - 1] === '0') {
// succ++;
// installMsg = {
// type: "depInstallSucc",
// depName: depList[lines.length - 1],
// result: lines,
// succ: succ
// };
// } else {
// installMsg = {
// type: "depInstallFail",
// depName: depList[lines.length - 1],
// result: lines
// };
// }
// if (that.win)
// that.win.webContents.send("kylinide.installWizard.msg", installMsg);
// // 监听 end 事件,表示文件读取完毕
// stream.on('end', () => {
// // 在这里可以对文件的内容进行处理
// // console.log(`resultFile 文件的最终内容:${data}`);
// });
stream.on('data', chunk => {
data += chunk;
// 在这里可以对文件的内容进行处理
const lines: string[] = []; // 存储每行内容的列表
const linesArray = data.split('\n');
linesArray.forEach(line => {
if (line)
lines.push(line);
});
// // 监听 error 事件,处理读取文件时的错误
// stream.on('error', err => {
// console.error(`读取 resultFile 文件时发生错误:${err}`);
// });
// });
// }
// });
installFsWatcher = fs.watch(resultFile, (evenType, filename) => {
if (evenType === "change") {
fs.readFile(resultFile, "utf8", (err, data) => {
if (err) {
console.error(`读取 resultFile 文件时发生错误:${err}`);
return;
}
// 计算文件变化的部分的偏移量
const newOffset = data.length;
// 如果文件没有发生变化,直接返回
if (newOffset === offset) {
return;
}
// 读取文件变化的部分
const changeData = data.slice(offset);
// 更新文件偏移量
offset = newOffset;
let result = changeData.trim();
// 处理文件变化的部分
console.log(`result ${resultIndex + 1} 结果为 ${result}`);
//每个dep的安装状态
let installMsg;
if (lines[lines.length - 1] === '0') {
succ++;
if (result === '') {
//此处是因为每次清零的时候会监听到改变
console.log(`result ${resultIndex + 1} 结果为空。`);
resultIndex = 0;
return;
} else if (result === '0') {
installMsg = {
type: "depInstallSucc",
depName: depList[lines.length - 1],
result: lines,
succ: succ
depName: depList[resultIndex],
result: "0",
succ: resultIndex
};
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", installMsg);
} else {
installMsg = {
type: "depInstallFail",
depName: depList[lines.length - 1],
result: lines
depName: depList[resultIndex],
result: "0",
succ: resultIndex //失败
};
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", installMsg);
}
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", installMsg);
// 监听 end 事件,表示文件读取完毕
stream.on('end', () => {
// 在这里可以对文件的内容进行处理
// console.log(`resultFile 文件的最终内容:${data}`);
});
// 监听 error 事件,处理读取文件时的错误
stream.on('error', err => {
console.error(`读取 resultFile 文件时发生错误:${err}`);
});
if (resultIndex == depList.length - 1) {
if (that.win)
that.win.webContents.send("kylinide.installWizard.removeFileLister", { type: "installFile" });
resultIndex = 0;
return;
}
resultIndex++;
});
}
});
startInstallDepWatch = 1;
});
ipcMain.on("kylinide.installWizard.removeFileLister", async (event, msg) => {
if (msg.type == "installFile") {
installFsWatcher.close();
} else if (msg.type == "extFile") {
extlogFsWatcher.close();
} else if (msg.type == "pkgFile") {
pkglogFsWatcher.close();
}
});
ipcMain.on('kylinide.installWizard.downloadExt', async (event, message) => {
@ -1926,7 +1993,7 @@ export class CodeApplication extends Disposable {
type: "extDownloadInit",
}
if (that.win)
that.win.webContents.send("kylinide.installWizard.msg", startDownloadExtMsg)
that.win.webContents.send("kylinide.installWizard.msg", startDownloadExtMsg);
lastExtLogPosition = 0;
fs.watch(extLogFile0, (eventType, filename) => {
@ -1970,7 +2037,7 @@ export class CodeApplication extends Disposable {
const resultFile = installConfig + '/resultFile';
const installFile = installConfig + '/install.sh';
const logFile0 = installConfig + '/log/install.1.log';
backupLogFile(logFile)
backupLogFile(logFile);
try {
fs.writeFileSync(resultFile, '');
@ -2088,6 +2155,7 @@ export class CodeApplication extends Disposable {
}
function executeScriptWithPkexec(scriptPath: string) {
console.log("executeScriptWithPkexec");
try {
const pkexecCommand = `pkexec sudo bash ${scriptPath}`;
spawn(pkexecCommand, { shell: true, stdio: 'inherit' });

View File

@ -59,7 +59,6 @@ export function modifyJsonProperty(filePath: string, propertyName: string, newVa
if (jsonData[propertyName]) {
let tmp = jsonData[propertyName];
tmp.push(...newValue);
console.log("tmp++++:" + tmp);
let tmparry = [...new Set(tmp)];
jsonData[propertyName] = tmparry;
console.log("selectKey setset:" + jsonData[propertyName]);

View File

@ -314,7 +314,9 @@ input[readonly] {
.progress-download,
.progress-install,
.pkg-progress-install {
.pkg-progress-install,
.progressinfo
{
position: absolute; /* 添加绝对定位 */
margin-left: 18%;
color: #000000;
@ -322,7 +324,9 @@ input[readonly] {
.download-view,
.install-view,
.pkg-install-view {
.pkg-install-view,
.progress-view
{
flex: 1;
height: 50px;
position: relative;

View File

@ -100,20 +100,24 @@
</div>
</div>
<div class="tab" id="onlineInstallTab4">
<div class="progress-container">
<div class="progress-container" style="display: none">
<progress class="download-view" value="60" max="100"> </progress>
<span class="progress-download">插件下载进度:60%</span>
</div>
<div class="progress-container">
<div class="progress-container" style="display: none">
<progress class="install-view" value="50" max="100"></progress>
<span class="progress-install">插件安装进度:50%</span>
</div>
<div class="progress-container">
<div class="progress-container" style="display: none">
<progress class="pkg-install-view" value="50" max="100" style="overflow:hidden;"></progress>
<span class="pkg-progress-install"></span>
</div>
<div class="progress-container">
<progress class="progress-view" value="50" max="100" style="overflow:hidden;"></progress>
<span class="progressinfo"></span>
</div>
<div class="detailInstallOutputTextbox">
<label for="detailInstallOutputTextbox">安装详情</label>

View File

@ -29,6 +29,13 @@ var firstClassList;
var gExtDownNum = 0;
var gSoftInstNum = 0;
var gExtInst = 0;
let extInstallState = new Map();
let extMap = {};
let extDownloadDoneCounter = 0;
let extInstallDoneCounter = 0;
let logShow = 0;
let progressNum = 0;
/**
* 窗口关闭按钮动作
*/
@ -168,6 +175,7 @@ ipcRenderer.on('kylinide.installWizard.init', (event, msg) => {
gExtDownNum = 0;
gSoftInstNum = 0;
gExtInst = 0;
progressNum = 0;
let depPath = msg.depInfoJsonPath;
let selectAll = document.getElementById("selectAll");
@ -340,6 +348,7 @@ function nextPrev(n) {
extInstallState.clear();
extDownloadDoneCounter = 0;
extInstallDoneCounter = 0;
progressNum = 0;
//去重
console.log("selectedCheckboxes.length:" + selectedCheckboxes.length);
@ -357,6 +366,7 @@ function nextPrev(n) {
}
}
console.log("extInstallState.size" + extInstallState.size);
if (selectedCheckboxes) {
// 遍历选中的复选框,并将它们的值添加到数组中
selectedCheckboxes.forEach(function (checkbox) {
@ -371,6 +381,9 @@ function nextPrev(n) {
document.getElementsByClassName("install-view")[0].value = 0;
document.getElementsByClassName("progress-install")[0].innerHTML = "插件安装进度:0%";
document.getElementsByClassName("progress-view")[0].value = 0;
document.getElementsByClassName("progressinfo")[0].innerHTML = "安装进度:0%";
if (depList.length === 0) {
let cname = document.getElementsByClassName("pkg-install-view")[0];
if (cname) {
@ -649,15 +662,19 @@ function setFisrtClass(depPath, jsonSelectKey) {
showDependences(depDataObject);
}
let extInstallState = new Map();
let extMap = {};
let extDownloadDoneCounter = 0;
let extInstallDoneCounter = 0;
let logShow = 0;
ipcRenderer.on("kylinide.installWizard.removeFileLister", (event, msg) => {
if (msg.type == "installFile") {
ipcRenderer.send("kylinide.installWizard.removeFileLister", { type: "installFile" })
} else if (msg.type == "extFile") {
ipcRenderer.send("kylinide.installWizard.removeFileLister", { type: "extFile" })
} else if (msg.type == "pkgFile") {
ipcRenderer.send("kylinide.installWizard.removeFileLister", { type: "pkgFile" })
}
});
ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
//数据处理
if (msg.type == "extStartDownload") {
// console.log("kylinide.installWizard.msg:extStartDownload:+" + msg.extName);
extMap = msg.extMap;
extInstallState.set(msg.extName, 1);
}
@ -682,6 +699,8 @@ ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
ipcRenderer.send("kylinide.installWizard.msg.process", installMsg);
}
else if (msg.type == "extInstalled") {
progressNum++;
const extName = getKeyByValue(msg.extFileName, extMap);
extInstallDoneCounter++;
let num = msg.extTotal;
@ -696,9 +715,11 @@ ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
document.getElementsByClassName("progress-install")[0].innerHTML = "插件安装进度:" + val + "%";
}
else if (msg.type == "extInstalledFail") {
progressNum++;
extInstallState.set(msg.extName, 3);
}
else if (msg.type == "downloadExtFail") {
progressNum++;
// const extName = getKeyByValue(msg.fileName, extMap);
console.log("kylinide.installWizard.msg:downloadExtFail: " + msg.extName);
extInstallState.set(msg.extName, 4);
@ -720,6 +741,7 @@ ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
}
}
else if (msg.type == "depInstallSucc") {
progressNum++;
let num = depList.length;
let val = document.getElementsByClassName("pkg-install-view")[0].value;
@ -738,6 +760,7 @@ ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
checkScripts(msg);
}
else if (msg.type === "depInstallFail") {
progressNum++;
extInstallState.set(msg.depName, 3);
}
else if (msg.type === "installLogPre") {
@ -760,14 +783,22 @@ ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
configList: configList
})
} else if (msg.type === "scriptExecFail") {
// console.log("kylinide.installWizard.msg:scriptExecFail");
progressNum++;
extInstallState.set(msg.depName, 16);
} else if (msg.type === "scriptExecSucc") {
// console.log("kylinide.installWizard.msg:scriptExecSucc");
progressNum++;
extInstallState.set(msg.depName, 15);
}
let dTpreElement = document.getElementById("detailInstallPre");
dTpreElement.innerHTML = '';
let dTformattedDoc = '';
document.getElementsByClassName("progress-view")[0].value = (progressNum / extInstallState.size) * 100;
document.getElementsByClassName("progressinfo")[0].innerHTML = "安装进度:" + ((progressNum / extInstallState.size) * 100).toFixed(2) + "%";
for (const [key, value] of extInstallState) {
if (key === undefined) continue;
switch (value) {
@ -804,58 +835,67 @@ ipcRenderer.on('kylinide.installWizard.msg', (event, msg) => {
}
}
dTpreElement.innerHTML = dTformattedDoc;
let flag = 0;
//检查所有处理是否已完成
for (const [key, value] of extInstallState) {
if (key === undefined) continue;
switch (value) {
case 0: flag++;
break;
case 1: flag++;
break;
case 2:
flag++;
break;
case 11:
flag++
break;
default:
// 处理默认情况
break;
}
}
if (flag === 0) {
//检查所有的开发场景是否都安装成功
let writeKeyJson = [];
let preFinish = document.getElementById("finishPage");
let showContext = preFinish.innerHTML;
showContext += "安装结束\n";
for (let key in configList) {
console.log("end:" + key);
//检查某个开发场景的软件包是否全部安装成功
let tmp = getNameFromKey(key);
if (checkInstalledSucc(key, configList, extInstallState)) {
writeKeyJson.push(key);
showContext += tmp + "安装成功\n";
} else {
showContext += tmp + "安装失败\n";
if (msg.type == "extInstalled" || msg.type == "extInstalledFail"
|| msg.type == "downloadExtFail" || msg.type == "depInstallSucc"
|| msg.type == "depInstallFail" || msg.type == "scriptExecFail" || msg.type == "scriptExecSucc") {
for (const [key, value] of extInstallState) {
if (key === undefined) continue;
switch (value) {
case 0: flag++;
break;
case 1: flag++;
break;
case 2:
flag++;
break;
case 11:
flag++
break;
default:
// 处理默认情况
break;
}
}
if (writeKeyJson.length > 0) {
ipcRenderer.send("kylinide.installWizard.init", { type: "writeKey", keys: writeKeyJson });
}
finishFlag = 0;
showContext += "点击结束按钮退出安装向导\n"
preFinish.innerHTML = showContext;
preFinish.style.display = 'block';
document.getElementById("installLogPre").style.display = 'none';
document.getElementById("detailInstallPre").style.display = 'none';
document.getElementById("extInstallLogPre").style.display = 'none';
document.getElementById("nextStep").innerText = setLanguage("finish");
document.getElementById("nextStep").disabled = false;
if (flag === 0) {
//检查所有的开发场景是否都安装成功
let writeKeyJson = [];
let preFinish = document.getElementById("finishPage");
let showContext = preFinish.innerHTML;
showContext += "安装结束\n";
for (let key in configList) {
console.log("end:" + key);
//检查某个开发场景的软件包是否全部安装成功
let tmp = getNameFromKey(key);
if (checkInstalledSucc(key, configList, extInstallState)) {
writeKeyJson.push(key);
showContext += tmp + "安装成功\n";
} else {
showContext += tmp + "安装失败\n";
}
}
if (writeKeyJson.length > 0) {
ipcRenderer.send("kylinide.installWizard.init", { type: "writeKey", keys: writeKeyJson });
}
finishFlag = 0;
showContext += "点击结束按钮退出安装向导\n"
preFinish.innerHTML = showContext;
preFinish.style.display = 'block';
document.getElementById("installLogPre").style.display = 'none';
document.getElementById("detailInstallPre").style.display = 'none';
document.getElementById("extInstallLogPre").style.display = 'none';
document.getElementById("nextStep").innerText = setLanguage("finish");
document.getElementById("nextStep").disabled = false;
logShow = 0;
ipcRenderer.send("kylinide.installWizard.removeFileLister", { type: "extFile" });
ipcRenderer.send("kylinide.installWizard.removeFileLister", { type: "pkgFile" });
}
}
// preElement.scrollTop = preElement.scrollHeight;
});
@ -987,4 +1027,5 @@ function removeAllListeners() {
ipcRenderer.removeAllListeners('kylinide.installWizard.justDownLoad');
ipcRenderer.removeAllListeners('message-to-jsb');
ipcRenderer.removeAllListeners('kylinide.installWizard.msg');
ipcRenderer.removeAllListeners('kylinide.installWizard.removeFileLister');
}