runner.ts 7.84 KB
Newer Older
1
import * as vscode from "vscode";
2 3 4 5
import { selectTaskDefault } from "./soitask";
import {
	getCompilerPath,
	getDebuggerPath,
6 7 8 9
	getDebuggerFlavor,
	getCompilationFlags,
	getCppHeaderPath,
	getSoiHeaderPath
10
} from "./compilerBundle";
11 12 13 14 15

import { getHelperPath } from "./helperBinary";

import * as fs from "fs-extra";
import { Store } from "./store";
16 17 18 19 20 21
import { taskTreeResourceEntry } from "./taskTreeView";
import {
	getWorkspaceOrShowError,
	getSoitaskFilesOfFulltaskOrError
} from "./storeHelper";
import { selectSample } from "./sample";
22 23

export class Runner {
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
	store: Store;
	port: Number;

	constructor(store: Store, port: Number) {
		this.store = store;
		this.port = port;
	}

	async compile(this: Runner, taskname: string): Promise<"Success" | "Failed"> {
		return new Promise(async resolve => {
			let workspace = getWorkspaceOrShowError(this.store).workspaceDir;
			let soitaskFiles = getSoitaskFilesOfFulltaskOrError(this.store, taskname);

			let compilerPath = getCompilerPath(this.store);

			try {
				await fs.readFile(compilerPath);
			} catch {
				vscode.window.showErrorMessage(
					"Compiler executable could not be found at it's expected path"
				);
				return;
			}
			let compileTaskDefinition = {
				label: "build " + taskname + " development",
				// "command": "clang++",
				// "args": ,
51
				type: "process",
52 53 54 55 56
				group: {
					kind: "build",
					isDefault: true
				}
			};
57 58

			let headerAsFlag = (path: string) => (path === "" ? [] : ["-I", path]);
59

60
			// Todo: allow customizable flags
61
			let execute = new vscode.ProcessExecution(
62
				compilerPath,
63 64 65
				[
					soitaskFiles.solutionWorkspacePath!,
					...getCompilationFlags(this.store),
66 67
					...headerAsFlag(getCppHeaderPath(this.store)),
					...headerAsFlag(getSoiHeaderPath(this.store)),
68 69
					"-o",
					taskname
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
				{
					cwd: workspace
				}
			);

			try {
				await fs.unlink(soitaskFiles.binaryFsPath);
			} catch (err) {
				if (err.code !== "ENOENT") {
					throw err;
				}
			}

			// 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 {
				await vscode.tasks.executeTask(compileTask);
			} catch (err) {
				resolve("Failed");
			}

			vscode.tasks.onDidEndTask(taskEnded => {
				if (taskEnded.execution.task.name === "compile" + taskname) {
					try {
						fs.readFile(soitaskFiles.binaryFsPath).then(() => {
							resolve("Success");
						});
					} catch (err) {
						if (err.code !== "EEXIST") {
							resolve("Failed");
						}
					}
				}
			});

			setTimeout(() => {
				resolve("Failed");
114
			}, 10000);
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
		});
	}

	async askAndCompile(
		this: Runner,

		taskname: string | undefined | vscode.Uri | taskTreeResourceEntry
	) {
		let task = await selectTaskDefault(this.store, taskname);

		if ((await this.compile(task)) === "Failed") {
			vscode.window.showErrorMessage("Compilation failed");
			return;
		}
	}

	async runTask(
		this: Runner,
		taskname: string,
		sampleInfo: string,
		sampleargs: string[]
	) {
		if ((await this.compile(taskname)) === "Failed") {
			vscode.window.showErrorMessage("Compilation failed");
			return;
		}

		let workspace = getWorkspaceOrShowError(this.store).workspaceDir;

		let helperPath = getHelperPath(this.store);
		try {
			await fs.readFile(helperPath);
		} catch {
			vscode.window.showErrorMessage(
				"Helper binary could not be found at it's expected path"
			);
			return;
		}

		let runTaskDefinition = {
			type: "shell",
			// "command": "clang++",
			// "args": ,
			group: {
				kind: "run",
				isDefault: true
			}
		};
		// Todo: allow customizable flags
		let execute = new vscode.ShellExecution(
			helperPath,
166
			sampleargs.concat(["--port", "" + this.port]),
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
			{
				env: {
					SOI_H_EOFCHECK: "1"
				},
				cwd: workspace
			}
		);
		let runTask = new vscode.Task(
			runTaskDefinition,
			vscode.TaskScope.Workspace,
			"run " + taskname + " " + sampleInfo,
			"soicode run",
			execute
		);
		try {
			await vscode.tasks.executeTask(runTask);
		} catch (err) {
			vscode.window.showErrorMessage("Error running sample: " + err.toString());
		}
	}

