implement several ProjectProperty types.
authorAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Mon, 14 Oct 2013 10:31:35 +0000 (19:31 +0900)
committerAtsushi Eno <atsushieno@veritas-vos-liberabit.com>
Fri, 29 Nov 2013 09:20:00 +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 df4513941db169a45898bdcd18cdf6184d47ccbe..40e1775a3ec849054443b33975fb35d6c187c413 100644 (file)
@@ -40,6 +40,7 @@ using Microsoft.Build.Internal;
 using Microsoft.Build.Execution;
 using Microsoft.Build.Framework;
 using Microsoft.Build.Logging;
+using System.Collections;
 
 namespace Microsoft.Build.Evaluation
 {
@@ -160,6 +161,10 @@ namespace Microsoft.Build.Evaluation
                
                void ProcessXml ()
                {
+                       foreach (DictionaryEntry p in Environment.GetEnvironmentVariables ())
+                               this.properties.Add (new EnvironmentProjectProperty (this, (string) p.Key, (string) p.Value));
+                       foreach (var p in GlobalProperties)
+                               this.properties.Add (new GlobalProjectProperty (this, p.Key, p.Value));
                        foreach (var child in Xml.Children) {
                                if (child is ProjectPropertyGroupElement)
                                        foreach (var p in ((ProjectPropertyGroupElement) child).Properties)
index c91d14ac2256108b6e6f0669c6b0e86530ec1a4b..b930f9b7d98506f6b5cfae0794fd66af385e1a52 100644 (file)
@@ -76,16 +76,14 @@ namespace Microsoft.Build.Evaluation
                Environment
        }
        
-       internal class XmlProjectProperty : ProjectProperty
+       internal abstract class BaseProjectProperty : ProjectProperty
        {
-               public XmlProjectProperty (Project project, ProjectPropertyElement xml, PropertyType propertyType)
+               public BaseProjectProperty (Project project, PropertyType propertyType)
                        : base (project)
                {
-                       this.xml = xml;
                        property_type = propertyType;
                }
                
-               ProjectPropertyElement xml;
                PropertyType property_type;
                
                public override bool IsEnvironmentProperty {
@@ -102,14 +100,26 @@ namespace Microsoft.Build.Evaluation
                public override bool IsReservedProperty {
                        get { return property_type == PropertyType.Reserved; }
                }
-               public override string Name {
-                       get { return xml.Name; }
-               }
                public override ProjectProperty Predecessor {
                        get {
                                throw new NotImplementedException ();
                        }
                }
+       }
+       
+       internal class XmlProjectProperty : BaseProjectProperty
+       {
+               public XmlProjectProperty (Project project, ProjectPropertyElement xml, PropertyType propertyType)
+                       : base (project, propertyType)
+               {
+                       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; }
@@ -118,5 +128,51 @@ namespace Microsoft.Build.Evaluation
                        get { return xml; }
                }
        }
+       
+       internal class EnvironmentProjectProperty : BaseProjectProperty
+       {
+               public EnvironmentProjectProperty (Project project, string name, string value)
+                       : base (project, PropertyType.Environment)
+               {
+                       this.name = name;
+                       this.value = value;
+               }
+               
+               readonly string name, 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)); }
+               }
+               public override ProjectPropertyElement Xml {
+                       get { return null; }
+               }
+       }
+       
+       internal class GlobalProjectProperty : BaseProjectProperty
+       {
+               public GlobalProjectProperty (Project project, string name, string value)
+                       : base (project, PropertyType.Global)
+               {
+                       this.name = name;
+                       this.value = value;
+               }
+               
+               readonly string name, 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)); }
+               }
+               public override ProjectPropertyElement Xml {
+                       get { return null; }
+               }
+       }
 }
 
index 2569b5b8c8d35ebaf3c0d90201eb7fc17108823d..8ce5468f21f0c2c97024b834ec8faf2700c57950 100644 (file)
@@ -6,6 +6,7 @@ using Microsoft.Build.Construction;
 using Microsoft.Build.Evaluation;
 using Microsoft.Build.Execution;
 using NUnit.Framework;
+using System.Collections.Generic;
 
 namespace MonoTests.Microsoft.Build.Evaluation
 {
@@ -21,7 +22,6 @@ namespace MonoTests.Microsoft.Build.Evaluation
     <Item/>
   </PropertyGroup>
 </Project>";
-                       var path = "file://localhost/foo.xml";
                        var xml = XmlReader.Create (new StringReader (project_xml));
                        var root = ProjectRootElement.Create (xml);
                        var pe = root.Properties.First ();
@@ -32,6 +32,32 @@ namespace MonoTests.Microsoft.Build.Evaluation
                        prop.UnevaluatedValue = "x";
                        Assert.AreEqual ("x", pe.Value, "#3");
                }
+               
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void UpdateGlobalPropertyValue ()
+               {
+                       string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
+                       var xml = XmlReader.Create (new StringReader (project_xml));
+                       var props = new Dictionary<string, string> ();
+                       props.Add ("GP", "GV");
+                       var root = ProjectRootElement.Create (xml);
+                       var proj = new Project (root, props, null);
+                       var pe = proj.Properties.First (p => p.IsGlobalProperty);
+                       pe.UnevaluatedValue = "UPDATED";
+               }
+               
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void UpdateEnvironmentPropertyValue ()
+               {
+                       string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' />";
+                       var xml = XmlReader.Create (new StringReader (project_xml));
+                       var root = ProjectRootElement.Create (xml);
+                       var proj = new Project (root);
+                       var pe = proj.Properties.First (p => p.IsEnvironmentProperty);
+                       pe.UnevaluatedValue = "UPDATED";
+               }
        }
 }