microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/appCenterCommandPalleteHandler.ts
106lines · modeblame
bb45fbe6max-mironov8 years ago | 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 { ILogger, LogLevel } from "../log/LogHelper"; | |
| 5 | import * as Q from "q"; | |
| 6 | import { AppCenterCommandExecutor } from "./command/commandExecutor"; | |
| 7 | import Auth from "../appcenter/auth/auth"; | |
| 8 | import { AppCenterClient } from "./api/index"; | |
a653acfemax-mironov8 years ago | 9 | import { Profile } from "./auth/profile/profile"; |
bb45fbe6max-mironov8 years ago | 10 | import { AppCenterClientFactory, createAppCenterClient } from "./api/createClient"; |
| 11 | import { SettingsHelper } from "../settingsHelper"; | |
640e6e98max-mironov8 years ago | 12 | import { AppCenterCommandType } from "./appCenterConstants"; |
| 13 | import { AppCenterExtensionManager } from "./appCenterExtensionManager"; | |
6a465861max-mironov8 years ago | 14 | import { ACStrings } from "./appCenterStrings"; |
a653acfemax-mironov8 years ago | 15 | import { ACUtils } from "./helpers/utils"; |
| 16 | import { VsCodeUtils } from "./helpers/vscodeUtils"; | |
bb45fbe6max-mironov8 years ago | 17 | |
| 18 | export class AppCenterCommandPalleteHandler { | |
| 19 | private commandExecutor: AppCenterCommandExecutor; | |
| 20 | private client: AppCenterClient; | |
| 21 | private logger: ILogger; | |
| 22 | private clientFactory: AppCenterClientFactory; | |
18c1b13amax-mironov8 years ago | 23 | private appCenterManager: AppCenterExtensionManager; |
bb45fbe6max-mironov8 years ago | 24 | |
| 25 | constructor(logger: ILogger) { | |
| 26 | this.commandExecutor = new AppCenterCommandExecutor(logger); | |
| 27 | this.clientFactory = createAppCenterClient(); | |
| 28 | this.logger = logger; | |
| 29 | } | |
| 30 | | |
18c1b13amax-mironov8 years ago | 31 | public set AppCenterManager(manager: AppCenterExtensionManager) { |
| 32 | this.appCenterManager = manager; | |
| 33 | } | |
| 34 | | |
| 35 | public run(command: AppCenterCommandType): Q.Promise<void> { | |
e96aa5fcmax-mironov8 years ago | 36 | return ACUtils.isCodePushProject(this.appCenterManager.projectRootPath).then((isCodePush: boolean) => { |
| 37 | if (!isCodePush) { | |
| 38 | VsCodeUtils.ShowWarningMessage(ACStrings.NoCodePushDetectedMsg); | |
24edcbd0max-mironov8 years ago | 39 | return Q.resolve(void 0); |
e96aa5fcmax-mironov8 years ago | 40 | } else { |
| 41 | // Login is special case | |
| 42 | if (command === AppCenterCommandType.Login) { | |
| 43 | return this.commandExecutor.login(this.appCenterManager); | |
| 44 | } | |
| 45 | | |
| 46 | return Auth.getProfile(this.appCenterManager.projectRootPath).then((profile: Profile) => { | |
| 47 | if (!profile) { | |
| 48 | VsCodeUtils.ShowWarningMessage(ACStrings.UserIsNotLoggedInMsg); | |
| 49 | return Q.resolve(void 0); | |
| 50 | } else { | |
| 51 | const clientOrNull: AppCenterClient | null = this.resolveAppCenterClient(profile); | |
| 52 | if (clientOrNull) { | |
| 53 | this.client = clientOrNull; | |
bb45fbe6max-mironov8 years ago | 54 | |
e96aa5fcmax-mironov8 years ago | 55 | switch (command) { |
| 56 | case (AppCenterCommandType.Logout): | |
| 57 | return this.commandExecutor.logout(this.appCenterManager); | |
bb45fbe6max-mironov8 years ago | 58 | |
e96aa5fcmax-mironov8 years ago | 59 | case (AppCenterCommandType.Whoami): |
| 60 | return this.commandExecutor.whoAmI(this.appCenterManager); | |
bb45fbe6max-mironov8 years ago | 61 | |
e96aa5fcmax-mironov8 years ago | 62 | case (AppCenterCommandType.SetCurrentApp): |
| 63 | return this.commandExecutor.setCurrentApp(this.client, this.appCenterManager); | |
640e6e98max-mironov8 years ago | 64 | |
e96aa5fcmax-mironov8 years ago | 65 | case (AppCenterCommandType.GetCurrentApp): |
| 66 | return this.commandExecutor.getCurrentApp(this.appCenterManager); | |
6a465861max-mironov8 years ago | 67 | |
e96aa5fcmax-mironov8 years ago | 68 | case (AppCenterCommandType.SetCurrentDeployment): |
| 69 | return this.commandExecutor.setCurrentDeployment(this.appCenterManager); | |
4a66f08bmax-mironov8 years ago | 70 | |
e96aa5fcmax-mironov8 years ago | 71 | case (AppCenterCommandType.CodePushReleaseReact): |
| 72 | return this.commandExecutor.releaseReact(this.client, this.appCenterManager); | |
6a465861max-mironov8 years ago | 73 | |
e96aa5fcmax-mironov8 years ago | 74 | case (AppCenterCommandType.ShowMenu): |
| 75 | return this.commandExecutor.showMenu(this.client, this.appCenterManager); | |
ef14e11bmax-mironov8 years ago | 76 | |
e96aa5fcmax-mironov8 years ago | 77 | case (AppCenterCommandType.SwitchMandatoryPropForRelease): |
| 78 | return this.commandExecutor.switchIsMandatoryForRelease(this.appCenterManager); | |
2ceda59emax-mironov8 years ago | 79 | |
e96aa5fcmax-mironov8 years ago | 80 | case (AppCenterCommandType.SetTargetBinaryVersionForRelease): |
| 81 | return this.commandExecutor.setTargetBinaryVersionForRelease(this.appCenterManager); | |
2ceda59emax-mironov8 years ago | 82 | |
e96aa5fcmax-mironov8 years ago | 83 | default: |
| 84 | throw new Error("Unknown App Center command!"); | |
| 85 | } | |
| 86 | } else { | |
| 87 | this.logger.log("Failed to get App Center client", LogLevel.Error); | |
| 88 | throw new Error("Failed to get App Center client!"); | |
| 89 | } | |
bb45fbe6max-mironov8 years ago | 90 | } |
e96aa5fcmax-mironov8 years ago | 91 | }); |
| 92 | } | |
bb45fbe6max-mironov8 years ago | 93 | }); |
| 94 | } | |
| 95 | | |
a653acfemax-mironov8 years ago | 96 | private resolveAppCenterClient(profile: Profile): AppCenterClient | null { |
bb45fbe6max-mironov8 years ago | 97 | if (!this.client) { |
a653acfemax-mironov8 years ago | 98 | if (profile) { |
| 99 | return this.clientFactory.fromProfile(profile, SettingsHelper.getAppCenterAPIEndpoint()); | |
bb45fbe6max-mironov8 years ago | 100 | } else { |
a653acfemax-mironov8 years ago | 101 | throw new Error("No App Center user specified!"); |
bb45fbe6max-mironov8 years ago | 102 | } |
| 103 | } | |
| 104 | return this.client; | |
| 105 | } | |
| 106 | } |