Skip to content

Commit

Permalink
Merge branch 'kcamp-GH-2610' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Aug 22, 2019
2 parents 325efb8 + dc30b91 commit b4b77c7
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Cake.Core.Tests/Data/MethodAliasGeneratorData.cs
Expand Up @@ -240,5 +240,11 @@ public static void NonGeneric_ExtensionMethodWithParameterAttributes(this ICakeC
{
throw new NotImplementedException();
}

[CakeMethodAlias]
public static dynamic NonGeneric_ExtensionMethodWithDynamicReturnValue(this ICakeContext context)
{
throw new NotImplementedException();
}
}
}
12 changes: 12 additions & 0 deletions src/Cake.Core.Tests/Data/PropertyAliasGeneratorData.cs
Expand Up @@ -44,6 +44,12 @@ public static int NonCached_Value_Type(this ICakeContext context)
return 42;
}

[CakePropertyAlias]
public static dynamic NonCached_Dynamic_Type(this ICakeContext context)
{
return new { };
}

[CakePropertyAlias(Cache = true)]
public static string Cached_Reference_Type(this ICakeContext context)
{
Expand All @@ -56,6 +62,12 @@ public static bool Cached_Value_Type(this ICakeContext context)
return true;
}

[CakePropertyAlias(Cache = true)]
public static dynamic Cached_Dynamic_Type(this ICakeContext context)
{
return new { };
}

[CakePropertyAlias]
[Obsolete]
public static int NonCached_Obsolete_ImplicitWarning_NoMessage(this ICakeContext context)
Expand Down
@@ -0,0 +1,4 @@
public dynamic NonGeneric_ExtensionMethodWithDynamicReturnValue()
{
return Cake.Core.Tests.Data.MethodAliasGeneratorData.NonGeneric_ExtensionMethodWithDynamicReturnValue(Context);
}
@@ -0,0 +1,12 @@
private dynamic _Cached_Dynamic_Type;
public dynamic Cached_Dynamic_Type
{
get
{
if (_Cached_Dynamic_Type==null)
{
_Cached_Dynamic_Type = Cake.Core.Tests.Data.PropertyAliasGeneratorData.Cached_Dynamic_Type(Context);
}
return _Cached_Dynamic_Type;
}
}
@@ -0,0 +1,7 @@
public dynamic NonCached_Dynamic_Type
{
get
{
return Cake.Core.Tests.Data.PropertyAliasGeneratorData.NonCached_Dynamic_Type(Context);
}
}
Expand Up @@ -55,6 +55,7 @@ public void Should_Throw_If_Method_Is_Null()
[InlineData("NonGeneric_ExtensionMethodWithOutputParameter")]
[InlineData("NonGeneric_ExtensionMethodWithGenericCollectionOfNestedType")]
[InlineData("NonGeneric_ExtensionMethodWithParameterAttributes")]
[InlineData("NonGeneric_ExtensionMethodWithDynamicReturnValue")]
public void Should_Return_Correct_Generated_Code_For_Non_Generic_Methods(string name)
{
// Given
Expand Down
Expand Up @@ -112,6 +112,7 @@ public void Should_Throw_If_Property_Alias_Returns_Void()

[Theory]
[InlineData("NonCached_Value_Type")]
[InlineData("NonCached_Dynamic_Type")]
public void Should_Return_Correct_Generated_Code_For_Non_Cached_Properties(string name)
{
// Given
Expand All @@ -127,6 +128,7 @@ public void Should_Return_Correct_Generated_Code_For_Non_Cached_Properties(strin
[Theory]
[InlineData("Cached_Reference_Type")]
[InlineData("Cached_Value_Type")]
[InlineData("Cached_Dynamic_Type")]
public void Should_Return_Correct_Generated_Code_For_Cached_Properties(string name)
{
// Given
Expand Down
11 changes: 8 additions & 3 deletions src/Cake.Core/Scripting/CodeGen/MethodAliasGenerator.cs
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using Cake.Core.Annotations;

Expand Down Expand Up @@ -148,9 +149,13 @@ public static string Generate(MethodInfo method, out string hash)

private static string GetReturnType(MethodInfo method)
{
return method.ReturnType == typeof(void)
? "void"
: method.ReturnType.GetFullName();
if (method.ReturnType == typeof(void))
{
return "void";
}

var isDynamic = method.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(DynamicAttribute), true).Any();
return isDynamic ? "dynamic" : method.ReturnType.GetFullName();
}

private static IEnumerable<string> GetProxyParameters(IEnumerable<ParameterInfo> parameters, bool includeType)
Expand Down
3 changes: 2 additions & 1 deletion src/Cake.Core/Scripting/CodeGen/PropertyAliasGenerator.cs
Expand Up @@ -251,7 +251,8 @@ private static string GenerateCachedCode(MethodInfo method, out string hash)

private static string GetReturnType(MethodInfo method)
{
return method.ReturnType.GetFullName();
var isDynamic = method.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(DynamicAttribute), true).Any();
return isDynamic ? "dynamic" : method.ReturnType.GetFullName();
}

private static string GetObsoleteMessage(MethodInfo method, ObsoleteAttribute attribute)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/Cake.Core/Scripting/AddinDirective.cake
Expand Up @@ -11,6 +11,8 @@ Task("Cake.Core.Scripting.AddinDirective.LoadNetStandardAddin")
var script = $@"#addin nuget:{Paths.Resources}/Cake.Core/Scripting/netstandard2.addin/bin/Release?package=netstandard2.addin&version=1.0.0
Information(""Magic number: {0}"", GetMagicNumber(false));
Information(""The answer to life: {0}"", TheAnswerToLife);
Information(""Get Dynamic Magic Number: {0}"", GetDynamicMagicNumber(false).MagicNumber);
Information(""Dynamic Magic Number: {0}"", TheDynamicAnswerToLife.TheAnswerToLife);
";
CakeExecuteExpression(script,
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Dynamic;
using Cake.Core;
using Cake.Core.Annotations;

Expand Down Expand Up @@ -26,4 +27,20 @@ public static int TheAnswerToLife(this ICakeContext context)
{
return 42;
}

[CakePropertyAlias(Cache = true)]
public static dynamic TheDynamicAnswerToLife(this ICakeContext context)
{
dynamic value = new ExpandoObject();
value.TheAnswerToLife = context.TheAnswerToLife();
return value;
}

[CakeMethodAlias]
public static dynamic GetDynamicMagicNumber(this ICakeContext context, bool value)
{
dynamic result = new ExpandoObject();
result.MagicNumber = context.GetMagicNumber(value);
return result;
}
}

0 comments on commit b4b77c7

Please sign in to comment.