feat: 避免打开同一个项目(home、open with vscode)

This commit is contained in:
Levi Yan
2025-05-31 22:51:28 +08:00
parent a8d5f4acb8
commit 91ac39ac0b
5 changed files with 46 additions and 9 deletions

View File

@@ -8,5 +8,6 @@
"User has logged out!":"用户已登出!", "User has logged out!":"用户已登出!",
"Installing vscode-server and devstar extension in container": "正在容器中安装vscode-server及devstar插件", "Installing vscode-server and devstar extension in container": "正在容器中安装vscode-server及devstar插件",
"Connected! Start installation": "连接成功,开始安装", "Connected! Start installation": "连接成功,开始安装",
"Installation completed!": "安装完成!" "Installation completed!": "安装完成!",
"Project has been open!": "该项目已经打开!"
} }

View File

@@ -2,7 +2,7 @@
"name": "devstar", "name": "devstar",
"displayName": "%displayName%", "displayName": "%displayName%",
"description": "%description%", "description": "%description%",
"version": "0.3.2", "version": "0.3.3",
"keywords": [], "keywords": [],
"publisher": "mengning", "publisher": "mengning",
"engines": { "engines": {

View File

@@ -68,11 +68,29 @@ export default class DSHome {
panel.webview.postMessage({ command: 'getUsername', data: { username: username } }) panel.webview.postMessage({ command: 'getUsername', data: { username: username } })
break; break;
} }
case 'getCurrentHost':
let currentHost = null
if (vscode.env.remoteName) {
// 远程环境需要确认当前host名称项目名称
const currentHostRecorded = this.context.globalState.get('currentHost')
currentHost = currentHostRecorded != undefined ? currentHostRecorded : ""
} else {
// 如果当前是本地环境,则没有限制
}
if (null == currentHost || "" == currentHost) {
panel.webview.postMessage({ command: 'getCurrentHost', data: {currentHost: ''}})
break;
} else {
panel.webview.postMessage({ command: 'getCurrentHost', data: {currentHost: currentHost}})
break;
}
case 'firstOpenRemoteFolder': case 'firstOpenRemoteFolder':
// data.host - project name
await this.remoteContainer.firstOpenProject(data.host, data.hostname, data.port, data.username, data.path, this.context) await this.remoteContainer.firstOpenProject(data.host, data.hostname, data.port, data.username, data.path, this.context)
break; break;
case 'openRemoteFolder': case 'openRemoteFolder':
this.remoteContainer.openRemoteFolder(data.host, data.port, data.username, data.path); this.remoteContainer.openRemoteFolder(data.host, data.port, data.username, data.path, this.context);
break; break;
case 'getDevstarDomain': case 'getDevstarDomain':
panel.webview.postMessage({ command: 'getDevstarDomain', data: { devstarDomain: this.devstarDomain } }) panel.webview.postMessage({ command: 'getDevstarDomain', data: { devstarDomain: this.devstarDomain } })
@@ -241,4 +259,4 @@ export default class DSHome {
</html>` </html>`
} }
} }

View File

@@ -48,9 +48,11 @@ export class DevStarExtension {
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)
} }
} else if (devstar_username === this.user.getUsernameFromLocal()) { } else if (devstar_username === this.user.getUsernameFromLocal()) {
// 如果同用户已经登录,则忽略 // 如果同用户已经登录,则忽略,直接打开项目
// 直接打开项目 // 检查项目是否已经打开
await this.remoteContainer.firstOpenProject(container_host, container_hostname,container_port, container_username, project_path, this.context) if (!this.isSameProject(container_host, this.context)) {
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
}
} else { } else {
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器 // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`, const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
@@ -101,6 +103,19 @@ export class DevStarExtension {
this.startDevStarHome(); this.startDevStarHome();
} }
isSameProject(project_name: string, context: vscode.ExtensionContext) {
// 远程环境且目标项目名称==currentHost则提示项目已经打开
if (vscode.env.remoteName) {
const currentHostRecorded = context.globalState.get('currentHost')
const currentHost = currentHostRecorded != undefined ? currentHostRecorded : ""
if (currentHost == project_name) {
vscode.window.showWarningMessage(vscode.l10n.t("Project has been open!"))
return true;
}
}
return false;
}
async startDevStarHome() { async startDevStarHome() {
vscode.commands.executeCommand('devstar.showHome'); vscode.commands.executeCommand('devstar.showHome');
} }

View File

@@ -30,7 +30,7 @@ export default class RemoteContainer {
.then((res) => { .then((res) => {
if (res === 'success') { if (res === 'success') {
// only success then open folder // only success then open folder
this.openRemoteFolder(host, port, username, path); this.openRemoteFolder(host, port, username, path, context);
} }
}) })
} }
@@ -232,7 +232,7 @@ export default class RemoteContainer {
* *
* @host 表示project name * @host 表示project name
*/ */
openRemoteFolder(host: string, port: number, username: string, path: string): void { openRemoteFolder(host: string, port: number, username: string, path: string, context:vscode.ExtensionContext): void {
if (vscode.env.remoteName) { if (vscode.env.remoteName) {
// 远程环境打开local terminal // 远程环境打开local terminal
vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => { vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
@@ -245,6 +245,9 @@ export default class RemoteContainer {
}) })
} else { } else {
// 本地环境 // 本地环境
// 在打开之前需要先将host的信息记录到global state中让home能够确认打开的host是哪个project的
context.globalState.update('currentHost', host)
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`); let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
terminal.show(true); terminal.show(true);
// 在原窗口打开 // 在原窗口打开