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;
44 Dictionary<Guid, string> guidToConfigPlatform;
45 Dictionary<string, string> absolutePathToConfigPlatform;
47 public AssignProjectConfiguration ()
52 public override bool Execute ()
54 if (String.IsNullOrEmpty (solutionConfigurationContents))
58 guidToConfigPlatform = new Dictionary<Guid, string> ();
59 absolutePathToConfigPlatform = new Dictionary<string, string> ();
61 xr = XmlReader.Create (new StringReader (solutionConfigurationContents));
66 if (xr.NodeType != XmlNodeType.Element)
69 string guid_str = xr.GetAttribute ("Project");
70 string abs_path = xr.GetAttribute ("AbsolutePath");
71 string config_str = xr.ReadString ();
73 if (String.IsNullOrEmpty (config_str))
77 if (TryParseGuid (guid_str, out guid))
78 guidToConfigPlatform [guid] = config_str;
80 if (!String.IsNullOrEmpty (abs_path)) {
81 abs_path = Path.GetFullPath (abs_path);
82 absolutePathToConfigPlatform [abs_path] = config_str;
85 } catch (XmlException xe) {
86 Log.LogError ("XmlException while parsing SolutionConfigurationContents: {0}",
91 ((IDisposable)xr).Dispose ();
94 List<ITaskItem> tempAssignedProjects = new List<ITaskItem> ();
95 List<ITaskItem> tempUnassignedProjects = new List<ITaskItem> ();
96 foreach (ITaskItem item in ProjectReferences) {
97 string config = GetConfigPlatformFromProjectReference (item);
99 if (String.IsNullOrEmpty (config)) {
100 tempUnassignedProjects.Add (item);
104 string [] parts = config.Split (new char [] {'|'}, 2);
106 ITaskItem new_item = new TaskItem (item);
108 new_item.SetMetadata ("SetConfiguration", "Configuration=" + parts [0]);
109 new_item.SetMetadata ("SetPlatform", "Platform=" +
110 ((parts.Length > 1) ? parts [1] : String.Empty));
112 tempAssignedProjects.Add (new_item);
115 assignedProjects = tempAssignedProjects.ToArray ();
116 unassignedProjects = tempUnassignedProjects.ToArray ();
121 string GetConfigPlatformFromProjectReference (ITaskItem item)
123 string guid_str = item.GetMetadata ("Project");
124 string proj_full_path = item.GetMetadata ("FullPath");
127 Guid guid = Guid.Empty;
128 if (TryParseGuid (guid_str, out guid) && guidToConfigPlatform.TryGetValue (guid, out config))
131 string abs_path = item.GetMetadata ("FullPath");
132 if (absolutePathToConfigPlatform.TryGetValue (abs_path, out config))
138 bool TryParseGuid (string guid_str, out Guid guid)
141 if (String.IsNullOrEmpty (guid_str))
145 guid = new Guid (guid_str);
146 } catch (ArgumentNullException) {
148 } catch (FormatException) {
150 } catch (OverflowException) {
159 public ITaskItem[] AssignedProjects {
160 get { return assignedProjects; }
161 set { assignedProjects = value; }
164 public string SolutionConfigurationContents {
165 get { return solutionConfigurationContents; }
166 set { solutionConfigurationContents = value; }
170 public ITaskItem[] UnassignedProjects {
171 get { return unassignedProjects; }
172 set { unassignedProjects = value; }