Skip to content

Commit acfa0e6

Browse files
committed
feat: add assembly filter to compile with custom compiler
1 parent f16985b commit acfa0e6

File tree

7 files changed

+165
-87
lines changed

7 files changed

+165
-87
lines changed

Editor/AssemblyFilterDrawer.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using UnityEditor;
6+
using UnityEditorInternal;
7+
using UnityEngine;
8+
9+
namespace Coffee.CSharpCompilerSettings
10+
{
11+
[CustomPropertyDrawer(typeof(AssemblyFilter))]
12+
internal class AssemblyFilterDrawer : PropertyDrawer
13+
{
14+
private bool _isInitialized;
15+
private SerializedProperty _includedAssemblies;
16+
private SerializedProperty _predefinedAssemblies;
17+
private ReorderableList _roIncludedAssemblies;
18+
19+
private void Initialize(SerializedProperty property)
20+
{
21+
if (_isInitialized) return;
22+
_isInitialized = true;
23+
24+
_predefinedAssemblies = property.FindPropertyRelative("m_PredefinedAssemblies");
25+
_includedAssemblies = property.FindPropertyRelative("m_IncludedAssemblies");
26+
27+
_roIncludedAssemblies = new ReorderableList(property.serializedObject, _includedAssemblies, true, true, true, true);
28+
_roIncludedAssemblies.drawHeaderCallback = rect =>
29+
{
30+
EditorGUI.PrefixLabel(rect, new GUIContent(_includedAssemblies.displayName));
31+
32+
rect.x += rect.width - 110;
33+
rect.width = 110;
34+
EditorGUI.LabelField(rect, "* Prefix '!' to exclude.", EditorStyles.miniLabel);
35+
};
36+
_roIncludedAssemblies.elementHeight = EditorGUIUtility.singleLineHeight + 2;
37+
_roIncludedAssemblies.drawElementCallback = (rect, index, active, focused) =>
38+
{
39+
var sp = _includedAssemblies.GetArrayElementAtIndex(index);
40+
rect.height = EditorGUIUtility.singleLineHeight;
41+
EditorGUI.PropertyField(rect, sp, GUIContent.none);
42+
};
43+
}
44+
45+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
46+
{
47+
Initialize(property);
48+
return _includedAssemblies.arraySize * (EditorGUIUtility.singleLineHeight + 2) + 47;
49+
}
50+
51+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
52+
{
53+
EditorGUI.BeginProperty(position, label, property);
54+
var p = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
55+
56+
_roIncludedAssemblies.DoList(new Rect(p.x - 3, p.y, p.width + 6, p.height));
57+
58+
EditorGUI.PropertyField(new Rect(p.x, p.y + p.height - 16, p.width, 16), _predefinedAssemblies);
59+
60+
EditorGUI.EndProperty();
61+
}
62+
}
63+
}

Editor/AssemblyFilterDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/CSharpProjectModifier.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,31 @@ private static string OnGeneratedCSProject(string path, string content)
2020

2121
// Modify define symbols.
2222
var defines = Regex.Match(content, "<DefineConstants>(.*)</DefineConstants>").Groups[1].Value.Split(';', ',');
23-
defines = Utils.ModifySymbols(defines, setting.AdditionalSymbols);
23+
defines = Utils.ModifySymbols(defines, setting.GetSymbolModifier(asmdefPath));
2424
var defineText = string.Join(";", defines);
2525
content = Regex.Replace(content, "<DefineConstants>(.*)</DefineConstants>", string.Format("<DefineConstants>{0}</DefineConstants>", defineText), RegexOptions.Multiline);
2626

27-
if (!setting.UseDefaultCompiler)
27+
if (setting.ShouldToUseCustomCompiler(asmdefPath))
2828
{
2929
// Language version.
3030
content = Regex.Replace(content, "<LangVersion>.*</LangVersion>", "<LangVersion>" + setting.LanguageVersion + "</LangVersion>", RegexOptions.Multiline);
31-
}
3231

33-
// Nullable.
34-
var value = setting.Nullable.ToString().ToLower();
35-
if (Regex.IsMatch(content, "<Nullable>.*</Nullable>"))
36-
{
37-
content = Regex.Replace(content, "<Nullable>.*</Nullable>", "<Nullable>" + value + "</Nullable>");
38-
}
39-
else
40-
{
41-
content = Regex.Replace(content, "(\\s+)(<LangVersion>.*</LangVersion>)([\r\n]+)", "$1$2$3$1<Nullable>" + value + "</Nullable>$3");
32+
// Nullable.
33+
if (!setting.IsSupportNullable)
34+
{
35+
var value = setting.Nullable.ToString().ToLower();
36+
if (Regex.IsMatch(content, "<Nullable>.*</Nullable>"))
37+
{
38+
content = Regex.Replace(content, "<Nullable>.*</Nullable>", "<Nullable>" + value + "</Nullable>");
39+
}
40+
else
41+
{
42+
content = Regex.Replace(content, "(\\s+)(<LangVersion>.*</LangVersion>)([\r\n]+)", "$1$2$3$1<Nullable>" + value + "</Nullable>$3");
43+
}
44+
}
4245
}
4346

