Compare commits

...

2 Commits

Author SHA1 Message Date
6cdf60ced1 fix: use VSCode Extension API for remote SSH connection to support Trae IDE
All checks were successful
CI/CD Pipeline for DevStar Extension / build (pull_request) Successful in 6m31s
2025-12-30 22:00:23 +08:00
629a9b4bf7 feat: support cmd parameter to use correct IDE CLI 2025-12-30 18:19:58 +08:00
2 changed files with 38 additions and 23 deletions

View File

@@ -67,6 +67,7 @@ export class DevStarExtension {
const accessToken = params.get('access_token');
const devstarUsername = params.get('devstar_username');
const rawDevstarDomain = params.get('devstar_domain');
const cmd = params.get('cmd') || 'code'; // 获取 cmd 参数,默认为 code
let devstarDomain = rawDevstarDomain;
if (rawDevstarDomain) {
try {
@@ -123,11 +124,11 @@ export class DevStarExtension {
// 如果没有用户登录,则直接登录;
const res = await this.user.login(accessToken, devstarUsername)
if (res === 'ok') {
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context, cmd)
}
} else if (devstarUsername === this.user.getUsernameFromLocal()) {
// 如果同用户已经登录,则忽略,直接打开项目
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context, cmd)
} else {
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
@@ -136,14 +137,14 @@ export class DevStarExtension {
// 如果没有用户登录,则直接登录;
const res = await this.user.login(accessToken, devstarUsername)
if (res === 'ok') {
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context, cmd)
}
} else if (selection === 'No') {
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath, cmd);
}
}
} else {
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath, cmd);
}
} else {
vscode.window.showErrorMessage('Missing required parameters.');
@@ -160,6 +161,7 @@ export class DevStarExtension {
const username = params.get('username');
const path = params.get('path');
const devstarDomain = params.get('devstar_domain')
const cmd = params.get('cmd') || 'code'; // 获取 cmd 参数,默认为 code
if (host && hostname && port && username && path) {
const container_host = host;
@@ -179,10 +181,10 @@ export class DevStarExtension {
// 将devstar domain存在global state中
context.globalState.update('devstarDomain', devstarDomain)
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context, cmd)
} else {
// devstarDomain参数不存在则不存储使用默认用户配置
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context, cmd)
}
}
}

View File

@@ -28,7 +28,7 @@ export default class RemoteContainer {
/**
* 第一次打开远程项目
*/
async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext, cmd: string = 'code') {
if (vscode.env.remoteName) {
try {
await vscode.commands.executeCommand('workbench.action.terminal.newLocal');
@@ -76,7 +76,7 @@ export default class RemoteContainer {
await this.firstConnect(host, hostname, username, port, path)
.then((res) => {
if (res === 'success') {
this.openRemoteFolder(host, port, username, path, context);
this.openRemoteFolder(host, port, username, path, context, cmd);
} else {
vscode.window.showErrorMessage('首次连接容器失败,请检查网络和容器状态。');
}
@@ -350,9 +350,9 @@ export default class RemoteContainer {
}
/**
* local env
* local env - 使用 Extension API 打开远程文件夹
*/
async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext): Promise<void> {
async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext, _cmd: string = 'code'): Promise<void> {
try {
const sshConfig = await this.getSSHConfig(host);
if (sshConfig) {
@@ -365,12 +365,18 @@ export default class RemoteContainer {
}
}
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
terminal.show(true);
// 使用 VSCode Extension API 打开远程连接
// 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效
// 格式: vscode-remote://ssh-remote+host/path
// 注意:不在 URI 中指定用户名,让 SSH config 中的 User root 配置生效
const remoteUri = vscode.Uri.parse(
`vscode-remote://ssh-remote+${host}${path}`
);
const command = `code --remote ssh-remote+root@${host}:${port} ${path} --reuse-window`;
terminal.sendText(command);
// 尝试使用 vscode.openFolder 命令
await vscode.commands.executeCommand('vscode.openFolder', remoteUri, {
forceNewWindow: false
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : '未知错误';
@@ -459,15 +465,22 @@ export default class RemoteContainer {
}
/**
* 打开项目(无须插件登录)
* 打开项目(无须插件登录)- 使用 Extension API
*/
export async function openProjectWithoutLogging(hostname: string, port: number, username: string, path: string): Promise<void> {
const command = `code --remote ssh-remote+${username}@${hostname}:${port} ${path} --reuse-window`;
export async function openProjectWithoutLogging(host: string, _port: number, _username: string, path: string, _cmd: string = 'code'): Promise<void> {
try {
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
terminal.show(true);
terminal.sendText(command);
// 使用 VSCode Extension API 打开远程连接
// 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效
// 格式: vscode-remote://ssh-remote+host/path
const remoteUri = vscode.Uri.parse(
`vscode-remote://ssh-remote+${host}${path}`
);
// 使用 vscode.openFolder 命令
await vscode.commands.executeCommand('vscode.openFolder', remoteUri, {
forceNewWindow: false
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : '未知错误';
vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);