extension.ts 7.79 KB
Newer Older
1 2 3 4
"use strict";
import * as vscode from "vscode";
import { TaskTreeViewProvider } from "./taskTreeView";
import { SamplesTreeViewProvider } from "./samplesTreeView";
5

6
import { initializeWorkspace } from "./soiworkspace";
7
import { createTask, createSoitaskFile, createCppFile } from "./soitask";
8 9 10 11 12 13 14 15
import {
	insertCppTemplate,
	subscribeInsertCppTemplateLens
} from "./lenses/emptyCppLens";
import {
	subscribeInsertSoitaskTemplateLens,
	insertSoitaskTemplate
} from "./lenses/emptySoitaskLens";
16 17 18
import { Runner } from "./runner";
import { Store } from "./store";
import { downloadHelperBinary } from "./helperBinary";
Timon Stampfli's avatar
Timon Stampfli committed
19

20 21 22
import { activate as activate_stoml } from "./better-stoml/src/extension";
import { HelperBinaryResultReceiver } from "./helperBinaryResultReceiver";
import { gotoSample } from "./sample";
23

24
const port = 16314;
25

26 27 28
export async function activate(context: vscode.ExtensionContext) {
	let store = new Store(context);
	let runner = new Runner(store, port);
29

30 31
	let createTaskCommand = vscode.commands.registerCommand(
		"soicode.createTask",
32
		createTask.bind(undefined, store)
33
	);
34
	context.subscriptions.push(createTaskCommand);
Timon Stampfli's avatar
Timon Stampfli committed
35

36 37
	let initializeWorkspaceCommand = vscode.commands.registerCommand(
		"soicode.initializeWorkspace",
38
		initializeWorkspace.bind(undefined, store)
39
	);
40
	context.subscriptions.push(initializeWorkspaceCommand);
41

42 43 44 45 46
	let openSampleCommand = vscode.commands.registerCommand(
		"soicode.openSample",
		gotoSample.bind(undefined, store)
	);
	context.subscriptions.push(openSampleCommand);
47 48 49

	let runSolutionOnSampleCommand = vscode.commands.registerCommand(
		"soicode.runSolutionOnSample",
50
		runner.runOneSample.bind(runner)
51
	);
52
	context.subscriptions.push(runSolutionOnSampleCommand);
53 54 55

	let runSolutionOnAllSamplesCommand = vscode.commands.registerCommand(
		"soicode.runSolutionOnAllSamples",
56
		runner.runAllSamples.bind(runner)
57
	);
58
	context.subscriptions.push(runSolutionOnAllSamplesCommand);
59 60 61

	let runSolutionWithManualInputCommand = vscode.commands.registerCommand(
		"soicode.runSolutionWithManualInput",
62
		runner.runWithManualInput.bind(runner)
63
	);
64
	context.subscriptions.push(runSolutionWithManualInputCommand);
65 66 67 68 69

	let debugSolutionOnSampleCommand = vscode.commands.registerCommand(
		"soicode.debugSolutionOnSample",
		initializeWorkspace
	);
70
	context.subscriptions.push(debugSolutionOnSampleCommand); // Todo
71 72 73

	let debugSolutionWithManualInputCommand = vscode.commands.registerCommand(
		"soicode.debugSolutionWithManualInput",
Timon Stampfli's avatar
Timon Stampfli committed
74
		runner.askAndDebugManually.bind(runner)
75
	);
76
	context.subscriptions.push(debugSolutionWithManualInputCommand); // Todo
77 78 79

	let compileCurrentSolutionCommand = vscode.commands.registerCommand(
		"soicode.compileCurrentSolution",
80
		runner.askAndCompile.bind(runner)
81
	);
82
	context.subscriptions.push(compileCurrentSolutionCommand);
83 84 85

	let addSoitaskTemplateCommand = vscode.commands.registerCommand(
		"soicode.addSoitaskTemplate",
86
		insertSoitaskTemplate.bind(undefined)
87 88 89 90 91
	);
	context.subscriptions.push(addSoitaskTemplateCommand);

	let addCppTemplateCommand = vscode.commands.registerCommand(
		"soicode.addCppTemplate",
92
		insertCppTemplate.bind(undefined)
93 94 95 96 97
	);
	context.subscriptions.push(addCppTemplateCommand);

	let createSoitaskForSolutionCommand = vscode.commands.registerCommand(
		"soicode.createSoitaskForSolution",
98
		createSoitaskFile.bind(undefined, store)
99
	);
100
	context.subscriptions.push(createSoitaskForSolutionCommand);
101 102 103

	let createSolutionForSoitaskCommand = vscode.commands.registerCommand(
		"soicode.createSolutionForSoitask",
104
		createCppFile.bind(undefined, store)
105
	);
106
	context.subscriptions.push(createSolutionForSoitaskCommand);
107

108 109
	let taskTreeViewProvider = new TaskTreeViewProvider(store);
	let samplesTreeViewProvider = new SamplesTreeViewProvider(store);
110

111 112
	vscode.window.createTreeView("soicode-tasks", {
		treeDataProvider: taskTreeViewProvider
113
	});
114 115
	vscode.window.createTreeView("soicode-samples", {
		treeDataProvider: samplesTreeViewProvider
116 117
	});

118 119 120 121 122 123 124
	subscribeInsertCppTemplateLens(context, store);
	subscribeInsertSoitaskTemplateLens(context, store);

	let helperBinaryResultReceiver = new HelperBinaryResultReceiver(store);
	helperBinaryResultReceiver.startServer(port);

	store.emitFsChange();
125

126
	await downloadHelperBinary(store);
127

128 129
	activate_stoml(context);

130
	console.log("SOICode has been loaded");
Timon Stampfli's avatar
Timon Stampfli committed
131 132
}

