[MS.Build] for item metadata access expression, remove surrounding ' and ".
authorAtsushi Eno <atsushieno@gmail.com>
Fri, 16 May 2014 12:58:38 +0000 (21:58 +0900)
committerAtsushi Eno <atsushieno@gmail.com>
Fri, 16 May 2014 13:00:05 +0000 (22:00 +0900)
The test case would explain what is not desired there.

mcs/class/Microsoft.Build/Microsoft.Build.Internal/ExpressionEvaluator.cs
mcs/class/Microsoft.Build/Test/Microsoft.Build.Execution/ProjectInstanceTest.cs

index 2cc8388ea40d77cbeb24ede0073997494a3bc66a..e41f7818d7914fc83e8f6f16f382ce1dd5f4528d 100644 (file)
@@ -420,16 +420,23 @@ namespace Microsoft.Build.Internal.Expressions
                        if (!items.Any ())
                                return null;
                        if (Application.Expressions == null)
-                               return string.Join (";", items.Select (item => context.EvaluateItem (itemType, item)));
+                               return string.Join (";", items.Select (item => Unwrap (context.EvaluateItem (itemType, item))));
                        else
                                return string.Join (";", items.Select (item => {
                                        context.ContextItem = item;
-                                       var ret = string.Concat (Application.Expressions.Select (e => e.EvaluateAsString (context)));
+                                       var ret = Unwrap (string.Concat (Application.Expressions.Select (e => e.EvaluateAsString (context))));
                                        context.ContextItem = null;
                                        return ret;
                                }));
                }
-               
+
+               static string Unwrap (string ret)
+               {
+                       if (ret.Length < 2 || ret [0] != ret [ret.Length - 1] || ret [0] != '"' && ret [0] != '\'')
+                               return ret;
+                       return ret.Substring (1, ret.Length - 2);
+               }
+
                public override object EvaluateAsObject (EvaluationContext context)
                {
                        return EvaluateAsString (context);
@@ -458,7 +465,7 @@ namespace Microsoft.Build.Internal.Expressions
                        var values = items.Select (i => (i is ProjectItem) ? ((ProjectItem) i).GetMetadataValue (metadataName) : ((ProjectItemInstance) i).GetMetadataValue (metadataName)).Where (s => !string.IsNullOrEmpty (s));
                        return string.Join (";", values);
                }
-               
+
                public override object EvaluateAsObject (EvaluationContext context)
                {
                        return EvaluateAsString (context);
index 1f33a64907f1d50bb28d0db228a32a89bde9bc4c..cf41928c19c0d7db1ac9a7541a7dfff4a8395ee6 100644 (file)
@@ -235,6 +235,36 @@ namespace MonoTests.Microsoft.Build.Execution
                        var proj = new ProjectInstance (root);
                        Assert.AreEqual ("xxx;yyy", proj.ExpandString ("@(FOO)"), "#1"); // so, metadata is gone...
                }
+
+               [Test]
+               public void EvaluatePropertyWithQuotation ()
+               {
+                       string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>
+  <ItemGroup>
+    <Foo Include='abc/xxx.txt' />
+  </ItemGroup>
+  <PropertyGroup>
+    <B>foobar</B>
+  </PropertyGroup>
+  <Target Name='default'>
+    <CreateProperty Value=""@(Foo->'%(Filename)%(Extension)')"">
+      <Output TaskParameter='Value' PropertyName='P' />
+    </CreateProperty>
+    <CreateProperty Value='$(B)|$(P)'>
+      <Output TaskParameter='Value' PropertyName='Q' />
+    </CreateProperty>
+  </Target>
+</Project>";
+                       var xml = XmlReader.Create (new StringReader (project_xml));
+                       var root = ProjectRootElement.Create (xml);
+                       root.FullPath = "ProjectInstanceTest.EvaluatePropertyWithQuotation.proj";
+                       var proj = new ProjectInstance (root);
+                       proj.Build ();
+                       var p = proj.GetProperty ("P");
+                       Assert.AreEqual ("xxx.txt", p.EvaluatedValue, "#1");
+                       var q = proj.GetProperty ("Q");
+                       Assert.AreEqual ("foobar|xxx.txt", q.EvaluatedValue, "#2");
+               }
        }
        
        namespace SubNamespace