Merge pull request #2338 from BogdanovKirill/httpwritefix3
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / AssignProjectConfiguration.cs
index 5f762c6e538ad201985e3dcab90f7cfb4787a0bf..6fd7954ab42c949f141c90ef3aeeec886a16c22d 100644 (file)
@@ -3,8 +3,10 @@
 //
 // Author:
 //   Marek Sieradzki (marek.sieradzki@gmail.com)
+//   Ankit Jain (jankit@novell.com)
 //
 // (C) 2006 Marek Sieradzki
+// Copyright 2009 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-#if NET_2_0
 
 using System;
 using System.IO;
+using System.Collections.Generic;
+using System.Xml;
 using Microsoft.Build.Framework;
+using Microsoft.Build.Utilities;
 
 namespace Microsoft.Build.Tasks {
        public class AssignProjectConfiguration : ResolveProjectBase {
@@ -45,9 +49,92 @@ namespace Microsoft.Build.Tasks {
                [MonoTODO]
                public override bool Execute ()
                {
-                       return false;
+                       if (String.IsNullOrEmpty (solutionConfigurationContents))
+                               return true;
+
+                       XmlReader xr = null;
+                       Dictionary<Guid, string> guidToConfigPlatform = null;
+                       try {
+                               xr = XmlReader.Create (new StringReader (solutionConfigurationContents));
+                               guidToConfigPlatform = new Dictionary<Guid, string> ();
+
+                               xr.Read ();
+                               while (!xr.EOF) {
+                                       xr.Read ();
+                                       if (xr.NodeType != XmlNodeType.Element)
+                                               continue;
+
+                                       string guid_str = xr.GetAttribute ("Project");
+                                       string config_str = xr.ReadString ();
+
+                                       Guid guid;
+                                       if (!String.IsNullOrEmpty (guid_str) && !String.IsNullOrEmpty (config_str) &&
+                                               TryParseGuid (guid_str, out guid))
+                                               guidToConfigPlatform [guid] = config_str;
+                               }
+                       } catch (XmlException xe) {
+                               Log.LogError ("XmlException while parsing SolutionConfigurationContents: {0}",
+                                               xe.ToString ());
+
+                               return false;
+                       } finally {
+                               ((IDisposable)xr).Dispose ();
+                       }
+
+                       List<ITaskItem> tempAssignedProjects = new List<ITaskItem> ();
+                       List<ITaskItem> tempUnassignedProjects = new List<ITaskItem> ();
+                       foreach (ITaskItem item in ProjectReferences) {
+                               string config;
+
+                               string guid_str = item.GetMetadata ("Project");
+
+                               Guid guid = Guid.Empty;
+                               if (!string.IsNullOrEmpty(guid_str) && !TryParseGuid (guid_str, out guid)) {
+                                       Log.LogError ("Project reference '{0}' has invalid or missing guid for metadata 'Project'.",
+                                                       item.ItemSpec);
+                                       return false;
+                               }
+
+                               if (guid != Guid.Empty && guidToConfigPlatform.TryGetValue (guid, out config)) {
+                                       string [] parts = config.Split (new char [] {'|'}, 2);
+
+                                       ITaskItem new_item = new TaskItem (item);
+
+                                       new_item.SetMetadata ("SetConfiguration", "Configuration=" + parts [0]);
+                                       new_item.SetMetadata ("SetPlatform", "Platform=" +
+                                                       ((parts.Length > 1) ? parts [1] : String.Empty));
+
+                                       tempAssignedProjects.Add (new_item);
+                               } else {
+                                       Log.LogWarning ("Project reference '{0}' could not be resolved.",
+                                                       item.ItemSpec);
+                                       tempUnassignedProjects.Add (item);
+                               }
+                       }
+
+                       assignedProjects = tempAssignedProjects.ToArray ();
+                       unassignedProjects = tempUnassignedProjects.ToArray ();
+
+                       return true;
                }
-               
+
+               bool TryParseGuid (string guid_str, out Guid guid)
+               {
+                       guid = Guid.Empty;
+                       try {
+                               guid = new Guid (guid_str);
+                       } catch (ArgumentNullException) {
+                               return false;
+                       } catch (FormatException) {
+                               return false;
+                       } catch (OverflowException) {
+                               return false;
+                       }
+
+                       return true;
+               }
+
+
                [Output]
                public ITaskItem[] AssignedProjects {
                        get { return assignedProjects; }
@@ -67,4 +154,3 @@ namespace Microsoft.Build.Tasks {
        }
 }
 
-#endif
\ No newline at end of file