feat(firstOpenProject): be based on os and shell version to provide different commands that open project in remote env
This commit is contained in:
		@@ -106,6 +106,7 @@
 | 
			
		||||
    "cheerio": "^1.0.0",
 | 
			
		||||
    "fs-plus": "~3.1.1",
 | 
			
		||||
    "node-ssh": "^13.2.0",
 | 
			
		||||
    "semver": "^7.7.2",
 | 
			
		||||
    "sshpk": "^1.18.0"
 | 
			
		||||
  },
 | 
			
		||||
  "devDependencies": {
 | 
			
		||||
 
 | 
			
		||||
@@ -71,7 +71,7 @@ export default class DSHome {
 | 
			
		||||
              }
 | 
			
		||||
            case 'firstOpenRemoteFolder':
 | 
			
		||||
              // data.host - project name
 | 
			
		||||
              await this.remoteContainer.firstOpenProject(data.host, data.hostname, data.port, data.username, data.path)
 | 
			
		||||
              await this.remoteContainer.firstOpenProject(data.host, data.hostname, data.port, data.username, data.path, this.context)
 | 
			
		||||
              break;
 | 
			
		||||
            case 'openRemoteFolder':
 | 
			
		||||
              this.remoteContainer.openRemoteFolder(data.host, data.port, data.username, data.path);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/main.ts
									
									
									
									
									
								
							@@ -5,6 +5,7 @@ import DSHome from './home';
 | 
			
		||||
import RemoteContainer, { openProjectWithoutLogging } from './remote-container';
 | 
			
		||||
import User from './user';
 | 
			
		||||
import DevstarAPIHandler from './devstar-api';
 | 
			
		||||
import * as os from 'os';
 | 
			
		||||
import * as utils from './utils';
 | 
			
		||||
 | 
			
		||||
export class DevStarExtension {
 | 
			
		||||
@@ -18,6 +19,16 @@ export class DevStarExtension {
 | 
			
		||||
    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') {
 | 
			
		||||
        utils.powershellVersion()
 | 
			
		||||
          .then(powershellVersion => {
 | 
			
		||||
            this.context.globalState.update('powershellVersion', powershellVersion)
 | 
			
		||||
          })
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // support for open with vscode in web
 | 
			
		||||
    const handler = vscode.window.registerUriHandler({
 | 
			
		||||
      handleUri: async (uri: vscode.Uri) => {
 | 
			
		||||
@@ -45,11 +56,11 @@ export class DevStarExtension {
 | 
			
		||||
                // 如果没有用户登录,则直接登录;
 | 
			
		||||
                const res = await this.user.login(access_token, devstar_username)
 | 
			
		||||
                if (res === 'ok') {
 | 
			
		||||
                  await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path)
 | 
			
		||||
                  await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
 | 
			
		||||
                }
 | 
			
		||||
              } else if (devstar_username === this.user.getUsernameFromLocal()) {
 | 
			
		||||
                // 如果同用户已经登录,则忽略,直接打开项目
 | 
			
		||||
                await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path)
 | 
			
		||||
                await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
 | 
			
		||||
              } else {
 | 
			
		||||
                // 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
 | 
			
		||||
                const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
 | 
			
		||||
@@ -58,7 +69,7 @@ export class DevStarExtension {
 | 
			
		||||
                  // 如果没有用户登录,则直接登录;
 | 
			
		||||
                  const res = await this.user.login(access_token, devstar_username)
 | 
			
		||||
                  if (res === 'ok') {
 | 
			
		||||
                    await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path)
 | 
			
		||||
                    await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
 | 
			
		||||
                  }
 | 
			
		||||
                } else if (selection === 'No') {
 | 
			
		||||
                  await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
 | 
			
		||||
@@ -86,7 +97,7 @@ export class DevStarExtension {
 | 
			
		||||
            const container_username = username;
 | 
			
		||||
            const project_path = decodeURIComponent(path);
 | 
			
		||||
 | 
			
		||||
            await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path)
 | 
			
		||||
            await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 
 | 
			
		||||
@@ -27,13 +27,26 @@ export default class RemoteContainer {
 | 
			
		||||
   * @param path 
 | 
			
		||||
   * @param context 用于支持远程项目环境
 | 
			
		||||
   */
 | 
			
		||||
  async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string) {
 | 
			
		||||
  async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
 | 
			
		||||
    if (vscode.env.remoteName) {
 | 
			
		||||
      // 远程环境
 | 
			
		||||
      vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
 | 
			
		||||
        const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
 | 
			
		||||
        if (terminal) {
 | 
			
		||||
          // 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}"`)
 | 
			
		||||
          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}"`)
 | 
			
		||||
          } 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}"`)
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    } else {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										16
									
								
								src/utils.ts
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/utils.ts
									
									
									
									
									
								
							@@ -117,3 +117,19 @@ export async function showErrorNotification(message: string): Promise<void> {
 | 
			
		||||
    vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://gitee.com/SuperIDE/DevStar/issues'));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export async function powershellVersion(): Promise<string> {
 | 
			
		||||
  return new Promise((resolve, reject) => {
 | 
			
		||||
    exec('powershell -Command "$PSVersionTable.PSVersion.ToString()"', (error, stdout, stderr) => {
 | 
			
		||||
      if (error) {
 | 
			
		||||
        reject(`Error executing PowerShell command: ${error.message}`);
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      if (stderr) {
 | 
			
		||||
        reject(`PowerShell command returned an error: ${stderr}`);
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      resolve(stdout.trim());
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user