[extension] Clone and open correct repo
This commit is contained in:
parent
559666e34a
commit
5d48bd0b37
@ -28,7 +28,7 @@ export class BWPanel {
|
|||||||
// Otherwise, create a new panel.
|
// Otherwise, create a new panel.
|
||||||
const panel = vscode.window.createWebviewPanel(
|
const panel = vscode.window.createWebviewPanel(
|
||||||
BWPanel.viewType,
|
BWPanel.viewType,
|
||||||
'VSinder',
|
'BWContest',
|
||||||
column || vscode.ViewColumn.One,
|
column || vscode.ViewColumn.One,
|
||||||
{
|
{
|
||||||
// Enable javascript in the webview
|
// Enable javascript in the webview
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { getNonce } from './getNonce';
|
import { getNonce } from './getNonce';
|
||||||
|
import { cloneAndOpenRepo } from './extension';
|
||||||
|
|
||||||
export class SidebarProvider implements vscode.WebviewViewProvider {
|
export class SidebarProvider implements vscode.WebviewViewProvider {
|
||||||
_view?: vscode.WebviewView;
|
_view?: vscode.WebviewView;
|
||||||
|
_context?: vscode.ExtensionContext;
|
||||||
|
|
||||||
constructor(private readonly _extensionUri: vscode.Uri) {}
|
constructor(private readonly _extensionUri: vscode.Uri, context: vscode.ExtensionContext) {
|
||||||
|
this._context = context;
|
||||||
|
}
|
||||||
|
|
||||||
public resolveWebviewView(webviewView: vscode.WebviewView) {
|
public resolveWebviewView(webviewView: vscode.WebviewView) {
|
||||||
this._view = webviewView;
|
this._view = webviewView;
|
||||||
@ -20,6 +24,34 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
|
|||||||
|
|
||||||
webviewView.webview.onDidReceiveMessage(async (data) => {
|
webviewView.webview.onDidReceiveMessage(async (data) => {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
|
case 'onStartup': {
|
||||||
|
const token: string | undefined = this._context?.globalState.get('token');
|
||||||
|
if (token) {
|
||||||
|
this._view?.webview.postMessage({
|
||||||
|
type: 'onSession',
|
||||||
|
value: token
|
||||||
|
});
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'onClone': {
|
||||||
|
if (!data.value || !data.value.contestId || !data.value.teamId) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await cloneAndOpenRepo(parseInt(data.value.contestId), parseInt(data.value.teamId));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'onLogin': {
|
||||||
|
if (!data.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this._context?.globalState.update('token', data.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'onLogout': {
|
||||||
|
this._context?.globalState.update('token', null);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'onInfo': {
|
case 'onInfo': {
|
||||||
if (!data.value) {
|
if (!data.value) {
|
||||||
return;
|
return;
|
||||||
@ -38,7 +70,6 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private _getHtmlForWebview(webview: vscode.Webview) {
|
private _getHtmlForWebview(webview: vscode.Webview) {
|
||||||
const styleResetUri = webview.asWebviewUri(
|
const styleResetUri = webview.asWebviewUri(
|
||||||
vscode.Uri.joinPath(this._extensionUri, 'media', 'reset.css')
|
vscode.Uri.joinPath(this._extensionUri, 'media', 'reset.css')
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { BWPanel } from './BWPanel';
|
|
||||||
import { SidebarProvider } from './SidebarProvider';
|
import { SidebarProvider } from './SidebarProvider';
|
||||||
import { notDeepEqual } from 'assert';
|
|
||||||
import * as child_process from 'child_process';
|
import * as child_process from 'child_process';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
|
|
||||||
@ -21,11 +19,30 @@ function closeAllWorkspaces() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cloneAndOpenRepo(baseUrl: string, path: string, contestId: number, teamId: number) {
|
export async function cloneAndOpenRepo(contestId: number, teamId: number) {
|
||||||
const repoUrl = `${baseUrl}/${contestId.toString()}/${teamId.toString()}.git`;
|
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`;
|
||||||
|
|
||||||
const repoName = repoUrl.split('/').pop()?.replace('.git', '')!;
|
const repoName = repoUrl.split('/').pop()?.replace('.git', '')!;
|
||||||
const clonedRepoPath = `${path}/${repoName}`;
|
|
||||||
|
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()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const clonedRepoPath = `${currentSettings.repoClonePath}/BWContest/${contestId.toString()}/${repoName}`;
|
||||||
|
|
||||||
if (fs.existsSync(clonedRepoPath)) {
|
if (fs.existsSync(clonedRepoPath)) {
|
||||||
const confirm = await vscode.window.showWarningMessage(
|
const confirm = await vscode.window.showWarningMessage(
|
||||||
@ -40,12 +57,16 @@ async function cloneAndOpenRepo(baseUrl: string, path: string, contestId: number
|
|||||||
fs.removeSync(clonedRepoPath);
|
fs.removeSync(clonedRepoPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
child_process.exec(`git clone ${repoUrl}`, { cwd: path }, (error, stdout, stderr) => {
|
child_process.exec(
|
||||||
|
`git clone ${repoUrl}`,
|
||||||
|
{ cwd: `${currentSettings.repoClonePath}/BWContest/${contestId.toString()}` },
|
||||||
|
(error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
vscode.window.showErrorMessage(`BWContest: Failed to clone repo: ${error.message}`);
|
vscode.window.showErrorMessage(`BWContest: Failed to clone repo: ${error.message}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const addedFolder = vscode.workspace.updateWorkspaceFolders(
|
const addedFolder = vscode.workspace.updateWorkspaceFolders(
|
||||||
vscode.workspace.workspaceFolders?.length ?? 0,
|
vscode.workspace.workspaceFolders?.length ?? 0,
|
||||||
@ -62,28 +83,12 @@ async function cloneAndOpenRepo(baseUrl: string, path: string, contestId: number
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function activate(context: vscode.ExtensionContext) {
|
export function activate(context: vscode.ExtensionContext) {
|
||||||
const sidebarProvider = new SidebarProvider(context.extensionUri);
|
const sidebarProvider = new SidebarProvider(context.extensionUri, context);
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider)
|
vscode.window.registerWebviewViewProvider('bwcontest-sidebar', sidebarProvider)
|
||||||
);
|
);
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(vscode.commands.registerCommand('bwcontest.helloWorld', () => {}));
|
||||||
vscode.commands.registerCommand('bwcontest.helloWorld', () => {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
cloneAndOpenRepo(currentSettings.repoBaseUrl, currentSettings.repoClonePath, 102, 1);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
context.subscriptions.push(
|
context.subscriptions.push(
|
||||||
vscode.commands.registerCommand('bwcontest.askQuestion', async () => {
|
vscode.commands.registerCommand('bwcontest.askQuestion', async () => {
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
function postMessage(message: any) {
|
function postMessage(message: any) {
|
||||||
vscode.postMessage(message);
|
vscode.postMessage(message);
|
||||||
}
|
}
|
||||||
@ -8,6 +10,31 @@ let password: HTMLInputElement;
|
|||||||
|
|
||||||
let sessionToken: string | undefined;
|
let sessionToken: string | undefined;
|
||||||
|
|
||||||
|
interface TeamData {
|
||||||
|
teamId: number,
|
||||||
|
contestId: number
|
||||||
|
}
|
||||||
|
|
||||||
|
let teamData: TeamData | undefined;
|
||||||
|
|
||||||
|
async function fetchTeamData() {
|
||||||
|
if (sessionToken) {
|
||||||
|
const res = await fetch(`http://localhost:5173/api/team/${sessionToken}`, {method: "GET"});
|
||||||
|
const data = await res.json();
|
||||||
|
if (!data.success) {
|
||||||
|
postMessage({type: 'onError', value: "BWContest: Failed to fetch team data"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
teamData = data.data as TeamData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onClone() {
|
||||||
|
if (teamData) {
|
||||||
|
postMessage({type: 'onClone', value: {contestId: teamData.contestId, teamId: teamData.teamId}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function onLogin() {
|
async function onLogin() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch("http://localhost:5173/api/team/login", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
|
const res = await fetch("http://localhost:5173/api/team/login", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
|
||||||
@ -23,9 +50,11 @@ async function onLogin() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sessionToken = data.token;
|
sessionToken = data.token;
|
||||||
|
postMessage({type: 'onLogin', value: sessionToken});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to fetch:', err);
|
console.error('Failed to fetch:', err);
|
||||||
}
|
}
|
||||||
|
fetchTeamData();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onLogout() {
|
async function onLogout() {
|
||||||
@ -34,16 +63,32 @@ async function onLogout() {
|
|||||||
})})
|
})})
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
postMessage({type: 'onError', value: 'Error logging out'});
|
postMessage({type: 'onError', value: 'Error logging out'});
|
||||||
|
sessionToken = undefined;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.success === true) {
|
if (data.success === true) {
|
||||||
postMessage({type: 'onInfo', value: 'BWContest: Logged out'});
|
postMessage({type: 'onInfo', value: 'BWContest: Logged out'});
|
||||||
sessionToken = undefined;
|
sessionToken = undefined;
|
||||||
|
postMessage({type: 'onLogout'});
|
||||||
} else {
|
} else {
|
||||||
postMessage({type: 'onError', value: 'Log out unsuccessful'});
|
postMessage({type: 'onError', value: 'Log out unsuccessful'});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.addEventListener("message", event => {
|
||||||
|
const message = (event as MessageEvent).data;
|
||||||
|
if (message.type === "onSession") {
|
||||||
|
if (message.value !== "") {
|
||||||
|
sessionToken = message.value;
|
||||||
|
fetchTeamData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
postMessage({type: "onStartup"});
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<h1>Contest</h1>
|
<h1>Contest</h1>
|
||||||
@ -58,4 +103,9 @@ async function onLogout() {
|
|||||||
<button on:click={onLogin}>Login</button>
|
<button on:click={onLogin}>Login</button>
|
||||||
{:else}
|
{:else}
|
||||||
<button on:click={onLogout}>Logout</button>
|
<button on:click={onLogout}>Logout</button>
|
||||||
|
{#if teamData}
|
||||||
|
<p>TeamID: {teamData.teamId}</p>
|
||||||
|
<p>ContestID: {teamData.contestId}</p>
|
||||||
|
<button on:click={onClone}>Clone and Open Repo</button>
|
||||||
|
{/if}
|
||||||
{/if}
|
{/if}
|
Loading…
Reference in New Issue
Block a user