6fd7954ab42c949f141c90ef3aeeec886a16c22d
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / AssignProjectConfiguration.cs
1 //
2 // AssignProjectConfiguration.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 //
8 // (C) 2006 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30
31 using System;
32 using System.IO;
33 using System.Collections.Generic;
34 using System.Xml;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.Tasks {
39         public class AssignProjectConfiguration : ResolveProjectBase {
40         
41                 ITaskItem[]     assignedProjects;
42                 string          solutionConfigurationContents;
43                 ITaskItem[]     unassignedProjects;
44         
45                 public AssignProjectConfiguration ()
46                 {
47                 }
48                 
49                 [MonoTODO]
50                 public override bool Execute ()
51                 {
52                         if (String.IsNullOrEmpty (solutionConfigurationContents))
53                                 return true;
54
55                         XmlReader xr = null;
56                         Dictionary<Guid, string> guidToConfigPlatform = null;
57                         try {
58                                 xr = XmlReader.Create (new StringReader (solutionConfigurationContents));
59                                 guidToConfigPlatform = new Dictionary<Guid, string> ();
60
61                                 xr.Read ();
62                                 while (!xr.EOF) {
63                                         xr.Read ();
64                                         if (xr.NodeType != XmlNodeType.Element)
65                                                 continue;
66
67                                         string guid_str = xr.GetAttribute ("Project");
68                                         string config_str = xr.ReadString ();
69
70                                         Guid guid;
71                                         if (!String.IsNullOrEmpty (guid_str) && !String.IsNullOrEmpty (config_str) &&
72                                                 TryParseGuid (guid_str, out guid))
73                                                 guidToConfigPlatform [guid] = config_str;
74                                 }
75                         } catch (XmlException xe) {
76                                 Log.LogError ("XmlException while parsing SolutionConfigurationContents: {0}",
77                                                 xe.ToString ());
78
79                                 return false;
80                         } finally {
81                                 ((IDisposable)xr).Dispose ();
82                         }
83
84                         List<ITaskItem> tempAssignedProjects = new List<ITaskItem> ();
85                         List<ITaskItem> tempUnassignedProjects = new List<ITaskItem> ();
86                         foreach (ITaskItem item in ProjectReferences) {
87                                 string config;
88
89                                 string guid_str = item.GetMetadata ("Project");
90
91                                 Guid guid = Guid.Empty;
92                                 if (!string.IsNullOrEmpty(guid_str) && !TryParseGuid (guid_str, out guid)) {
93                                         Log.LogError ("Project reference '{0}' has invalid or missing guid for metadata 'Project'.",
94                                                         item.ItemSpec);
95                                         return false;
96                                 }
97
98                                 if (guid != Guid.Empty && guidToConfigPlatform.TryGetValue (guid, out config)) {
99                                         string [] parts = config.Split (new char [] {'|'}, 2);
100
101                                         ITaskItem new_item = new TaskItem (item);
102
103                                         new_item.SetMetadata ("SetConfiguration", "Configuration=" + parts [0]);
104                                         new_item.SetMetadata ("SetPlatform", "Platform=" +
105                                                         ((parts.Length > 1) ? parts [1] : String.Empty));
106
107                                         tempAssignedProjects.Add (new_item);
108                                 } else {
109                                         Log.LogWarning ("Project reference '{0}' could not be resolved.",
110                                                         item.ItemSpec);
111                                         tempUnassignedProjects.Add (item);
112                                 }
113                         }
114
115                         assignedProjects = tempAssignedProjects.ToArray ();
116                         unassignedProjects = tempUnassignedProjects.ToArray ();
117
118                         return true;
119                 }
120
121                 bool TryParseGuid (string guid_str, out Guid guid)
122                 {
123                         guid = Guid.Empty;
124                         try {
125                                 guid = new Guid (guid_str);
126                         } catch (ArgumentNullException) {
127                                 return false;
128                         } catch (FormatException) {
129                                 return false;
130                         } catch (OverflowException) {
131                                 return false;
132                         }
133
134                         return true;
135                 }
136
137
138                 [Output]
139                 public ITaskItem[] AssignedProjects {
140                         get { return assignedProjects; }
141                         set { assignedProjects = value; }
142                 }
143                 
144                 public string SolutionConfigurationContents {
145                         get { return solutionConfigurationContents; }
146                         set { solutionConfigurationContents = value; }
147                 }
148                 
149                 [Output]
150                 public ITaskItem[] UnassignedProjects {
151                         get { return unassignedProjects; }
152                         set { unassignedProjects = value; }
153                 }
154         }
155 }
156