-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from sharwell/reference-assemblies
Support reference assemblies
- Loading branch information
Showing
11 changed files
with
1,449 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 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
89 changes: 20 additions & 69 deletions
89
...rosoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/MetadataReferences.cs
This file contains 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,95 +1,46 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.IO; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.CodeAnalysis; | ||
|
||
#if NETSTANDARD1_5 | ||
using System.IO; | ||
#endif | ||
|
||
namespace Microsoft.CodeAnalysis.Testing | ||
{ | ||
/// <summary> | ||
/// Metadata references used to create test projects. | ||
/// </summary> | ||
public static class MetadataReferences | ||
{ | ||
public static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location).WithAliases(ImmutableArray.Create("global", "corlib")); | ||
public static readonly MetadataReference SystemReference = MetadataReference.CreateFromFile(typeof(System.Diagnostics.Debug).GetTypeInfo().Assembly.Location).WithAliases(ImmutableArray.Create("global", "system")); | ||
public static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location); | ||
public static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).GetTypeInfo().Assembly.Location); | ||
public static readonly MetadataReference SystemCollectionsImmutableReference = MetadataReference.CreateFromFile(typeof(ImmutableArray).GetTypeInfo().Assembly.Location); | ||
|
||
internal static readonly MetadataReference? MscorlibFacadeReference; | ||
internal static readonly MetadataReference? SystemRuntimeReference; | ||
internal static readonly MetadataReference? SystemValueTupleReference; | ||
|
||
private static MetadataReference? _microsoftVisualBasicReference; | ||
private static readonly Func<string, DocumentationProvider?> s_createDocumentationProvider; | ||
|
||
static MetadataReferences() | ||
{ | ||
#if NETSTANDARD1_5 | ||
if (typeof(string).GetTypeInfo().Assembly.ExportedTypes.Any(x => x.Name == "System.ValueTuple")) | ||
{ | ||
// mscorlib contains ValueTuple, so no need to add a separate reference | ||
MscorlibFacadeReference = null; | ||
SystemRuntimeReference = null; | ||
SystemValueTupleReference = null; | ||
} | ||
else | ||
{ | ||
MscorlibFacadeReference = MetadataReference.CreateFromFile(Path.Combine(Path.GetDirectoryName(typeof(Lazy<,>).GetTypeInfo().Assembly.Location), "mscorlib.dll")); | ||
SystemRuntimeReference = MetadataReference.CreateFromFile(typeof(Lazy<,>).GetTypeInfo().Assembly.Location); | ||
SystemValueTupleReference = MetadataReference.CreateFromFile(typeof(ValueTuple<,>).GetTypeInfo().Assembly.Location); | ||
} | ||
#elif NETSTANDARD2_0 | ||
// mscorlib contains ValueTuple, so no need to add a separate reference | ||
MscorlibFacadeReference = null; | ||
SystemRuntimeReference = null; | ||
SystemValueTupleReference = null; | ||
#elif NET452 | ||
// System.Object is already in mscorlib | ||
MscorlibFacadeReference = null; | ||
Func<string, DocumentationProvider?> createDocumentationProvider = _ => null; | ||
|
||
var systemRuntime = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(x => x.GetName().Name == "System.Runtime"); | ||
if (systemRuntime != null) | ||
var xmlDocumentationProvider = typeof(Workspace).GetTypeInfo().Assembly.GetType("Microsoft.CodeAnalysis.XmlDocumentationProvider"); | ||
if (xmlDocumentationProvider is object) | ||
{ | ||
SystemRuntimeReference = MetadataReference.CreateFromFile(systemRuntime.Location); | ||
var createFromFile = xmlDocumentationProvider.GetTypeInfo().GetMethod("CreateFromFile", new[] { typeof(string) }); | ||
if (createFromFile is object) | ||
{ | ||
var xmlDocCommentFilePath = Expression.Parameter(typeof(string), "xmlDocCommentFilePath"); | ||
var body = Expression.Convert( | ||
Expression.Call(createFromFile, xmlDocCommentFilePath), | ||
typeof(DocumentationProvider)); | ||
var expression = Expression.Lambda<Func<string, DocumentationProvider>>(body, xmlDocCommentFilePath); | ||
createDocumentationProvider = expression.Compile(); | ||
} | ||
} | ||
|
||
var systemValueTuple = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(x => x.GetName().Name == "System.ValueTuple"); | ||
if (systemValueTuple != null) | ||
{ | ||
SystemValueTupleReference = MetadataReference.CreateFromFile(systemValueTuple.Location); | ||
} | ||
#else | ||
#error Unsupported target framework. | ||
#endif | ||
s_createDocumentationProvider = createDocumentationProvider; | ||
} | ||
|
||
public static MetadataReference MicrosoftVisualBasicReference | ||
internal static MetadataReference CreateReferenceFromFile(string path) | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
get | ||
{ | ||
if (_microsoftVisualBasicReference is null) | ||
{ | ||
try | ||
{ | ||
_microsoftVisualBasicReference = MetadataReference.CreateFromFile(typeof(Microsoft.VisualBasic.Strings).GetTypeInfo().Assembly.Location); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new PlatformNotSupportedException("Microsoft.VisualBasic is not supported on this platform", e); | ||
} | ||
} | ||
|
||
return _microsoftVisualBasicReference; | ||
} | ||
var documentationFile = Path.ChangeExtension(path, ".xml"); | ||
return MetadataReference.CreateFromFile(path, documentation: s_createDocumentationProvider(documentationFile)); | ||
} | ||
} | ||
} |
This file contains 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.