Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Evaluation / ProjectProperty.cs
index 2c6f4fc45377919c600d01039d27145f0d3d8530..f2bdc9eda791767beeea6248d967f3b58ad2bddd 100644 (file)
@@ -30,6 +30,9 @@ using System;
 using System.Linq;
 using Microsoft.Build.Construction;
 using Microsoft.Build.Internal;
+using System.Collections.Generic;
+using System.Reflection;
+using System.IO;
 
 namespace Microsoft.Build.Evaluation
 {
@@ -44,9 +47,8 @@ namespace Microsoft.Build.Evaluation
                        Project = project;
                }
 
-               string evaluated_value; // see UpdateEvaluatedValue().
                public string EvaluatedValue {
-                       get { return evaluated_value; }
+                       get { return InternalEvaluatedValue; }
                }
 
                public abstract bool IsEnvironmentProperty { get; }
@@ -58,6 +60,10 @@ namespace Microsoft.Build.Evaluation
 
                public abstract bool IsReservedProperty { get; }
 
+               internal virtual bool IsWellKnownProperty {
+                       get { return false; }
+               }
+
                public abstract string Name { get; }
 
                public abstract ProjectProperty Predecessor { get; }
@@ -68,10 +74,7 @@ namespace Microsoft.Build.Evaluation
 
                public abstract ProjectPropertyElement Xml { get; }
                
-               internal void UpdateEvaluatedValue ()
-               {
-                       evaluated_value = Project.ExpandString (UnevaluatedValue);
-               }
+               internal abstract string InternalEvaluatedValue { get; }
        }
 
        // copy from MS.Build.Engine/BuildProperty.cs
@@ -89,7 +92,7 @@ namespace Microsoft.Build.Evaluation
                {
                        property_type = propertyType;
                        this.name = name;
-                       predecessor = project.Properties.FirstOrDefault (p => p.Name == name);
+                       predecessor = project.Properties.FirstOrDefault (p => p.Name.Equals (name, StringComparison.OrdinalIgnoreCase));
                        if (predecessor != null)
                                project.RemoveProperty (predecessor);
                }
@@ -108,9 +111,7 @@ namespace Microsoft.Build.Evaluation
                        get { return property_type == PropertyType.Global; }
                }
                public override bool IsImported {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+                       get { return false; }
                }
                public override bool IsReservedProperty {
                        get { return property_type == PropertyType.Reserved; }
@@ -121,36 +122,102 @@ namespace Microsoft.Build.Evaluation
                }
        }
        
-       internal class XmlProjectProperty : BaseProjectProperty
+       internal abstract class ImmutableProjectProperty : BaseProjectProperty
+       {
+               public ImmutableProjectProperty (Project project, PropertyType propertyType, string name)
+                       : base (project, propertyType, name)
+               {
+               }
+               
+               internal override string InternalEvaluatedValue {
+                       get { return UnevaluatedValue; }
+               }
+       }
+       
+       internal abstract class MutableProjectProperty : BaseProjectProperty
        {
-               public XmlProjectProperty (Project project, ProjectPropertyElement xml, PropertyType propertyType)
+               public MutableProjectProperty (Project project, PropertyType propertyType, string name)
+                       : base (project, propertyType, name)
+               {
+               }
+               
+               string evaluated_value; // see UpdateEvaluatedValue().
+               internal void UpdateEvaluatedValue ()
+               {
+                       evaluated_value = Project.ExpandString (UnevaluatedValue);
+               }
+               
+               internal override string InternalEvaluatedValue {
+                       get { return evaluated_value; }
+               }
+       }
+       
+       internal class XmlProjectProperty : MutableProjectProperty
+       {
+               public XmlProjectProperty (Project project, ProjectPropertyElement xml, PropertyType propertyType, bool isImported)
                        : base (project, propertyType, xml.Name)
                {
                        this.xml = xml;
+                       this.is_imported = isImported;
                        UpdateEvaluatedValue ();
                }
                
-               ProjectPropertyElement xml;
+               readonly ProjectPropertyElement xml;
+               readonly bool is_imported;
+               
+               public override bool IsImported {
+                       get { return is_imported; }
+               }
                
                public override string UnevaluatedValue {
                        get { return xml.Value; }
                        set { xml.Value = value; }
                }
+               
                public override ProjectPropertyElement Xml {
                        get { return xml; }
                }
        }
        
-       internal class EnvironmentProjectProperty : BaseProjectProperty
+       internal class EnvironmentProjectProperty : ImmutableProjectProperty
        {
-               public EnvironmentProjectProperty (Project project, string name, string value)
+               static string extensions_path;
+               internal static string DefaultExtensionsPath {
+                       get {
+                               if (extensions_path == null) {
+                                       // NOTE: code from mcs/tools/gacutil/driver.cs
+                                       PropertyInfo gac = typeof (System.Environment).GetProperty (
+                                                       "GacPath", BindingFlags.Static | BindingFlags.NonPublic);
+
+                                       if (gac != null) {
+                                               MethodInfo get_gac = gac.GetGetMethod (true);
+                                               string gac_path = (string) get_gac.Invoke (null, null);
+                                               extensions_path = Path.GetFullPath (Path.Combine (
+                                                                       gac_path, Path.Combine ("..", "xbuild")));
+                                       }
+                               }
+                               return extensions_path;
+                       }
+               }
+               
+               public EnvironmentProjectProperty (Project project, string name, string value, bool wellknown = false)
                        : base (project, PropertyType.Environment, name)
                {
                        this.value = value;
-                       UpdateEvaluatedValue ();
+                       this.wellknown = wellknown;
                }
                
                readonly string value;
+               readonly bool wellknown;
+
+               internal override bool IsWellKnownProperty {
+                       get { return wellknown; }
+               }
+
+               // It can override possible another environment vairable property BUT never gives Predecessor.
+               public override ProjectProperty Predecessor {
+                       get { return null; }
+               }
                
                public override string UnevaluatedValue {
                        get { return value; }
@@ -161,13 +228,12 @@ namespace Microsoft.Build.Evaluation
                }
        }
        
-       internal class GlobalProjectProperty : BaseProjectProperty
+       internal class GlobalProjectProperty : ImmutableProjectProperty
        {
                public GlobalProjectProperty (Project project, string name, string value)
                        : base (project, PropertyType.Global, name)
                {
                        this.value = value;
-                       UpdateEvaluatedValue ();
                }
                
                readonly string value;
@@ -181,7 +247,7 @@ namespace Microsoft.Build.Evaluation
                }
        }
        
-       internal class ManuallyAddedProjectProperty : BaseProjectProperty
+       internal class ManuallyAddedProjectProperty : MutableProjectProperty
        {
                public ManuallyAddedProjectProperty (Project project, string name, string value)
                        : base (project, PropertyType.Normal, name)
@@ -195,4 +261,28 @@ namespace Microsoft.Build.Evaluation
                        get { return null; }
                }
        }
+       
+       internal class ReservedProjectProperty : ImmutableProjectProperty
+       {
+               public ReservedProjectProperty (Project project, string name, Func<string> value)
+                       : base (project, PropertyType.Reserved, name)
+               {
+                       this.value = value;
+               }
+
+               // make sure it does not give access to any possible attempted overrrides.
+               public override ProjectProperty Predecessor {
+                       get { return null; }
+               }
+
+               readonly Func<string> value;
+               public override string UnevaluatedValue {
+                       get { return value (); }
+                       set { throw new InvalidOperationException (string.Format ("You cannot change value of reserved property '{0}'.", Name)); }
+               }
+               
+               public override ProjectPropertyElement Xml {
+                       get { return null; }
+               }
+       }
 }