Skip to content
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

Light up System.ClientModel interface decorations on public API surface #8386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/dotnet/APIView/APIView/Languages/CompilationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public static class CompilationFactory
{
private static HashSet<string> AllowedAssemblies = new HashSet<string>(new[]
{
"Microsoft.Bcl.AsyncInterfaces"
"Microsoft.Bcl.AsyncInterfaces",
"System.ClientModel"

}, StringComparer.InvariantCultureIgnoreCase);

public static IAssemblySymbol GetCompilation(string file)
Expand All @@ -24,7 +26,7 @@ public static IAssemblySymbol GetCompilation(string file)
}
}

public static IAssemblySymbol GetCompilation(Stream stream, Stream documentationStream)
public static IAssemblySymbol GetCompilation(Stream stream, Stream documentationStream, IEnumerable<string> dependencyPaths = null)
{
PortableExecutableReference reference;

Expand Down Expand Up @@ -59,6 +61,17 @@ public static IAssemblySymbol GetCompilation(Stream stream, Stream documentation
compilation = compilation.AddReferences(MetadataReference.CreateFromFile(tpl));
}
}
if (dependencyPaths != null)
{
foreach (var dependencyFile in dependencyPaths)
{
if (!File.Exists(dependencyFile) || !AllowedAssemblies.Contains(Path.GetFileNameWithoutExtension(dependencyFile)))
{
continue;
}
compilation = compilation.AddReferences(MetadataReference.CreateFromFile(dependencyFile));
}
}

return (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(reference);
}
Expand Down
1 change: 1 addition & 0 deletions src/dotnet/APIView/APIViewWeb/APIViewWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19367-01" PrivateAssets="All" />
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NuGet.Protocol" Version="6.10.0" />
<PackageReference Include="Octokit" Version="9.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using ApiView;
using NuGet.Common;
using NuGet.Packaging;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;

namespace APIViewWeb
{
Expand Down Expand Up @@ -44,9 +50,10 @@ public override bool CanUpdate(string versionString)
return versionString != CodeFileBuilder.CurrentVersion;
}

public override Task<CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
public override async Task<CodeFile> GetCodeFileAsync(string originalName, Stream stream, bool runAnalysis)
{
ZipArchive archive = null;
string dependencyFilesTempDir = null;
try
{
Stream dllStream = stream;
Expand All @@ -62,7 +69,7 @@ public override Task<CodeFile> GetCodeFileAsync(string originalName, Stream stre
var dllEntries = archive.Entries.Where(entry => IsDll(entry.Name)).ToArray();
if (dllEntries.Length == 0)
{
return Task.FromResult(GetDummyReviewCodeFile(originalName, dependencies));
return GetDummyReviewCodeFile(originalName, dependencies);
}

var dllEntry = dllEntries.First();
Expand Down Expand Up @@ -99,17 +106,24 @@ public override Task<CodeFile> GetCodeFileAsync(string originalName, Stream stre
}
}

var assemblySymbol = CompilationFactory.GetCompilation(dllStream, docStream);
dependencyFilesTempDir = await ExtractNugetDependencies(dependencies).ConfigureAwait(false);
var dependencyFilePaths = Directory.EnumerateFiles(dependencyFilesTempDir, "*.dll", SearchOption.AllDirectories);

var assemblySymbol = CompilationFactory.GetCompilation(dllStream, docStream, dependencyFilePaths);
if (assemblySymbol == null)
{
return Task.FromResult(GetDummyReviewCodeFile(originalName, dependencies));
return GetDummyReviewCodeFile(originalName, dependencies);
}

return Task.FromResult(new CodeFileBuilder().Build(assemblySymbol, runAnalysis, dependencies));
return new CodeFileBuilder().Build(assemblySymbol, runAnalysis, dependencies);
}
finally
{
archive?.Dispose();
if (dependencyFilesTempDir != null && Directory.Exists(dependencyFilesTempDir))
{
Directory.Delete(dependencyFilesTempDir, true);
}
}
}

Expand All @@ -132,13 +146,58 @@ private CodeFile GetDummyReviewCodeFile(string originalName, List<DependencyInfo
CodeFileBuilder.BuildDependencies(builder, dependencies);

return new CodeFile()
{
{
Name = reviewName,
Language = "C#",
VersionString = CodeFileBuilder.CurrentVersion,
PackageName = packageName,
Tokens = builder.Tokens.ToArray()
};
}

/// <summary>
/// Resolves the NuGet package dependencies and extracts them to a temporary folder. It is the responsibility of teh caller to clean up the folder.
/// </summary>
/// <param name="dependencyInfos">The dependency infos</param>
/// <returns>A temporary path where the dependency files were extracted.</returns>
private async Task<string> ExtractNugetDependencies(List<DependencyInfo> dependencyInfos)
{
string tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
SourceCacheContext cache = new SourceCacheContext();
SourceRepository repository = NuGet.Protocol.Core.Types.Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
try
{
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>().ConfigureAwait(false);
foreach (var dep in dependencyInfos)
{
using (MemoryStream packageStream = new MemoryStream())
{
if (await resource.CopyNupkgToStreamAsync(
dep.Name,
new NuGetVersion(dep.Version),
packageStream,
cache,
NullLogger.Instance,
CancellationToken.None))
{
using PackageArchiveReader reader = new PackageArchiveReader(packageStream);
NuspecReader nuspec = reader.NuspecReader;
var file = reader.GetFiles().FirstOrDefault(f => f.EndsWith(dep.Name + ".dll"));
if (file != null)
{
var fileInfo = new FileInfo(file);
var path = Path.Combine(tempFolder, dep.Name, fileInfo.Name);
var tmp = reader.ExtractFile(file, path, NullLogger.Instance);
}
}
}
}
}
finally
{
cache.Dispose();
}
return tempFolder;
}
}
}