4447
// Additional contents.
45-
// content = Regex.Replace(content, "^</Project>", "<!-- C# Settings For Unity -->", RegexOptions.Singleline);
4648
content = Regex.Replace(content, "[\r\n]+</Project>[\r\n]*", "\r\n<!-- C# Settings For Unity -->");
4749
{
4850
content += NewLine + " <ItemGroup>";

Editor/CscSettingsProvider.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ internal class CscSettingsProvider
99
{
1010
private static SerializedObject serializedObject;
1111
private static SerializedProperty s_EnableLogging;
12+
private static SerializedProperty s_CompilerFilter;
1213
private static SerializedProperty s_AnalyzerFilter;
13-
private static SerializedProperty s_PredefinedAssemblies;
1414
private static ReorderableList s_RoAnalyzerPackages;
15-
private static ReorderableList s_RoIncludedAssemblies;
1615

1716
[SettingsProvider]
1817
private static SettingsProvider CreateSettingsProvider()
1918
{
2019
serializedObject = new SerializedObject(CscSettingsAsset.instance);
2120
s_EnableLogging = serializedObject.FindProperty("m_EnableLogging");
21+
s_CompilerFilter = serializedObject.FindProperty("m_CompilerFilter");
2222
s_AnalyzerFilter = serializedObject.FindProperty("m_AnalyzerFilter");
23-
s_PredefinedAssemblies = s_AnalyzerFilter.FindPropertyRelative("m_PredefinedAssemblies");
2423

2524
var analyzerPackages = serializedObject.FindProperty("m_AnalyzerPackages");
2625
s_RoAnalyzerPackages = new ReorderableList(serializedObject, analyzerPackages);
@@ -36,28 +35,10 @@ private static SettingsProvider CreateSettingsProvider()
3635
for (var i = 0; i < analyzerPackages.arraySize; i++)
3736
{
3837
var sp = analyzerPackages.GetArrayElementAtIndex(i);
39-
sp.FindPropertyRelative("m_Category").intValue = (int)NugetPackage.CategoryType.Analyzer;
38+
sp.FindPropertyRelative("m_Category").intValue = (int) NugetPackage.CategoryType.Analyzer;
4039
}
4140
};
4241

43-
var includedAssemblies = s_AnalyzerFilter.FindPropertyRelative("m_IncludedAssemblies");
44-
s_RoIncludedAssemblies = new ReorderableList(serializedObject, includedAssemblies);
45-
s_RoIncludedAssemblies.drawHeaderCallback = rect =>
46-
{
47-
EditorGUI.PrefixLabel(rect, new GUIContent(includedAssemblies.displayName));
48-
49-
rect.x += rect.width - 100;
50-
rect.width = 100;
51-
EditorGUI.LabelField(rect, "* Prefix '!' to exclude.", EditorStyles.miniLabel);
52-
};
53-
s_RoIncludedAssemblies.elementHeight = EditorGUIUtility.singleLineHeight + 2;
54-
s_RoIncludedAssemblies.drawElementCallback = (rect, index, active, focused) =>
55-
{
56-
var sp = includedAssemblies.GetArrayElementAtIndex(index);
57-
rect.height = EditorGUIUtility.singleLineHeight;
58-
EditorGUI.PropertyField(rect, sp, GUIContent.none);
59-
};
60-
6142
var keywords = SettingsProvider.GetSearchKeywordsFromSerializedObject(serializedObject);
6243
return new SettingsProvider("Project/C# Compiler", SettingsScope.Project)
6344
{
@@ -70,20 +51,30 @@ private static SettingsProvider CreateSettingsProvider()
7051
private static void OnGUI(string searchContext)
7152
{
7253
EditorGUILayout.LabelField("Compiler", EditorStyles.boldLabel);
73-
InspectorGUI.DrawCompilerPackage(serializedObject);
54+
if (InspectorGUI.DrawCompilerPackage(serializedObject))
55+
{
56+
using (new GUILayout.HorizontalScope())
57+
{
58+
GUILayout.Space(4);
59+
using (new GUILayout.VerticalScope())
60+
{
61+
GUILayout.Space(10);
62+
EditorGUILayout.PropertyField(s_CompilerFilter, GUIContent.none, true);
63+
}
64+
}
65+
}
66+
7467
EditorGUILayout.Space();
7568

7669
EditorGUILayout.LabelField("Analyzer", EditorStyles.boldLabel);
7770
using (new GUILayout.HorizontalScope())
7871
{
79-
GUILayout.Space(20);
72+
GUILayout.Space(4);
8073
using (new GUILayout.VerticalScope())
8174
{
8275
NugetPackageCatalog.CurrentCategory = NugetPackage.CategoryType.Analyzer;
8376
s_RoAnalyzerPackages.DoLayoutList();
84-
s_RoIncludedAssemblies.DoLayoutList();
85-
GUILayout.Space(-18);
86-
EditorGUILayout.PropertyField(s_PredefinedAssemblies);
77+
EditorGUILayout.PropertyField(s_AnalyzerFilter, GUIContent.none, true);
8778
}
8879
}
8980

Editor/InspectorGUI.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,13 @@ private static void OnPostHeaderGUI(Editor editor)
140140
EditorGUILayout.EndVertical();
141141
}
142142

143-
public static void DrawCompilerPackage(SerializedObject so)
143+
public static bool DrawCompilerPackage(SerializedObject so)
144144
{
145145
var spCompilerType = so.FindProperty("m_CompilerType");
146146
EditorGUILayout.PropertyField(spCompilerType);
147147

148-
if (spCompilerType.intValue == (int) CompilerType.CustomPackage)
148+
bool isCustomPackage = spCompilerType.intValue == (int) CompilerType.CustomPackage;
149+
if (isCustomPackage)
149150
{
150151
EditorGUI.indentLevel++;
151152
NugetPackageCatalog.CurrentCategory = NugetPackage.CategoryType.Compiler;
@@ -154,6 +155,8 @@ public static void DrawCompilerPackage(SerializedObject so)
154155
EditorGUILayout.PropertyField(so.FindProperty("m_Nullable"));
155156
EditorGUI.indentLevel--;
156157
}
158+
159+
return isCustomPackage;
157160
}
158161

159162
public static void DrawControl(bool changed, Action onRevert = null, Action onApply = null, Action onReload = null, Action onPublish = null)

Plugins/CSharpCompilerSettings/Core.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,38 @@ public static string ModifyResponseFile(CscSettingsAsset setting, string assembl
8484
text = Regex.Replace(text, "[\r\n]+", "\n");
8585
text = Regex.Replace(text, "^-", "/");
8686
text = Regex.Replace(text, "\n/debug\n", "\n/debug:portable\n");
87-
text = Regex.Replace(text, "\n/nullable.*", "");
8887
text += "\n/preferreduilang:en-US";
8988

90-
// Compiler
91-
if (!setting.UseDefaultCompiler)
89+
// Custom compiler.
90+
if (setting.ShouldToUseCustomCompiler(asmdefPath))
9291
{
9392
// Change language version.
9493
text = Regex.Replace(text, "\n/langversion:[^\n]+\n", "\n/langversion:" + setting.LanguageVersion + "\n");
9594

9695
// Nullable.
96+
text = Regex.Replace(text, "\n/nullable.*", "");
9797
if (setting.IsSupportNullable)
98+
{
9899
text += "\n/nullable:" + setting.Nullable.ToString().ToLower();
100+
}
99101
}
100102

101103
// Modify scripting define symbols.
102-
if (!string.IsNullOrEmpty(setting.AdditionalSymbols))
104+
var symbolModifier = setting.GetSymbolModifier(asmdefPath);
105+
if (!string.IsNullOrEmpty(symbolModifier))
103106
{
104107
var defines = Regex.Matches(text, "^/define:(.*)$", RegexOptions.Multiline)
105108
.Cast<Match>()
106109
.Select(x => x.Groups[1].Value);
107110
text = Regex.Replace(text, "[\r\n]+/define:[^\r\n]+", "");
108-
var modifiedDefines = Utils.ModifySymbols(defines, setting.AdditionalSymbols);
111+
var modifiedDefines = Utils.ModifySymbols(defines, symbolModifier);
109112
foreach (var d in modifiedDefines)
110113
text += "\n/define:" + d;
111114
}
112115

113116
// Analyzer.
114117
var globalSettings = CscSettingsAsset.instance;
115-
if (globalSettings.ShouldToRecompileToAnalyze(asmdefPath))
118+
if (globalSettings.ShouldToUseAnalyzer(asmdefPath))
116119
{
117120
// Analyzer dlls.
118121
foreach (var package in globalSettings.AnalyzerPackages)
@@ -175,8 +178,8 @@ private static void ChangeCompilerProcess(object compiler, object scriptAssembly
175178
// Response file.
176179
var responseFile = Regex.Replace(psi.Arguments, "^.*@(.+)$", "$1");
177180

178-
// Compiler
179-
if (!setting.UseDefaultCompiler)
181+
// Change to custom compiler.
182+
if (setting.ShouldToUseCustomCompiler(asmdefPath))
180183
{
181184
var compilerInfo = CompilerInfo.GetInstalledInfo(setting.CompilerPackage.PackageId);
182185

@@ -247,7 +250,7 @@ public static void OnAssemblyCompilationStarted(string name)
247250

248251
var globalSettings = CscSettingsAsset.instance;
249252
var settings = GetSettings();
250-
if (!settings.ShouldToRecompile && !globalSettings.ShouldToRecompileToAnalyze(asmdefPath))
253+
if (!globalSettings.ShouldToRecompile(asmdefPath))
251254
{
252255
Logger.LogWarning(" <color=#bbbb44><Skipped> Assembly <b>'{0}'</b> does not need to be recompiled.</color>", assemblyName);
253256
return;

0 commit comments

Comments
 (0)