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

Load Razor source generators from SDK #9210

Merged
merged 20 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ public void AddDocument_InvalidExtension()
public void AddAnalyzerReferences_NullAnalyzer_Throws() =>
EmptyCS.Invoking(x => x.AddAnalyzerReferences([null])).Should().Throw<ArgumentNullException>();

#if NET
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

[TestMethod]
public void AddAnalyzerReferences_AddAnalyzer() =>
EmptyCS.AddAnalyzerReferences(SourceGeneratorProvider.SourceGenerators).Project.AnalyzerReferences.Should().BeEquivalentTo(SourceGeneratorProvider.SourceGenerators);

#endif

private static void AssertAdditionalDocumentContains(ProjectBuilder builder, string fileName) =>
builder.Project.AdditionalDocuments.Should().ContainSingle(x => x.Name == Path.Combine(Directory.GetCurrentDirectory(), "TestCases", fileName));

Expand Down
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,35 @@

namespace SonarAnalyzer.TestFramework.Test.Common;

#if NET
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

[TestClass]
public class SourceGeneratorProviderTest
{
private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));

[TestMethod]
public void SourceGenerators_ContainsRazorSourceGenerator() =>
SourceGeneratorProvider.SourceGenerators.Should().ContainSingle().Which.FullPath.Should().EndWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");

[TestMethod]
public void RazorSourceGenerator_HasCorrectPath() =>
RazorSourceGenerator.FullPath
.Should()
.Be(Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
.Should().Be(Path.Combine(SourceGeneratorProvider.LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));

[TestMethod]
public void RazorSourceGenerator_LoadsCorrectAssembly() =>
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators");

private static AnalyzerFileReference RazorSourceGenerator =>
SourceGeneratorProvider.SourceGenerators.Single(x => x.FullPath.EndsWith("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));
[TestMethod]
public void LatestSdkFolder_ReturnsCorrectPath()
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
{
var expectedPath = Directory.GetDirectories(Path.Combine(Directory.GetParent(typeof(object).Assembly.Location).Parent.Parent.Parent.FullName, "sdk"), $"{typeof(object).Assembly.GetName().Version.Major}.*", SearchOption.TopDirectoryOnly)
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
.OrderByDescending(x => new DirectoryInfo(x).Name)
.FirstOrDefault();
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
SourceGeneratorProvider.LatestSdkFolder().Should().Be(expectedPath);
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,34 @@ namespace SonarAnalyzer.TestFramework.Common;
public static class SourceGeneratorProvider
{
private static readonly string RazorSourceGeneratorPath =
Path.Combine(Path.GetDirectoryName(typeof(SourceGeneratorProvider).Assembly.Location), "Dependencies", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
Path.Combine(LatestSdkFolder(), "Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll");
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

public static AnalyzerFileReference[] SourceGenerators { get; } =
[
new(RazorSourceGeneratorPath, new AssemblyLoader())
new(CheckRazorSourceGeneratorPath(), new AssemblyLoader())
];

public static string CheckRazorSourceGeneratorPath() =>
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
File.Exists(RazorSourceGeneratorPath) ? RazorSourceGeneratorPath : throw new FileNotFoundException($"Razor sourcegenerator not found: {RazorSourceGeneratorPath}");
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

public static string LatestSdkFolder()
{
var objectAssembly = typeof(object).Assembly;
var objectAssemblyDirectory = new FileInfo(objectAssembly.Location).Directory; // C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.4
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
var dotnetDirectory = objectAssemblyDirectory.Parent.Parent.Parent; // C:\Program Files\dotnet
var sdkDirectory = Path.Combine(dotnetDirectory.FullName, "sdk"); // C:\Program Files\dotnet\sdk
if (!Directory.Exists(sdkDirectory))
{
throw new NotSupportedException($"Razor analysis is only supported for .Net Core.");
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
}
var specificMajorVersionSdkDirectories = Directory.GetDirectories(sdkDirectory, $"{objectAssembly.GetName().Version.Major}.*", SearchOption.TopDirectoryOnly);
if (specificMajorVersionSdkDirectories.Length == 0)
{
throw new DirectoryNotFoundException($"SDK directory not found for version {objectAssembly.GetName().Version.Major}");
}
return specificMajorVersionSdkDirectories.OrderByDescending(x => Version.Parse(new DirectoryInfo(x).Name)).FirstOrDefault();
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
}

private sealed class AssemblyLoader : IAnalyzerAssemblyLoader
{
public void AddDependencyLocation(string fullPath) { }
Expand Down
Binary file not shown.