Skip to content

opt: improved analyzer collect approach #173

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 25 additions & 9 deletions Assets/Scripts/Editor/Compilation/DotnetExeCompilator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Xml.Linq;
using FastScriptReload.Editor.AssemblyPostProcess;
using HarmonyLib;
using ImmersiveVRTools.Editor.Common.Cache;
Expand All @@ -31,8 +32,7 @@ public class DotnetExeDynamicCompilation : DynamicCompilationBase
private static readonly List<string> _analyzers = new List<string>();
static DotnetExeDynamicCompilation()
{
const string RoslynAnalyzerExtension = ".dll";
const string RoslynAnalyzerKeyword = "RoslynAnalyzer";
const string DefaultUnityProjectFilePath = "Assembly-CSharp.csproj";
#if UNITY_EDITOR_WIN
const string dotnetExecutablePath = "dotnet.exe";
#else
Expand All @@ -43,17 +43,33 @@ static DotnetExeDynamicCompilation()
_cscDll = FindFileOrThrow("csc.dll"); //even on mac/linux need to find dll and use, not no extension one
_tempFolder = Path.GetTempPath();

foreach (var guid in AssetDatabase.FindAssets("t: " + nameof(DefaultAsset)))
if (File.Exists(DefaultUnityProjectFilePath))
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
if (!assetPath.EndsWith(RoslynAnalyzerExtension)) continue;
var asset = AssetDatabase.LoadAssetAtPath<DefaultAsset>(assetPath);
var assetLabels = AssetDatabase.GetLabels(asset);
if (assetLabels.Contains(RoslynAnalyzerKeyword))
try
{
_analyzers.Add(Path.GetFullPath(assetPath));
var xmlCSProj = File.ReadAllText(DefaultUnityProjectFilePath);
var xdoc = XDocument.Parse(xmlCSProj);
var analyzers = xdoc.Root.Elements("ItemGroup")
.Elements("Analyzer")
.Select(e => e.Attribute("Include").Value)
.Where(File.Exists)
.ToArray()
;

foreach (var analyzer in analyzers)
{
_analyzers.Add(analyzer);
}
}
catch (Exception e)
{
LoggerScoped.LogWarning($"Unable to collect roslyn analyzers. {e}");
}
}
else
{
LoggerScoped.LogWarning($"{DefaultUnityProjectFilePath} does not exists, hence analyzers in project are ignored.");
}

EditorApplication.playModeStateChanged += obj =>
{
Expand Down