bw-hspc-contest-env/extension/bwcontest/src/extension.ts

118 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-05-04 22:09:35 -04:00
import * as vscode from 'vscode';
2023-05-06 00:01:27 -04:00
import { SidebarProvider } from './SidebarProvider';
2023-05-06 11:33:10 -04:00
import * as child_process from 'child_process';
import * as fs from 'fs-extra';
2023-05-07 11:01:27 -04:00
import { BWPanel } from './problemPanel';
2023-05-06 11:33:10 -04:00
2023-05-07 16:30:42 -04:00
export interface BWContestSettings {
2023-05-06 11:33:10 -04:00
repoBaseUrl: string;
repoClonePath: string;
2023-05-07 16:30:42 -04:00
javaPath: string;
}
export function extensionSettings(): BWContestSettings {
return vscode.workspace.getConfiguration().get<BWContestSettings>('BWContest')!;
2023-05-06 11:33:10 -04:00
}
function closeAllWorkspaces() {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
return;
}
const removedFolders = vscode.workspace.updateWorkspaceFolders(0, workspaceFolders.length);
if (!removedFolders) {
return;
}
}
export async function cloneAndOpenRepo(contestId: number, teamId: number) {
const currentSettings = vscode.workspace.getConfiguration().get<BWContestSettings>('BWContest');
if (!currentSettings || currentSettings.repoBaseUrl == '') {
vscode.window.showErrorMessage('BWContest: BWContest.repoBaseURL not set');
return;
}
if (!currentSettings || currentSettings.repoClonePath == '') {
vscode.window.showErrorMessage('BWContest: BWContest.repoClonePath not set');
return;
}
const repoUrl = `${currentSettings.repoBaseUrl}/${contestId.toString()}/${teamId.toString()}.git`;
2023-05-06 11:33:10 -04:00
const repoName = repoUrl.split('/').pop()?.replace('.git', '')!;
if (!fs.existsSync(`${currentSettings.repoClonePath}/BWContest`)) {
fs.mkdirSync(`${currentSettings.repoClonePath}/BWContest`);
}
if (!fs.existsSync(`${currentSettings.repoClonePath}/BWContest/${contestId.toString()}`)) {
fs.mkdirSync(`${currentSettings.repoClonePath}/BWContest/${contestId.toString()}`);
}
2023-05-07 11:01:27 -04:00
const clonedRepoPath = `${
currentSettings.repoClonePath
}/BWContest/${contestId.toString()}/${repoName}`;
2023-05-06 11:33:10 -04:00
if (fs.existsSync(clonedRepoPath)) {
const confirm = await vscode.window.showWarningMessage(
'The repo already exists. Do you want to replace it?',
'Delete and Replace',
'Cancel'
);
if (confirm !== 'Delete and Replace') {
return;
}
closeAllWorkspaces();
fs.removeSync(clonedRepoPath);
}
child_process.exec(
`git clone ${repoUrl}`,
{ cwd: `${currentSettings.repoClonePath}/BWContest/${contestId.toString()}` },
(error, stdout, stderr) => {
if (error) {
vscode.window.showErrorMessage(`BWContest: Failed to clone repo: ${error.message}`);
return;
}
2023-05-06 11:33:10 -04:00
}
);
2023-05-06 11:33:10 -04:00
const addedFolder = vscode.workspace.updateWorkspaceFolders(
vscode.workspace.workspaceFolders?.length ?? 0,
0,
{ uri: vscode.Uri.file(clonedRepoPath), name: 'BWContest' }
);
if (!addedFolder) {
vscode.window.showErrorMessage('BWContest: Failed to open cloned repo');
return;
}
vscode.window.showInformationMessage('BWContest: Repo cloned and opened');
}
2023-05-04 22:09:35 -04:00
export function activate(context: vscode.ExtensionContext) {
const sidebarProvider = new SidebarProvider(context.extensionUri, context);
2023-05-06 00:01:27 -04:00
context.subscriptions.push(
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider)
);
2023-05-04 22:09:35 -04:00
2023-05-07 11:01:27 -04:00
context.subscriptions.push(
vscode.commands.registerCommand('bwcontest.helloWorld', () => {
BWPanel.createOrShow(context.extensionUri, context);
})
);
2023-05-04 22:09:35 -04:00
2023-05-06 00:01:27 -04:00
context.subscriptions.push(
vscode.commands.registerCommand('bwcontest.askQuestion', async () => {
const answer = await vscode.window.showInformationMessage('How was your day?', 'good', 'bad');
if (answer === 'bad') {
vscode.window.showInformationMessage('Sorry to hear that');
} else {
console.log(answer);
}
})
);
2023-05-04 22:09:35 -04:00
}
export function deactivate() {}