microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/appcenter/command/commandExecutor.ts
497lines · 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 * as vscode from "vscode"; | |
| 5 | import * as Q from "q"; | |
| 6 | import * as qs from "qs"; | |
| 7 | import * as os from "os"; | |
| 8 | | |
| 9 | import { ILogger, LogLevel } from "../../log/LogHelper"; | |
| 10 | import Auth from "../../appcenter/auth/auth"; | |
2ceda59emax-mironov8 years ago | 11 | import { AppCenterLoginType, ACConstants, AppCenterOS, CurrentAppDeployments, Deployment, ACCommandNames } from "../appCenterConstants"; |
ef14e11bmax-mironov8 years ago | 12 | import { Profile } from "../../appcenter/auth/profile/profile"; |
bb45fbe6max-mironov8 years ago | 13 | import { SettingsHelper } from "../../settingsHelper"; |
e26df9e4max-mironov8 years ago | 14 | import { AppCenterClient, models } from "../api/index"; |
ff1d5ab0max-mironov8 years ago | 15 | import { DefaultApp, ICodePushReleaseParams } from "./commandParams"; |
640e6e98max-mironov8 years ago | 16 | import { AppCenterExtensionManager } from "../appCenterExtensionManager"; |
6a465861max-mironov8 years ago | 17 | import { ACStrings } from "../appCenterStrings"; |
bf370babSergey Akhalkov8 years ago | 18 | import CodePushRelease from "../codepush/release"; |
a653acfemax-mironov8 years ago | 19 | import { ACUtils } from "../helpers/utils"; |
85a7b4aaannakocheshkova8 years ago | 20 | import { updateContents, reactNative, fileUtils } from "../codepush/codepush-sdk/src/index"; |
a446900cSergey Akhalkov8 years ago | 21 | import BundleConfig = reactNative.BundleConfig; |
e26df9e4max-mironov8 years ago | 22 | import { getQPromisifiedClientResult } from "../api/createClient"; |
2ceda59emax-mironov8 years ago | 23 | import { validRange } from "semver"; |
24edcbd0max-mironov8 years ago | 24 | import { VsCodeUtils, IButtonMessageItem } from "../helpers/vscodeUtils"; |
bb45fbe6max-mironov8 years ago | 25 | |
| 26 | interface IAppCenterAuth { | |
640e6e98max-mironov8 years ago | 27 | login(appcenterManager: AppCenterExtensionManager): Q.Promise<void>; |
6a465861max-mironov8 years ago | 28 | logout(appcenterManager: AppCenterExtensionManager): Q.Promise<void>; |
a653acfemax-mironov8 years ago | 29 | whoAmI(appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
6a465861max-mironov8 years ago | 30 | } |
| 31 | | |
| 32 | interface IAppCenterApps { | |
2ceda59emax-mironov8 years ago | 33 | getCurrentApp(appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
e26df9e4max-mironov8 years ago | 34 | setCurrentApp(client: AppCenterClient, appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
4a66f08bmax-mironov8 years ago | 35 | setCurrentDeployment(appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
bb45fbe6max-mironov8 years ago | 36 | } |
| 37 | | |
| 38 | interface IAppCenterCodePush { | |
ef14e11bmax-mironov8 years ago | 39 | showMenu(client: AppCenterClient, appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
6a465861max-mironov8 years ago | 40 | releaseReact(client: AppCenterClient, appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
2ceda59emax-mironov8 years ago | 41 | switchIsMandatoryForRelease(appCenterManager: AppCenterExtensionManager): Q.Promise<void>; |
| 42 | setTargetBinaryVersionForRelease(appCenterManager: AppCenterExtensionManager): Q.Promise<void>; | |
bb45fbe6max-mironov8 years ago | 43 | } |
| 44 | | |
6a465861max-mironov8 years ago | 45 | export class AppCenterCommandExecutor implements IAppCenterAuth, IAppCenterCodePush, IAppCenterApps { |
bb45fbe6max-mironov8 years ago | 46 | private logger: ILogger; |
| 47 | | |
| 48 | constructor(logger: ILogger) { | |
| 49 | this.logger = logger; | |
| 50 | } | |
| 51 | | |
640e6e98max-mironov8 years ago | 52 | public login(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
b73e66f1max-mironov8 years ago | 53 | const appCenterLoginOptions: string[] = Object.keys(AppCenterLoginType).filter(k => typeof AppCenterLoginType[k as any] === "number"); |
6a465861max-mironov8 years ago | 54 | vscode.window.showQuickPick(appCenterLoginOptions, { placeHolder: ACStrings.SelectLoginTypeMsg }) |
ef14e11bmax-mironov8 years ago | 55 | .then((loginType) => { |
| 56 | switch (loginType) { | |
| 57 | case (AppCenterLoginType[AppCenterLoginType.Interactive]): | |
24edcbd0max-mironov8 years ago | 58 | const messageItems: IButtonMessageItem[] = []; |
| 59 | const loginUrl = `${SettingsHelper.getAppCenterLoginEndpoint()}?${qs.stringify({ hostname: os.hostname()})}`; | |
| 60 | messageItems.push({ title : ACStrings.OkBtnLabel, | |
| 61 | url : loginUrl }); | |
| 62 | | |
| 63 | return VsCodeUtils.ShowInformationMessage(ACStrings.PleaseLoginViaBrowser, ...messageItems) | |
| 64 | .then((selection: IButtonMessageItem | undefined) => { | |
| 65 | if (selection) { | |
ef14e11bmax-mironov8 years ago | 66 | return vscode.window.showInputBox({ prompt: ACStrings.PleaseProvideToken, ignoreFocusOut: true }) |
| 67 | .then(token => { | |
| 68 | this.loginWithToken(token, appCenterManager); | |
| 69 | }); | |
| 70 | } else return Q.resolve(void 0); | |
| 71 | }); | |
| 72 | case (AppCenterLoginType[AppCenterLoginType.Token]): | |
| 73 | return vscode.window.showInputBox({ prompt: ACStrings.PleaseProvideToken , ignoreFocusOut: true}) | |
| 74 | .then(token => { | |
| 75 | return this.loginWithToken(token, appCenterManager); | |
| 76 | }); | |
| 77 | default: | |
| 78 | // User canel login otherwise | |
| 79 | return Q.resolve(void 0); | |
| 80 | } | |
bb45fbe6max-mironov8 years ago | 81 | }); |
b73e66f1max-mironov8 years ago | 82 | return Q.resolve(void 0); |
bb45fbe6max-mironov8 years ago | 83 | } |
| 84 | | |
6a465861max-mironov8 years ago | 85 | public logout(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
2ceda59emax-mironov8 years ago | 86 | return Auth.doLogout(appCenterManager.projectRootPath).then(() => { |
24edcbd0max-mironov8 years ago | 87 | VsCodeUtils.ShowInformationMessage(ACStrings.UserLoggedOutMsg); |
ef14e11bmax-mironov8 years ago | 88 | return appCenterManager.setupAppCenterStatusBar(null); |
| 89 | }).catch(() => { | |
| 90 | this.logger.log("An errro occured on logout", LogLevel.Error); | |
| 91 | }); | |
bb45fbe6max-mironov8 years ago | 92 | } |
| 93 | | |
a653acfemax-mironov8 years ago | 94 | public whoAmI(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
| 95 | return Auth.getProfile(appCenterManager.projectRootPath).then((profile: Profile | null) => { | |
| 96 | if (profile && profile.displayName) { | |
24edcbd0max-mironov8 years ago | 97 | VsCodeUtils.ShowInformationMessage(ACStrings.YouAreLoggedInMsg(profile.displayName)); |
a653acfemax-mironov8 years ago | 98 | } else { |
24edcbd0max-mironov8 years ago | 99 | VsCodeUtils.ShowInformationMessage(ACStrings.UserIsNotLoggedInMsg); |
a653acfemax-mironov8 years ago | 100 | } |
24edcbd0max-mironov8 years ago | 101 | return Q.resolve(void 0); |
a653acfemax-mironov8 years ago | 102 | }); |
bb45fbe6max-mironov8 years ago | 103 | } |
| 104 | | |
4a66f08bmax-mironov8 years ago | 105 | public setCurrentDeployment(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
2ceda59emax-mironov8 years ago | 106 | this.restoreCurrentApp(appCenterManager.projectRootPath) |
| 107 | .then((currentApp: DefaultApp) => { | |
| 108 | if (currentApp && currentApp.currentAppDeployments && currentApp.currentAppDeployments.codePushDeployments) { | |
| 109 | const deploymentOptions: string[] = currentApp.currentAppDeployments.codePushDeployments.map((deployment) => { | |
| 110 | return deployment.name; | |
| 111 | }); | |
| 112 | vscode.window.showQuickPick(deploymentOptions, { placeHolder: ACStrings.SelectCurrentDeploymentMsg }) | |
| 113 | .then((deploymentName) => { | |
| 114 | if (deploymentName) { | |
| 115 | this.saveCurrentApp( | |
| 116 | appCenterManager.projectRootPath, | |
| 117 | currentApp.identifier, | |
| 118 | AppCenterOS[currentApp.os], { | |
| 119 | currentDeploymentName: deploymentName, | |
| 120 | codePushDeployments: currentApp.currentAppDeployments.codePushDeployments, | |
| 121 | }, | |
| 122 | currentApp.targetBinaryVersion, | |
| 123 | currentApp.isMandatory | |
d3db396bmax-mironov8 years ago | 124 | ); |
| 125 | VsCodeUtils.ShowInformationMessage(ACStrings.YourCurrentDeploymentMsg(deploymentName)); | |
4a66f08bmax-mironov8 years ago | 126 | } |
| 127 | }); | |
d3db396bmax-mironov8 years ago | 128 | } else { |
| 129 | VsCodeUtils.ShowInformationMessage(ACStrings.NoCurrentAppSetMsg); | |
4a66f08bmax-mironov8 years ago | 130 | } |
| 131 | }); | |
| 132 | return Q.resolve(void 0); | |
| 133 | } | |
| 134 | | |
2ceda59emax-mironov8 years ago | 135 | public getCurrentApp(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
| 136 | this.restoreCurrentApp(appCenterManager.projectRootPath).then((app: DefaultApp) => { | |
84abb0c7max-mironov8 years ago | 137 | if (app) { |
24edcbd0max-mironov8 years ago | 138 | VsCodeUtils.ShowInformationMessage(ACStrings.YourCurrentAppMsg(app.identifier)); |
84abb0c7max-mironov8 years ago | 139 | } else { |
24edcbd0max-mironov8 years ago | 140 | VsCodeUtils.ShowInformationMessage(ACStrings.NoCurrentAppSetMsg); |
84abb0c7max-mironov8 years ago | 141 | } |
86466695max-mironov8 years ago | 142 | }); |
6a465861max-mironov8 years ago | 143 | return Q.resolve(void 0); |
bb45fbe6max-mironov8 years ago | 144 | } |
640e6e98max-mironov8 years ago | 145 | |
e26df9e4max-mironov8 years ago | 146 | public setCurrentApp(client: AppCenterClient, appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
7574c61amax-mironov8 years ago | 147 | vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: "Get Apps"}, p => { |
| 148 | return new Promise((resolve, reject) => { | |
| 149 | p.report({message: ACStrings.FetchAppsStatusBarMessage }); | |
| 150 | getQPromisifiedClientResult(client.account.apps.list()).then((apps: models.AppResponse[]) => { | |
| 151 | const appsList: models.AppResponse[] = apps; | |
| 152 | const reactNativeApps = appsList.filter(app => app.platform === ACConstants.AppCenterReactNativePlatformName); | |
| 153 | resolve(reactNativeApps); | |
| 154 | }); | |
| 155 | }); | |
| 156 | }).then((rnApps: models.AppResponse[]) => { | |
| 157 | let options = rnApps.map(app => { | |
e26df9e4max-mironov8 years ago | 158 | return { |
| 159 | label: `${app.name} (${app.os})`, | |
| 160 | description: app.displayName, | |
| 161 | target: app.name, | |
| 162 | }; | |
| 163 | }); | |
| 164 | vscode.window.showQuickPick(options, { placeHolder: ACStrings.ProvideCurrentAppPromptMsg }) | |
| 165 | .then((selected: {label: string, description: string, target: string}) => { | |
| 166 | if (selected) { | |
7574c61amax-mironov8 years ago | 167 | const selectedApps: models.AppResponse[] = rnApps.filter(app => app.name === selected.target); |
e26df9e4max-mironov8 years ago | 168 | if (selectedApps && selectedApps.length === 1) { |
| 169 | const selectedApp: models.AppResponse = selectedApps[0]; | |
| 170 | const selectedAppName: string = `${selectedApp.owner.name}/${selectedApp.name}`; | |
2ceda59emax-mironov8 years ago | 171 | const OS: AppCenterOS = AppCenterOS[selectedApp.os.toLowerCase()]; |
7574c61amax-mironov8 years ago | 172 | |
| 173 | vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: "Get Deployments"}, p => { | |
| 174 | return new Promise((resolve, reject) => { | |
| 175 | p.report({message: ACStrings.FetchDeploymentsStatusBarMessage }); | |
| 176 | getQPromisifiedClientResult(client.codepush.codePushDeployments.list(selectedApp.name, selectedApp.owner.name)) | |
| 177 | .then((deployments: models.Deployment[]) => { | |
ef14e11bmax-mironov8 years ago | 178 | resolve(deployments.sort((a, b): any => { |
| 179 | return a.name < b.name; // sort alphabetically | |
| 180 | })); | |
7574c61amax-mironov8 years ago | 181 | }); |
| 182 | }); | |
ef14e11bmax-mironov8 years ago | 183 | }) |
| 184 | .then((appDeployments: models.Deployment[]) => { | |
| 185 | let currentDeployments: CurrentAppDeployments | null = null; | |
7574c61amax-mironov8 years ago | 186 | if (appDeployments.length > 0) { |
2ceda59emax-mironov8 years ago | 187 | const deployments: Deployment[] = appDeployments.map((d) => { |
| 188 | return { | |
| 189 | name: d.name, | |
| 190 | }; | |
| 191 | }); | |
ef14e11bmax-mironov8 years ago | 192 | currentDeployments = { |
2ceda59emax-mironov8 years ago | 193 | codePushDeployments: deployments, |
7574c61amax-mironov8 years ago | 194 | currentDeploymentName: appDeployments[0].name, // Select 1st one by default |
4a66f08bmax-mironov8 years ago | 195 | }; |
e26df9e4max-mironov8 years ago | 196 | } |
2ceda59emax-mironov8 years ago | 197 | this.saveCurrentApp( |
| 198 | appCenterManager.projectRootPath, | |
| 199 | selectedAppName, | |
| 200 | OS, | |
| 201 | currentDeployments, | |
| 202 | ACConstants.AppCenterDefaultTargetBinaryVersion, | |
| 203 | ACConstants.AppCenterDefaultIsMandatoryParam) | |
ef14e11bmax-mironov8 years ago | 204 | .then((app: DefaultApp | null) => { |
4a66f08bmax-mironov8 years ago | 205 | if (app) { |
24edcbd0max-mironov8 years ago | 206 | return VsCodeUtils.ShowInformationMessage(ACStrings.YourCurrentAppAndDeployemntMsg(selected.target |
ef14e11bmax-mironov8 years ago | 207 | , app.currentAppDeployments.currentDeploymentName)); |
| 208 | } else { | |
| 209 | this.logger.error("Failed to save current app"); | |
| 210 | return Q.resolve(void 0); | |
4a66f08bmax-mironov8 years ago | 211 | } |
| 212 | }); | |
e26df9e4max-mironov8 years ago | 213 | }); |
| 214 | } | |
bb685befmax-mironov8 years ago | 215 | } |
| 216 | }); | |
| 217 | }); | |
7574c61amax-mironov8 years ago | 218 | return Q.resolve(void 0); |
640e6e98max-mironov8 years ago | 219 | } |
6a465861max-mironov8 years ago | 220 | |
| 221 | public releaseReact(client: AppCenterClient, appCenterManager: AppCenterExtensionManager): Q.Promise<void> { | |
fbbc4447Sergey Akhalkov8 years ago | 222 | let codePushRelaseParams = <ICodePushReleaseParams>{}; |
| 223 | const projectRootPath: string = appCenterManager.projectRootPath; | |
9588b66eSergey Akhalkov8 years ago | 224 | return Q.Promise<any>((resolve, reject) => { |
2ceda59emax-mironov8 years ago | 225 | let updateContentsDirectory: string; |
| 226 | let isMandatory: boolean; | |
9588b66eSergey Akhalkov8 years ago | 227 | vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: "Get Apps" }, p => { |
| 228 | return new Promise<DefaultApp>((appResolve, appReject) => { | |
| 229 | p.report({ message: ACStrings.GettingAppInfoMessage }); | |
2ceda59emax-mironov8 years ago | 230 | this.restoreCurrentApp(appCenterManager.projectRootPath) |
9588b66eSergey Akhalkov8 years ago | 231 | .then((currentApp: DefaultApp) => appResolve(<DefaultApp>currentApp)) |
| 232 | .catch(err => appReject(err)); | |
| 233 | }).then((currentApp: DefaultApp): any => { | |
| 234 | p.report({ message: ACStrings.DetectingAppVersionMessage }); | |
| 235 | if (!currentApp) { | |
ef14e11bmax-mironov8 years ago | 236 | throw new Error(`No current app has been specified.`); |
9588b66eSergey Akhalkov8 years ago | 237 | } |
2ceda59emax-mironov8 years ago | 238 | if (!currentApp.os || !reactNative.isValidOS(currentApp.os)) { |
ef14e11bmax-mironov8 years ago | 239 | throw new Error(`OS must be "android", "ios", or "windows".`); |
9588b66eSergey Akhalkov8 years ago | 240 | } |
| 241 | codePushRelaseParams.app = currentApp; | |
ef14e11bmax-mironov8 years ago | 242 | codePushRelaseParams.deploymentName = currentApp.currentAppDeployments.currentDeploymentName; |
9588b66eSergey Akhalkov8 years ago | 243 | currentApp.os = currentApp.os.toLowerCase(); |
2ceda59emax-mironov8 years ago | 244 | isMandatory = !!currentApp.isMandatory; |
| 245 | if (currentApp.targetBinaryVersion !== ACConstants.AppCenterDefaultTargetBinaryVersion) { | |
| 246 | return currentApp.targetBinaryVersion; | |
| 247 | } else { | |
| 248 | switch (currentApp.os) { | |
| 249 | case "android": return reactNative.getAndroidAppVersion(projectRootPath); | |
| 250 | case "ios": return reactNative.getiOSAppVersion(projectRootPath); | |
| 251 | case "windows": return reactNative.getWindowsAppVersion(projectRootPath); | |
| 252 | default: throw new Error(`OS must be "android", "ios", or "windows".`); | |
| 253 | } | |
9588b66eSergey Akhalkov8 years ago | 254 | } |
| 255 | }).then((appVersion: string) => { | |
192cf71emax-mironov8 years ago | 256 | p.report({ message: ACStrings.RunningBundleCommandMessage }); |
9588b66eSergey Akhalkov8 years ago | 257 | codePushRelaseParams.appVersion = appVersion; |
| 258 | return reactNative.makeUpdateContents(<BundleConfig>{ | |
| 259 | os: codePushRelaseParams.app.os, | |
| 260 | projectRootPath: projectRootPath, | |
| 261 | }); | |
| 262 | }).then((pathToUpdateContents: string) => { | |
| 263 | p.report({ message: ACStrings.ArchivingUpdateContentsMessage }); | |
| 264 | updateContentsDirectory = pathToUpdateContents; | |
2ceda59emax-mironov8 years ago | 265 | this.logger.log(`CodePush updated contents directory path: ${updateContentsDirectory}`, LogLevel.Debug); |
9588b66eSergey Akhalkov8 years ago | 266 | return updateContents.zip(pathToUpdateContents, projectRootPath); |
| 267 | }).then((pathToZippedBundle: string) => { | |
| 268 | p.report({ message: ACStrings.ReleasingUpdateContentsMessage }); | |
| 269 | codePushRelaseParams.updatedContentZipPath = pathToZippedBundle; | |
2ceda59emax-mironov8 years ago | 270 | codePushRelaseParams.isMandatory = isMandatory; |
9588b66eSergey Akhalkov8 years ago | 271 | return new Promise<any>((publishResolve, publishReject) => { |
bf370babSergey Akhalkov8 years ago | 272 | Auth.getProfile(projectRootPath) |
| 273 | .then((profile: Profile) => { | |
774e7f5fSergey Akhalkov8 years ago | 274 | return profile.accessToken; |
| 275 | }).then((token: string) => { | |
| 276 | codePushRelaseParams.token = token; | |
bf370babSergey Akhalkov8 years ago | 277 | return CodePushRelease.exec(client, codePushRelaseParams, this.logger); |
| 278 | }).then((response: any) => publishResolve(response)) | |
9588b66eSergey Akhalkov8 years ago | 279 | .catch((error: any) => publishReject(error)); |
| 280 | }); | |
| 281 | }).then((response: any) => { | |
| 282 | if (response.succeeded && response.result) { | |
24edcbd0max-mironov8 years ago | 283 | VsCodeUtils.ShowInformationMessage(`Successfully released an update to the "${codePushRelaseParams.deploymentName}" deployment of the "${codePushRelaseParams.app.appName}" app`); |
9588b66eSergey Akhalkov8 years ago | 284 | resolve(response.result); |
| 285 | } else { | |
24edcbd0max-mironov8 years ago | 286 | VsCodeUtils.ShowErrorMessage(response.errorMessage); |
9588b66eSergey Akhalkov8 years ago | 287 | } |
| 288 | fileUtils.rmDir(codePushRelaseParams.updatedContentZipPath); | |
| 289 | }).catch((error: Error) => { | |
ef14e11bmax-mironov8 years ago | 290 | if (error && error.message) { |
24edcbd0max-mironov8 years ago | 291 | VsCodeUtils.ShowErrorMessage(`An error occured on doing Code Push release. ${error.message}`); |
ef14e11bmax-mironov8 years ago | 292 | } else { |
24edcbd0max-mironov8 years ago | 293 | VsCodeUtils.ShowErrorMessage("An error occured on doing Code Push release"); |
ef14e11bmax-mironov8 years ago | 294 | } |
| 295 | | |
9588b66eSergey Akhalkov8 years ago | 296 | fileUtils.rmDir(codePushRelaseParams.updatedContentZipPath); |
42b1aa55Sergey Akhalkov8 years ago | 297 | }); |
9588b66eSergey Akhalkov8 years ago | 298 | }); |
ff1d5ab0max-mironov8 years ago | 299 | }); |
6a465861max-mironov8 years ago | 300 | } |
| 301 | | |
ef14e11bmax-mironov8 years ago | 302 | public showMenu(client: AppCenterClient, appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
2ceda59emax-mironov8 years ago | 303 | return Auth.getProfile(appCenterManager.projectRootPath).then((profile: Profile | null) => { |
ef14e11bmax-mironov8 years ago | 304 | let defaultApp: DefaultApp | null = null; |
| 305 | if (profile && profile.defaultApp) { | |
| 306 | defaultApp = profile.defaultApp; | |
| 307 | } | |
2ceda59emax-mironov8 years ago | 308 | let menuPlaceHolederTitle = ACStrings.MenuTitlePlaceholder; |
ef14e11bmax-mironov8 years ago | 309 | let appCenterMenuOptions = [ |
| 310 | { | |
2ceda59emax-mironov8 years ago | 311 | label: ACStrings.ReleaseReactMenuText(defaultApp), |
| 312 | description: "", | |
ef14e11bmax-mironov8 years ago | 313 | target: ACCommandNames.CodePushReleaseReact, |
| 314 | }, | |
| 315 | { | |
2ceda59emax-mironov8 years ago | 316 | label: ACStrings.SetCurrentAppMenuText(defaultApp), |
| 317 | description: "", | |
ef14e11bmax-mironov8 years ago | 318 | target: ACCommandNames.SetCurrentApp, |
| 319 | }, | |
| 320 | { | |
| 321 | label: ACStrings.LogoutMenuLabel, | |
2ceda59emax-mironov8 years ago | 322 | description: "", |
ef14e11bmax-mironov8 years ago | 323 | target: ACCommandNames.Logout, |
| 324 | }, | |
| 325 | ]; | |
| 326 | | |
| 327 | // This item is avaliable only if we have specified app already | |
2ceda59emax-mironov8 years ago | 328 | if (defaultApp && defaultApp.currentAppDeployments) { |
ef14e11bmax-mironov8 years ago | 329 | // Let logout command be always the last one in the list |
| 330 | appCenterMenuOptions.splice(appCenterMenuOptions.length - 1, 0, | |
| 331 | { | |
2ceda59emax-mironov8 years ago | 332 | label: ACStrings.SetCurrentAppDeploymentText(defaultApp), |
| 333 | description: "", | |
ef14e11bmax-mironov8 years ago | 334 | target: ACCommandNames.SetCurrentDeployment, |
| 335 | } | |
| 336 | ); | |
2ceda59emax-mironov8 years ago | 337 | appCenterMenuOptions.splice(appCenterMenuOptions.length - 1, 0, |
| 338 | { | |
| 339 | label: ACStrings.SetCurrentAppTargetBinaryVersionText(defaultApp), | |
| 340 | description: "", | |
| 341 | target: ACCommandNames.SetTargetBinaryVersionForRelease, | |
| 342 | } | |
| 343 | ); | |
| 344 | appCenterMenuOptions.splice(appCenterMenuOptions.length - 1, 0, | |
| 345 | { | |
| 346 | label: ACStrings.SetCurrentAppIsMandatoryText(defaultApp), | |
| 347 | description: "", | |
| 348 | target: ACCommandNames.SwitchMandatoryPropertyForRelease, | |
| 349 | } | |
| 350 | ); | |
ef14e11bmax-mironov8 years ago | 351 | } |
| 352 | | |
| 353 | return vscode.window.showQuickPick(appCenterMenuOptions, { placeHolder: menuPlaceHolederTitle }) | |
| 354 | .then((selected: {label: string, description: string, target: string}) => { | |
| 355 | if (!selected) { | |
| 356 | // user cancel selection | |
| 357 | return Q.resolve(void 0); | |
| 358 | } | |
| 359 | switch (selected.target) { | |
| 360 | case (ACCommandNames.SetCurrentApp): | |
| 361 | return this.setCurrentApp(client, appCenterManager); | |
| 362 | | |
| 363 | case (ACCommandNames.SetCurrentDeployment): | |
| 364 | return this.setCurrentDeployment(appCenterManager); | |
| 365 | | |
| 366 | case (ACCommandNames.CodePushReleaseReact): | |
| 367 | return this.releaseReact(client, appCenterManager); | |
| 368 | | |
2ceda59emax-mironov8 years ago | 369 | case (ACCommandNames.SetTargetBinaryVersionForRelease): |
| 370 | return this.setTargetBinaryVersionForRelease(appCenterManager); | |
| 371 | | |
| 372 | case (ACCommandNames.SwitchMandatoryPropertyForRelease): | |
| 373 | return this.switchIsMandatoryForRelease(appCenterManager); | |
| 374 | | |
ef14e11bmax-mironov8 years ago | 375 | case (ACCommandNames.Logout): |
| 376 | return this.logout(appCenterManager); | |
| 377 | | |
| 378 | default: | |
| 379 | // Ideally shouldn't be there :) | |
| 380 | this.logger.error("Unknown appcenter show menu command"); | |
| 381 | return Q.resolve(void 0); | |
| 382 | } | |
| 383 | }); | |
| 384 | }); | |
| 385 | } | |
| 386 | | |
2ceda59emax-mironov8 years ago | 387 | public switchIsMandatoryForRelease(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { |
| 388 | this.restoreCurrentApp(appCenterManager.projectRootPath).then((app: DefaultApp) => { | |
| 389 | if (app) { | |
| 390 | const newMandatoryValue = !!!app.isMandatory; | |
| 391 | const osVal: AppCenterOS = AppCenterOS[app.os]; | |
| 392 | this.saveCurrentApp( | |
| 393 | appCenterManager.projectRootPath, | |
| 394 | app.identifier, | |
| 395 | osVal, { | |
| 396 | currentDeploymentName: app.currentAppDeployments.currentDeploymentName, | |
| 397 | codePushDeployments: app.currentAppDeployments.codePushDeployments, | |
| 398 | }, | |
| 399 | app.targetBinaryVersion, | |
| 400 | newMandatoryValue | |
| 401 | ).then(() => { | |
b1d4af3cmax-mironov8 years ago | 402 | VsCodeUtils.ShowInformationMessage(`Changed release to ${newMandatoryValue ? "Mandatory" : "NOT Mandatory"}`); |
2ceda59emax-mironov8 years ago | 403 | }); |
| 404 | } else { | |
24edcbd0max-mironov8 years ago | 405 | VsCodeUtils.ShowInformationMessage(ACStrings.NoCurrentAppSetMsg); |
2ceda59emax-mironov8 years ago | 406 | } |
| 407 | }); | |
| 408 | return Q.resolve(void 0); | |
| 409 | } | |
| 410 | | |
| 411 | public setTargetBinaryVersionForRelease(appCenterManager: AppCenterExtensionManager): Q.Promise<void> { | |
| 412 | vscode.window.showInputBox({ prompt: ACStrings.PleaseProvideTargetBinaryVersion, ignoreFocusOut: true }) | |
| 413 | .then(appVersion => { | |
24edcbd0max-mironov8 years ago | 414 | if (appVersion === ACConstants.AppCenterDefaultTargetBinaryVersion || (appVersion && !!validRange(appVersion))) { |
2ceda59emax-mironov8 years ago | 415 | return this.restoreCurrentApp(appCenterManager.projectRootPath).then((app: DefaultApp) => { |
| 416 | if (app) { | |
| 417 | return this.saveCurrentApp( | |
| 418 | appCenterManager.projectRootPath, | |
| 419 | app.identifier, | |
| 420 | AppCenterOS[app.os], { | |
| 421 | currentDeploymentName: app.currentAppDeployments.currentDeploymentName, | |
| 422 | codePushDeployments: app.currentAppDeployments.codePushDeployments, | |
| 423 | }, | |
| 424 | appVersion, | |
| 425 | app.isMandatory | |
| 426 | ).then(() => { | |
24edcbd0max-mironov8 years ago | 427 | if (appVersion) { |
| 428 | VsCodeUtils.ShowInformationMessage(`Changed target binary version to '${appVersion}'`); | |
| 429 | } else { | |
| 430 | VsCodeUtils.ShowInformationMessage(`Changed target binary version to automatically fetched`); | |
| 431 | } | |
2ceda59emax-mironov8 years ago | 432 | }); |
| 433 | } else { | |
24edcbd0max-mironov8 years ago | 434 | VsCodeUtils.ShowInformationMessage(ACStrings.NoCurrentAppSetMsg); |
2ceda59emax-mironov8 years ago | 435 | return Q.resolve(void 0); |
| 436 | } | |
| 437 | }); | |
d3db396bmax-mironov8 years ago | 438 | } else if (appVersion === undefined) { |
| 439 | // if user press esc do nothing then | |
| 440 | return Q.resolve(void 0); | |
2ceda59emax-mironov8 years ago | 441 | } else { |
24edcbd0max-mironov8 years ago | 442 | VsCodeUtils.ShowWarningMessage(ACStrings.InvalidAppVersionParamMsg); |
2ceda59emax-mironov8 years ago | 443 | return Q.resolve(void 0); |
| 444 | } | |
| 445 | }); | |
| 446 | return Q.resolve(void 0); | |
| 447 | } | |
| 448 | | |
| 449 | private saveCurrentApp(projectRootPath: string, | |
| 450 | currentAppName: string, | |
| 451 | appOS: AppCenterOS, | |
| 452 | currentAppDeployments: CurrentAppDeployments | null, | |
| 453 | targetBinaryVersion: string, | |
| 454 | isMandatory: boolean): Q.Promise<DefaultApp | null> { | |
| 455 | const defaultApp = ACUtils.toDefaultApp(currentAppName, appOS, currentAppDeployments, targetBinaryVersion, isMandatory); | |
bb685befmax-mironov8 years ago | 456 | if (!defaultApp) { |
24edcbd0max-mironov8 years ago | 457 | VsCodeUtils.ShowWarningMessage(ACStrings.InvalidCurrentAppNameMsg); |
e26df9e4max-mironov8 years ago | 458 | return Q.resolve(null); |
bb685befmax-mironov8 years ago | 459 | } |
65b8d098max-mironov8 years ago | 460 | |
2ceda59emax-mironov8 years ago | 461 | return Auth.getProfile(projectRootPath).then((profile: Profile | null) => { |
ef14e11bmax-mironov8 years ago | 462 | if (profile) { |
| 463 | profile.defaultApp = defaultApp; | |
2ceda59emax-mironov8 years ago | 464 | profile.save(projectRootPath); |
ef14e11bmax-mironov8 years ago | 465 | return Q.resolve(defaultApp); |
| 466 | } else { | |
| 467 | // No profile - not logged in? | |
24edcbd0max-mironov8 years ago | 468 | VsCodeUtils.ShowWarningMessage(ACStrings.UserIsNotLoggedInMsg); |
ef14e11bmax-mironov8 years ago | 469 | return Q.resolve(null); |
| 470 | } | |
| 471 | }); | |
bb685befmax-mironov8 years ago | 472 | } |
| 473 | | |
2ceda59emax-mironov8 years ago | 474 | private restoreCurrentApp(projectRootPath: string): Q.Promise<DefaultApp | null> { |
| 475 | return Auth.getProfile(projectRootPath).then((profile: Profile | null) => { | |
ef14e11bmax-mironov8 years ago | 476 | if (profile && profile.defaultApp) { |
| 477 | return Q.resolve(profile.defaultApp); | |
bb685befmax-mironov8 years ago | 478 | } |
ef14e11bmax-mironov8 years ago | 479 | return Q.resolve(null); |
| 480 | }); | |
6a465861max-mironov8 years ago | 481 | } |
| 482 | | |
ff1d5ab0max-mironov8 years ago | 483 | private loginWithToken(token: string | undefined, appCenterManager: AppCenterExtensionManager) { |
| 484 | if (!token) { | |
| 485 | return; | |
| 486 | } | |
2ceda59emax-mironov8 years ago | 487 | return Auth.doTokenLogin(token, appCenterManager.projectRootPath).then((profile: Profile) => { |
84abb0c7max-mironov8 years ago | 488 | if (!profile) { |
| 489 | this.logger.log("Failed to fetch user info from server", LogLevel.Error); | |
24edcbd0max-mironov8 years ago | 490 | VsCodeUtils.ShowWarningMessage(ACStrings.FailedToExecuteLoginMsg); |
84abb0c7max-mironov8 years ago | 491 | return; |
| 492 | } | |
24edcbd0max-mironov8 years ago | 493 | VsCodeUtils.ShowInformationMessage(ACStrings.YouAreLoggedInMsg(profile.displayName)); |
ef14e11bmax-mironov8 years ago | 494 | return appCenterManager.setupAppCenterStatusBar(profile); |
6a465861max-mironov8 years ago | 495 | }); |
| 496 | } | |
bb45fbe6max-mironov8 years ago | 497 | } |