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

110 lines
3.1 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 fs from 'fs-extra';
2023-10-15 18:35:49 -04:00
import urlJoin from 'url-join';
2023-10-15 18:44:17 -04:00
import git from 'isomorphic-git';
import path = require('path');
import http from 'isomorphic-git/http/node';
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;
2023-08-26 10:33:31 -04:00
webUrl: string;
2023-05-06 11:33:10 -04:00
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;
}
2023-08-26 10:33:31 -04:00
if (!currentSettings || currentSettings.webUrl == '') {
vscode.window.showErrorMessage('BWContest: BWContest.webUrl not set');
return;
}
2023-10-15 18:35:49 -04:00
const repoUrl = urlJoin(
currentSettings.repoBaseUrl,
contestId.toString(),
`${teamId.toString()}.git`
);
2023-05-06 11:33:10 -04:00
2023-10-16 13:51:50 -04:00
const repoName = teamId.toString();
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);
}
2023-10-15 21:01:05 -04:00
const dir = path.join(currentSettings.repoClonePath, 'BWContest', contestId.toString(), repoName);
2023-10-15 18:44:17 -04:00
await git.clone({ fs, http, dir, url: repoUrl });
2023-05-06 11:33:10 -04:00
2023-10-15 21:01:05 -04:00
closeAllWorkspaces();
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) {
2023-10-15 18:35:49 -04:00
const sidebarProvider = new SidebarProvider(
context.extensionUri,
context,
extensionSettings().webUrl
2023-05-07 11:01:27 -04:00
);
2023-05-06 00:01:27 -04:00
context.subscriptions.push(
2023-10-15 18:35:49 -04:00
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider)
2023-05-06 00:01:27 -04:00
);
2023-05-04 22:09:35 -04:00
}
export function deactivate() {}