Files
devstar_plugin/src/main.ts

121 lines
4.8 KiB
TypeScript
Raw Normal View History

import * as vscode from 'vscode';
import QuickAccessTreeProvider from './views/quick-access-tree';
import DSHome from './home';
import RemoteContainer, {openProjectWithoutLogging} from './remote-container';
import User from './user';
2024-06-27 01:20:37 +08:00
2024-09-18 13:52:19 +08:00
export class DevStarExtension {
user: User;
remoteContainer: RemoteContainer;
dsHome: DSHome;
2024-06-27 01:20:37 +08:00
2024-07-02 12:15:39 +08:00
constructor(private context: vscode.ExtensionContext) {
this.user = new User(context);
this.remoteContainer = new RemoteContainer(this.user);
const handler = vscode.window.registerUriHandler({
handleUri: async (uri: vscode.Uri) => {
if (uri.path === '/openProject') {
const params = new URLSearchParams(uri.query);
const host = params.get('host');
const port = params.get('port');
const username = params.get('username');
const path = params.get('path');
const access_token = params.get('access_token');
const devstar_username = params.get('devstar_username');
2025-02-25 11:43:06 +08:00
if (host && port && username && path) {
const container_host = host;
const container_port = parseInt(port, 10);
const container_username = username;
const project_path = decodeURIComponent(path);
if (access_token && devstar_username) {
if (!this.user.isLogged()) {
// 如果没有用户登录,则直接登录;
this.user.setUserTokenToLocal(access_token)
this.user.setUsernameToLocal(devstar_username)
// 使用登录已经登录过的容器连接方法
await this.remoteContainer.firstConnect(container_host, container_username, container_port)
.then((_res) => {
if (_res == 'success') {
// only success then open folder
this.remoteContainer.openRemoteFolder(container_host, container_port, container_username, project_path);
}
})
} else if (devstar_username === this.user.getUsernameFromLocal()){
// 如果同用户已经登录,则忽略;
// 使用登录已经登录过的容器连接方法
await this.remoteContainer.firstConnect(container_host, container_username, container_port)
.then((_res) => {
if (_res == 'success') {
// only success then open folder
this.remoteContainer.openRemoteFolder(container_host, container_port, container_username, project_path);
}
})
} else {
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
'Yes', 'No',);
if (selection === 'Yes') {
this.user.setUserTokenToLocal(access_token);
this.user.setUsernameToLocal(devstar_username);
// 使用登录已经登录过的容器连接方法
await this.remoteContainer.firstConnect(container_host, container_username, container_port)
.then((_res) => {
if (_res == 'success') {
// only success then open folder
this.remoteContainer.openRemoteFolder(container_host, container_port, container_username, project_path);
}
})
} else if (selection === 'No') {
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
}
2025-02-25 11:43:06 +08:00
}
} else {
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
}
} else {
vscode.window.showErrorMessage('Missing required parameters.');
}
}
}
});
context.subscriptions.push(handler);
2024-07-02 12:15:39 +08:00
context.subscriptions.push(
2024-06-27 01:20:37 +08:00
vscode.window.registerTreeDataProvider(
'devstar.quickAccess',
2024-06-27 01:20:37 +08:00
new QuickAccessTreeProvider()
)
);
this.dsHome = new DSHome(context);
this.registerGlobalCommands(context);
2024-09-18 13:52:19 +08:00
this.startDevStarHome();
2024-06-27 01:20:37 +08:00
}
2024-09-18 13:52:19 +08:00
async startDevStarHome() {
vscode.commands.executeCommand('devstar.showHome');
2024-06-27 01:20:37 +08:00
}
2024-07-02 12:15:39 +08:00
registerGlobalCommands(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('devstar.showHome', (url: string) =>
this.dsHome.toggle(url)
2025-02-25 11:43:06 +08:00
)
2024-06-27 01:20:37 +08:00
);
}
}
2024-07-02 12:15:39 +08:00
export function activate(context: vscode.ExtensionContext) {
2024-09-18 13:52:19 +08:00
return new DevStarExtension(context);
2024-06-27 01:20:37 +08:00
}
export function deactivate() {
}