Skip to content

Commit

Permalink
refactor: Remove System.Collections.Immutable dependency. (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson authored Dec 15, 2019
1 parent 81bde60 commit 8479308
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
47 changes: 38 additions & 9 deletions src/Pharmacist.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ public static async Task<int> Main(string[] args)
{
try
{
string referenceAssembliesLocation;
if (!string.IsNullOrWhiteSpace(options.ReferenceAssemblies))
var referenceAssembliesLocation = !string.IsNullOrWhiteSpace(options.ReferenceAssemblies)
? options.ReferenceAssemblies!
: ReferenceLocator.GetReferenceLocation();

if (string.IsNullOrWhiteSpace(options.OutputPath))
{
referenceAssembliesLocation = options.ReferenceAssemblies;
throw new Exception("Invalid output path for the event generation.");
}
else

if (string.IsNullOrWhiteSpace(options.OutputPrefix))
{
throw new Exception("Invalid output prefix for the event generation.");
}

if (options.Platforms == null)
{
referenceAssembliesLocation = ReferenceLocator.GetReferenceLocation();
throw new Exception("Invalid platforms for the event generation.");
}

await ObservablesForEventGenerator.ExtractEventsFromPlatforms(options.OutputPath, options.OutputPrefix, ".cs", referenceAssembliesLocation, options.Platforms).ConfigureAwait(false);
await ObservablesForEventGenerator.ExtractEventsFromPlatforms(options.OutputPath!, options.OutputPrefix!, ".cs", referenceAssembliesLocation, options.Platforms).ConfigureAwait(false);

return ExitCode.Success;
}
Expand All @@ -75,9 +84,24 @@ public static async Task<int> Main(string[] args)
{
using (var writer = new StreamWriter(Path.Combine(options.OutputPath, options.OutputPrefix + ".cs")))
{
await ObservablesForEventGenerator.WriteHeader(writer, options.Assemblies).ConfigureAwait(false);
if (options.Assemblies == null)
{
throw new Exception("Invalid specified assemblies for observable generation.");
}

await ObservablesForEventGenerator.ExtractEventsFromAssemblies(writer, options.Assemblies, options.SearchDirectories, options.TargetFramework).ConfigureAwait(false);
if (options.SearchDirectories == null)
{
throw new Exception("Invalid search directories specified for observable generation.");
}

if (string.IsNullOrWhiteSpace(options.TargetFramework))
{
throw new Exception("Invalid target framework for the event generation.");
}

await ObservablesForEventGenerator.WriteHeader(writer, options.Assemblies!).ConfigureAwait(false);

await ObservablesForEventGenerator.ExtractEventsFromAssemblies(writer, options.Assemblies!, options.SearchDirectories!, options.TargetFramework!).ConfigureAwait(false);
}

return ExitCode.Success;
Expand All @@ -94,8 +118,13 @@ public static async Task<int> Main(string[] args)
{
using (var writer = new StreamWriter(Path.Combine(options.OutputPath, options.OutputPrefix + ".cs")))
{
if (string.IsNullOrWhiteSpace(options.TargetFramework))
{
throw new Exception("Invalid target framework for the event generation.");
}

var packageIdentity = new[] { new LibraryRange(options.NugetPackageName, VersionRange.Parse(options.NugetVersion), LibraryDependencyTarget.Package) };
var nugetFramework = options.TargetFramework.ToFrameworks();
var nugetFramework = options.TargetFramework!.ToFrameworks();
await ObservablesForEventGenerator.WriteHeader(writer, packageIdentity).ConfigureAwait(false);
await ObservablesForEventGenerator.ExtractEventsFromNuGetPackages(writer, packageIdentity, nugetFramework, options.PackageFolder).ConfigureAwait(false);
}
Expand Down
12 changes: 8 additions & 4 deletions src/Pharmacist.Core/Generation/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;

Expand All @@ -21,7 +20,7 @@ namespace Pharmacist.Core.Generation
/// </summary>
internal static class ReflectionExtensions
{
private static readonly ConcurrentDictionary<ICompilation, ImmutableDictionary<string, ImmutableList<ITypeDefinition>>> _typeNameMapping = new ConcurrentDictionary<ICompilation, ImmutableDictionary<string, ImmutableList<ITypeDefinition>>>();
private static readonly ConcurrentDictionary<ICompilation, Dictionary<string, List<ITypeDefinition>>> _typeNameMapping = new ConcurrentDictionary<ICompilation, Dictionary<string, List<ITypeDefinition>>>();
private static readonly ConcurrentDictionary<ICompilation, IEnumerable<ITypeDefinition>> _publicNonGenericTypeMapping = new ConcurrentDictionary<ICompilation, IEnumerable<ITypeDefinition>>();
private static readonly ConcurrentDictionary<ICompilation, IEnumerable<ITypeDefinition>> _publicEventsTypeMapping = new ConcurrentDictionary<ICompilation, IEnumerable<ITypeDefinition>>();

Expand Down Expand Up @@ -57,9 +56,14 @@ public static IEnumerable<ITypeDefinition> GetPublicTypesWithStaticEvents(this I
/// <returns>The name of the items.</returns>
public static IReadOnlyCollection<ITypeDefinition> GetReferenceTypeDefinitionsWithFullName(this ICompilation compilation, string name)
{
var map = _typeNameMapping.GetOrAdd(compilation, comp => comp.ReferencedModules.Concat(compilation.Modules).SelectMany(x => x.TypeDefinitions).GroupBy(x => x.ReflectionName).ToImmutableDictionary(x => x.Key, x => x.ToImmutableList()));
var map = _typeNameMapping.GetOrAdd(compilation, comp => comp.ReferencedModules.Concat(compilation.Modules).SelectMany(x => x.TypeDefinitions).GroupBy(x => x.ReflectionName).ToDictionary(x => x.Key, x => x.ToList()));

return map.GetValueOrDefault(name) ?? ImmutableList<ITypeDefinition>.Empty;
if (map.TryGetValue(name, out var value))
{
return value;
}

return Array.Empty<ITypeDefinition>();
}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/Pharmacist.Core/Pharmacist.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<PackageReference Include="NuGet.LibraryModel" Version="5.4.0" />
<PackageReference Include="polly" Version="7.2.0" />
<PackageReference Include="splat" Version="9.3.1" />
<PackageReference Include="system.collections.immutable" Version="1.7.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.0" />
</ItemGroup>

Expand Down

0 comments on commit 8479308

Please sign in to comment.