microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/appCenterExtensionManager.ts
123lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { Disposable, StatusBarAlignment, StatusBarItem, window } from "vscode"; |
| 5 | import Auth from "../appcenter/auth/auth"; |
| 6 | import { ACStrings } from "./appCenterStrings"; |
| 7 | import * as Q from "q"; |
| 8 | import { ACCommandNames, ACConstants } from "./appCenterConstants"; |
| 9 | import { Profile } from "./auth/profile/profile"; |
| 10 | import { ACUtils } from "./appCenterUtils"; |
| 11 | import { DefaultApp } from "./command/commandParams"; |
| 12 | |
| 13 | export class AppCenterExtensionManager implements Disposable { |
| 14 | private loginStatusBarItem: StatusBarItem; |
| 15 | private currentAppStatusBarItem: StatusBarItem; |
| 16 | private currentDeploymentStatusBarItem: StatusBarItem; |
| 17 | private _projectRootPath: string; |
| 18 | |
| 19 | public constructor(projectRootPath: string) { |
| 20 | this._projectRootPath = projectRootPath; |
| 21 | } |
| 22 | |
| 23 | public get projectRootPath(): string { |
| 24 | return this._projectRootPath; |
| 25 | } |
| 26 | |
| 27 | public setup(): Q.Promise<void> { |
| 28 | this.loginStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 12); |
| 29 | this.currentAppStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 11); |
| 30 | this.currentDeploymentStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left, 10); |
| 31 | |
| 32 | return Auth.whoAmI().then((profile: Profile) => { |
| 33 | if (!profile) { |
| 34 | return this.setupNotAuthenticatedStatusBar(); |
| 35 | } else { |
| 36 | this.setupAppCenterStatusBarsWithCurrentApp(<DefaultApp>profile.defaultApp); |
| 37 | return this.setupAuthenticatedStatusBar(profile.userName); |
| 38 | } |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | public dispose() { |
| 43 | if (this.loginStatusBarItem) { |
| 44 | this.loginStatusBarItem.dispose(); |
| 45 | } |
| 46 | if (this.currentAppStatusBarItem) { |
| 47 | this.currentAppStatusBarItem.dispose(); |
| 48 | } |
| 49 | if (this.currentDeploymentStatusBarItem) { |
| 50 | this.currentDeploymentStatusBarItem.dispose(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public setupNotAuthenticatedStatusBar() { |
| 55 | this.setStatusBar(this.loginStatusBarItem, |
| 56 | `$(icon octicon-person) Login to App Center`, |
| 57 | ACStrings.UserMustSignIn, |
| 58 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.Login}` |
| 59 | ); |
| 60 | if (this.currentAppStatusBarItem) { |
| 61 | this.currentAppStatusBarItem.hide(); |
| 62 | } |
| 63 | if (this.currentDeploymentStatusBarItem) { |
| 64 | this.currentDeploymentStatusBarItem.hide(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public setupAuthenticatedStatusBar(userName: string) { |
| 69 | this.setStatusBar(this.loginStatusBarItem, |
| 70 | `$(icon octicon-person) ${userName} $(icon octicon-arrow-right)`, |
| 71 | ACStrings.YouAreLoggedInMsg(userName), |
| 72 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.Logout}` |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public setupAppCenterStatusBarsWithCurrentApp(currentApp: DefaultApp) { |
| 77 | if (currentApp) { |
| 78 | this.setCurrentAppStatusBar(ACUtils.formatAppNameForStatusBar(currentApp)); |
| 79 | this.setCurrentDeploymentStatusBar(ACUtils.formatDeploymentNameForStatusBar(currentApp.currentAppDeployment)); |
| 80 | } else { |
| 81 | this.setCurrentAppStatusBar(null); |
| 82 | this.setCurrentDeploymentStatusBar(null); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public setCurrentDeploymentStatusBar(deploymentName: string | null) { |
| 87 | if (deploymentName) { |
| 88 | return this.setStatusBar(this.currentDeploymentStatusBarItem, |
| 89 | `$(icon octicon-cloud-upload) ${deploymentName}`, |
| 90 | ACStrings.YourCurrentDeploymentMsg(deploymentName), |
| 91 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.SetCurrentDeployment}`); |
| 92 | } else { |
| 93 | return this.setStatusBar(this.currentDeploymentStatusBarItem, |
| 94 | `$(icon octicon-alert) ${ACStrings.NoCurrentDeploymentSetMsg}`, |
| 95 | ACStrings.PleaseProvideCurrentDeploymentMsg, |
| 96 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.SetCurrentDeployment}`); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public setCurrentAppStatusBar(appName: string | null) { |
| 101 | if (appName) { |
| 102 | return this.setStatusBar(this.currentAppStatusBarItem, |
| 103 | `$(icon octicon-browser) ${appName} $(icon octicon-arrow-right)`, |
| 104 | ACStrings.YourCurrentAppMsg(appName), |
| 105 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.SetCurrentApp}`); |
| 106 | } else { |
| 107 | return this.setStatusBar(this.currentAppStatusBarItem, |
| 108 | `$(icon octicon-alert) ${ACStrings.NoCurrentAppSetMsg} $(icon octicon-arrow-right)`, |
| 109 | ACStrings.PleaseProvideCurrentAppMsg, |
| 110 | `${ACConstants.ExtensionPrefixName}.${ACCommandNames.SetCurrentApp}`); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private setStatusBar(statusBar: StatusBarItem, text: string, tooltip: string, commandOnClick?: string): void { |
| 115 | if (statusBar !== undefined) { |
| 116 | statusBar.command = commandOnClick; // undefined clears the command |
| 117 | statusBar.text = text; |
| 118 | statusBar.tooltip = tooltip; |
| 119 | statusBar.show(); |
| 120 | statusBar.color = ACConstants.AppCenterCodePushStatusBarColor; |
| 121 | } |
| 122 | } |
| 123 | } |