2 // AssignProjectConfiguration.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2006 Marek Sieradzki
9 // Copyright 2009 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections.Generic;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace Microsoft.Build.Tasks {
39 public class AssignProjectConfiguration : ResolveProjectBase {
41 ITaskItem[] assignedProjects;
42 string solutionConfigurationContents;
43 ITaskItem[] unassignedProjects;
45 public AssignProjectConfiguration ()
50 public override bool Execute ()
52 if (String.IsNullOrEmpty (solutionConfigurationContents))
56 Dictionary<Guid, string> guidToConfigPlatform = null;
58 xr = XmlReader.Create (new StringReader (solutionConfigurationContents));
59 guidToConfigPlatform = new Dictionary<Guid, string> ();
64 if (xr.NodeType != XmlNodeType.Element)
67 string guid_str = xr.GetAttribute ("Project");
68 string config_str = xr.ReadString ();
71 if (!String.IsNullOrEmpty (guid_str) && !String.IsNullOrEmpty (config_str) &&
72 TryParseGuid (guid_str, out guid))
73 guidToConfigPlatform [guid] = config_str;
75 } catch (XmlException xe) {
76 Log.LogError ("XmlException while parsing SolutionConfigurationContents: {0}",
81 ((IDisposable)xr).Dispose ();
84 List<ITaskItem> tempAssignedProjects = new List<ITaskItem> ();
85 List<ITaskItem> tempUnassignedProjects = new List<ITaskItem> ();
86 foreach (ITaskItem item in ProjectReferences) {
89 string guid_str = item.GetMetadata ("Project");
91 if (!TryParseGuid (guid_str, out guid)) {
92 Log.LogError ("Project reference '{0}' has invalid or missing guid for metadata 'Project'.",
97 if (guidToConfigPlatform.TryGetValue (guid, out config)) {
98 string [] parts = config.Split (new char [] {'|'}, 2);
100 ITaskItem new_item = new TaskItem (item);
102 new_item.SetMetadata ("SetConfiguration", "Configuration=" + parts [0]);
103 new_item.SetMetadata ("SetPlatform", "Platform=" +
104 ((parts.Length > 1) ? parts [1] : String.Empty));
106 tempAssignedProjects.Add (new_item);
108 Log.LogWarning ("Project reference '{0}' could not be resolved.",
110 tempUnassignedProjects.Add (item);
114 assignedProjects = tempAssignedProjects.ToArray ();
115 unassignedProjects = tempUnassignedProjects.ToArray ();
120 bool TryParseGuid (string guid_str, out Guid guid)
124 guid = new Guid (guid_str);
125 } catch (ArgumentNullException) {
127 } catch (FormatException) {
129 } catch (OverflowException) {
138 public ITaskItem[] AssignedProjects {
139 get { return assignedProjects; }
140 set { assignedProjects = value; }
143 public string SolutionConfigurationContents {
144 get { return solutionConfigurationContents; }
145 set { solutionConfigurationContents = value; }
149 public ITaskItem[] UnassignedProjects {
150 get { return unassignedProjects; }
151 set { unassignedProjects = value; }