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 2 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
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -32,10 +32,6 @@ public class SourceGeneratorProviderTest
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().EndWith(Path.Combine("Sdks", "Microsoft.NET.Sdk.Razor", "source-generators", "Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators.dll"));

[TestMethod]
public void RazorSourceGenerator_ExistsLocally() =>
File.Exists(RazorSourceGenerator.FullPath).Should().BeTrue();
Expand All @@ -45,18 +41,22 @@ public class SourceGeneratorProviderTest
RazorSourceGenerator.GetAssembly().GetName().Name.Should().Be("Microsoft.CodeAnalysis.Razor.Compiler.SourceGenerators");

[TestMethod]
public void LatestSdkFolder_ReturnsCorrectPath()
public void LatestSdkFolder_ReturnsAssemblyMajor()
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
var latestSdkVersion = Version.Parse(Path.GetFileName(latestSdkFolder));
Version.TryParse(Path.GetFileName(latestSdkFolder), out var latestSdkVersion).Should().BeTrue();
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
latestSdkVersion.Major.Should().Be(typeof(object).Assembly.GetName().Version.Major);
}

[TestMethod]
public void LatestSdkFolder_ReturnLatest()
{
var latestSdkFolder = SourceGeneratorProvider.LatestSdkFolder();
Version.TryParse(Path.GetFileName(latestSdkFolder), out var latestSdkVersion).Should().BeTrue();
CristianAmbrosini marked this conversation as resolved.
Show resolved Hide resolved
var parentDirectory = Directory.GetParent(latestSdkFolder);
parentDirectory.Name.Should().Be("sdk", "Parent directory of the latest SDK should be 'sdk'");

Directory.GetDirectories(parentDirectory.FullName, $"{typeof(object).Assembly.GetName().Version.Major}.*", SearchOption.TopDirectoryOnly)
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
.Any(x => IsHigherVersion(x, latestSdkVersion))
.Should()
.BeFalse("There should be no SDK folders with a higher version number than the latest SDK folder");
.Should().NotContain(x => IsHigherVersion(x, latestSdkVersion), "There should be no SDK folders with a higher version number than the latest SDK folder");
}

private static bool IsHigherVersion(string directory, Version referenceVersion) =>
Expand Down
Expand Up @@ -49,9 +49,11 @@ public static string LatestSdkFolder()
$"This may be because you are not using .NET Core. " +
$"Please note that Razor analysis is only supported when using .NET Core.");
}
return Directory.GetDirectories(sdkDirectory, $"{objectAssembly.GetName().Version.Major}.*", SearchOption.TopDirectoryOnly) is { Length: > 0 } specificMajorVersionSdkDirectories
? specificMajorVersionSdkDirectories.OrderByDescending(x => Version.Parse(new DirectoryInfo(x).Name)).First()
: throw new DirectoryNotFoundException($"SDK directory not found for version {objectAssembly.GetName().Version.Major}");
return Directory.GetDirectories(sdkDirectory, $"{objectAssembly.GetName().Version.Major}.*", SearchOption.TopDirectoryOnly)
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
.OrderByDescending(x => Version.Parse(new DirectoryInfo(x).Name))
.FirstOrDefault() is { } latestSdkDirectory
? latestSdkDirectory
: throw new DirectoryNotFoundException($"SDK directory not found for version {objectAssembly.GetName().Version.Major}");
}

private sealed class AssemblyLoader : IAnalyzerAssemblyLoader
Expand Down