From 54809dcff387c5042d320547c642dd13bb84a8ad Mon Sep 17 00:00:00 2001 From: Timon Stampfli Date: Thu, 5 Nov 2020 14:35:47 +0100 Subject: [PATCH] fixing nested async return issue --- src/runner.ts | 280 ++++++++++++++++++++++++++------------------------ 1 file changed, 145 insertions(+), 135 deletions(-) diff --git a/src/runner.ts b/src/runner.ts index 50fae5a..6e48ed7 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -45,161 +45,171 @@ export class Runner { } async compile(this: Runner, taskname: string): Promise<"Success" | "Failed"> { - this.store.logInfo("compiler", `starting the compilation of ${taskname}`); - const workspace = getWorkspaceOrShowError(this.store).workspaceDir; - const soitaskFiles = getSoitaskFilesOfFulltaskOrError(this.store, taskname); + return new Promise(async resolve => { + this.store.logInfo("compiler", `starting the compilation of ${taskname}`); + const workspace = getWorkspaceOrShowError(this.store).workspaceDir; + const soitaskFiles = getSoitaskFilesOfFulltaskOrError( + this.store, + taskname + ); - const compilerPath = getCompilerPath(this.store); + const compilerPath = getCompilerPath(this.store); - let finished: vscode.Disposable[] = []; + let finished: vscode.Disposable[] = []; + let finishedTimeout: vscode.Disposable[] = []; - let timer = new Timer(10000); - finished.push(timer); - const cleanup = () => { - for (let d of finished) { - d.dispose(); - } - }; + let timer = new Timer(10000); + finished.push(timer); + const cleanup = (abort?: boolean) => { + for (let d of finished) { + d.dispose(); + } + if (abort === true) { + for (let d of finishedTimeout) { + d.dispose(); + } + } + }; - try { - await fs.readFile(compilerPath); - } catch (err) { - vscode.window.showErrorMessage( - "Compiler executable not found at expected path" - ); - this.store.logError( - "compilation", - `file at compilerpath '${compilerPath}' not found`, - err - ); - cleanup(); - return "Failed"; - } - try { - await fs.unlink(soitaskFiles.binaryFsPath); - } catch (err) { - if (err.code === "ENOENT") { - this.store.logInfo( - "compilation", - `compiled binary '${soitaskFiles.binaryFsPath}' not found`, - err + try { + await fs.readFile(compilerPath); + } catch (err) { + vscode.window.showErrorMessage( + "Compiler executable not found at expected path" ); - } else { - cleanup(); - this.store.handleError( + this.store.logError( "compilation", - `deleting binary '${soitaskFiles.binaryFsPath}' failed`, + `file at compilerpath '${compilerPath}' not found`, err ); + cleanup(true); + resolve("Failed"); } - } - - const compileTaskDefinition = { - label: "build " + taskname + " development", - // "command": "clang++", - // "args": , - type: "process", - group: { - kind: "build", - isDefault: true + try { + await fs.unlink(soitaskFiles.binaryFsPath); + } catch (err) { + if (err.code === "ENOENT") { + this.store.logInfo( + "compilation", + `compiled binary '${soitaskFiles.binaryFsPath}' not found`, + err + ); + } else { + cleanup(true); + this.store.handleError( + "compilation", + `deleting binary '${soitaskFiles.binaryFsPath}' failed`, + err + ); + } } - }; - const headerAsFlag = (path: string) => (path === "" ? [] : ["-I", path]); + const compileTaskDefinition = { + label: "build " + taskname + " development", + // "command": "clang++", + // "args": , + type: "process", + group: { + kind: "build", + isDefault: true + } + }; + + const headerAsFlag = (path: string) => (path === "" ? [] : ["-I", path]); + + // Todo: allow customizable flags + const execute = new vscode.ProcessExecution( + compilerPath, + [ + soitaskFiles.solutionWorkspacePath!, + ...getCompilationFlags(this.store), + ...headerAsFlag(getCppHeaderPath(this.store)), + ...headerAsFlag(getSoiHeaderPath(this.store)), + "-o", + taskname + ], + { + cwd: workspace + } + ); - // Todo: allow customizable flags - const execute = new vscode.ProcessExecution( - compilerPath, - [ - soitaskFiles.solutionWorkspacePath!, - ...getCompilationFlags(this.store), - ...headerAsFlag(getCppHeaderPath(this.store)), - ...headerAsFlag(getSoiHeaderPath(this.store)), - "-o", - taskname - ], - { - cwd: workspace + // let compileTask = new vscode.Task(compileTaskDefinition, vscode.TaskScope.Workspace, "compile" + taskname, "c++", execute, "$gcc"); + let compileTask = new vscode.Task( + compileTaskDefinition, + vscode.TaskScope.Workspace, + "compile" + taskname, + "soic", + execute + ); + try { + let compileTaskExec = vscode.tasks.executeTask(compileTask); + compileTaskExec.then(e => { + this.store.logInfo("compiler", `finished vscode.task.executeTask`); + }); + finishedTimeout.push( + new vscode.Disposable(() => { + this.store.logError( + "compiler", + "aborted compilation, killing process" + ); + compileTaskExec.then(e => { + e.terminate(); + }); + }) + ); + } catch (err) { + this.store.logError("compilation", "executing task failed", err); + cleanup(true); + resolve("Failed"); } - ); - // let compileTask = new vscode.Task(compileTaskDefinition, vscode.TaskScope.Workspace, "compile" + taskname, "c++", execute, "$gcc"); - let compileTask = new vscode.Task( - compileTaskDefinition, - vscode.TaskScope.Workspace, - "compile" + taskname, - "soic", - execute - ); - try { - let compileTaskExec = vscode.tasks.executeTask(compileTask); - compileTaskExec.then(e => { - this.store.logInfo("compiler", `finished vscode.task.executeTask`); - }); finished.push( - new vscode.Disposable(() => { - this.store.logError( - "compiler", - "aborted compilation, killing process" - ); - compileTaskExec.then(e => { - e.terminate(); - }); + vscode.tasks.onDidEndTask(async taskEnded => { + if (taskEnded.execution.task.name === "compile" + taskname) { + try { + await fs.readFile(soitaskFiles.binaryFsPath); + cleanup(); + resolve("Success"); + } catch (err) { + this.store.logError( + "compilation", + `reading binary '${soitaskFiles.binaryFsPath}" failed`, + err + ); + if (err.code !== "EEXIST") { + cleanup(true); + resolve("Failed"); + } + } + } }) ); - } catch (err) { - this.store.logError("compilation", "executing task failed", err); - cleanup(); - return "Failed"; - } - - finished.push( - vscode.tasks.onDidEndTask(taskEnded => { - if (taskEnded.execution.task.name === "compile" + taskname) { - try { - fs.readFile(soitaskFiles.binaryFsPath).then(() => { - cleanup(); - return "Success"; - }); - } catch (err) { - this.store.logError( - "compilation", - `reading binary '${soitaskFiles.binaryFsPath}" failed`, - err + finished.push( + vscode.tasks.onDidStartTaskProcess(evtStartProc => { + if (evtStartProc.execution.task.name === compileTask.name) { + this.store.logInfo( + "compiler", + `compiling started successfully with pid '${evtStartProc.processId}'` ); - if (err.code !== "EEXIST") { - cleanup(); - return "Failed"; - } } - } - }) - ); - finished.push( - vscode.tasks.onDidStartTaskProcess(evtStartProc => { - if (evtStartProc.execution.task.name === compileTask.name) { - this.store.logInfo( - "compiler", - `compiling started successfully with pid '${evtStartProc.processId}'` - ); - } - }) - ); - finished.push( - vscode.tasks.onDidEndTaskProcess(evtEndProc => { - if (evtEndProc.execution.task.name === compileTask.name) { - this.store.logInfo( - "compiler", - `compiling ended successfully with exit code '${evtEndProc.exitCode}'` - ); - } - }) - ); + }) + ); + finished.push( + vscode.tasks.onDidEndTaskProcess(evtEndProc => { + if (evtEndProc.execution.task.name === compileTask.name) { + this.store.logInfo( + "compiler", + `compiling ended successfully with exit code '${evtEndProc.exitCode}'` + ); + } + }) + ); - await timer.timer; - this.store.logError("compilation", `timed out compilation`); - cleanup(); - return "Failed"; + await timer.timer; + this.store.logError("compilation", `timed out compilation`); + cleanup(true); + resolve("Failed"); + }); } async askAndCompile( -- GitLab