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';
|
2024-03-05 17:50:16 -05:00
|
|
|
import outputPanelLog from './outputPanelLog';
|
2024-03-05 19:31:11 -05:00
|
|
|
import {
|
|
|
|
startTeamStatusPollingOnActivation,
|
|
|
|
stopTeamStatusPolling,
|
|
|
|
useFastPolling
|
|
|
|
} from './contestMonitor/pollingService';
|
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;
|
2024-03-05 17:50:16 -05:00
|
|
|
debugFastPolling: boolean;
|
2023-05-07 16:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 12:36:34 -04:00
|
|
|
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();
|
2023-05-06 12:36:34 -04:00
|
|
|
|
|
|
|
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);
|
2024-03-05 17:50:16 -05:00
|
|
|
outputPanelLog.info(`Running 'git clone' to directory: ${dir}`);
|
|
|
|
try {
|
|
|
|
await git.clone({ fs, http, dir, url: repoUrl });
|
2024-03-05 19:31:11 -05:00
|
|
|
} catch (error) {
|
|
|
|
outputPanelLog.error(
|
|
|
|
"Failed to 'git clone'. The git server might be incorrectly configured. Error: " + error
|
|
|
|
);
|
2024-03-05 17:50:16 -05:00
|
|
|
throw error;
|
|
|
|
}
|
2023-05-06 11:33:10 -04:00
|
|
|
|
2024-03-05 19:31:11 -05:00
|
|
|
outputPanelLog.info('Closing workspaces...');
|
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) {
|
2024-03-05 19:31:11 -05:00
|
|
|
outputPanelLog.info('BWContest Extension Activated');
|
2024-03-05 17:50:16 -05:00
|
|
|
|
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
|
|
|
);
|
2024-03-05 17:50:16 -05:00
|
|
|
|
|
|
|
let fastPolling = extensionSettings().debugFastPolling;
|
|
|
|
useFastPolling(fastPolling);
|
|
|
|
|
2023-05-06 00:01:27 -04:00
|
|
|
context.subscriptions.push(
|
2024-03-05 17:50:16 -05:00
|
|
|
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider),
|
|
|
|
vscode.commands.registerCommand('bwcontest.toggleFastPolling', () => {
|
|
|
|
if (!extensionSettings().debugFastPolling) {
|
2024-03-05 19:31:11 -05:00
|
|
|
outputPanelLog.trace('Tried to toggle fast polling, but not allowed.');
|
2024-03-05 17:50:16 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fastPolling = !fastPolling;
|
|
|
|
useFastPolling(fastPolling);
|
|
|
|
})
|
2023-05-06 00:01:27 -04:00
|
|
|
);
|
2024-03-05 17:50:16 -05:00
|
|
|
|
|
|
|
startTeamStatusPollingOnActivation(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deactivate() {
|
2024-03-05 19:31:11 -05:00
|
|
|
outputPanelLog.info('BWContest Extension Deactivated');
|
2024-03-05 17:50:16 -05:00
|
|
|
stopTeamStatusPolling();
|
2023-05-04 22:09:35 -04:00
|
|
|
}
|