第一次运行插件时编译

This commit is contained in:
fofolee 2020-05-23 15:12:07 +08:00
parent 232c2435bd
commit 3e43317d16

View File

@ -6,18 +6,44 @@ const fs = require('fs');
const process = require('process'); const process = require('process');
const { clipboard, shell } = require('electron') const { clipboard, shell } = require('electron')
isDev = /unsafe-\w+\.asar/.test(__dirname) ? false : true isDev = /[a-zA-Z0-9\-]+\.asar/.test(__dirname) ? false : true
basename = path.basename basename = path.basename
copy = clipboard.writeText copy = clipboard.writeText
open = shell.showItemInFolder open = shell.showItemInFolder
GetBinPath = ExeFile => { GetFilePath = file => {
if (isDev) { if (isDev) {
return path.join(__dirname, 'bin', ExeFile) return path.join(__dirname, 'bin', file)
} else { } else {
return path.join(__dirname.replace(/(unsafe-\w+\.asar)/,'$1.unpacked'), 'bin', ExeFile) return path.join(__dirname.replace(/([a-zA-Z0-9\-]+\.asar)/,'$1.unpacked'), 'bin', file)
}
}
compileFile = script => {
// utools.showNotification('插件初始化中...')
var compilerPath;
var scriptPath = GetFilePath(script)
var scriptDir = path.dirname(scriptPath)
var dotnet3 = 'C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe'
var dotnet4 = 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe'
if (fs.existsSync(dotnet4)) {
compilerPath = dotnet4
} else {
if (fs.existsSync(dotnet3)) {
compilerPath = dotnet3
} else {
utools.showNotification('插件初始化失败,需要安装.net framework 4.0')
utools.outPlugin();
return
}
}
try {
execSync(`pushd ${scriptDir} && ${compilerPath} ${script}`)
} catch (error) {
utools.showNotification('插件初始化失败请下载upx版进行手动安装')
utools.outPlugin();
} }
} }
@ -30,8 +56,10 @@ tasklist = () =>
{ {
var tasklist = []; var tasklist = [];
if (isWin) { if (isWin) {
execFile(GetBinPath('ProcessKiller.exe'), ['getProcess'], { encoding: 'buffer' },(err, stdout, stderr) => { var binPath = GetFilePath('ProcessKiller.exe');
err && reject(iconv.decode(stderr, 'gb18030')); if(!fs.existsSync(binPath)) compileFile('ProcessKiller.cs')
execFile(binPath, ['getProcess'], { encoding: 'buffer' },(err, stdout, stderr) => {
if(err) reject(utools.showNotification(iconv.decode(stderr, 'gb18030')));
data = JSON.parse(iconv.decode(stdout, 'gb18030')); data = JSON.parse(iconv.decode(stdout, 'gb18030'));
data = data.sort((x,y) => { data = data.sort((x,y) => {
return y.WorkingSet - x.WorkingSet; return y.WorkingSet - x.WorkingSet;
@ -40,6 +68,7 @@ tasklist = () =>
}) })
} else { } else {
exec('ps -A -o pid -o %cpu -o %mem -o user -o comm | sed 1d | sort -rnk 3', (err, stdout, stderr) => { exec('ps -A -o pid -o %cpu -o %mem -o user -o comm | sed 1d | sort -rnk 3', (err, stdout, stderr) => {
if(err) reject(utools.showNotification(stderr))
lines = stdout.split('\n'); lines = stdout.split('\n');
lines.forEach(line => { lines.forEach(line => {
if (line) { if (line) {
@ -61,8 +90,7 @@ taskkill = (pid, restart) =>
try { try {
process.kill(pid); process.kill(pid);
} catch (error) { } catch (error) {
utools.showNotification('权限不足请以管理员权限运行uTools'); reject(utools.showNotification('权限不足请以管理员权限运行uTools'));
reject(error);
} }
if(restart){ if(restart){
utools.showNotification('重启进程成功!') utools.showNotification('重启进程成功!')
@ -95,8 +123,8 @@ GetIcons = PathList =>
new Promise((reslove, reject) => { new Promise((reslove, reject) => {
if (isWin) { if (isWin) {
PathList = PathList.join("|").replace("\\", "/"); PathList = PathList.join("|").replace("\\", "/");
execFile(GetBinPath('ProcessKiller.exe'), ["getIcons", PathList],{ encoding: 'buffer' },(error, stdout, stderr) => { execFile(GetFilePath('ProcessKiller.exe'), ["getIcons", PathList],{ encoding: 'buffer' },(error, stdout, stderr) => {
error && reject(iconv.decode(stderr, 'gb18030')); if(error) reject(utools.showNotification(iconv.decode(stderr, 'gb18030')));
data = JSON.parse(iconv.decode(stdout, 'gb18030')); data = JSON.parse(iconv.decode(stdout, 'gb18030'));
reslove(data); reslove(data);
}); });