refactor: remove operations that are no longer used under Remote Env
This commit is contained in:
		@@ -37,7 +37,7 @@ export default class RemoteContainer {
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    } else {
 | 
			
		||||
      await this.firstConnect(host, hostname, username, port, context)
 | 
			
		||||
      await this.firstConnect(host, hostname, username, port)
 | 
			
		||||
        .then((res) => {
 | 
			
		||||
          if (res === 'success') {
 | 
			
		||||
            //  only success then open folder
 | 
			
		||||
@@ -49,16 +49,15 @@ export default class RemoteContainer {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * 本地/远程项目环境下,第一次连接其他项目
 | 
			
		||||
   * local environment,第一次连接其他项目
 | 
			
		||||
   * @param host 项目名称
 | 
			
		||||
   * @param hostname ip
 | 
			
		||||
   * @param username 
 | 
			
		||||
   * @param port 
 | 
			
		||||
   * @param context 用于支持远程项目环境
 | 
			
		||||
   * @returns 成功返回success
 | 
			
		||||
   */
 | 
			
		||||
  // connect with key
 | 
			
		||||
  async firstConnect(host: string, hostname: string, username: string, port: number, context: vscode.ExtensionContext): Promise<string> {
 | 
			
		||||
  async firstConnect(host: string, hostname: string, username: string, port: number): Promise<string> {
 | 
			
		||||
    return new Promise(async (resolve) => {
 | 
			
		||||
      const ssh = new NodeSSH();
 | 
			
		||||
      vscode.window.withProgress({
 | 
			
		||||
@@ -67,7 +66,7 @@ export default class RemoteContainer {
 | 
			
		||||
        cancellable: false
 | 
			
		||||
      }, async (progress) => {
 | 
			
		||||
        try {
 | 
			
		||||
          // 无论local/remote,均需检查公私钥是否存在,如果不存在,需要创建(因为有可能在remote上创建了,但是local未创建)
 | 
			
		||||
          // 检查公私钥是否存在,如果不存在,需要创建
 | 
			
		||||
          if (!this.user.existUserPrivateKey() || !this.user.existUserPublicKey()) {
 | 
			
		||||
            await this.user.createUserSSHKey()
 | 
			
		||||
            // 上传公钥
 | 
			
		||||
@@ -81,23 +80,22 @@ export default class RemoteContainer {
 | 
			
		||||
          console.error("Failed to first connect container: ", error)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (vscode.env.remoteName) {
 | 
			
		||||
          // 远程环境
 | 
			
		||||
          try {
 | 
			
		||||
            // 第一次连接
 | 
			
		||||
            await ssh.connect({
 | 
			
		||||
              host: hostname,
 | 
			
		||||
              username: username,
 | 
			
		||||
              port: port,
 | 
			
		||||
              privateKeyPath: this.user.getUserPrivateKeyPath()
 | 
			
		||||
            });
 | 
			
		||||
            progress.report({ message: vscode.l10n.t("Connected! Start installation") });
 | 
			
		||||
        // 本地环境
 | 
			
		||||
        try {
 | 
			
		||||
          // connect with key
 | 
			
		||||
          await ssh.connect({
 | 
			
		||||
            host: hostname,
 | 
			
		||||
            username: username,
 | 
			
		||||
            port: port,
 | 
			
		||||
            privateKeyPath: this.user.getUserPrivateKeyPath()
 | 
			
		||||
          });
 | 
			
		||||
          progress.report({ message: vscode.l10n.t("Connected! Start installation") });
 | 
			
		||||
 | 
			
		||||
            // 安装vscode-server and devstar extension
 | 
			
		||||
            const vscodeCommitId = await utils.getVsCodeCommitId()
 | 
			
		||||
            if ("" != vscodeCommitId) {
 | 
			
		||||
              const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
			
		||||
              const installVscodeServerScript = `
 | 
			
		||||
          // install vscode-server and devstar extension
 | 
			
		||||
          const vscodeCommitId = await utils.getVsCodeCommitId()
 | 
			
		||||
          if ("" != vscodeCommitId) {
 | 
			
		||||
            const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
			
		||||
            const installVscodeServerScript = `
 | 
			
		||||
            mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
			
		||||
            if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
 | 
			
		||||
                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
			
		||||
@@ -110,97 +108,20 @@ export default class RemoteContainer {
 | 
			
		||||
                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
			
		||||
            fi
 | 
			
		||||
            `;
 | 
			
		||||
              await ssh.execCommand(installVscodeServerScript);
 | 
			
		||||
              console.log("vscode-server and extension installed");
 | 
			
		||||
              vscode.window.showInformationMessage(vscode.l10n.t('Installation completed!'));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await ssh.dispose();
 | 
			
		||||
 | 
			
		||||
            // ssh信息存储到ssh config file中
 | 
			
		||||
            // 远程项目环境,利用global state中记录的localSystemName来决定执行的命令
 | 
			
		||||
            const localSystemName = utils.getLocalSystemName(context)
 | 
			
		||||
            const localSSHConfigPath = utils.getLocalSSHConfigPath(context);
 | 
			
		||||
            const privateKeyPath = this.user.getLocalUserPrivateKeyPath();
 | 
			
		||||
            const newSShConfigContent =
 | 
			
		||||
              `\nHost ${host}\n  HostName ${hostname}\n  Port ${port}\n  User ${username}\n  PreferredAuthentications publickey\n  IdentityFile ${privateKeyPath}\n  `;
 | 
			
		||||
            let commandToAppend: string;
 | 
			
		||||
            const delimiter = `EOF_SSH_CONFIG_${Date.now()}`; // 使用一个唯一的分隔符用于 here-docs (Unix) 或 here-strings (PowerShell)
 | 
			
		||||
 | 
			
		||||
            if (localSystemName === "win32") {
 | 
			
		||||
              // --- Windows 平台 ---
 | 
			
		||||
              // 使用 PowerShell 的 Here-String (@'...'@) 来保证多行文本输入的健壮性
 | 
			
		||||
              // @'...'@ 内的内容会按字面解释,减少转义问题
 | 
			
		||||
              const escapedContent = newSShConfigContent.replace(/'/g, "''"); // 对内容中的单引号进行转义(替换为两个单引号),以防万一变量值包含单引号
 | 
			
		||||
              const psTargetPath = localSSHConfigPath;
 | 
			
		||||
              // 将内容包裹在 @'\n...\n'@ 中,确保前后有换行
 | 
			
		||||
              commandToAppend = `Add-Content -Path '${psTargetPath}' -Value @'\n${escapedContent}\n'@`;
 | 
			
		||||
            } else if (localSystemName === "darwin" || localSystemName === "linux") {
 | 
			
		||||
              // --- Linux, macOS, 或其他类 Unix 系统 ---
 | 
			
		||||
              const unixTargetPath = `"${localSSHConfigPath}"`;
 | 
			
		||||
              commandToAppend = `cat << '${delimiter}' >> ${unixTargetPath}
 | 
			
		||||
                ${newSShConfigContent}
 | 
			
		||||
                ${delimiter}
 | 
			
		||||
                `;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // 通过local terminal命令行写入
 | 
			
		||||
            vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
 | 
			
		||||
              const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
 | 
			
		||||
              if (terminal) {
 | 
			
		||||
                terminal.sendText(commandToAppend, true);
 | 
			
		||||
              }
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
            resolve('success')
 | 
			
		||||
          } catch (error) {
 | 
			
		||||
            console.error('Failed to install vscode-server and extension: ', error);
 | 
			
		||||
            await ssh.dispose();
 | 
			
		||||
            await ssh.execCommand(installVscodeServerScript);
 | 
			
		||||
            console.log("vscode-server and extension installed");
 | 
			
		||||
            vscode.window.showInformationMessage(vscode.l10n.t('Installation completed!'));
 | 
			
		||||
          }
 | 
			
		||||
        } else {
 | 
			
		||||
          // 本地环境
 | 
			
		||||
          try {
 | 
			
		||||
            // connect with key
 | 
			
		||||
            await ssh.connect({
 | 
			
		||||
              host: hostname,
 | 
			
		||||
              username: username,
 | 
			
		||||
              port: port,
 | 
			
		||||
              privateKeyPath: this.user.getUserPrivateKeyPath()
 | 
			
		||||
            });
 | 
			
		||||
            progress.report({ message: vscode.l10n.t("Connected! Start installation") });
 | 
			
		||||
 | 
			
		||||
            // install vscode-server and devstar extension
 | 
			
		||||
            const vscodeCommitId = await utils.getVsCodeCommitId()
 | 
			
		||||
            if ("" != vscodeCommitId) {
 | 
			
		||||
              const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
			
		||||
              const installVscodeServerScript = `
 | 
			
		||||
            mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
			
		||||
            if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
 | 
			
		||||
                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
			
		||||
            else
 | 
			
		||||
                wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
 | 
			
		||||
                mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
			
		||||
                cd ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
			
		||||
                tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
 | 
			
		||||
                rm vscode-server-linux-x64.tar.gz && \\
 | 
			
		||||
                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
			
		||||
            fi
 | 
			
		||||
            `;
 | 
			
		||||
              await ssh.execCommand(installVscodeServerScript);
 | 
			
		||||
              console.log("vscode-server and extension installed");
 | 
			
		||||
              vscode.window.showInformationMessage(vscode.l10n.t('Installation completed!'));
 | 
			
		||||
            }
 | 
			
		||||
          await ssh.dispose();
 | 
			
		||||
 | 
			
		||||
            await ssh.dispose();
 | 
			
		||||
          // only connect successfully then save the host info
 | 
			
		||||
          await this.storeProjectSSHInfo(host, hostname, port, username)
 | 
			
		||||
 | 
			
		||||
            // only connect successfully then save the host info
 | 
			
		||||
            await this.storeProjectSSHInfo(host, hostname, port, username)
 | 
			
		||||
 | 
			
		||||
            resolve('success')
 | 
			
		||||
          } catch (error) {
 | 
			
		||||
            console.error('Failed to install vscode-server and extension: ', error);
 | 
			
		||||
            await ssh.dispose();
 | 
			
		||||
          }
 | 
			
		||||
          resolve('success')
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
          console.error('Failed to install vscode-server and extension: ', error);
 | 
			
		||||
          await ssh.dispose();
 | 
			
		||||
        }
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
@@ -241,26 +162,16 @@ export default class RemoteContainer {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * local env
 | 
			
		||||
   * 仅支持已经成功连接,并在ssh config file中存储ssh信息的项目连接。
 | 
			
		||||
   * 
 | 
			
		||||
   * @host 表示project name
 | 
			
		||||
   */
 | 
			
		||||
  openRemoteFolder(host: string, port: number, username: string, path: string, context:vscode.ExtensionContext): void {
 | 
			
		||||
    if (vscode.env.remoteName) {
 | 
			
		||||
      // 远程环境,打开local terminal
 | 
			
		||||
      vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
 | 
			
		||||
        // 获取最后一个打开的终端实例
 | 
			
		||||
        const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
 | 
			
		||||
        if (terminal) {
 | 
			
		||||
          // 在新窗口打开
 | 
			
		||||
          terminal.sendText(`code --remote ssh-remote+${username}@${host}:${port} ${path} --new-window`)
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    } else {
 | 
			
		||||
      // 本地环境
 | 
			
		||||
  openRemoteFolder(host: string, port: number, username: string, path: string, context: vscode.ExtensionContext): void {
 | 
			
		||||
    // local env
 | 
			
		||||
    if (vscode.env.remoteName === undefined) {
 | 
			
		||||
      // 在打开之前,需要先将host的信息记录到global state中,让home能够确认打开的host是哪个project的
 | 
			
		||||
      context.globalState.update('currentHost', host)
 | 
			
		||||
 | 
			
		||||
      let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
 | 
			
		||||
      terminal.show(true);
 | 
			
		||||
      // 在原窗口打开
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user