Skip to content

Commit

Permalink
Handle events other than event pattern, further refining the fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson committed May 1, 2019
1 parent 994e9c2 commit f8f56e6
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="stylecop.analyzers" Version="1.1.1-rc.114" PrivateAssets="all" />
<PackageReference Include="stylecop.analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.3" PrivateAssets="all" />
<PackageReference Include="Roslynator.Analyzers" Version="2.1.0-rc" PrivateAssets="All" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/EventBuilder.Console/EventBuilder.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="commandlineparser" Version="2.4.3" />
<PackageReference Include="commandlineparser" Version="2.5.0" />
<PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
<PackageReference Include="serilog.sinks.coloredconsole" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
Expand Down
30 changes: 29 additions & 1 deletion src/EventBuilder.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using CommandLine;
using CommandLine.Text;
using EventBuilder.CommandOptions;
using EventBuilder.Core;
using EventBuilder.Core.NuGet;

using NuGet.Packaging.Core;
using NuGet.Versioning;

using Serilog;
using Serilog.Events;
using Parser = CommandLine.Parser;
Expand Down Expand Up @@ -64,7 +70,29 @@ public static async Task<int> Main(string[] args)
{
try
{
await ObservablesForEventGenerator.ExtractEventsFromPlatforms(options.OutputPath, options.OutputPrefix, options.Assemblies, options.SearchDirectories).ConfigureAwait(false);
using (var stream = new FileStream(Path.Combine(options.OutputPath, options.OutputPrefix + ".cs"), FileMode.Create, FileAccess.Write))
{
await ObservablesForEventGenerator.ExtractEventsFromAssemblies(stream, options.Assemblies, options.SearchDirectories).ConfigureAwait(false);
}

return ExitCode.Success;
}
catch (Exception ex)
{
Log.Fatal(ex.ToString());
return ExitCode.Error;
}
},
async (NugetCommandLineOptions options) =>
{
try
{
using (var stream = new FileStream(Path.Combine(options.OutputPath, options.OutputPrefix + ".cs"), FileMode.Create, FileAccess.Write))
{
var packageIdentity = new PackageIdentity(options.NugetPackageName, new NuGetVersion(options.NugetVersion));
var nugetFramework = options.TargetFramework.ToFramework();
await ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(stream, packageIdentity, nugetFramework).ConfigureAwait(false);
}

return ExitCode.Success;
}
Expand Down
15 changes: 14 additions & 1 deletion src/EventBuilder.Core/NuGet/NuGetPackageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -62,7 +64,7 @@ static NuGetPackageHelper()
supportPackageIdentities = (supportPackageIdentities ?? Array.Empty<PackageIdentity>()).Concat(defaultSupportLibrary).Distinct();

// Combine together the primary/secondary packages, boolean value to indicate if we should include in our output.
var packagesToDownload = new List<(PackageIdentity packageIdentity, bool includeFiles)> { (packageIdentity, true) };
var packagesToDownload = new SortedSet<(PackageIdentity packageIdentity, bool includeFiles)>(NuGetPackageIdentityComparer.Default) { (packageIdentity, true) };
packagesToDownload.AddRange(supportPackageIdentities.Select(x => (x, false)));

return await Task.WhenAll(packagesToDownload
Expand Down Expand Up @@ -168,5 +170,16 @@ private static NuGetFramework GetFrameworkFromPath(this PackageReaderBase reader

return nuGetFramework;
}

private class NuGetPackageIdentityComparer : IComparer<(PackageIdentity, bool)>
{
public static NuGetPackageIdentityComparer Default { get; } = new NuGetPackageIdentityComparer();

/// <inheritdoc />
public int Compare((PackageIdentity, bool) x, (PackageIdentity, bool) y)
{
return x.Item1.CompareTo(y.Item1);
}
}
}
}
11 changes: 5 additions & 6 deletions src/EventBuilder.Core/ObservablesForEventGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ public static async Task ExtractEventsFromAssemblies(Stream outputStream, IEnume

var compilationOutputSyntax = SyntaxFactory.CompilationUnit().WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(_resolvers.SelectMany(x => x.Create(compilation))));

