1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Microsoft.TeamFoundation.WorkItemTracking.Client;
5: using Microsoft.TeamFoundation.Client;
6: using System.Linq;
7: using System.Xml.Linq;
8: using System.Xml;
9: using Microsoft.Build.Utilities;
10: using Microsoft.Build.Framework;
11:
12:
13: namespace ModifyGlobalList
14: { 15: public class ClearBuildTypeFromGlobalList : Microsoft.Build.Utilities.Task
16: { 17: private string _teamFoundationServerURL;
18: private string _buildDefinitionName;
19: private string _teamProject;
20:
21: private const string GLOBAL_LIST_ELEMENT_NAME = "GLOBALLIST";
22: private const string GLOBAL_LIST_MEMBER_ELEMENT_NAME = "LISTITEM";
23: private const string GLOBAL_LIST_ELEMENT_NAME_VALUE = "Builds - ";
24:
25:
26: [Required]
27: public string TeamFoundationServerURL
28: { 29: get
30: { 31: return _teamFoundationServerURL;
32: }
33: set
34: { 35: _teamFoundationServerURL = value;
36: }
37:
38: }
39:
40: [Required]
41: public string BuildDefinitionName
42: { 43: get
44: { 45: return _buildDefinitionName;
46: }
47:
48: set
49: { 50: _buildDefinitionName = value;
51: }
52: }
53: [Required]
54: public string TeamProject
55: { 56: get
57: { 58: return _teamProject;
59: }
60:
61: set
62: { 63: _teamProject = value;
64: }
65: }
66:
67: public ClearBuildTypeFromGlobalList()
68: { 69:
70: }
71:
72: public override bool Execute()
73: { 74: bool isOk = true;
75:
76: string globalListToPrune = GLOBAL_LIST_ELEMENT_NAME_VALUE + TeamProject;
77:
78:
79:
80:
81: TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(_teamFoundationServerURL);
82: WorkItemStore _wiStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
83:
84:
85: XmlDocument globalLists = _wiStore.ExportGlobalLists();
86: XElement xGlobalLists = XElement.Load(new XmlNodeReader(globalLists));
87: IEnumerable<XElement> listItemsToDelete = (from listItems in
88: (
89: (from gl in xGlobalLists.Elements(GLOBAL_LIST_ELEMENT_NAME)
90: where ((string)gl.Attribute("name")).ToUpper() == globalListToPrune.ToUpper() 91: select gl
92: ).First<XElement>()
93: ).Elements(GLOBAL_LIST_MEMBER_ELEMENT_NAME)
94: where ((string)listItems.Attribute("value")).ToUpper().StartsWith(_buildDefinitionName.ToUpper()) 95: select listItems
96: );
97:
98:
99:
100: listItemsToDelete.DescendantsAndSelf().Remove<XElement>();
101:
102: _wiStore.ImportGlobalLists(xGlobalLists.ToString());
103:
104: return isOk;
105:
106: }
107:
108: }
109: }
110:
111:
112:
113:
114: