109 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			109 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import * as vscode from 'vscode';
 | 
						|
import QuickAccessTreeProvider from './views/quick-access-tree';
 | 
						|
import DSHome from './home';
 | 
						|
import RemoteContainer, { openProjectWithoutLogging } from './remote-container';
 | 
						|
import User from './user';
 | 
						|
import DevstarAPIHandler from './devstar-api';
 | 
						|
 | 
						|
export class DevStarExtension {
 | 
						|
  user: User;
 | 
						|
  remoteContainer: RemoteContainer;
 | 
						|
  dsHome: DSHome;
 | 
						|
 | 
						|
  constructor(private context: vscode.ExtensionContext) {
 | 
						|
    this.user = new User(context);
 | 
						|
    this.remoteContainer = new RemoteContainer(this.user);
 | 
						|
    this.dsHome = new DSHome(context);
 | 
						|
 | 
						|
    const handler = vscode.window.registerUriHandler({
 | 
						|
      handleUri: async (uri: vscode.Uri) => {
 | 
						|
        const devstarAPIHandler = new DevstarAPIHandler()
 | 
						|
 | 
						|
        if (uri.path === '/openProject') {
 | 
						|
          const params = new URLSearchParams(uri.query);
 | 
						|
          const host = params.get('host');
 | 
						|
          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');
 | 
						|
 | 
						|
          if (host && port && username && path) {
 | 
						|
            const container_host = host;
 | 
						|
            const container_port = parseInt(port, 10);
 | 
						|
            const container_username = username;
 | 
						|
            const project_path = decodeURIComponent(path);
 | 
						|
 | 
						|
            if (access_token && devstar_username) {
 | 
						|
              if (!this.user.isLogged()) {
 | 
						|
                // 如果没有用户登录,则直接登录;
 | 
						|
                const res = await this.user.login(access_token, devstar_username)
 | 
						|
                if (res === 'ok') {
 | 
						|
                  await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
 | 
						|
                }
 | 
						|
              } else if (devstar_username === this.user.getUsernameFromLocal()) {
 | 
						|
                // 如果同用户已经登录,则忽略;
 | 
						|
                // 直接打开项目
 | 
						|
                await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
 | 
						|
              } 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)
 | 
						|
                  if (res === 'ok') {
 | 
						|
                    await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
 | 
						|
                  }
 | 
						|
                } else if (selection === 'No') {
 | 
						|
                  await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
 | 
						|
                }
 | 
						|
              }
 | 
						|
            } else {
 | 
						|
              await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
 | 
						|
            }
 | 
						|
          } else {
 | 
						|
            vscode.window.showErrorMessage('Missing required parameters.');
 | 
						|
          }
 | 
						|
        }
 | 
						|
      }
 | 
						|
    });
 | 
						|
 | 
						|
    context.subscriptions.push(handler);
 | 
						|
 | 
						|
    context.subscriptions.push(
 | 
						|
      vscode.window.registerTreeDataProvider(
 | 
						|
        'devstar.quickAccess',
 | 
						|
        new QuickAccessTreeProvider()
 | 
						|
      )
 | 
						|
    );
 | 
						|
 | 
						|
    this.registerGlobalCommands(context);
 | 
						|
 | 
						|
    this.startDevStarHome();
 | 
						|
  }
 | 
						|
 | 
						|
  async startDevStarHome() {
 | 
						|
    vscode.commands.executeCommand('devstar.showHome');
 | 
						|
  }
 | 
						|
 | 
						|
 | 
						|
  registerGlobalCommands(context: vscode.ExtensionContext) {
 | 
						|
    context.subscriptions.push(
 | 
						|
      vscode.commands.registerCommand('devstar.showHome', (url: string) =>
 | 
						|
        this.dsHome.toggle(url)
 | 
						|
      ),
 | 
						|
      vscode.commands.registerCommand('devstar.logout', () => {
 | 
						|
        this.user.logout()
 | 
						|
      })
 | 
						|
    );
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
export function activate(context: vscode.ExtensionContext) {
 | 
						|
  return new DevStarExtension(context);
 | 
						|
}
 | 
						|
 | 
						|
export function deactivate() {
 | 
						|
}
 |