diff --git a/extension/bwcontest/src/SidebarProvider.ts b/extension/bwcontest/src/SidebarProvider.ts index ca5fd24..232e45f 100644 --- a/extension/bwcontest/src/SidebarProvider.ts +++ b/extension/bwcontest/src/SidebarProvider.ts @@ -4,9 +4,12 @@ import { cloneAndOpenRepo } from './extension'; import { BWPanel } from './problemPanel'; import urlJoin from 'url-join'; +export type ContestLanguage = 'Java' | 'CSharp'; + export type TeamData = { teamId: number; contestId: number; + language: ContestLanguage; }; export type WebviewMessageType = { msg: 'onLogin'; data: TeamData } | { msg: 'onLogout' }; diff --git a/extension/bwcontest/src/problemPanel.ts b/extension/bwcontest/src/problemPanel.ts index 56d532e..8eba23e 100644 --- a/extension/bwcontest/src/problemPanel.ts +++ b/extension/bwcontest/src/problemPanel.ts @@ -6,6 +6,7 @@ import { runJava } from './run/java'; import { join } from 'path'; import { TeamData } from './SidebarProvider'; import { submitProblem } from './submit'; +import { runCSharp } from './run/csharp'; export type ProblemData = { id: number; @@ -143,7 +144,7 @@ export class BWPanel { vscode.window.showErrorMessage('Already Running'); return; } - const problem = this.problemData.find((p) => (p.id === problemId)); + const problem = this.problemData.find((p) => p.id === problemId); if (problem === undefined) { return; } @@ -153,33 +154,56 @@ export class BWPanel { this.webviewPostMessage({ msg: 'onRunning' }); this.webviewPostMessage({ msg: 'onRunningOutput', data: '[Compiling...]' }); - const killFunc = await runJava( - join( - repoDir, - 'BWContest', - teamData.contestId.toString(), - teamData.teamId.toString(), - problem.pascalName - ), - join( - repoDir, - 'BWContest', - teamData.contestId.toString(), - teamData.teamId.toString(), + let killFunc: (() => void) | undefined; + if (teamData.language === 'Java') { + killFunc = await runJava( + join( + repoDir, + 'BWContest', + teamData.contestId.toString(), + teamData.teamId.toString(), + problem.pascalName + ), + join( + repoDir, + 'BWContest', + teamData.contestId.toString(), + teamData.teamId.toString(), + problem.pascalName, + `${problem.pascalName}.java` + ), problem.pascalName, - `${problem.pascalName}.java` - ), - problem.pascalName, - input, - (data: string) => { - outputBuffer.push(data); - this.webviewPostMessage({ msg: 'onRunningOutput', data: outputBuffer.join('') }); - }, - () => { - this.runningProgram = undefined; - this.webviewPostMessage({ msg: 'onRunningDone' }); - } - ); + input, + (data: string) => { + outputBuffer.push(data); + this.webviewPostMessage({ msg: 'onRunningOutput', data: outputBuffer.join('') }); + }, + () => { + this.runningProgram = undefined; + this.webviewPostMessage({ msg: 'onRunningDone' }); + } + ); + } else if (teamData.language === 'CSharp') { + killFunc = await runCSharp( + join( + repoDir, + 'BWContest', + teamData.contestId.toString(), + teamData.teamId.toString(), + problem.pascalName + ), + input, + (data: string) => { + outputBuffer.push(data); + this.webviewPostMessage({ msg: 'onRunningOutput', data: outputBuffer.join('') }); + }, + () => { + this.runningProgram = undefined; + this.webviewPostMessage({ msg: 'onRunningDone' }); + } + ); + } + if (killFunc !== undefined) { this.runningProgram = { problemId: problemId, diff --git a/extension/bwcontest/src/run/csharp.ts b/extension/bwcontest/src/run/csharp.ts new file mode 100644 index 0000000..7cb7165 --- /dev/null +++ b/extension/bwcontest/src/run/csharp.ts @@ -0,0 +1,63 @@ +import * as fs from 'fs-extra'; +import { join } from 'path'; +import os = require('os'); +import { spawn } from 'child_process'; + +import kill = require('tree-kill'); + +export async function runCSharp( + srcDir: string, + input: string, + outputCallback: (data: string) => void, + doneCallback: () => void +): Promise<(() => void) | undefined> { + const tempDir = os.tmpdir(); + const buildDir = join(tempDir, 'bwcontest_csharp'); + if (await fs.exists(buildDir)) { + await fs.remove(buildDir); + } + await fs.mkdir(buildDir); + + const child = spawn('dotnet run', { shell: true, cwd: srcDir }); + + child.stdout.setEncoding('utf8'); + child.stdout.on('data', (data) => { + outputCallback(data.toString()); + }); + child.stderr.setEncoding('utf8'); + child.stderr.on('data', (data) => { + outputCallback(data.toString()); + }); + child.stdin.write(input); + child.stdin.end(); + + let done = false; + + child.on('close', () => { + if (done === false) { + done = true; + doneCallback(); + } + }); + + setTimeout(() => { + if (done === false) { + console.log('\n[30 seconds reached, killing process...]'); + done = true; + if (child.pid) { + kill(child.pid); + } + outputCallback('\n[Timeout after 30 seconds]'); + doneCallback(); + } + }, 30000); + + return () => { + if (child.pid) { + done = true; + kill(child.pid); + outputCallback('\n[Manually stopped]'); + doneCallback(); + } + }; +} diff --git a/extension/bwcontest/webviews/components/Sidebar.svelte b/extension/bwcontest/webviews/components/Sidebar.svelte index cd78b06..e4132a1 100644 --- a/extension/bwcontest/webviews/components/Sidebar.svelte +++ b/extension/bwcontest/webviews/components/Sidebar.svelte @@ -33,6 +33,8 @@ postMessage({ msg: 'onLogout' }); + loggedIn = false; + teamData = undefined; } function onTestAndSubmit() { @@ -49,8 +51,8 @@ loggedIn = true; teamData = m.data; } else if (m.msg === 'onLogout') { - loggedIn = false; - teamData = undefined; + // loggedIn = false; + // teamData = undefined; } }); @@ -68,6 +70,7 @@ {:else} {#if teamData} +

Language: {teamData.language}

TeamID: {teamData.teamId}

ContestID: {teamData.contestId}