-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feature/xamarin essentials events #1682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
glennawatson
merged 11 commits into
reactiveui:master
from
michaelstonis:feature/xamarin-essentials-events
Aug 7, 2018
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63e0f2a
Merge pull request #1 from reactiveui/master
michaelstonis 93703bf
Provides functionality to generate event mapping for Xamarin.Essentials
michaelstonis 57d9305
removed WinRT reference filtering from StaticEventTemplateInformation
michaelstonis 92198c0
Pinning assembly to the `netstandard2.0` version
michaelstonis 59c50b1
updating csproj formatting to be consistent with other projects
michaelstonis 8f3b10d
update to latest packages
michaelstonis 1648109
Updated the Xamarin.Forms template
michaelstonis 83d5925
move XamarinEssentialsTemplate to same ItemGroup
michaelstonis 2916c40
Merge remote-tracking branch 'upstream/master' into feature/xamarin-e…
michaelstonis a259bda
Merge branch 'master' into feature/xamarin-essentials-events
glennawatson b954949
Merge branch 'master' into feature/xamarin-essentials-events
glennawatson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
src/EventBuilder/Cecil/StaticEventTemplateInformation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using EventBuilder.Entities; | ||
using Mono.Cecil; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace EventBuilder.Cecil | ||
{ | ||
public static class StaticEventTemplateInformation | ||
{ | ||
private static string GetEventArgsTypeForEvent(EventDefinition ei) | ||
{ | ||
// Find the EventArgs type parameter of the event via digging around via reflection | ||
var type = ei.EventType.Resolve(); | ||
var invoke = type.Methods.First(x => x.Name == "Invoke"); | ||
if (invoke.Parameters.Count < 1) return null; | ||
|
||
var param = invoke.Parameters.Count == 1 ? invoke.Parameters[0] : invoke.Parameters[1]; | ||
var ret = param.ParameterType.FullName; | ||
|
||
var generic = ei.EventType as GenericInstanceType; | ||
if (generic != null) | ||
{ | ||
foreach ( | ||
var kvp in | ||
type.GenericParameters.Zip(generic.GenericArguments, (name, actual) => new {name, actual})) | ||
{ | ||
var realType = GetRealTypeName(kvp.actual); | ||
|
||
ret = ret.Replace(kvp.name.FullName, realType); | ||
} | ||
} | ||
|
||
// NB: Inner types in Mono.Cecil get reported as 'Foo/Bar' | ||
return ret.Replace('/', '.'); | ||
} | ||
|
||
private static string GetRealTypeName(TypeDefinition t) | ||
{ | ||
if (t.GenericParameters.Count == 0) return t.FullName; | ||
|
||
var ret = string.Format("{0}<{1}>", | ||
t.Namespace + "." + t.Name, | ||
string.Join(",", t.GenericParameters.Select(x => GetRealTypeName(x.Resolve())))); | ||
|
||
// NB: Inner types in Mono.Cecil get reported as 'Foo/Bar' | ||
return ret.Replace('/', '.'); | ||
} | ||
|
||
private static string GetRealTypeName(TypeReference t) | ||
{ | ||
var generic = t as GenericInstanceType; | ||
if (generic == null) return t.FullName; | ||
|
||
var ret = string.Format("{0}<{1}>", | ||
generic.Namespace + "." + generic.Name, | ||
string.Join(",", generic.GenericArguments.Select(x => GetRealTypeName(x)))); | ||
|
||
// NB: Inner types in Mono.Cecil get reported as 'Foo/Bar' | ||
return ret.Replace('/', '.'); | ||
} | ||
|
||
private static EventDefinition[] GetPublicEvents(TypeDefinition t) | ||
{ | ||
return | ||
t.Events | ||
|
||
.Where(x => | ||
{ | ||
return x.AddMethod.IsPublic && GetEventArgsTypeForEvent(x) != null; | ||
}) | ||
.ToArray(); | ||
} | ||
|
||
public static NamespaceInfo[] Create(AssemblyDefinition[] targetAssemblies) | ||
{ | ||
var publicTypesWithEvents = targetAssemblies | ||
.SelectMany(x => SafeTypes.GetSafeTypes(x)) | ||
.Where(x => x.IsPublic && !x.HasGenericParameters) | ||
.Select(x => new {Type = x, Events = GetPublicEvents(x)}) | ||
.Where(x => x.Events.Length > 0) | ||
.ToArray(); | ||
|
||
var garbageNamespaceList = new[] | ||
{ | ||
"ReactiveUI.Events" | ||
}; | ||
|
||
var namespaceData = publicTypesWithEvents | ||
.GroupBy(x => x.Type.Namespace) | ||
.Where(x => !garbageNamespaceList.Contains(x.Key)) | ||
.Select(x => new NamespaceInfo | ||
{ | ||
Name = x.Key, | ||
Types = x.Select(y => new PublicTypeInfo | ||
{ | ||
Name = y.Type.Name, | ||
Type = y.Type, | ||
Events = y.Events.Select(z => new PublicEventInfo | ||
{ | ||
Name = z.Name, | ||
EventHandlerType = GetRealTypeName(z.EventType), | ||
EventArgsType = GetEventArgsTypeForEvent(z) | ||
}).ToArray() | ||
}).ToArray() | ||
}).ToArray(); | ||
|
||
foreach (var type in namespaceData.SelectMany(x => x.Types)) | ||
{ | ||
var parentWithEvents = GetParents(type.Type).FirstOrDefault(x => GetPublicEvents(x).Any()); | ||
if (parentWithEvents == null) | ||
continue; | ||
|
||
type.Parent = new ParentInfo {Name = parentWithEvents.FullName}; | ||
} | ||
|
||
return namespaceData; | ||
} | ||
|
||
private static IEnumerable<TypeDefinition> GetParents(TypeDefinition type) | ||
{ | ||
var current = type.BaseType != null && type.BaseType.ToString() != "System.Object" | ||
? type.BaseType.Resolve() | ||
: null; | ||
|
||
while (current != null) | ||
{ | ||
yield return current.Resolve(); | ||
|
||
current = current.BaseType != null | ||
? current.BaseType.Resolve() | ||
: null; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
<Project Sdk="MSBuild.Sdk.Extras"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>net461</TargetFrameworks> | ||
<AssemblyName>EventBuilder</AssemblyName> | ||
<RootNamespace>EventBuilder</RootNamespace> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Content Include="DefaultTemplate.mustache"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net461</TargetFramework> | ||
<AssemblyName>EventBuilder</AssemblyName> | ||
<RootNamespace>EventBuilder</RootNamespace> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Content Include="DefaultTemplate.mustache"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
<Content Include="XamarinEssentialsTemplate.mustache"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommandLineParser" Version="1.9.71" /> | ||
<PackageReference Include="Microsoft.Web.Xdt" Version="2.1.1" /> | ||
<PackageReference Include="Mono.Cecil" Version="0.9.6.1" /> | ||
<PackageReference Include="NuGet.Core" Version="2.10.1" /> | ||
<PackageReference Include="Nustache" Version="1.15.3.7" /> | ||
<PackageReference Include="Polly" Version="3.0.0" /> | ||
<PackageReference Include="Serilog" Version="1.5.14" /> | ||
<PackageReference Include="NuGet.Core" Version="2.14.0" /> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using NuGet; | ||
using Polly; | ||
using Serilog; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace EventBuilder.Platforms | ||
{ | ||
public class Essentials : BasePlatform | ||
{ | ||
private const string _packageName = "Xamarin.Essentials"; | ||
|
||
public override AutoPlatform Platform => AutoPlatform.Essentials; | ||
|
||
public Essentials() | ||
{ | ||
var packageUnzipPath = Environment.CurrentDirectory; | ||
|
||
var retryPolicy = Policy | ||
.Handle<Exception>() | ||
.WaitAndRetry( | ||
5, | ||
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), | ||
(exception, timeSpan, context) => | ||
{ | ||
Log.Warning( | ||
"An exception was thrown whilst retrieving or installing {packageName}: {exception}", | ||
_packageName, exception); | ||
}); | ||
|
||
retryPolicy.Execute(() => | ||
{ | ||
var repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2"); | ||
var packageManager = new PackageManager(repo, packageUnzipPath); | ||
var fpid = packageManager.SourceRepository.FindPackagesById(_packageName); | ||
var package = fpid.Single(x => x.Version.ToString() == "0.9.1-preview"); | ||
|
||
packageManager.InstallPackage(package, true, true); | ||
|
||
Log.Debug("Using Xamarin Essentials {Version} released on {Published}", package.Version, package.Published); | ||
Log.Debug("{ReleaseNotes}", package.ReleaseNotes); | ||
}); | ||
|
||
var xamarinForms = | ||
Directory.GetFiles(packageUnzipPath, | ||
"Xamarin.Essentials.dll", SearchOption.AllDirectories); | ||
|
||
var latestVersion = xamarinForms.First(x => x.Contains("netstandard1.0")); | ||
Assemblies.Add(latestVersion); | ||
|
||
if (PlatformHelper.IsRunningOnMono()) | ||
{ | ||
CecilSearchDirectories.Add( | ||
@"/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks/.NETPortable/v4.5/Profile/Profile111"); | ||
} | ||
else | ||
{ | ||
CecilSearchDirectories.Add(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile111"); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be fun doing TVOS who wins first :)