using (StreamWriter streamWriter = new StreamWriter(outputStream))
{
await streamWriter.WriteAsync(await TemplateManager.GetTemplateAsync(TemplateManager.HeaderTemplate).ConfigureAwait(false)).ConfigureAwait(false);
await streamWriter.WriteAsync(Environment.NewLine).ConfigureAwait(false);
await streamWriter.WriteAsync(compilationOutputSyntax.NormalizeWhitespace(elasticTrivia: true).ToString()).ConfigureAwait(false);
}
StreamWriter streamWriter = new StreamWriter(outputStream);
await streamWriter.WriteAsync(await TemplateManager.GetTemplateAsync(TemplateManager.HeaderTemplate).ConfigureAwait(false)).ConfigureAwait(false);
await streamWriter.WriteAsync(Environment.NewLine).ConfigureAwait(false);
await streamWriter.WriteAsync(compilationOutputSyntax.NormalizeWhitespace(elasticTrivia: true).ToString()).ConfigureAwait(false);
await streamWriter.FlushAsync().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public EventBuilderCompiler(IEnumerable<IModuleReference> modules, IEnumerable<s
Init(modules, searchDirectories.ToList());
}

protected EventBuilderCompiler()
{
}

/// <summary>
/// Gets the main module we are extracting information from.
/// This is mostly just here due to ILDecompile needing it.
/// </summary>
public IModule MainModule
{
get
Expand All @@ -51,6 +51,9 @@ public IModule MainModule
}
}

/// <summary>
/// Gets the modules we want to extract events from.
/// </summary>
public IReadOnlyList<IModule> Modules
{
get
Expand All @@ -64,6 +67,10 @@ public IReadOnlyList<IModule> Modules
}
}

/// <summary>
/// Gets the referenced modules. These are support modules where we want additional information about types from.
/// This will likely be either the system reference libraries or .NET Standard libraries.
/// </summary>
public IReadOnlyList<IModule> ReferencedModules
{
get
Expand All @@ -77,6 +84,9 @@ public IReadOnlyList<IModule> ReferencedModules
}
}

/// <summary>
/// Gets the root namespace for our assemblies. We can start analyzing from here.
/// </summary>
public INamespace RootNamespace
{
get
Expand All @@ -96,8 +106,14 @@ public INamespace RootNamespace
}
}

/// <summary>
/// Gets the comparer we are going to use for comparing names of items. We just compare ordinally.
/// </summary>
public StringComparer NameComparer => StringComparer.Ordinal;

/// <summary>
/// Gets the cache manager. This is mostly here for ILDecompile.
/// </summary>
public CacheManager CacheManager { get; } = new CacheManager();

public virtual INamespace GetNamespaceForExternAlias(string alias)
Expand Down
18 changes: 2 additions & 16 deletions src/EventBuilder.Core/Reflection/Generators/DelegateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ private static MethodDeclarationSyntax GenerateMethodDeclaration(string observab
// Produces:
// /// <inheritdoc />
// public override void MethodName(params..) => _methodName.OnNext(...);
InvocationExpressionSyntax methodBody = InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName(observableName), IdentifierName("OnNext")));
var methodBody = InvocationExpression(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName(observableName), IdentifierName("OnNext")));

var methodParameterList = GenerateMethodParameters(method);
var methodParameterList = method.GenerateMethodParameters();

// If we have any members call our observables with the parameters.
if (method.Parameters.Count > 0)
Expand Down Expand Up @@ -147,19 +147,5 @@ private static MethodDeclarationSyntax GenerateMethodDeclaration(string observab
.WithLeadingTrivia(XmlSyntaxFactory.InheritdocSyntax)
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}

private static ParameterListSyntax GenerateMethodParameters(IMethod method)
{
if (method.Parameters.Count == 0)
{
return ParameterList();
}

return ParameterList(
SeparatedList(
method.Parameters.Select(
x => Parameter(Identifier(x.Name))
.WithType(IdentifierName(x.Type.GenerateFullGenericName())))));
}
}
}
Loading

0 comments on commit f8f56e6

Please sign in to comment.