Skip to content

Commit 9f7334b

Browse files
committed
refactor
1 parent 0b06968 commit 9f7334b

20 files changed

+442
-483
lines changed

Editor/CscSettingsProvider.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ internal class CscSettingsProvider
1111
private static SerializedProperty s_EnableLogging;
1212
private static SerializedProperty s_AnalyzerFilter;
1313
private static SerializedProperty s_PredefinedAssemblies;
14-
private static SerializedProperty s_IncludedAssemblies;
15-
private static ReorderableList s_RoAnalyzerPackages = null;
16-
14+
private static ReorderableList s_RoAnalyzerPackages;
15+
private static ReorderableList s_RoIncludedAssemblies;
1716

1817
[SettingsProvider]
1918
private static SettingsProvider CreateSettingsProvider()
@@ -22,14 +21,32 @@ private static SettingsProvider CreateSettingsProvider()
2221
s_EnableLogging = serializedObject.FindProperty("m_EnableLogging");
2322
s_AnalyzerFilter = serializedObject.FindProperty("m_AnalyzerFilter");
2423
s_PredefinedAssemblies = s_AnalyzerFilter.FindPropertyRelative("m_PredefinedAssemblies");
25-
s_IncludedAssemblies = s_AnalyzerFilter.FindPropertyRelative("m_IncludedAssemblies");
2624

27-
s_RoAnalyzerPackages = new ReorderableList(serializedObject, serializedObject.FindProperty("m_AnalyzerPackages"));
25+
var analyzerPackages = serializedObject.FindProperty("m_AnalyzerPackages");
26+
s_RoAnalyzerPackages = new ReorderableList(serializedObject, analyzerPackages);
2827
s_RoAnalyzerPackages.drawHeaderCallback = rect => EditorGUI.PrefixLabel(rect, GUIUtility.GetControlID(FocusType.Passive), new GUIContent("Analyzer Packages"));
2928
s_RoAnalyzerPackages.elementHeight = NugetPackageDrawer.Height;
3029
s_RoAnalyzerPackages.drawElementCallback = (rect, index, active, focused) =>
3130
{
32-
var sp = s_RoAnalyzerPackages.serializedProperty.GetArrayElementAtIndex(index);
31+
var sp = analyzerPackages.GetArrayElementAtIndex(index);
32+
EditorGUI.PropertyField(rect, sp, GUIContent.none);
33+
};
34+
35+
var includedAssemblies = s_AnalyzerFilter.FindPropertyRelative("m_IncludedAssemblies");
36+
s_RoIncludedAssemblies = new ReorderableList(serializedObject, includedAssemblies);
37+
s_RoIncludedAssemblies.drawHeaderCallback = rect =>
38+
{
39+
EditorGUI.PrefixLabel(rect, new GUIContent(includedAssemblies.displayName));
40+
41+
rect.x += rect.width - 100;
42+
rect.width = 100;
43+
EditorGUI.LabelField(rect, "* Prefix '!' to exclude.", EditorStyles.miniLabel);
44+
};
45+
s_RoIncludedAssemblies.elementHeight = EditorGUIUtility.singleLineHeight + 2;
46+
s_RoIncludedAssemblies.drawElementCallback = (rect, index, active, focused) =>
47+
{
48+
var sp = includedAssemblies.GetArrayElementAtIndex(index);
49+
rect.height = EditorGUIUtility.singleLineHeight;
3350
EditorGUI.PropertyField(rect, sp, GUIContent.none);
3451
};
3552

@@ -44,6 +61,7 @@ private static SettingsProvider CreateSettingsProvider()
4461

