2024-07-11 12:42:23 +08:00
|
|
|
import * as vscode from 'vscode';
|
2024-07-01 12:25:28 +08:00
|
|
|
import QuickAccessTreeProvider from './views/quick-access-tree';
|
2024-07-16 23:28:56 +08:00
|
|
|
import DSHome from './home';
|
2025-03-02 23:22:51 +08:00
|
|
|
import RemoteContainer, { openProjectWithoutLogging } from './remote-container';
|
2025-02-26 00:47:00 +08:00
|
|
|
import User from './user';
|
2025-03-03 13:22:01 +08:00
|
|
|
import DevstarAPIHandler from './devstar-api';
|
2024-06-27 01:20:37 +08:00
|
|
|
|
2024-09-18 13:52:19 +08:00
|
|
|
export class DevStarExtension {
|
2025-02-26 00:47:00 +08:00
|
|
|
user: User;
|
|
|
|
remoteContainer: RemoteContainer;
|
2024-07-16 23:28:56 +08:00
|
|
|
dsHome: DSHome;
|
2024-06-27 01:20:37 +08:00
|
|
|
|
2024-07-02 12:15:39 +08:00
|
|
|
constructor(private context: vscode.ExtensionContext) {
|
2025-02-26 00:47:00 +08:00
|
|
|
this.user = new User(context);
|
|
|
|
this.remoteContainer = new RemoteContainer(this.user);
|
2025-03-03 13:22:01 +08:00
|
|
|
this.dsHome = new DSHome(context);
|
2025-02-19 10:16:46 +08:00
|
|
|
|
|
|
|
const handler = vscode.window.registerUriHandler({
|
|
|
|
handleUri: async (uri: vscode.Uri) => {
|
2025-03-23 17:17:32 +08:00
|
|
|
const devstarAPIHandler = new DevstarAPIHandler()
|
|
|
|
|
2025-02-19 10:16:46 +08:00
|
|
|
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');
|
2025-02-26 00:47:00 +08:00
|
|
|
const access_token = params.get('access_token');
|
|
|
|
const devstar_username = params.get('devstar_username');
|
2025-02-19 10:16:46 +08:00
|
|
|
|
2025-02-25 11:43:06 +08:00
|
|
|
if (host && port && username && path) {
|
2025-02-26 00:47:00 +08:00
|
|
|
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()) {
|
|
|
|
// 如果没有用户登录,则直接登录;
|
2025-03-23 20:31:31 +08:00
|
|
|
const res = await this.user.login(access_token, devstar_username)
|
|
|
|
if (res === 'ok') {
|
|
|
|
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
|
|
|
|
} else {
|
|
|
|
console.error(res)
|
|
|
|
}
|
2025-03-02 23:33:18 +08:00
|
|
|
} else if (devstar_username === this.user.getUsernameFromLocal()) {
|
2025-02-26 11:04:36 +08:00
|
|
|
// 如果同用户已经登录,则忽略;
|
2025-03-12 10:23:31 +08:00
|
|
|
// 直接打开项目
|
2025-03-02 23:33:18 +08:00
|
|
|
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
|
2025-02-26 00:47:00 +08:00
|
|
|
} else {
|
|
|
|
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
|
|
|
|
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
|
2025-03-02 23:22:51 +08:00
|
|
|
'Yes', 'No',);
|
2025-02-26 00:47:00 +08:00
|
|
|
if (selection === 'Yes') {
|
2025-03-23 20:31:31 +08:00
|
|
|
// 如果没有用户登录,则直接登录;
|
|
|
|
const res = await this.user.login(access_token, devstar_username)
|
|
|
|
if (res === 'ok') {
|
|
|
|
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
|
|
|
|
} else {
|
|
|
|
console.error(res)
|
|
|
|
}
|
2025-02-26 00:47:00 +08:00
|
|
|
} else if (selection === 'No') {
|
|
|
|
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
|
|
|
|
}
|
2025-02-26 11:04:36 +08:00
|
|
|
}
|
2025-02-26 00:47:00 +08:00
|
|
|
} else {
|
|
|
|
await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
|
|
|
|
}
|
2025-02-19 10:16:46 +08:00
|
|
|
} 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(
|
2024-07-16 23:28:56 +08:00
|
|
|
'devstar.quickAccess',
|
2024-06-27 01:20:37 +08:00
|
|
|
new QuickAccessTreeProvider()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2025-02-26 00:47:00 +08:00
|
|
|
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() {
|
2024-07-16 23:28:56 +08:00
|
|
|
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(
|
2024-07-16 23:28:56 +08:00
|
|
|
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() {
|
|
|
|
}
|