	async runWithManualInput(
		this: Runner,
		taskname: string | undefined | vscode.Uri | taskTreeResourceEntry
	) {
192 193 194 195 196
		taskname = await selectTaskDefault(this.store, taskname);
		let soitaskFile = getSoitaskFilesOfFulltaskOrError(this.store, taskname);
		await this.runTask(taskname, "User Input", [
			"runWithManualInput",
			"--binaryPath",
197
			soitaskFile.binaryWorkspacePath
198
		]);
199 200 201 202 203
	}
	async runAllSamples(
		this: Runner,
		taskname: string | undefined | vscode.Uri | taskTreeResourceEntry
	) {
204 205
		taskname = await selectTaskDefault(this.store, taskname);
		let soitaskFile = getSoitaskFilesOfFulltaskOrError(this.store, taskname);
206

207
		await this.runTask(taskname, "All Samples", [
208
			"runAllSamples",
209
			"--binaryPath",
210
			soitaskFile.binaryWorkspacePath,
211
			"--soitaskPath",
212
			soitaskFile.soitaskWorkspacePath!
213 214 215 216 217 218 219
		]);
	}
	async runOneSample(
		this: Runner,
		taskname: string | undefined | vscode.Uri,
		samplename: string | undefined
	) {
220 221
		taskname = await selectTaskDefault(this.store, taskname);
		let soitaskFiles = getSoitaskFilesOfFulltaskOrError(this.store, taskname);
222

223
		let sample = await selectSample(this.store, taskname, samplename);
224

225 226 227
		await this.runTask(taskname, "sample", [
			"runOneSample",
			"--binaryPath",
228
			soitaskFiles.binaryWorkspacePath,
229
			"--soitaskPath",
230
			soitaskFiles.soitaskWorkspacePath!,
231
			"--sampleName",
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
			sample
		]);
	}

	async debug(
		this: Runner,
		taskname: string,
		extraconfig: any
	): Promise<"Successful" | "Failed"> {
		if ((await this.compile(taskname)) === "Failed") {
			vscode.window.showErrorMessage("Compilation failed");
			return "Failed";
		}

		let workspace = getWorkspaceOrShowError(this.store).workspaceDir;
		let soitaskFiles = getSoitaskFilesOfFulltaskOrError(this.store, taskname);
		let debuggerPath = getDebuggerPath(this.store);
249
		let debuggerFlavor = getDebuggerFlavor(this.store);
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

		if (debuggerPath === null) {
			vscode.window.showErrorMessage("Debuggerpath could not be determined");
			return "Failed";
		}
		try {
			await fs.readFile(debuggerPath);
		} catch {
			vscode.window.showErrorMessage(
				"Debugger executable could not be found at it's expected path"
			);
			return "Failed";
		}

		if (debuggerFlavor === null) {
			vscode.window.showErrorMessage("Couldn't determine the type of debugger");
			return "Failed";
		}

		let debugConfig = Object.assign(
			{
				name: "Debug " + taskname,
				type: "cppdbg",
				request: "launch",
				program: soitaskFiles.binaryFsPath,
				MiMode: debuggerFlavor,
				cwd: workspace,
				setupCommands: [
					{
						description: "Enable pretty-printing for debugger",
						text: "-enable-pretty-printing",
						ignoreFailures: true
					}
				]
			},
			extraconfig
		);

		try {
			await fs.readFile(soitaskFiles.binaryFsPath);
		} catch (err) {
			if (err.code !== "EEXIST") {
				vscode.window.showErrorMessage(
					"Unknown error occured when checking binary: " + err.toString()
				);
				throw err;
			} else {
				vscode.window.showErrorMessage(
					"The binary you try to debug doesn't exist" + err.toString()
				);
			}
		}
		let workspaceFolder = vscode.workspace.getWorkspaceFolder(
			vscode.Uri.file(soitaskFiles.binaryFsPath)
		);

		console.log("Debug parameters:", workspaceFolder, debugConfig);

		let startedSuccessfully = await vscode.debug.startDebugging(
			workspaceFolder,
			debugConfig
		);
		if (startedSuccessfully) {
			return "Successful";
		} else {
			return "Failed";
		}
	}

	async askAndDebugManually(
		this: Runner,
		taskname: string | undefined | vscode.Uri | taskTreeResourceEntry
	) {
		let task = await selectTaskDefault(this.store, taskname);
		if ((await this.debug(task, {})) === "Failed") {
			vscode.window.showErrorMessage("Debugging failed");
			return;
		}
	}
}