implement Project.RemoveProperty() and ProjectProperty.Predecessor (i.e. property...
authorAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Mon, 14 Oct 2013 11:20:34 +0000 (20:20 +0900)
committerAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Fri, 29 Nov 2013 09:20:01 +0000 (18:20 +0900)
mcs/class/Microsoft.Build/Microsoft.Build.Evaluation/Project.cs
mcs/class/Microsoft.Build/Microsoft.Build.Evaluation/ProjectProperty.cs
mcs/class/Microsoft.Build/Test/Microsoft.Build.Evaluation/ProjectPropertyTest.cs

index 40e1775a3ec849054443b33975fb35d6c187c413..6e4bf58c3452d3924317120f98d56a8d71b8f631 100644 (file)
@@ -364,7 +364,11 @@ namespace Microsoft.Build.Evaluation
 
                public bool RemoveProperty (ProjectProperty property)
                {
-                       throw new NotImplementedException ();
+                       var removed = properties.FirstOrDefault (p => p.Name == property.Name);
+                       if (removed == null)
+                               return false;
+                       properties.Remove (removed);
+                       return true;
                }
 
                public void Save ()
index b930f9b7d98506f6b5cfae0794fd66af385e1a52..c7c8e36abe1c63372615bf278292f0567fa00875 100644 (file)
@@ -26,6 +26,7 @@
 //
 
 using System;
+using System.Linq;
 using Microsoft.Build.Construction;
 
 namespace Microsoft.Build.Evaluation
@@ -78,14 +79,23 @@ namespace Microsoft.Build.Evaluation
        
        internal abstract class BaseProjectProperty : ProjectProperty
        {
-               public BaseProjectProperty (Project project, PropertyType propertyType)
+               public BaseProjectProperty (Project project, PropertyType propertyType, string name)
                        : base (project)
                {
                        property_type = propertyType;
+                       this.name = name;
+                       predecessor = project.Properties.FirstOrDefault (p => p.Name == name);
+                       if (predecessor != null)
+                               project.RemoveProperty (predecessor);
                }
                
                PropertyType property_type;
                
+               readonly string name;
+               public override string Name {
+                       get { return name; }
+               }
+               
                public override bool IsEnvironmentProperty {
                        get { return property_type == PropertyType.Environment; }
                }
@@ -100,26 +110,22 @@ namespace Microsoft.Build.Evaluation
                public override bool IsReservedProperty {
                        get { return property_type == PropertyType.Reserved; }
                }
+               readonly ProjectProperty predecessor; 
                public override ProjectProperty Predecessor {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+                       get { return predecessor; }
                }
        }
        
        internal class XmlProjectProperty : BaseProjectProperty
        {
                public XmlProjectProperty (Project project, ProjectPropertyElement xml, PropertyType propertyType)
-                       : base (project, propertyType)
+                       : base (project, propertyType, xml.Name)
                {
                        this.xml = xml;
                }
                
                ProjectPropertyElement xml;
                
-               public override string Name {
-                       get { return xml.Name; }
-               }
                public override string UnevaluatedValue {
                        get { return xml.Value; }
                        set { xml.Value = value; }
@@ -132,17 +138,13 @@ namespace Microsoft.Build.Evaluation
        internal class EnvironmentProjectProperty : BaseProjectProperty
        {
                public EnvironmentProjectProperty (Project project, string name, string value)
-                       : base (project, PropertyType.Environment)
+                       : base (project, PropertyType.Environment, name)
                {
-                       this.name = name;
                        this.value = value;
                }
                
-               readonly string name, value;
+               readonly string value;
                
-               public override string Name {
-                       get { return name; }
-               }
                public override string UnevaluatedValue {
                        get { return value; }
                        set { throw new InvalidOperationException (string.Format ("You cannot change value of environment property '{0}'.", Name)); }
@@ -155,17 +157,13 @@ namespace Microsoft.Build.Evaluation
        internal class GlobalProjectProperty : BaseProjectProperty
        {
                public GlobalProjectProperty (Project project, string name, string value)
-                       : base (project, PropertyType.Global)
+                       : base (project, PropertyType.Global, name)
                {
-                       this.name = name;
                        this.value = value;
                }
                
-               readonly string name, value;
+               readonly string value;
                
-               public override string Name {
-                       get { return name; }
-               }
                public override string UnevaluatedValue {
                        get { return value; }
                        set { throw new InvalidOperationException (string.Format ("You cannot change value of global property '{0}'.", Name)); }
index 8ce5468f21f0c2c97024b834ec8faf2700c57950..2e235cc9664120f454a33d63919bfa468755f796 100644 (file)
@@ -20,6 +20,9 @@ namespace MonoTests.Microsoft.Build.Evaluation
   <PropertyGroup>
     <Foo>Bar</Foo>
     <Item/>
+    <X>1</X>
+    <X>2</X>
+    <PATH>overriden</PATH>
   </PropertyGroup>
 </Project>";
                        var xml = XmlReader.Create (new StringReader (project_xml));
@@ -31,6 +34,18 @@ namespace MonoTests.Microsoft.Build.Evaluation
                        Assert.AreEqual ("Bar", prop.UnevaluatedValue, "#2");
                        prop.UnevaluatedValue = "x";
                        Assert.AreEqual ("x", pe.Value, "#3");
+                       
+                       prop = proj.Properties.First (p => p.Name == "X");
+                       Assert.AreEqual ("2", prop.UnevaluatedValue, "#4");
+                       Assert.IsNotNull (prop.Predecessor, "#5");
+                       Assert.AreEqual ("1", prop.Predecessor.UnevaluatedValue, "#6");
+                       
+                       // environment property could also be Predecessor (and removed...maybe.
+                       // I could reproduce only NRE = .NET bug with environment property so far.)
+                       prop = proj.Properties.First (p => p.Name == "PATH");
+                       Assert.AreEqual ("2", prop.UnevaluatedValue, "#7");
+                       Assert.IsNotNull (prop.Predecessor, "#5");
+                       Assert.AreEqual ("1", prop.Predecessor.UnevaluatedValue, "#6");
                }
                
                [Test]