Skip to content

Commit

Permalink
Making Xamarin.iOS use the PCL code generator
Browse files Browse the repository at this point in the history
Adding tuple support to it
  • Loading branch information
darkl committed Jul 19, 2018
1 parent 6b96deb commit b2a12f8
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 31 deletions.
1 change: 0 additions & 1 deletion NuGet/WampSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
<dependency id="NETStandard.Library" version="[2.0.0, )" />
<dependency id="System.Collections.Immutable" version="[1.3.1, )" />
<dependency id="System.Reactive" version="[4.0.0, )" />
<dependency id="System.Reflection.DispatchProxy" version="[4.4.0, )" />
<dependency id="System.Threading.Tasks.Dataflow" version="[4.7.0, )" />
<dependency id="System.ValueTuple" version="[4.4.0, )" />
</group>
Expand Down
6 changes: 1 addition & 5 deletions src/Xamarin.iOS10/WampSharp/WampSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);NET45;NETCORE;PCL;LIBLOG_PUBLIC;LIBLOG_PORTABLE;TPL;ASYNC_LOCAL;ASYNC;WAMPCRA;DISPATCH_PROXY;MANUAL_PROXY;</DefineConstants>
<DefineConstants>$(DefineConstants);NET45;NETCORE;PCL;LIBLOG_PUBLIC;LIBLOG_PORTABLE;TPL;ASYNC_LOCAL;ASYNC;WAMPCRA;MANUAL_PROXY;</DefineConstants>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand All @@ -35,8 +35,4 @@
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Reflection.DispatchProxy" Version="4.4.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if !CASTLE && !DISPATCH_PROXY
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -119,9 +118,13 @@ private IProxyMethodWriter GetWriter(MethodInfo method)

private static void ValidateMethod(MethodInfo method)
{
if (typeof (Task).IsAssignableFrom(method.ReturnType))
if (!typeof(Task).IsAssignableFrom(method.ReturnType))
{
if (method.IsDefined(typeof (WampProgressiveResultProcedureAttribute)))
MethodInfoValidation.ValidateSyncMethod(method);
}
else
{
if (method.IsDefined(typeof(WampProgressiveResultProcedureAttribute)))
{
MethodInfoValidation.ValidateProgressiveMethod(method);
}
Expand Down Expand Up @@ -158,5 +161,4 @@ private string GetInterfaceName(Type interfaceType)
}
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#if !CASTLE && !DISPATCH_PROXY
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using WampSharp.Core.Utilities.ValueTuple;
using TaskExtensions = WampSharp.Core.Utilities.TaskExtensions;

namespace WampSharp.CodeGeneration
{
Expand Down Expand Up @@ -96,6 +100,51 @@ private static string Prettify(string fullName)

return result;
}

public static string GetFormattedReturnType(MethodInfo method)
{
if (!method.ReturnsTuple())
{
return FormatType(method.ReturnType);
}
else
{
Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);

IEnumerable<Type> tupleElementTypes =
returnType.GetValueTupleElementTypes();

TupleElementNamesAttribute tupleElementNamesAttribute =
method.ReturnParameter.GetCustomAttribute<TupleElementNamesAttribute>();

IEnumerable<string> tupleElementsIdentifiers;

IEnumerable<string> tupleElementTypesIdentifiers = tupleElementTypes
.Select(x => FormatType(x));

if (tupleElementNamesAttribute == null)
{
tupleElementsIdentifiers = tupleElementTypesIdentifiers;
}
else
{
tupleElementsIdentifiers =
tupleElementTypesIdentifiers
.Zip(tupleElementNamesAttribute.TransformNames,
(type, name) => $"{type} {name}");
}

string tupleIdentifierContent = string.Join(", ", tupleElementsIdentifiers);

if (typeof(Task).IsAssignableFrom(method.ReturnType))
{
return $"Task<({tupleIdentifierContent})>";
}
else
{
return $"({tupleIdentifierContent})";
}
}
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if !CASTLE && !DISPATCH_PROXY
using System.Reflection;
using System.Reflection;

namespace WampSharp.CodeGeneration
{
Expand All @@ -8,5 +7,4 @@ internal interface IProxyMethodWriter
string WriteField(int methodIndex, MethodInfo method);
string WriteMethod(int methodIndex, MethodInfo method);
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if !CASTLE && !DISPATCH_PROXY
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -110,7 +109,7 @@ public string WriteMethod(int methodIndex, MethodInfo method)
new[] {"this", "___array"});

Type returnType = method.ReturnType;
dictionary["returnType"] = FormatTypeExtensions.FormatType(returnType);
dictionary["returnType"] = FormatTypeExtensions.GetFormattedReturnType(method);

if (returnType != typeof(void))
{
Expand Down Expand Up @@ -165,5 +164,4 @@ private string GetCallerParameter(ParameterInfo parameter)
}
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if !CASTLE && !DISPATCH_PROXY
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -87,7 +86,7 @@ public string WriteMethod(int methodIndex, MethodInfo method)
new[] {"this"}.Concat(specialParameters).Concat(methodCallParameters
.Select(x => x.Name)));

dictionary["returnType"] = FormatTypeExtensions.FormatType(method.ReturnType);
dictionary["returnType"] = FormatTypeExtensions.GetFormattedReturnType(method);



Expand Down Expand Up @@ -137,5 +136,4 @@ public string WriteField(int methodIndex, MethodInfo method)
return CodeGenerationHelper.ProcessTemplate(mFieldTemplate, dictionary);
}
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if !CASTLE && !DISPATCH_PROXY
using System;
using System.Collections.Generic;
using System.Reflection;
Expand Down Expand Up @@ -30,5 +29,4 @@ public static string ProcessTemplate(string template, IDictionary<string, string
return result;
}
}
}
#endif
}

0 comments on commit b2a12f8

Please sign in to comment.