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.
34 using System.Collections.Generic;
36 using Microsoft.Build.Framework;
37 using Microsoft.Build.Utilities;
39 namespace Microsoft.Build.Tasks {
40 public class AssignProjectConfiguration : ResolveProjectBase {
42 ITaskItem[] assignedProjects;
43 string solutionConfigurationContents;
44 ITaskItem[] unassignedProjects;
46 public AssignProjectConfiguration ()
51 public override bool Execute ()
53 if (String.IsNullOrEmpty (solutionConfigurationContents))
57 Dictionary<Guid, string> guidToConfigPlatform = null;
59 xr = XmlReader.Create (new StringReader (solutionConfigurationContents));
60 guidToConfigPlatform = new Dictionary<Guid, string> ();
65 if (xr.NodeType != XmlNodeType.Element)
68 string guid_str = xr.GetAttribute ("Project");
69 string config_str = xr.ReadString ();
72 if (!String.IsNullOrEmpty (guid_str) && !String.IsNullOrEmpty (config_str) &&
73 TryParseGuid (guid_str, out guid))
74 guidToConfigPlatform [guid] = config_str;
76 } catch (XmlException xe) {
77 Log.LogError ("XmlException while parsing SolutionConfigurationContents: {0}",
82 ((IDisposable)xr).Dispose ();
85 List<ITaskItem> tempAssignedProjects = new List<ITaskItem> ();
86 List<ITaskItem> tempUnassignedProjects = new List<ITaskItem> ();
87 foreach (ITaskItem item in ProjectReferences) {
90 string guid_str = item.GetMetadata ("Project");
92 if (!TryParseGuid (guid_str, out guid)) {
93 Log.LogError ("Project reference '{0}' has invalid or missing guid for metadata 'Project'.",
98 if (guidToConfigPlatform.TryGetValue (guid, out config)) {
99 string [] parts = config.Split (new char [] {'|'}, 2);
101 ITaskItem new_item = new TaskItem (item);
103 new_item.SetMetadata ("SetConfiguration", "Configuration=" + parts [0]);
104 new_item.SetMetadata ("SetPlatform", "Platform=" +
105 ((parts.Length > 1) ? parts [1] : String.Empty));
107 tempAssignedProjects.Add (new_item);
109 Log.LogWarning ("Project reference '{0}' could not be resolved.",
111 tempUnassignedProjects.Add (item);
115 assignedProjects = tempAssignedProjects.ToArray ();
116 unassignedProjects = tempUnassignedProjects.ToArray ();
121 bool TryParseGuid (string guid_str, out Guid guid)
125 guid = new Guid (guid_str);
126 } catch (ArgumentNullException) {
128 } catch (FormatException) {
130 } catch (OverflowException) {
139 public ITaskItem[] AssignedProjects {
140 get { return assignedProjects; }
141 set { assignedProjects = value; }
144 public string SolutionConfigurationContents {
145 get { return solutionConfigurationContents; }
146 set { solutionConfigurationContents = value; }
150 public ITaskItem[] UnassignedProjects {
151 get { return unassignedProjects; }
152 set { unassignedProjects = value; }