diff --git a/plugin/lib/quickcommand.js b/plugin/lib/quickcommand.js index f033b2b..1bb294d 100644 --- a/plugin/lib/quickcommand.js +++ b/plugin/lib/quickcommand.js @@ -3,6 +3,8 @@ const electron = require("electron"); const fs = require("fs"); const kill = require("tree-kill"); const iconv = require("iconv-lite"); +const path = require("path"); + const getQuickcommandTempFile = require("./getQuickcommandTempFile"); const getCommandToLaunchTerminal = require("./getCommandToLaunchTerminal"); @@ -179,7 +181,7 @@ if (process.platform === "win32") { quickcommand.runVbs = function (script) { return new Promise((reslove, reject) => { var tempfile = getQuickcommandTempFile("vbs", "TempVBSScript"); - fs.writeFile(tempfile, iconv.encode(script, "gbk"), (err) => { + fs.writeFile(tempfile, iconv.encode(script, "gbk"), () => { child_process.exec( `cscript.exe /nologo "${tempfile}"`, { @@ -189,6 +191,7 @@ if (process.platform === "win32") { (err, stdout, stderr) => { if (err) reject(iconv.decode(stderr, "gbk")); else reslove(iconv.decode(stdout, "gbk")); + fs.unlink(tempfile, () => {}); } ); }); @@ -211,6 +214,53 @@ if (process.platform === "win32") { ); }); }; + + // 运行C#脚本 + quickcommand.runCsharp = function (script) { + return new Promise((reslove, reject) => { + // 找到csc.exe + let cscPath = path.join( + process.env.WINDIR, + "Microsoft.NET", + "Framework", + "v4.0.30319", + "csc.exe" + ); + if (!fs.existsSync(cscPath)) { + cscPath = path.join( + process.env.WINDIR, + "Microsoft.NET", + "Framework", + "v3.5", + "csc.exe" + ); + } + if (!fs.existsSync(cscPath)) { + return reject("未安装.NET Framework"); + } + // 写入临时文件 + let tempCsharpFile = getQuickcommandTempFile("cs", "TempCsharpScript"); + let tempBuildFile = getQuickcommandTempFile("exe", "TempCsharpBuildExe"); + + fs.writeFile(tempCsharpFile, iconv.encode(script, "gbk"), (err) => { + if (err) return reject(err.toString()); + // 运行csc.exe + child_process.exec( + `${cscPath} /nologo /out:${tempBuildFile} ${tempCsharpFile} && ${tempBuildFile}`, + { + encoding: "buffer", + windowsHide: true, + }, + (err, stdout) => { + if (err) reject(iconv.decode(stdout, "gbk")); + else reslove(iconv.decode(stdout, "gbk")); + fs.unlink(tempCsharpFile, () => {}); + fs.unlink(tempBuildFile, () => {}); + } + ); + }); + }); + }; } if (process.platform === "darwin") { diff --git a/src/plugins/monaco/types/quickcommand.api.d.ts b/src/plugins/monaco/types/quickcommand.api.d.ts index f706505..90a340f 100644 --- a/src/plugins/monaco/types/quickcommand.api.d.ts +++ b/src/plugins/monaco/types/quickcommand.api.d.ts @@ -342,6 +342,29 @@ interface quickcommandApi { */ runPowerShell(script: string): Promise; + /** + * 运行C#脚本,并返回运行结果 (Promise) + * + * 需要安装.NET Framework + * + * ```js + * const script = `using System; + * class Program { + * static void Main(string[] args) { + * Console.WriteLine("Hello, World!"); + * } + * }` + * quickcommand.runCsharp(script).then(res => { + * console.log(res) + * }).catch(err => { + * console.log(err) + * }) + * ``` + * + * @param script C# 脚本代码 + */ + runCsharp(script: string): Promise; + /** * MacOS 下运行 AppleScript 脚本并返回运行结果 (Promise) *