Skip to content

Commit

Permalink
BinaryReference.Compile
Browse files Browse the repository at this point in the history
Better name than MetadataReferences.CreateBinary
  • Loading branch information
JohanLarsson committed Dec 6, 2021
1 parent 73a55bd commit d141c96
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 59 deletions.
46 changes: 46 additions & 0 deletions Gu.Roslyn.Asserts/MetadataReferences/BinaryReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace Gu.Roslyn.Asserts;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;

/// <summary>
/// Helper for getting meta data reference from code.
/// </summary>
public static class BinaryReference
{
/// <summary>
/// Create a binary reference from strings.
/// This is useful when testing for example deriving from a base class not in source.
/// </summary>
/// <param name="code">The code to create a dll project from.</param>
/// <returns>A <see cref="MetadataReference"/>.</returns>
public static MetadataReference Compile(params string[] code)
{
return Compile(code, Settings.Default.WithCompilationOptions(CodeFactory.DllCompilationOptions));
}

/// <summary>
/// Create a binary reference from strings.
/// This is useful when testing for example deriving from a base class not in source.
/// </summary>
/// <param name="code">The code to create a dll project from.</param>
/// <param name="settings">The <see cref="Settings"/>.</param>
/// <returns>A <see cref="MetadataReference"/>.</returns>
public static MetadataReference Compile(IEnumerable<string> code, Settings? settings = null)
{
settings ??= Settings.Default;
var sln = CodeFactory.CreateSolutionWithOneProject(
code,
settings.ParseOptions,
settings.CompilationOptions,
settings.MetadataReferences);
RoslynAssert.NoCompilerDiagnostics(sln);

using var ms = new MemoryStream();
_ = sln.Projects.Single().GetCompilationAsync().GetAwaiter().GetResult()!.Emit(ms);
ms.Position = 0;
return MetadataReference.CreateFromStream(ms);
}
}
60 changes: 1 addition & 59 deletions Gu.Roslyn.Asserts/MetadataReferences/MetadataReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,17 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;

/// <summary>
/// Helper for getting meta data references from <see cref="MetadataReferenceAttribute"/> and <see cref="MetadataReferencesAttribute"/>.
/// </summary>
public static class MetadataReferences
public static partial class MetadataReferences
{
private static ImmutableArray<MetadataReference> fromAttributes;

/// <summary>
/// Get the meta data references specified with <see cref="MetadataReferenceAttribute"/> and <see cref="MetadataReferencesAttribute"/> in the test assemblies.
/// </summary>
[Obsolete("Use Settings.Default")]
public static ImmutableArray<MetadataReference> FromAttributes()
{
if (!fromAttributes.IsDefault)
{
return fromAttributes;
}

var set = new HashSet<MetadataReference>(MetadataReferenceComparer.Default);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var attributes = Attribute.GetCustomAttributes(assembly, typeof(MetadataReferenceAttribute));
foreach (var single in attributes.Cast<MetadataReferenceAttribute>())
{
set.Add(single.MetadataReference);
}

attributes = Attribute.GetCustomAttributes(assembly, typeof(TransitiveMetadataReferencesAttribute));
foreach (var transitive in attributes.Cast<TransitiveMetadataReferencesAttribute>())
{
set.UnionWith(transitive.MetadataReferences);
}

if (assembly.GetCustomAttribute<MetadataReferencesAttribute>() is { } attribute)
{
set.UnionWith(attribute.MetadataReferences);
}
}

fromAttributes = ImmutableArray.CreateRange(set);
return fromAttributes;
}

/// <summary>
/// Create a <see cref="MetadataReference"/> for the <paramref name="assembly"/>.
/// Checks reference assemblies first.
Expand Down Expand Up @@ -83,27 +46,6 @@ public static MetadataReference CreateFromFile(string assemblyFile)
return MetadataReference.CreateFromFile(assemblyFile);
}

/// <summary>
/// Create a binary reference from strings.
/// This is useful when testing for example deriving from a base class not in source.
/// </summary>
/// <param name="code">The code to create a dll project from.</param>
/// <returns>A <see cref="MetadataReference"/>.</returns>
public static MetadataReference CreateBinary(params string[] code)
{
var sln = CodeFactory.CreateSolutionWithOneProject(
code,
Settings.Default.ParseOptions,
CodeFactory.DllCompilationOptions,
Settings.Default.MetadataReferences);
RoslynAssert.NoCompilerDiagnostics(sln);

using var ms = new MemoryStream();
_ = sln.Projects.Single().GetCompilationAsync().GetAwaiter().GetResult()!.Emit(ms);
ms.Position = 0;
return MetadataReference.CreateFromStream(ms);
}

/// <summary>
/// Get the <see cref="MetadataReference"/> for <paramref name="typesInAssemblies"/> and all assemblies referenced by <paramref name="typesInAssemblies"/>.
/// </summary>
Expand Down
57 changes: 57 additions & 0 deletions Gu.Roslyn.Asserts/Obsolete/MetadataReferences.Obsolete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace Gu.Roslyn.Asserts
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;

public static partial class MetadataReferences
{
/// <summary>
/// Get the meta data references specified with <see cref="MetadataReferenceAttribute"/> and <see cref="MetadataReferencesAttribute"/> in the test assemblies.
/// </summary>
[Obsolete("Use Settings.Default")]
public static ImmutableArray<MetadataReference> FromAttributes()
{
if (!fromAttributes.IsDefault)
{
return fromAttributes;
}

var set = new HashSet<MetadataReference>(MetadataReferenceComparer.Default);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var attributes = Attribute.GetCustomAttributes(assembly, typeof(MetadataReferenceAttribute));
foreach (var single in attributes.Cast<MetadataReferenceAttribute>())
{
set.Add(single.MetadataReference);
}

attributes = Attribute.GetCustomAttributes(assembly, typeof(TransitiveMetadataReferencesAttribute));
foreach (var transitive in attributes.Cast<TransitiveMetadataReferencesAttribute>())
{
set.UnionWith(transitive.MetadataReferences);
}

if (assembly.GetCustomAttribute<MetadataReferencesAttribute>() is { } attribute)
{
set.UnionWith(attribute.MetadataReferences);
}
}

fromAttributes = ImmutableArray.CreateRange(set);
return fromAttributes;
}

/// <summary>
/// Create a binary reference from strings.
/// This is useful when testing for example deriving from a base class not in source.
/// </summary>
/// <param name="code">The code to create a dll project from.</param>
/// <returns>A <see cref="MetadataReference"/>.</returns>
[Obsolete("Use MetadataReference.Compile()")]
public static MetadataReference CreateBinary(params string[] code) => BinaryReference.Compile(code);
}
}

0 comments on commit d141c96

Please sign in to comment.