4562
private static void OnGUI(string searchContext)
4663
{
64+
EditorGUILayout.LabelField("Compiler", EditorStyles.boldLabel);
4765
InspectorGUI.DrawCompilerPackage(serializedObject);
4866
EditorGUILayout.Space();
4967

@@ -55,7 +73,7 @@ private static void OnGUI(string searchContext)
5573
{
5674
NugetPackageCatalog.CurrentCategory = NugetPackage.CategoryType.Analyzer;
5775
s_RoAnalyzerPackages.DoLayoutList();
58-
EditorGUILayout.PropertyField(s_IncludedAssemblies);
76+
s_RoIncludedAssemblies.DoLayoutList();
5977
GUILayout.Space(-16);
6078
EditorGUILayout.PropertyField(s_PredefinedAssemblies);
6179
}

Editor/InspectorGUI.cs

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal static class InspectorGUI
2929
private static bool _hasPortableDll = false;
3030
private static bool _changed = false;
3131
private static string _assetPath;
32+
private static ReorderableList _symbolModifier;
33+
3234

3335
private static string[] _ignoredAssetPaths =
3436
{
@@ -72,14 +74,37 @@ private static void OnPostHeaderGUI(Editor editor)
7274
_changed = false;
7375
}
7476

77+
if (_symbolModifier == null)
78+
{
79+
_symbolModifier = new ReorderableList(_serializedObject, _serializedObject.FindProperty("m_SymbolModifier"));
80+
_symbolModifier.elementHeight = EditorGUIUtility.singleLineHeight + 2;
81+
_symbolModifier.drawHeaderCallback = rect =>
82+
{
83+
var property = _symbolModifier.serializedProperty;
84+
EditorGUI.PrefixLabel(rect, new GUIContent(property.displayName));
85+
86+
rect.x += rect.width - 100;
87+
rect.width = 100;
88+
EditorGUI.LabelField(rect, "* Prefix '!' to exclude.", EditorStyles.miniLabel);
89+
};
90+
_symbolModifier.drawElementCallback = (rect, index, active, focused) =>
91+
{
92+
var sp = _symbolModifier.serializedProperty.GetArrayElementAtIndex(index);
93+
rect.height = EditorGUIUtility.singleLineHeight;
94+
EditorGUI.PropertyField(rect, sp, GUIContent.none);
95+
};
96+
}
97+
98+
7599
// Enable.
76100
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
77101
EditorGUI.BeginChangeCheck();
78102
_hasPortableDll = EditorGUILayout.ToggleLeft(s_EnableText, _hasPortableDll, EditorStyles.boldLabel);
79103
if (_hasPortableDll)
80104
{
81105
DrawCompilerPackage(_serializedObject);
82-
EditorGUILayout.PropertyField(_serializedObject.FindProperty("m_ModifySymbols"));
106+
_symbolModifier.serializedProperty = _serializedObject.FindProperty("m_SymbolModifier");
107+
_symbolModifier.DoLayoutList();
83108
}
84109

85110
_changed |= EditorGUI.EndChangeCheck();
@@ -202,63 +227,4 @@ private static void EnablePortableDll(string asmdefPath, bool enabled)
202227
}
203228
}
204229
}
205-
206-
[CustomPropertyDrawer(typeof(SplitAttribute))]
207-
public class SplitDrawer : PropertyDrawer
208-
{
209-
private ReorderableList _ro = null;
210-
private List<string> _list;
211-
private static GUIContent s_PrefixHint = new GUIContent("* Prefix '!' to exclude");
212-
213-
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
214-
{
215-
if (property.propertyType != SerializedPropertyType.String) return EditorGUIUtility.singleLineHeight;
216-
217-
var s = property.stringValue;
218-
var c = (attribute as SplitAttribute).separater;
219-
var count = s.Length - s.Replace(c.ToString(), "").Length + 1;
220-
return count * (EditorGUIUtility.singleLineHeight + 2) + 36;
221-
}
222-
223-
// Draw the property inside the given rect
224-
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
225-
{
226-
if (property.propertyType != SerializedPropertyType.String)
227-
{
228-
EditorGUI.LabelField(position, label.text, "Use Split with string.");
229-
return;
230-
}
231-
232-
var separater = (attribute as SplitAttribute).separater;
233-
if (_ro == null)
234-
{
235-
_list = property.stringValue.Split(separater).ToList();
236-
237-
_ro = new ReorderableList(_list, typeof(string));
238-
_ro.drawHeaderCallback = rect =>
239-
{
240-
EditorGUI.PrefixLabel(rect, GUIUtility.GetControlID(FocusType.Passive), label);
241-
rect.x += rect.width - 100;
242-
rect.width = 100;
243-
EditorGUI.LabelField(rect, s_PrefixHint, EditorStyles.miniLabel);
244-
};
245-
_ro.onAddCallback = list => list.list.Add("");
246-
_ro.elementHeight = EditorGUIUtility.singleLineHeight + 2;
247-
_ro.drawElementCallback = (rect, index, active, focused) =>
248-
{
249-
rect.height = EditorGUIUtility.singleLineHeight;
250-
_list[index] = EditorGUI.TextField(rect, _list[index]);
251-
};
252-
}
253-
254-
EditorGUI.BeginChangeCheck();
255-
_ro.DoList(position);
256-
if (EditorGUI.EndChangeCheck())
257-
{
258-
property.stringValue = 0 < _list.Count
259-
? _list.Aggregate((a, b) => a + ";" + b)
260-
: "";
261-
}
262-
}
263-
}
264230
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using System.Runtime.CompilerServices;
22

