Skip to content

Commit ddcb23c

Browse files
committed
Optimize Roslyn analyzers usage
1 parent 9a41ede commit ddcb23c

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/UnityNuGet/RegistryCache.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ RegistryEntry packageEntry
633633
string? meta;
634634

635635
string fileExtension = Path.GetExtension(fileInUnityPackage);
636+
636637
if (fileExtension == ".dll")
637638
{
638639
meta = UnityMeta.GetMetaForDll(
@@ -664,6 +665,20 @@ RegistryEntry packageEntry
664665
// write meta file
665666
await WriteTextFileToTar(tarArchive, $"{fileInUnityPackage}.meta", meta);
666667
}
668+
669+
// Write analyzer asmdef
670+
// Check Analyzer Scope section: https://docs.unity3d.com/Manual/roslyn-analyzers.html
671+
var analyzerAsmdef = CreateAnalyzerAmsdef(identity);
672+
var analyzerAsmdefAsJson = analyzerAsmdef.ToJson();
673+
string analyzerAsmdefFileName = $"{identity.Id}.asmdef";
674+
await WriteTextFileToTar(tarArchive, analyzerAsmdefFileName, analyzerAsmdefAsJson);
675+
await WriteTextFileToTar(tarArchive, $"{analyzerAsmdefFileName}.meta", UnityMeta.GetMetaForExtension(GetStableGuid(identity, analyzerAsmdefFileName), ".asmdef")!);
676+
677+
// Write empty script (Necessary to compile the asmdef file)
678+
var emptyScriptContent = UnityScript.GetEmptyScript();
679+
const string emptyScriptFileName = "EmptyScript.cs";
680+
await WriteTextFileToTar(tarArchive, emptyScriptFileName, emptyScriptContent);
681+
await WriteTextFileToTar(tarArchive, $"{emptyScriptFileName}.meta", UnityMeta.GetMetaForExtension(GetStableGuid(identity, emptyScriptFileName), ".cs")!);
667682
}
668683

669684
// Get all known platform definitions
@@ -1067,6 +1082,15 @@ private static UnityPackage CreateUnityPackage(NpmPackageInfo npmPackageInfo, Np
10671082
return unityPackage;
10681083
}
10691084

1085+
private static UnityAsmdef CreateAnalyzerAmsdef(PackageIdentity packageIdentity)
1086+
{
1087+
return new()
1088+
{
1089+
Name = $"{packageIdentity.Id}_Unity", // Add _Unity suffix because Unity has a validation so that assemblies names do not collide with asmdefs assembly names
1090+
IncludePlatforms = new string[] { "Editor" }
1091+
};
1092+
}
1093+
10701094
private static Guid StringToGuid(string text)
10711095
{
10721096
var guid = new byte[16];

src/UnityNuGet/UnityAsmdef.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Newtonsoft.Json;
2+
3+
namespace UnityNuGet
4+
{
5+
public class UnityAsmdef : JsonObjectBase
6+
{
7+
[JsonProperty("name")]
8+
public string? Name { get; set; }
9+
10+
// The values come from: https://docs.unity3d.com/ScriptReference/BuildTarget.html
11+
[JsonProperty("includePlatforms")]
12+
public string[]? IncludePlatforms { get; set; }
13+
}
14+
}

src/UnityNuGet/UnityMeta.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal static class UnityMeta
1717
case ".pdb":
1818
break;
1919
case ".asmdef":
20+
case ".cs":
2021
case ".json":
2122
case ".md":
2223
case ".txt":

src/UnityNuGet/UnityScript.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace UnityNuGet
2+
{
3+
internal static class UnityScript
4+
{
5+
public static string GetEmptyScript() => string.Empty;
6+
}
7+
}

0 commit comments

Comments
 (0)