Merge pull request #704 from jgagnon/master
[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                                 Guid guid;
91                                 if (!TryParseGuid (guid_str, out guid)) {
92                                         Log.LogError ("Project reference '{0}' has invalid or missing guid for metadata 'Project'.",
93                                                         item.ItemSpec);
94                                         return false;
95                                 }
96
97                                 if (guidToConfigPlatform.TryGetValue (guid, out config)) {
98                                         string [] parts = config.Split (new char [] {'|'}, 2);
99
100                                         ITaskItem new_item = new TaskItem (item);
101
102                                         new_item.SetMetadata ("SetConfiguration", "Configuration=" + parts [0]);
103                                         new_item.SetMetadata ("SetPlatform", "Platform=" +
104                                                         ((parts.Length > 1) ? parts [1] : String.Empty));
105
106                                         tempAssignedProjects.Add (new_item);
107                                 } else {
108                                         Log.LogWarning ("Project reference '{0}' could not be resolved.",
109                                                         item.ItemSpec);
110                                         tempUnassignedProjects.Add (item);
111                                 }
112                         }
113
114                         assignedProjects = tempAssignedProjects.ToArray ();
115                         unassignedProjects = tempUnassignedProjects.ToArray ();
116
117                         return true;
118                 }
119
120                 bool TryParseGuid (string guid_str, out Guid guid)
121                 {
122                         guid = Guid.Empty;
123                         try {
124                                 guid = new Guid (guid_str);
125                         } catch (ArgumentNullException) {
126                                 return false;
127                         } catch (FormatException) {
128                                 return false;
129                         } catch (OverflowException) {
130                                 return false;
131                         }
132
133                         return true;
134                 }
135
136
137                 [Output]
138                 public ITaskItem[] AssignedProjects {
139                         get { return assignedProjects; }
140                         set { assignedProjects = value; }
141                 }
142                 
143                 public string SolutionConfigurationContents {
144                         get { return solutionConfigurationContents; }
145                         set { solutionConfigurationContents = value; }
146                 }
147                 
148                 [Output]
149                 public ITaskItem[] UnassignedProjects {
150                         get { return unassignedProjects; }
151                         set { unassignedProjects = value; }
152                 }
153         }
154 }
155