Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bdovaz committed Sep 29, 2023
1 parent a227562 commit 4ec0d47
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
35 changes: 29 additions & 6 deletions src/UnityNuGet.Tests/NuGetHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ namespace UnityNuGet.Tests
{
public class NuGetHelperTests
{
[Test]
[TestCase("analyzers/dotnet/roslyn3.8/cs/Test.resources.dll")]
[TestCase("analyzers/dotnet/roslyn3.8/Test.resources.dll")]
[TestCase("analyzers/dotnet/cs/Test.resources.dll")]
[TestCase("analyzers/dotnet/Test.resources.dll")]
[TestCase("analyzers/Test.resources.dll")]
public void IsApplicableAnalyzerResource_Valid(string input)
{
Assert.True(NuGetHelper.IsApplicableAnalyzerResource(input));
}

[Test]
[TestCase("analyzers/dotnet/roslyn3.8/vb/cs/Test.resources.dll")]
[TestCase("analyzers/dotnet/roslyn3.8/cs/Test.dll")]
[TestCase("analyzers/dotnet/roslyn3.8/Test.dll")]
[TestCase("analyzers/dotnet/vb/Test.dll")]
[TestCase("analyzers/dotnet/cs/Test.dll")]
[TestCase("analyzers/dotnet/Test.dll")]
[TestCase("analyzers/Test.dll")]
public void IsApplicableAnalyzerResource_Invalid(string input)
{
Assert.False(NuGetHelper.IsApplicableAnalyzerResource(input));
}

// Examples:
// Meziantou.Analyzer -> analyzers/dotnet/roslyn3.8/cs/*
// Microsoft.Unity.Analyzers -> analyzers/dotnet/cs/*
Expand All @@ -22,18 +46,17 @@ public class NuGetHelperTests
[TestCase("analyzers/dotnet/cs/Test.dll")]
[TestCase("analyzers/dotnet/Test.dll")]
[TestCase("analyzers/Test.dll")]
public void IsApplicableUnityAnalyzer_Valid(string input)
public void IsApplicableUnitySupportedRoslynVersionFolder_Valid(string input)
{
Assert.True(NuGetHelper.IsApplicableUnityAnalyzer(input));
Assert.True(NuGetHelper.IsApplicableUnitySupportedRoslynVersionFolder(input));
}

[Test]
[TestCase("analyzers/dotnet/roslyn4.0/cs/Test.dll")]
[TestCase("analyzers/dotnet/roslyn4.0/Test.dll")]
[TestCase("analyzers/dotnet/vb/Test.dll")]
[TestCase("analyzers/Test.resources.dll")]
public void IsApplicableUnityAnalyzer_Invalid(string input)
public void IsApplicableUnitySupportedRoslynVersionFolder_Invalid(string input)
{
Assert.False(NuGetHelper.IsApplicableUnityAnalyzer(input));
Assert.False(NuGetHelper.IsApplicableUnitySupportedRoslynVersionFolder(input));
}

[Test]
Expand Down
21 changes: 19 additions & 2 deletions src/UnityNuGet/NuGetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static class NuGetHelper
private static readonly Version unityRoslynSupportedVersion = new(3, 8, 0);

// https://github.com/dotnet/sdk/blob/2838d93742658300698b2194882d57fd978fb168/src/Tasks/Microsoft.NET.Build.Tasks/NuGetUtils.NuGet.cs#L50
public static bool IsApplicableAnalyzer(string file) => IsApplicableAnalyzer(file, "C#");

private static bool IsApplicableAnalyzer(string file, string projectLanguage)
{
// This logic is preserved from previous implementations.
Expand Down Expand Up @@ -44,7 +46,22 @@ bool FileMatchesProjectLanguage()
return IsAnalyzer() && FileMatchesProjectLanguage();
}

public static bool IsApplicableUnityAnalyzer(string file)
public static bool IsApplicableAnalyzerResource(string file)
{
bool IsResource()
{
return file.StartsWith("analyzers", StringComparison.Ordinal)
&& file.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase);
}

bool CS() => file.Contains("/cs/", StringComparison.OrdinalIgnoreCase);
bool VB() => file.Contains("/vb/", StringComparison.OrdinalIgnoreCase);

// Czech locale is cs, catch /vb/cs/
return IsResource() && CS() && !VB();
}

public static bool IsApplicableUnitySupportedRoslynVersionFolder(string file)
{
var roslynVersionMatch = roslynVersionRegex.Match(file);

Expand All @@ -53,7 +70,7 @@ public static bool IsApplicableUnityAnalyzer(string file)
int.Parse(roslynVersionMatch.Groups[1].Value) == unityRoslynSupportedVersion.Major &&
int.Parse(roslynVersionMatch.Groups[2].Value) == unityRoslynSupportedVersion.Minor;

return IsApplicableAnalyzer(file, "C#") && (!hasRoslynVersionFolder || hasUnitySupportedRoslynVersionFolder);
return !hasRoslynVersionFolder || hasUnitySupportedRoslynVersionFolder;
}

public static IEnumerable<(FrameworkSpecificGroup, RegistryTargetFramework)> GetClosestFrameworkSpecificGroups(IEnumerable<FrameworkSpecificGroup> versions, IEnumerable<RegistryTargetFramework> targetFrameworks)
Expand Down
26 changes: 20 additions & 6 deletions src/UnityNuGet/RegistryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ RegistryEntry packageEntry
var packageFiles = await packageReader.GetItemsAsync(PackagingConstants.Folders.Analyzers, CancellationToken.None);

// https://learn.microsoft.com/en-us/nuget/guides/analyzers-conventions#analyzers-path-format
var analyzerFiles = packageFiles.SelectMany(p => p.Items).Where(p => NuGetHelper.IsApplicableUnityAnalyzer(p)).ToArray();
var analyzerFiles = packageFiles
.SelectMany(p => p.Items)
.Where(p => NuGetHelper.IsApplicableUnitySupportedRoslynVersionFolder(p) && (NuGetHelper.IsApplicableAnalyzer(p) || NuGetHelper.IsApplicableAnalyzerResource(p)))
.ToArray();

var createdDirectoryList = new List<string>();

Expand Down Expand Up @@ -635,11 +638,22 @@ RegistryEntry packageEntry

if (fileExtension == ".dll")
{
meta = UnityMeta.GetMetaForDll(
GetStableGuid(identity, fileInUnityPackage),
new PlatformDefinition(UnityOs.AnyOs, UnityCpu.None, isEditorConfig: false),
new string[] { "RoslynAnalyzer" },
Array.Empty<string>());
if (NuGetHelper.IsApplicableAnalyzer(analyzerFile))
{
meta = UnityMeta.GetMetaForDll(
GetStableGuid(identity, fileInUnityPackage),
new PlatformDefinition(UnityOs.AnyOs, UnityCpu.None, isEditorConfig: false),
new string[] { "RoslynAnalyzer" },
Array.Empty<string>());
}
else
{
meta = UnityMeta.GetMetaForDll(
GetStableGuid(identity, fileInUnityPackage),
new PlatformDefinition(UnityOs.AnyOs, UnityCpu.None, isEditorConfig: false),
Array.Empty<string>(),
Array.Empty<string>());
}
}
else
{
Expand Down

0 comments on commit 4ec0d47

Please sign in to comment.