33
[assembly: InternalsVisibleTo("CSharpCompilerSettings.Editor")]
4-
[assembly: InternalsVisibleTo("CSharpCompilerSettings.Test")]
4+
[assembly: InternalsVisibleTo("CSharpCompilerSettingsTests")]

Plugins/CSharpCompilerSettings/Core.cs

Lines changed: 79 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,27 @@ public static string GetPortableDllPath(string asmdefPath)
5454
.FirstOrDefault(x => Regex.IsMatch(x, "CSharpCompilerSettings_[0-9a-zA-Z]{32}.dll"));
5555
}
5656

57-
5857
private static bool IsInSameDirectory(string path)
5958
{
6059
if (string.IsNullOrEmpty(path)) return false;
6160

62-
var dir = Path.GetFullPath(Path.GetDirectoryName(path));
63-
var coreAssemblyLocationDir = Path.GetFullPath(Path.GetDirectoryName(typeof(Core).Assembly.Location));
61+
var dir = Path.GetFullPath(Path.GetDirectoryName(path)).ToLower();
62+
var coreAssemblyLocationDir = Path.GetFullPath(Path.GetDirectoryName(typeof(Core).Assembly.Location)).ToLower();
6463
return dir == coreAssemblyLocationDir;
6564
}
6665

67-
private static string FindAsmdef()
66+
private static string FindAsmdef(string path = null)
6867
{
69-
var asmdefPath = Directory.GetFiles(Path.GetDirectoryName(typeof(Core).Assembly.Location), "*.asmdef")
68+
if (string.IsNullOrEmpty(path))
69+
{
70+
var assemblyPath = typeof(Core).Assembly.Location;
71+
assemblyPath = AssetDatabase.FindAssets("t:DefaultAsset " + Path.GetFileNameWithoutExtension(assemblyPath))
72+
.Select(AssetDatabase.GUIDToAssetPath)
73+
.FirstOrDefault(x => x.EndsWith(".dll"));
74+
path = Path.GetDirectoryName(assemblyPath);
75+
}
76+
77+
var asmdefPath = Directory.GetFiles(path, "*.asmdef")
7078
.FirstOrDefault(x => x.EndsWith(".asmdef")) ?? "";
7179

7280
return asmdefPath.Replace('\\', '/').Replace(Environment.CurrentDirectory.Replace('\\', '/') + "/", "");
@@ -89,7 +97,8 @@ private static void ChangeCompilerProcess(object compiler, object scriptAssembly
8997
var command = oldCommand.Replace(EditorApplication.applicationContentsPath.Replace('\\', '/'), "@APP_CONTENTS@");
9098
var isDefaultCsc = Regex.IsMatch(command, "@APP_CONTENTS@/[^ ]*(mcs|csc)");
9199
var assemblyName = Path.GetFileNameWithoutExtension(scriptAssembly.Get("Filename") as string);
92-
var originPath = scriptAssembly.Get("OriginPath") as string;
100+
var asmdefDir = scriptAssembly.Get("OriginPath") as string;
101+
var asmdefPath = string.IsNullOrEmpty(asmdefDir) ? "" : FindAsmdef(asmdefDir);
93102

94103
// csc is not Unity default. It is already modified.
95104
if (!isDefaultCsc)
@@ -101,62 +110,72 @@ private static void ChangeCompilerProcess(object compiler, object scriptAssembly
101110
// Kill current process.
102111
compiler.Call("Dispose");
103112

104-
var compilerInfo = CompilerInfo.GetInstalledInfo(setting.CompilerPackage.PackageId);
105-
106-
// csc is not installed. Restart current process.
107-
if (!compilerInfo.IsValid)
108-
{
109-
Logger.LogWarning(" <color=#bbbb44><Skipped> C# compiler '{0}' is not installed. Restart compiler process: {1}</color>", compilerInfo.Path, oldCommand);
110-
111-
var currentProgram = tProgram.New(psi);
112-
currentProgram.Call("Start");
113-
compiler.Set("process", currentProgram, fiProcess);
114-
return;
115-
}
116-
117-
// Change exe file path.
113+
// Response file.
118114
var responseFile = Regex.Replace(psi.Arguments, "^.*@(.+)$", "$1");
119-
compilerInfo.Setup(psi, responseFile);
120-
121115
var text = File.ReadAllText(responseFile);
122116
text = Regex.Replace(text, "[\r\n]+", "\n");
123117
text = Regex.Replace(text, "^-", "/");
124-
text = Regex.Replace(text, "\n/langversion:[^\n]+\n", "\n/langversion:" + setting.LanguageVersion + "\n");
125118
text = Regex.Replace(text, "\n/debug\n", "\n/debug:portable\n");
126119
text += "\n/preferreduilang:en-US";
127120

128-
// Nullable.
129-
text += "\n/nullable:" + setting.Nullable.ToString().ToLower();
121+
// Compiler
122+
if (!setting.UseDefaultCompiler)
123+
{
124+
var compilerInfo = CompilerInfo.GetInstalledInfo(setting.CompilerPackage.PackageId);
125+
126+
// csc is not installed. Restart current process.
127+
if (!compilerInfo.IsValid)
128+
{
129+
Logger.LogWarning(" <color=#bbbb44><Skipped> C# compiler '{0}' is not installed. Restart compiler process: {1}</color>", compilerInfo.Path, oldCommand);
130+
131+
var currentProgram = tProgram.New(psi);
132+
currentProgram.Call("Start");
133+
compiler.Set("process", currentProgram, fiProcess);
134+
return;
135+
}
136+
137+
// Change exe file path.
138+
compilerInfo.Setup(psi, responseFile);
139+
text = Regex.Replace(text, "\n/langversion:[^\n]+\n", "\n/langversion:" + setting.LanguageVersion + "\n");
140+
141+
// Nullable.
142+
text += "\n/nullable:" + setting.Nullable.ToString().ToLower();
143+
}
130144

131145
// Modify scripting define symbols.
132-
var defines = Regex.Matches(text, "^/define:(.*)$", RegexOptions.Multiline)
133-
.Cast<Match>()
134-
.Select(x => x.Groups[1].Value);
135-
text = Regex.Replace(text, "[\r\n]+/define:[^\r\n]+", "");
136-
var modifiedDefines = Utils.ModifySymbols(defines, setting.AdditionalSymbols);
137-
foreach (var d in modifiedDefines)
138-
text += "\n/define:" + d;
139-
140-
// Setup analyzer.
141-
foreach (var package in setting.AnalyzerPackages)
146+
if (!string.IsNullOrEmpty(setting.AdditionalSymbols))
142147
{
143-
var analyzerInfo = AnalyzerInfo.GetInstalledInfo(package.PackageId);
144-
foreach (var dll in analyzerInfo.DllFiles)
145-
text += string.Format("\n/analyzer:\"{0}\"", dll);
148+
var defines = Regex.Matches(text, "^/define:(.*)$", RegexOptions.Multiline)
149+
.Cast<Match>()
150+
.Select(x => x.Groups[1].Value);
151+
text = Regex.Replace(text, "[\r\n]+/define:[^\r\n]+", "");
152+
var modifiedDefines = Utils.ModifySymbols(defines, setting.AdditionalSymbols);
153+
foreach (var d in modifiedDefines)
154+
text += "\n/define:" + d;
146155
}
147156

148-
// Ruleset.
149-
var rulesets = new[] {"Assets/Default.ruleset"}
150-
.Concat(string.IsNullOrEmpty(originPath)
151-
? new[] {"Assets/" + assemblyName + ".ruleset"}
152-
: Directory.GetFiles(originPath, "*.ruleset"))
153-
.Where(File.Exists);
154-
155-
foreach (var ruleset in rulesets)
156-
text += string.Format("\n/ruleset:\"{0}\"", ruleset);
157-
157+
// Analyzer.
158+
var globalSettings = CscSettingsAsset.instance;
159+
if (globalSettings.ShouldToRecompileToAnalyze(asmdefPath))
160+
{
161+
// Analyzer dlls.
162+
foreach (var package in globalSettings.AnalyzerPackages)
163+
{
164+
var analyzerInfo = AnalyzerInfo.GetInstalledInfo(package.PackageId);
165+
foreach (var dll in analyzerInfo.DllFiles)
166+
text += string.Format("\n/analyzer:\"{0}\"", dll);
167+
}
158168

169+
// Ruleset.
170+
var rulesets = new[] {"Assets/Default.ruleset"}
171+
.Concat(string.IsNullOrEmpty(asmdefDir)
172+
? new[] {"Assets/" + assemblyName + ".ruleset"}
173+
: Directory.GetFiles(asmdefDir, "*.ruleset"))
174+
.Where(File.Exists);
159175

176+
foreach (var ruleset in rulesets)
177+
text += string.Format("\n/ruleset:\"{0}\"", ruleset);
178+
}
160179

161180
// Replace NewLine and save.
162181
text = Regex.Replace(text, "\n", Environment.NewLine);
@@ -188,12 +207,13 @@ private static void OnAssemblyCompilationStarted(string name)
188207
var compilerTasks = tEditorCompilationInterface.Get("Instance").Get("compilationTask").Get("compilerTasks") as IDictionary;
189208
var scriptAssembly = compilerTasks.Keys.Cast<object>().FirstOrDefault(x => (x.Get("Filename") as string) == assemblyName + ".dll");
190209
if (scriptAssembly == null)
210+
{
211+
Logger.LogWarning(" <color=#bbbb44><Skipped> scriptAssembly <b>'{0}'</b> is not found.</color>", assemblyName);
191212
return;
213+
}
192214

193215
var asmdefDir = scriptAssembly.Get("OriginPath") as string;
194-
var asmdefPath = string.IsNullOrEmpty(asmdefDir)
195-
? ""
196-
: Directory.GetFiles(asmdefDir, "*.asmdef").First().Replace('\\', '/').Replace(Environment.CurrentDirectory.Replace('\\', '/') + "/", "");
216+
var asmdefPath = string.IsNullOrEmpty(asmdefDir) ? "" : FindAsmdef(asmdefDir);
197217
if (IsGlobal && GetPortableDllPath(asmdefPath) != null)
198218
{
199219
Logger.LogWarning(" <color=#bbbb44><Skipped> Local CSharpCompilerSettings.*.dll for <b>'{0}'</b> is found.</color>", assemblyName);
@@ -206,10 +226,9 @@ private static void OnAssemblyCompilationStarted(string name)
206226
return;
207227
}
208228

209-
var settings = IsGlobal
210-
? CscSettingsAsset.instance
211-
: CscSettingsAsset.GetAtPath(asmdefPath);
212-
if (!settings || !settings.ShouldToRecompile)
229+
var globalSettings = CscSettingsAsset.instance;
230+
var settings = GetSettings();
231+
if (!settings.ShouldToRecompile && !globalSettings.ShouldToRecompileToAnalyze(asmdefPath))
213232
{
214233
Logger.LogWarning(" <color=#bbbb44><Skipped> Assembly <b>'{0}'</b> does not need to be recompiled.</color>", assemblyName);
215234
return;
@@ -276,11 +295,12 @@ static Core()
276295

277296
// Install custom csc before compilation.
278297
var settings = GetSettings();
279-
if (!settings || settings.UseDefaultCompiler) return;
280-
CompilerInfo.GetInstalledInfo(settings.CompilerPackage.PackageId);
298+
if (!settings.UseDefaultCompiler)
299+
CompilerInfo.GetInstalledInfo(settings.CompilerPackage.PackageId);
281300

282-
foreach (var package in settings.AnalyzerPackages)
283-
AnalyzerInfo.GetInstalledInfo(package.PackageId);
301+
if (IsGlobal)
302+
foreach (var package in settings.AnalyzerPackages.Where(x => x.IsValid))
303+
AnalyzerInfo.GetInstalledInfo(package.PackageId);
284304

285305
// If Unity 2020.2 or newer, request re-compilation.
286306
var version = Application.unityVersion.Split('.');

0 commit comments

Comments
 (0)