diff --git a/src/main.ts b/src/main.ts index 7bb66b2..e35232a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,11 +14,6 @@ export class DevStarExtension { dsHome: DSHome; constructor(private context: vscode.ExtensionContext) { - this.user = new User(context); - // 只保持一个User实例 - this.remoteContainer = new RemoteContainer(this.user); - this.dsHome = new DSHome(context, this.user); - // 确定local系统是否为win,如果是,保存powershell版本 if (vscode.env.remoteName === undefined) { if (os.platform() === 'win32') { @@ -29,11 +24,32 @@ export class DevStarExtension { } } - // support for open with vscode in web + // 只保持一个User实例 + + // 如果global state中devstarDomain的值不为空,则在global state中存储一个键值对为devstarDomain_和devstar domain,并把devstarDomain的值置空. + // 这时如果remote窗口需要打开项目,且global state中的devstarDomain_键存在、值不为空, + // 则取出并清掉devstarDomain_键值对,并通过参数将devstar domain传递给/openProjectSkippingLoginCheck,由其将devstar domain继续存储在devstarDomain中 + const devstarDomain: string|undefined = context.globalState.get('devstarDomain') + if (devstarDomain != undefined && devstarDomain != "") { + console.log('domain in global state', devstarDomain) + // global state中存在devstarDomain + this.user = new User(context, devstarDomain) + this.remoteContainer = new RemoteContainer(this.user); + this.dsHome = new DSHome(context, this.user, devstarDomain) + + context.globalState.update('devstarDomain_'+vscode.env.sessionId, devstarDomain) + context.globalState.update('devstarDomain', "") + } else { + this.user = new User(context); + this.remoteContainer = new RemoteContainer(this.user); + this.dsHome = new DSHome(context, this.user); + } + const handler = vscode.window.registerUriHandler({ handleUri: async (uri: vscode.Uri) => { - const devstarAPIHandler = new DevstarAPIHandler() - + /** + * 支持open with vscode的入口 + */ if (uri.path === '/openProject') { const params = new URLSearchParams(uri.query); const host = params.get('host'); @@ -41,47 +57,62 @@ export class DevStarExtension { 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'); + const accessToken = params.get('access_token'); + const devstarUsername = params.get('devstar_username'); + const devstarDomain = params.get('devstar_domain'); if (host && hostname && port && username && path) { - const container_host = host; - const container_hostname = hostname - const container_port = parseInt(port, 10); - const container_username = username; - const project_path = decodeURIComponent(path); + const containerHost = host; + const containerHostname = hostname + const containerPort = parseInt(port, 10); + const containerUsername = username; + const projectPath = decodeURIComponent(path); + + if (accessToken && devstarUsername && devstarDomain) { + // 修改user、remote-container、home中的devstar domain和hostname + this.user.setDevstarDomainAndHostname(devstarDomain) + this.remoteContainer.setUser(this.user) + this.dsHome.setDevstarDomain(devstarDomain) + this.dsHome.setUser(this.user) + this.dsHome.setRemoteContainer(this.remoteContainer) + vscode.commands.executeCommand('devstar.showHome'); + + // 将devstar domain存在global state中 + context.globalState.update('devstarDomain', devstarDomain) - if (access_token && devstar_username) { if (!this.user.isLogged()) { // 如果没有用户登录,则直接登录; - const res = await this.user.login(access_token, devstar_username) + const res = await this.user.login(accessToken, devstarUsername) if (res === 'ok') { - await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context) + await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context) } - } else if (devstar_username === this.user.getUsernameFromLocal()) { + } else if (devstarUsername === this.user.getUsernameFromLocal()) { // 如果同用户已经登录,则忽略,直接打开项目 - await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context) + await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context) } else { // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器 const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`, 'Yes', 'No',); if (selection === 'Yes') { // 如果没有用户登录,则直接登录; - const res = await this.user.login(access_token, devstar_username) + const res = await this.user.login(accessToken, devstarUsername) if (res === 'ok') { - await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context) + await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context) } } else if (selection === 'No') { - await openProjectWithoutLogging(container_host, container_port, container_username, project_path); + await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath); } } } else { - await openProjectWithoutLogging(container_host, container_port, container_username, project_path); + await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath); } } else { vscode.window.showErrorMessage('Missing required parameters.'); } } else if (uri.path === "/openProjectSkippingLoginCheck") { + /** + * 支持remote打开local窗口后再打开项目的入口 + */ // 仅有已登录、不用改变登录状态时,用此流程 const params = new URLSearchParams(uri.query); const host = params.get('host'); @@ -89,14 +120,28 @@ export class DevStarExtension { const port = params.get('port'); const username = params.get('username'); const path = params.get('path'); + const devstarDomain = params.get('devstar_domain') - if (host && hostname && port && username && path) { + if (host && hostname && port && username && path && devstarDomain) { const container_host = host; const container_hostname = hostname const container_port = parseInt(port, 10); const container_username = username; const project_path = decodeURIComponent(path); + // 修改user、remote-container、home中的devstar domain和hostname + this.user.setDevstarDomainAndHostname(devstarDomain) + this.remoteContainer.setUser(this.user) + this.dsHome.setDevstarDomain(devstarDomain) + this.dsHome.setUser(this.user) + this.dsHome.setRemoteContainer(this.remoteContainer) + vscode.commands.executeCommand('devstar.showHome'); + + // 如果devstarDomain的值不为空,将其存储在global state中 + if (devstarDomain != "") { + this.context.globalState.update("devstarDomain", devstarDomain) + } + await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context) } } diff --git a/src/remote-container.ts b/src/remote-container.ts index 1b76f1d..90f655b 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -37,19 +37,28 @@ export default class RemoteContainer { vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => { const terminal = vscode.window.terminals[vscode.window.terminals.length - 1]; if (terminal) { + let devstarDomain: string|undefined = context.globalState.get("devstarDomain_"+vscode.env.sessionId) + if (devstarDomain !=undefined && devstarDomain != "") { + // 使用global state中devstarDomain_存储的domain + // 清理键值对 + context.globalState.update("devstarDomain_"+vscode.env.sessionId, undefined) + } else { + devstarDomain = "" + } + // vscode协议 // 根据系统+命令行版本确定命令 const semver = require('semver') const powershellVersion = context.globalState.get('powershellVersion') const powershell_semver_compatible_version = semver.coerce(powershellVersion) if (powershellVersion === undefined) - terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`) + terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`) else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) { // win & powershell >= 5.1.26100.0 - terminal.sendText(`code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`) + terminal.sendText(`code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`) } else { // win & powershell < 5.1.26100.0 - terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`) + terminal.sendText(`code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`) } } })