133 134 135 136 137 138 139 140 141 142
// async function updateIsSOITaskContext() {
// 	let result = await vscode.workspace.findFiles(".soitask");
// 	console.log(".soitask files: ", result);
// 	let setContextResult = await vscode.commands.executeCommand(
// 		"setContext",
// 		"isSOITask",
// 		result.length >= 1
// 	);
// 	console.log(setContextResult);
// }
Timon Stampfli's avatar
Timon Stampfli committed
143

Timon Stampfli's avatar
Timon Stampfli committed
144
// this method is called when your extension is deactivated
145
export function deactivate() {}
146

147 148 149 150 151
// function sleep(ms: number) {
// 	return new Promise(resolve => {
// 		setTimeout(resolve, ms);
// 	});
// }
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
// async function createEmptyTask() {
// 	await vscode.window.setStatusBarMessage("Creating new Task");
// 	let solutionName = undefined;
// 	while (true) {
// 		solutionName = await vscode.window.showInputBox({
// 			prompt: "Please select a name for the new Task"
// 		});
// 		if (solutionName !== undefined && /^[a-z0-9A-Z]+$/.test(solutionName)) {
// 			break;
// 		}
// 		let errorMessageClicked = new Promise(async resolve => {
// 			let answer = await vscode.window.showErrorMessage(
// 				"Please Choose a name for the new task consisting of alphanumeric symbols",
// 				"Ok",
// 				"Cancel"
// 			);
// 			if (answer === undefined || answer === "Cancel") {
// 				return;
// 			}
// 			resolve();
// 		});
// 		let sleepTimedout = sleep(2000);
// 		await Promise.race([sleepTimedout, errorMessageClicked]);
// 	}
// 	let parentDir;
// 	try {
// 		parentDir = await vscode.window.showOpenDialog({
// 			openLabel: "Create new Task in this Folder",
// 			canSelectFiles: false,
// 			canSelectFolders: true,
// 			canSelectMany: false
// 		});
// 	} catch (err) {
// 		vscode.window.showErrorMessage(err);
// 		return;
// 	}

// 	if (parentDir === undefined || parentDir.length !== 1) {
// 		vscode.window.showErrorMessage("You have to select exactly one directory");
// 		return;
// 	}
// 	let solutionDir = path.join(parentDir[0].fsPath, solutionName);
// 	try {
// 		await fs.mkdir(solutionDir);
// 		const files = await fs.readdir(solutionDir);
// 		if (files.length > 0) {
// 			vscode.window.showErrorMessage("Task directory should be empty");
// 			return;
// 		}
// 	} catch {
// 		vscode.window.showErrorMessage("Directory for new task already exists");
// 	}

// 	let cppFile = path.join(solutionDir, solutionName + ".cpp");
// 	// Write files into new Workdir
// 	try {
// 		// await fs.writeFile(cppFile, cppFileContent(solutionName));
// 		// await fs.mkdir(path.join(solutionDir, ".vscode"));
// 		//await fs.mkdir(path.join(solutionDir, ".vscode", "buildDir"));
// 		// await fs.writeFile(path.join(solutionDir, ".vscode", "launch.json"), launchConfig(solutionName));
// 		// await fs.writeFile(path.join(solutionDir, ".vscode", "tasks.json"), taskJson(solutionName));
// 		// await fs.writeFile(path.join(solutionDir, ".vscode", "c_cpp_properties.json"), cCppProperties(solutionName));
// 		// await fs.writeFile(path.join(solutionDir, ".vscode", "settings.json"), settingsFile(solutionName));
// 		// await fs.writeFile(path.join(solutionDir, ".soicode"), soiCodeFile(solutionName));
// 	} catch (err) {
// 		vscode.window.showErrorMessage(err.toString());
// 	}

// 	// There is a race condition in this code. once opening the folder is done, every extension is killed and gets restarted
// 	vscode.commands.executeCommand(
// 		"vscode.openFolder",
// 		vscode.Uri.parse("file://" + solutionDir)
// 	);
// 	//vscode.workspace.openTextDocument(vscode.Uri.parse('file://' + cppFile));
// 	//vscode.commands.executeCommand('workbench.action.files.openFile', vscode.Uri.parse('file://' + cppFile));
// 	vscode.commands.executeCommand("vscode.open", cppFile);
// 	vscode.commands.executeCommand("workbench.view.explorer");
// }