PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/Product/ProjectWizards/CloudServiceWizard.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 188 lines | 138 code | 26 blank | 24 comment | 27 complexity | 87e27fcd03c5120891b51793a9abbf81 MD5 | raw file
  1. // Python Tools for Visual Studio
  2. // Copyright(c) Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the License); you may not use
  6. // this file except in compliance with the License. You may obtain a copy of the
  7. // License at http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  10. // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
  11. // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. //
  14. // See the Apache Version 2.0 License for specific language governing
  15. // permissions and limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Reflection;
  22. using Microsoft.PythonTools.Infrastructure;
  23. using Microsoft.VisualStudio.Settings;
  24. using Microsoft.VisualStudio.TemplateWizard;
  25. using Microsoft.VisualStudioTools;
  26. using Project = EnvDTE.Project;
  27. using ProjectItem = EnvDTE.ProjectItem;
  28. namespace Microsoft.PythonTools.ProjectWizards {
  29. public sealed class CloudServiceWizard : IWizard {
  30. private IWizard _wizard;
  31. private readonly bool _recommendUpgrade;
  32. #if DEV14
  33. const string AzureToolsDownload = "http://go.microsoft.com/fwlink/?linkid=518003";
  34. #elif DEV15
  35. const string AzureToolsDownload = "http://go.microsoft.com/fwlink/?LinkId=760649";
  36. #else
  37. #error Unsupported VS version
  38. #endif
  39. const string DontShowUpgradeDialogAgainProperty = "SuppressUpgradeAzureTools";
  40. private static bool ShouldRecommendUpgrade(Assembly asm) {
  41. var attr = asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
  42. .OfType<AssemblyFileVersionAttribute>()
  43. .FirstOrDefault();
  44. Version ver;
  45. if (attr != null && Version.TryParse(attr.Version, out ver)) {
  46. Debug.WriteLine(ver);
  47. // 2.4 is where we added integration, so we should recommend it
  48. // to people who don't have it.
  49. return ver < new Version(2, 4);
  50. }
  51. return false;
  52. }
  53. public CloudServiceWizard() {
  54. try {
  55. // If we fail to find the wizard, we will redirect the user to
  56. // the WebPI download.
  57. var asm = Assembly.Load("Microsoft.VisualStudio.CloudService.Wizard,Version=1.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a");
  58. _recommendUpgrade = ShouldRecommendUpgrade(asm);
  59. var type = asm.GetType("Microsoft.VisualStudio.CloudService.Wizard.CloudServiceWizard");
  60. _wizard = type.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[0]) as IWizard;
  61. } catch (ArgumentException) {
  62. } catch (BadImageFormatException) {
  63. } catch (IOException) {
  64. } catch (MemberAccessException) {
  65. }
  66. }
  67. public void BeforeOpeningFile(ProjectItem projectItem) {
  68. if (_wizard != null) {
  69. _wizard.BeforeOpeningFile(projectItem);
  70. }
  71. }
  72. public void ProjectFinishedGenerating(Project project) {
  73. if (_wizard != null) {
  74. _wizard.ProjectFinishedGenerating(project);
  75. }
  76. }
  77. public void ProjectItemFinishedGenerating(ProjectItem projectItem) {
  78. if (_wizard != null) {
  79. _wizard.ProjectItemFinishedGenerating(projectItem);
  80. }
  81. }
  82. public void RunFinished() {
  83. if (_wizard != null) {
  84. _wizard.RunFinished();
  85. }
  86. }
  87. public bool ShouldAddProjectItem(string filePath) {
  88. if (_wizard != null) {
  89. return _wizard.ShouldAddProjectItem(filePath);
  90. }
  91. return false;
  92. }
  93. public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) {
  94. var provider = WizardHelpers.GetProvider(automationObject);
  95. if (_wizard == null) {
  96. try {
  97. Directory.Delete(replacementsDictionary["$destinationdirectory$"]);
  98. Directory.Delete(replacementsDictionary["$solutiondirectory$"]);
  99. } catch {
  100. // If it fails (doesn't exist/contains files/read-only), let the directory stay.
  101. }
  102. var dlg = new TaskDialog(provider) {
  103. Title = Strings.ProductTitle,
  104. MainInstruction = Strings.AzureToolsRequired,
  105. Content = Strings.AzureToolsInstallInstructions,
  106. AllowCancellation = true
  107. };
  108. var download = new TaskDialogButton(Strings.DownloadAndInstall);
  109. dlg.Buttons.Add(download);
  110. dlg.Buttons.Add(TaskDialogButton.Cancel);
  111. if (dlg.ShowModal() == download) {
  112. Process.Start(new ProcessStartInfo(AzureToolsDownload));
  113. throw new WizardCancelledException();
  114. }
  115. // User cancelled, so go back to the New Project dialog
  116. throw new WizardBackoutException();
  117. }
  118. if (_recommendUpgrade) {
  119. var sm = SettingsManagerCreator.GetSettingsManager(provider);
  120. var store = sm.GetReadOnlySettingsStore(SettingsScope.UserSettings);
  121. if (!store.CollectionExists(PythonConstants.DontShowUpgradeDialogAgainCollection) ||
  122. !store.GetBoolean(PythonConstants.DontShowUpgradeDialogAgainCollection, DontShowUpgradeDialogAgainProperty, false)) {
  123. var dlg = new TaskDialog(provider) {
  124. Title = Strings.ProductTitle,
  125. MainInstruction = Strings.AzureToolsUpgradeRecommended,
  126. Content = Strings.AzureToolsUpgradeInstructions,
  127. AllowCancellation = true,
  128. VerificationText = Strings.DontShowAgain
  129. };
  130. var download = new TaskDialogButton(Strings.DownloadAndInstall);
  131. dlg.Buttons.Add(download);
  132. var cont = new TaskDialogButton(Strings.ContinueWithoutAzureToolsUpgrade);
  133. dlg.Buttons.Add(cont);
  134. dlg.Buttons.Add(TaskDialogButton.Cancel);
  135. var response = dlg.ShowModal();
  136. if (response != cont) {
  137. try {
  138. Directory.Delete(replacementsDictionary["$destinationdirectory$"]);
  139. Directory.Delete(replacementsDictionary["$solutiondirectory$"]);
  140. } catch {
  141. // If it fails (doesn't exist/contains files/read-only), let the directory stay.
  142. }
  143. }
  144. if (dlg.SelectedVerified) {
  145. var rwStore = sm.GetWritableSettingsStore(SettingsScope.UserSettings);
  146. rwStore.CreateCollection(PythonConstants.DontShowUpgradeDialogAgainCollection);
  147. rwStore.SetBoolean(PythonConstants.DontShowUpgradeDialogAgainCollection, DontShowUpgradeDialogAgainProperty, true);
  148. }
  149. if (response == download) {
  150. Process.Start(new ProcessStartInfo(AzureToolsDownload));
  151. throw new WizardCancelledException();
  152. } else if (response == TaskDialogButton.Cancel) {
  153. // User cancelled, so go back to the New Project dialog
  154. throw new WizardBackoutException();
  155. }
  156. }
  157. }
  158. // Run the original wizard to get the right replacements
  159. _wizard.RunStarted(automationObject, replacementsDictionary, runKind, customParams);
  160. }
  161. }
  162. }