PageRenderTime 1096ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/Python/Product/PythonTools/PythonTools/Project/ImportWizard/ProjectCustomizations.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 222 lines | 165 code | 42 blank | 15 comment | 10 complexity | f38846836855abe50a83e5897318c684 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.Collections.Generic;
  17. using System.Linq;
  18. using Microsoft.Build.Construction;
  19. namespace Microsoft.PythonTools.Project.ImportWizard {
  20. abstract class ProjectCustomization {
  21. public abstract string DisplayName {
  22. get;
  23. }
  24. public override string ToString() {
  25. return DisplayName;
  26. }
  27. public abstract void Process(
  28. ProjectRootElement project,
  29. Dictionary<string, ProjectPropertyGroupElement> groups
  30. );
  31. protected static void AddOrSetProperty(ProjectRootElement project, string name, string value) {
  32. bool anySet = false;
  33. foreach (var prop in project.Properties.Where(p => p.Name == name)) {
  34. prop.Value = value;
  35. anySet = true;
  36. }
  37. if (!anySet) {
  38. project.AddProperty(name, value);
  39. }
  40. }
  41. protected static void AddOrSetProperty(ProjectPropertyGroupElement group, string name, string value) {
  42. bool anySet = false;
  43. foreach (var prop in group.Properties.Where(p => p.Name == name)) {
  44. prop.Value = value;
  45. anySet = true;
  46. }
  47. if (!anySet) {
  48. group.AddProperty(name, value);
  49. }
  50. }
  51. }
  52. class DefaultProjectCustomization : ProjectCustomization {
  53. public static readonly ProjectCustomization Instance = new DefaultProjectCustomization();
  54. private DefaultProjectCustomization() { }
  55. public override string DisplayName {
  56. get {
  57. return Strings.ImportWizardDefaultProjectCustomization;
  58. }
  59. }
  60. public override void Process(
  61. ProjectRootElement project,
  62. Dictionary<string, ProjectPropertyGroupElement> groups
  63. ) {
  64. ProjectPropertyGroupElement imports;
  65. if (!groups.TryGetValue("Imports", out imports)) {
  66. imports = project.AddPropertyGroup();
  67. }
  68. AddOrSetProperty(imports, "PtvsTargetsFile", @"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets");
  69. project.AddImport("$(PtvsTargetsFile)").Condition = "Exists($(PtvsTargetsFile))";
  70. project.AddImport(@"$(MSBuildToolsPath)\Microsoft.Common.targets").Condition = "!Exists($(PtvsTargetsFile))";
  71. }
  72. }
  73. class BottleProjectCustomization : ProjectCustomization {
  74. public static readonly ProjectCustomization Instance = new BottleProjectCustomization();
  75. private BottleProjectCustomization() { }
  76. public override string DisplayName {
  77. get {
  78. return Strings.ImportWizardBottleProjectCustomization;
  79. }
  80. }
  81. public override void Process(
  82. ProjectRootElement project,
  83. Dictionary<string, ProjectPropertyGroupElement> groups
  84. ) {
  85. ProjectPropertyGroupElement globals;
  86. if (!groups.TryGetValue("Globals", out globals)) {
  87. globals = project.AddPropertyGroup();
  88. }
  89. AddOrSetProperty(globals, "ProjectTypeGuids", "{e614c764-6d9e-4607-9337-b7073809a0bd};{1b580a1a-fdb3-4b32-83e1-6407eb2722e6};{349c5851-65df-11da-9384-00065b846f21};{888888a0-9f3d-457c-b088-3a5042f75d52}");
  90. AddOrSetProperty(globals, "LaunchProvider", PythonConstants.WebLauncherName);
  91. AddOrSetProperty(globals, "PythonDebugWebServerCommandArguments", "--debug $(CommandLineArguments)");
  92. AddOrSetProperty(globals, "PythonWsgiHandler", "{StartupModule}.wsgi_app()");
  93. project.AddImport(@"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.Web.targets");
  94. }
  95. }
  96. class DjangoProjectCustomization : ProjectCustomization {
  97. public static readonly ProjectCustomization Instance = new DjangoProjectCustomization();
  98. private DjangoProjectCustomization() { }
  99. public override string DisplayName {
  100. get {
  101. return Strings.ImportWizardDjangoProjectCustomization;
  102. }
  103. }
  104. public override void Process(
  105. ProjectRootElement project,
  106. Dictionary<string, ProjectPropertyGroupElement> groups
  107. ) {
  108. ProjectPropertyGroupElement globals;
  109. if (!groups.TryGetValue("Globals", out globals)) {
  110. globals = project.AddPropertyGroup();
  111. }
  112. AddOrSetProperty(globals, "StartupFile", "manage.py");
  113. AddOrSetProperty(globals, "ProjectTypeGuids", "{5F0BE9CA-D677-4A4D-8806-6076C0FAAD37};{349c5851-65df-11da-9384-00065b846f21};{888888a0-9f3d-457c-b088-3a5042f75d52}");
  114. AddOrSetProperty(globals, "LaunchProvider", "Django launcher");
  115. project.AddImport(@"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.Django.targets");
  116. }
  117. }
  118. class FlaskProjectCustomization : ProjectCustomization {
  119. public static readonly ProjectCustomization Instance = new FlaskProjectCustomization();
  120. private FlaskProjectCustomization() { }
  121. public override string DisplayName {
  122. get {
  123. return Strings.ImportWizardFlaskProjectCustomization;
  124. }
  125. }
  126. public override void Process(
  127. ProjectRootElement project,
  128. Dictionary<string, ProjectPropertyGroupElement> groups
  129. ) {
  130. ProjectPropertyGroupElement globals;
  131. if (!groups.TryGetValue("Globals", out globals)) {
  132. globals = project.AddPropertyGroup();
  133. }
  134. AddOrSetProperty(globals, "ProjectTypeGuids", "{789894c7-04a9-4a11-a6b5-3f4435165112};{1b580a1a-fdb3-4b32-83e1-6407eb2722e6};{349c5851-65df-11da-9384-00065b846f21};{888888a0-9f3d-457c-b088-3a5042f75d52}");
  135. AddOrSetProperty(globals, "LaunchProvider", PythonConstants.WebLauncherName);
  136. AddOrSetProperty(globals, "PythonWsgiHandler", "{StartupModule}.wsgi_app");
  137. project.AddImport(@"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.Web.targets");
  138. }
  139. }
  140. class GenericWebProjectCustomization : ProjectCustomization {
  141. public static readonly ProjectCustomization Instance = new GenericWebProjectCustomization();
  142. private GenericWebProjectCustomization() { }
  143. public override string DisplayName {
  144. get {
  145. return Strings.ImportWizardGenericWebProjectCustomization;
  146. }
  147. }
  148. public override void Process(
  149. ProjectRootElement project,
  150. Dictionary<string, ProjectPropertyGroupElement> groups
  151. ) {
  152. ProjectPropertyGroupElement globals;
  153. if (!groups.TryGetValue("Globals", out globals)) {
  154. globals = project.AddPropertyGroup();
  155. }
  156. AddOrSetProperty(globals, "ProjectTypeGuids", "{1b580a1a-fdb3-4b32-83e1-6407eb2722e6};{349c5851-65df-11da-9384-00065b846f21};{888888a0-9f3d-457c-b088-3a5042f75d52}");
  157. AddOrSetProperty(globals, "LaunchProvider", PythonConstants.WebLauncherName);
  158. project.AddImport(@"$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.Web.targets");
  159. }
  160. }
  161. class UwpProjectCustomization : ProjectCustomization {
  162. public static readonly ProjectCustomization Instance = new UwpProjectCustomization();
  163. private UwpProjectCustomization() { }
  164. public override string DisplayName {
  165. get {
  166. return Strings.ImportWizardUwpProjectCustomization;
  167. }
  168. }
  169. public override void Process(
  170. ProjectRootElement project,
  171. Dictionary<string, ProjectPropertyGroupElement> groups
  172. ) {
  173. ProjectPropertyGroupElement globals;
  174. if (!groups.TryGetValue("Globals", out globals)) {
  175. globals = project.AddPropertyGroup();
  176. }
  177. AddOrSetProperty(globals, "ProjectTypeGuids", "{2b557614-1a2b-4903-b9df-ed20d7b63f3a};{888888A0-9F3D-457C-B088-3A5042F75D52}");
  178. }
  179. }
  180. }