PageRenderTime 86ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Editions/TeamExplorer10/Main/TeamProjectManager.Modules.BuildProcessTemplates/Tasks.cs

#
C# | 161 lines | 151 code | 8 blank | 2 comment | 26 complexity | a73e034891629716e81f7361a8419cfe MD5 | raw file
  1. using Microsoft.TeamFoundation.Build.Client;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using TeamProjectManager.Common.Infrastructure;
  7. namespace TeamProjectManager.Modules.BuildProcessTemplates
  8. {
  9. internal static class Tasks
  10. {
  11. public static IList<BuildProcessTemplateInfo> GetBuildProcessTemplates(ApplicationTask task, IBuildServer buildServer, IEnumerable<string> teamProjectNames)
  12. {
  13. var processTemplates = new List<BuildProcessTemplateInfo>();
  14. var step = 0;
  15. foreach (var teamProjectName in teamProjectNames)
  16. {
  17. task.SetProgress(step++, string.Format(CultureInfo.CurrentCulture, "Processing Team Project \"{0}\"", teamProjectName));
  18. try
  19. {
  20. var teamProjectBuildDefinitions = buildServer.QueryBuildDefinitions(teamProjectName, QueryOptions.Process);
  21. foreach (var processTemplate in buildServer.QueryProcessTemplates(teamProjectName))
  22. {
  23. var processTemplateBuildDefinitions = new List<IBuildDefinition>();
  24. foreach (var teamProjectBuildDefinition in teamProjectBuildDefinitions)
  25. {
  26. if (teamProjectBuildDefinition.Process != null && BuildProcessTemplateInfo.AreEquivalent(teamProjectBuildDefinition.Process, processTemplate))
  27. {
  28. processTemplateBuildDefinitions.Add(teamProjectBuildDefinition);
  29. }
  30. }
  31. processTemplates.Add(new BuildProcessTemplateInfo(processTemplate, processTemplateBuildDefinitions));
  32. }
  33. }
  34. catch (Exception exc)
  35. {
  36. task.SetWarning(string.Format(CultureInfo.CurrentCulture, "An error occurred while processing Team Project \"{0}\"", teamProjectName), exc);
  37. }
  38. if (task.IsCanceled)
  39. {
  40. task.Status = "Canceled";
  41. break;
  42. }
  43. }
  44. return processTemplates;
  45. }
  46. public static void RegisterBuildProcessTemplate(ApplicationTask task, IBuildServer buildServer, IEnumerable<string> teamProjectNames, string templateServerPath, ProcessTemplateType templateType, bool registerIfTemplateDoesNotExist, bool unregisterAllOtherTemplates, bool unregisterAllOtherTemplatesIncludesUpgradeTemplate, bool simulate)
  47. {
  48. var step = 0;
  49. foreach (var teamProjectName in teamProjectNames)
  50. {
  51. try
  52. {
  53. task.SetProgress(step++, string.Format(CultureInfo.CurrentCulture, "Processing Team Project \"{0}\"", teamProjectName));
  54. var allTemplates = buildServer.QueryProcessTemplates(teamProjectName);
  55. var matchingTemplates = allTemplates.Where(t => t.ServerPath.Equals(templateServerPath, StringComparison.OrdinalIgnoreCase)).ToList();
  56. if (unregisterAllOtherTemplates)
  57. {
  58. var templatesToUnregister = allTemplates.Except(matchingTemplates);
  59. if (!unregisterAllOtherTemplatesIncludesUpgradeTemplate)
  60. {
  61. templatesToUnregister = templatesToUnregister.Where(t => t.TemplateType != ProcessTemplateType.Upgrade);
  62. }
  63. foreach (var templateToUnregister in templatesToUnregister)
  64. {
  65. task.Status = string.Format(CultureInfo.CurrentCulture, "Unregistering existing build process template \"{0}\"", templateToUnregister.ServerPath);
  66. var buildDefinitions = buildServer.QueryBuildDefinitions(teamProjectName, QueryOptions.Process);
  67. foreach (var buildDefinition in buildDefinitions)
  68. {
  69. if (buildDefinition.Process != null && BuildProcessTemplateInfo.AreEquivalent(buildDefinition.Process, templateToUnregister))
  70. {
  71. task.SetWarning(string.Format(CultureInfo.CurrentCulture, "WARNING - The build \"{0}\" uses the build process template \"{1}\" that is being unregistered", buildDefinition.Name, templateToUnregister.ServerPath));
  72. }
  73. }
  74. if (!simulate)
  75. {
  76. templateToUnregister.Delete();
  77. }
  78. }
  79. }
  80. if (!(unregisterAllOtherTemplates && unregisterAllOtherTemplatesIncludesUpgradeTemplate))
  81. {
  82. if (templateType == ProcessTemplateType.Default || templateType == ProcessTemplateType.Upgrade)
  83. {
  84. // There can be only one upgrade or default template for a team project.
  85. // Make sure there isn't already a template with that type.
  86. foreach (var template in allTemplates.Except(matchingTemplates).Where(t => t.TemplateType == templateType))
  87. {
  88. task.Status = string.Format(CultureInfo.CurrentCulture, "Changing type of existing build process template \"{0}\" from \"{1}\" to \"{2}\"", template.ServerPath, template.TemplateType.ToString(), ProcessTemplateType.Custom.ToString());
  89. if (!simulate)
  90. {
  91. template.TemplateType = ProcessTemplateType.Custom;
  92. template.Save();
  93. }
  94. }
  95. }
  96. }
  97. if (registerIfTemplateDoesNotExist && !matchingTemplates.Any())
  98. {
  99. task.Status = string.Format(CultureInfo.CurrentCulture, "Registering new build process template \"{0}\" as type \"{1}\"", templateServerPath, templateType.ToString());
  100. if (!simulate)
  101. {
  102. var template = buildServer.CreateProcessTemplate(teamProjectName, templateServerPath);
  103. template.TemplateType = templateType;
  104. template.Save();
  105. }
  106. }
  107. else
  108. {
  109. foreach (var template in matchingTemplates.Where(t => t.TemplateType != templateType))
  110. {
  111. task.Status = string.Format(CultureInfo.CurrentCulture, "Changing type of existing build process template \"{0}\" from \"{1}\" to \"{2}\"", template.ServerPath, template.TemplateType.ToString(), templateType.ToString());
  112. if (!simulate)
  113. {
  114. template.TemplateType = templateType;
  115. template.Save();
  116. }
  117. }
  118. }
  119. }
  120. catch (Exception exc)
  121. {
  122. task.SetError(string.Format(CultureInfo.CurrentCulture, "An error occurred while registering the build process template \"{0}\" for Team Project \"{1}\"", templateServerPath, teamProjectName), exc);
  123. }
  124. if (task.IsCanceled)
  125. {
  126. task.Status = "Canceled";
  127. break;
  128. }
  129. }
  130. }
  131. public static void UnregisterBuildProcessTemplates(ApplicationTask task, IEnumerable<IProcessTemplate> buildProcessTemplates)
  132. {
  133. var step = 0;
  134. foreach (var buildProcessTemplate in buildProcessTemplates)
  135. {
  136. try
  137. {
  138. task.SetProgress(step++, string.Format(CultureInfo.CurrentCulture, "Unregistering build process template \"{0}\"", buildProcessTemplate.ServerPath));
  139. buildProcessTemplate.Delete();
  140. }
  141. catch (Exception exc)
  142. {
  143. task.SetError(string.Format(CultureInfo.CurrentCulture, "An error occurred while unregistering the build process template \"{0}\" for Team Project \"{1}\"", buildProcessTemplate.ServerPath, buildProcessTemplate.TeamProject), exc);
  144. }
  145. if (task.IsCanceled)
  146. {
  147. task.Status = "Canceled";
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. }