[extension] Add CSharp runner

This commit is contained in:
orosmatthew 2023-11-15 21:01:12 -05:00
parent 9482a1f46b
commit 93b18d6caa
4 changed files with 122 additions and 29 deletions

View File

@ -4,9 +4,12 @@ import { cloneAndOpenRepo } from './extension';
import { BWPanel } from './problemPanel'; import { BWPanel } from './problemPanel';
import urlJoin from 'url-join'; import urlJoin from 'url-join';
export type ContestLanguage = 'Java' | 'CSharp';
export type TeamData = { export type TeamData = {
teamId: number; teamId: number;
contestId: number; contestId: number;
language: ContestLanguage;
}; };
export type WebviewMessageType = { msg: 'onLogin'; data: TeamData } | { msg: 'onLogout' }; export type WebviewMessageType = { msg: 'onLogin'; data: TeamData } | { msg: 'onLogout' };

View File

@ -6,6 +6,7 @@ import { runJava } from './run/java';
import { join } from 'path'; import { join } from 'path';
import { TeamData } from './SidebarProvider'; import { TeamData } from './SidebarProvider';
import { submitProblem } from './submit'; import { submitProblem } from './submit';
import { runCSharp } from './run/csharp';
export type ProblemData = { export type ProblemData = {
id: number; id: number;
@ -143,7 +144,7 @@ export class BWPanel {
vscode.window.showErrorMessage('Already Running'); vscode.window.showErrorMessage('Already Running');
return; return;
} }
const problem = this.problemData.find((p) => (p.id === problemId)); const problem = this.problemData.find((p) => p.id === problemId);
if (problem === undefined) { if (problem === undefined) {
return; return;
} }
@ -153,7 +154,9 @@ export class BWPanel {
this.webviewPostMessage({ msg: 'onRunning' }); this.webviewPostMessage({ msg: 'onRunning' });
this.webviewPostMessage({ msg: 'onRunningOutput', data: '[Compiling...]' }); this.webviewPostMessage({ msg: 'onRunningOutput', data: '[Compiling...]' });
const killFunc = await runJava( let killFunc: (() => void) | undefined;
if (teamData.language === 'Java') {
killFunc = await runJava(
join( join(
repoDir, repoDir,
'BWContest', 'BWContest',
@ -180,6 +183,27 @@ export class BWPanel {
this.webviewPostMessage({ msg: 'onRunningDone' }); 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) { if (killFunc !== undefined) {
this.runningProgram = { this.runningProgram = {
problemId: problemId, problemId: problemId,

View File

@ -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();
}
};
}

View File

@ -33,6 +33,8 @@
postMessage({ postMessage({
msg: 'onLogout' msg: 'onLogout'
}); });
loggedIn = false;
teamData = undefined;
} }
function onTestAndSubmit() { function onTestAndSubmit() {
@ -49,8 +51,8 @@
loggedIn = true; loggedIn = true;
teamData = m.data; teamData = m.data;
} else if (m.msg === 'onLogout') { } else if (m.msg === 'onLogout') {
loggedIn = false; // loggedIn = false;
teamData = undefined; // teamData = undefined;
} }
}); });
</script> </script>
@ -68,6 +70,7 @@
{:else} {:else}
<button on:click={onLogout}>Logout</button> <button on:click={onLogout}>Logout</button>
{#if teamData} {#if teamData}
<p>Language: {teamData.language}</p>
<p>TeamID: {teamData.teamId}</p> <p>TeamID: {teamData.teamId}</p>
<p>ContestID: {teamData.contestId}</p> <p>ContestID: {teamData.contestId}</p>
<button on:click={onClone}>Clone and Open Repo</button> <button on:click={onClone}>Clone and Open Repo</button>