Skip to content

Commit 9d3c071

Browse files
simonferquelPackage Release Automation
authored andcommitted
Integration: - Performance improvements with `EditorApplication.update` callbacks. Project generation: - Add extra compiler options for analyzers and source generators.
2 parents e31ca0c + 5b586bc commit 9d3c071

File tree

9 files changed

+193
-25
lines changed

9 files changed

+193
-25
lines changed

Packages/com.unity.ide.visualstudio.tests/Tests/Editor/CSProjectTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,79 @@ public void RoslynAnalyzerRulesetPaths_WillBeIncluded()
818818
}
819819
#endif
820820

821+
#if UNITY_2021_3_OR_NEWER && !UNITY_2022_1 // we have support in 2021.3, 2022.2 but without a backport in 2022.1
822+
[Test]
823+
public void AnalyzerConfigPath_WillBeIncluded()
824+
{
825+
try
826+
{
827+
const string analyzerConfigPath = "Assets/Default.globalconfig";
828+
m_Builder.WithAnalyzerConfigPath(analyzerConfigPath)
829+
.Build()
830+
.Sync();
831+
832+
XMLUtilities.AssertAnalyzerConfigPathIsIncluded(
833+
XMLUtilities.FromText(m_Builder.ReadProjectFile(m_Builder.Assembly)),
834+
analyzerConfigPath.MakeAbsolutePath().NormalizePathSeparators());
835+
}
836+
finally
837+
{
838+
m_Builder.CleanUp();
839+
}
840+
}
841+
842+
[Test]
843+
public void AdditionalFilePaths_WillBeIncluded()
844+
{
845+
try
846+
{
847+
const string responseFile = "csc.rsp";
848+
m_Builder
849+
.WithAdditionalFilePaths(new[] { "FileA.Analyzer.additionalfile", "/FileB.Analyzer.additionalfile" })
850+
.WithResponseFileData(m_Builder.Assembly, responseFile, otherArguments: new[] { "/additionalfile:FileArsp.Analyzer.additionalfile", "/additionalfile:/FileBrsp.Analyzer.additionalfile" })
851+
.Build()
852+
.Sync();
853+
854+
XMLUtilities.AssertAdditionalFilePathsAreIncluded(
855+
XMLUtilities.FromText(m_Builder.ReadProjectFile(m_Builder.Assembly)),
856+
new[]
857+
{
858+
"FileA.Analyzer.additionalfile".MakeAbsolutePath().NormalizePathSeparators(),
859+
"/FileB.Analyzer.additionalfile".NormalizePathSeparators(),
860+
"FileArsp.Analyzer.additionalfile".MakeAbsolutePath().NormalizePathSeparators(),
861+
"/FileBrsp.Analyzer.additionalfile".NormalizePathSeparators(),
862+
});
863+
}
864+
finally
865+
{
866+
m_Builder.CleanUp();
867+
}
868+
}
869+
870+
[Test]
871+
public void AdditionalFilePaths_NoDuplicate()
872+
{
873+
try
874+
{
875+
m_Builder
876+
.WithAdditionalFilePaths(new[] { "FileA.Analyzer.additionalfile", "FileA.Analyzer.additionalfile" })
877+
.Build()
878+
.Sync();
879+
880+
XMLUtilities.AssertAdditionalFilePathsAreIncluded(
881+
XMLUtilities.FromText(m_Builder.ReadProjectFile(m_Builder.Assembly)),
882+
new[]
883+
{
884+
"FileA.Analyzer.additionalfile".MakeAbsolutePath().NormalizePathSeparators(),
885+
});
886+
}
887+
finally
888+
{
889+
m_Builder.CleanUp();
890+
}
891+
}
892+
#endif
893+
821894
[Test]
822895
public void DllInSourceFiles_WillBeAddedAsReference()
823896
{

Packages/com.unity.ide.visualstudio.tests/Tests/Editor/SynchronizerBuilder.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,26 @@ public SynchronizerBuilder WithRulesetPath(string rulesetFilePath)
209209
}
210210
#endif
211211

212+
#if UNITY_2021_3_OR_NEWER && !UNITY_2022_1 // we have support in 2021.3, 2022.2 but without a backport in 2022.1
213+
public SynchronizerBuilder WithAdditionalFilePaths(string[] additionalFilePaths)
214+
{
215+
foreach (Assembly assembly in m_Assemblies)
216+
{
217+
assembly.compilerOptions.RoslynAdditionalFilePaths = additionalFilePaths;
218+
}
219+
return WithAnalyzerSupport();
220+
}
221+
222+
public SynchronizerBuilder WithAnalyzerConfigPath(string analyzerConfigPath)
223+
{
224+
foreach (Assembly assembly in m_Assemblies)
225+
{
226+
assembly.compilerOptions.AnalyzerConfigPath = analyzerConfigPath;
227+
}
228+
return WithAnalyzerSupport();
229+
}
230+
#endif
231+
212232
public class MyMockIExternalCodeEditor : VisualStudioEditor
213233
{
214234
private Version LatestLanguageVersionSupported = new Version(7, 3);

Packages/com.unity.ide.visualstudio.tests/Tests/Editor/XMLUtilities.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ internal static void AssertAnalyzerRuleSetsAreIncluded(XmlDocument projectXml, s
3333
CollectionAssert.Contains(projectXml.SelectElementValues("/msb:Project/msb:PropertyGroup/msb:CodeAnalysisRuleSet"), expectedRuleSetPath);
3434
}
3535

36+
public static void AssertAdditionalFilePathsAreIncluded(XmlDocument projectXml, IEnumerable<string> expectedPaths)
37+
{
38+
foreach (string path in expectedPaths.Select(FileUtility.NormalizePathSeparators))
39+
{
40+
CollectionAssert.Contains(
41+
projectXml.SelectAttributeValues("/msb:Project/msb:ItemGroup/msb:AdditionalFiles/@Include"), path);
42+
}
43+
}
44+
45+
internal static void AssertAnalyzerConfigPathIsIncluded(XmlDocument projectXml, string path)
46+
{
47+
CollectionAssert.Contains(projectXml.SelectAttributeValues("/msb:Project/msb:ItemGroup/msb:EditorConfigFiles/@Include"), path);
48+
}
49+
3650
static XmlNamespaceManager GetModifiedXmlNamespaceManager(XmlDocument projectXml)
3751
{
3852
var xmlNamespaces = new XmlNamespaceManager(projectXml.NameTable);

Packages/com.unity.ide.visualstudio.tests/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "com.unity.ide.visualstudio.tests",
44
"displayName": "Visual Studio Editor",
55
"description": "Code editor integration for supporting Visual Studio as code editor for unity. Adds support for generating csproj files for intellisense purposes, auto discovery of installations, etc.",
6-
"version": "2.0.16",
6+
"version": "2.0.18",
77
"unity": "2019.4",
88
"unityRelease": "25f1",
99
"dependencies": {

Packages/com.unity.ide.visualstudio/CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
# Code Editor Package for Visual Studio
22

3+
## [2.0.18] - 2023-03-17
4+
5+
Integration:
6+
7+
- Performance improvements with `EditorApplication.update` callbacks.
8+
9+
Project generation:
10+
11+
- Add extra compiler options for analyzers and source generators.
12+
13+
14+
## [2.0.17] - 2022-12-06
15+
16+
Integration:
17+
18+
- Fix rare deadlocks while discovering or launching Visual Studio on Windows.
19+
- Improve launching Visual Studio on macOs.
20+
21+
Project generation:
22+
23+
- Include analyzers from response files.
24+
- Update supported C# versions.
25+
- Performance improvements.
26+
27+
328
## [2.0.16] - 2022-06-08
429

530
Integration:

Packages/com.unity.ide.visualstudio/Editor/ProjectGeneration/ProjectGeneration.cs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -664,33 +664,50 @@ private static IEnumerable<string> GetOtherArguments(ResponseFileData[] response
664664
}
665665
}
666666

667-
private string[] GetAnalyzers(Assembly assembly, ResponseFileData[] responseFilesData, out string rulesetPath)
667+
private void SetAnalyzerAndSourceGeneratorProperties(Assembly assembly, ResponseFileData[] responseFilesData, ProjectProperties properties)
668668
{
669-
rulesetPath = null;
670-
671669
if (m_CurrentInstallation == null || !m_CurrentInstallation.SupportsAnalyzers)
672-
return Array.Empty<string>();
673-
670+
return;
671+
674672
// Analyzers provided by VisualStudio
675-
List<string> analyzers = new List<string>(m_CurrentInstallation.GetAnalyzers());
673+
var analyzers = new List<string>(m_CurrentInstallation.GetAnalyzers());
674+
var additionalFilePaths = new List<string>();
675+
var rulesetPath = string.Empty;
676+
var analyzerConfigPath = string.Empty;
676677

677678
#if UNITY_2020_2_OR_NEWER
678679
// Analyzers + ruleset provided by Unity
679680
analyzers.AddRange(assembly.compilerOptions.RoslynAnalyzerDllPaths);
681+
rulesetPath = assembly.compilerOptions.RoslynAnalyzerRulesetPath;
682+
#endif
680683

681-
rulesetPath = assembly
682-
.compilerOptions
683-
.RoslynAnalyzerRulesetPath
684-
.MakeAbsolutePath()
685-
.NormalizePathSeparators();
684+
#if UNITY_2021_3_OR_NEWER && !UNITY_2022_1 // we have support in 2021.3, 2022.2 but without a backport in 2022.1
685+
additionalFilePaths.AddRange(assembly.compilerOptions.RoslynAdditionalFilePaths);
686+
analyzerConfigPath = assembly.compilerOptions.AnalyzerConfigPath;
686687
#endif
687688

688-
// Analyzers provided by csc.rsp
689+
// Analyzers and additional files provided by csc.rsp
689690
analyzers.AddRange(GetOtherArguments(responseFilesData, new HashSet<string>(new[] { "analyzer", "a" })));
691+
additionalFilePaths.AddRange(GetOtherArguments(responseFilesData, new HashSet<string>(new[] { "additionalfile" })));
690692

691-
return analyzers
693+
properties.RulesetPath = ToNormalizedPath(rulesetPath);
694+
properties.Analyzers = ToNormalizedPaths(analyzers);
695+
properties.AnalyzerConfigPath = ToNormalizedPath(analyzerConfigPath);
696+
properties.AdditionalFilePaths = ToNormalizedPaths(additionalFilePaths);
697+
}
698+
699+
private string ToNormalizedPath(string path)
700+
{
701+
return path
702+
.MakeAbsolutePath()
703+
.NormalizePathSeparators();
704+
}
705+
706+
private string[] ToNormalizedPaths(IEnumerable<string> values)
707+
{
708+
return values
692709
.Where(a => !string.IsNullOrEmpty(a))
693-
.Select(a => a.MakeAbsolutePath().NormalizePathSeparators())
710+
.Select(a => ToNormalizedPath(a))
694711
.Distinct()
695712
.ToArray();
696713
}
@@ -702,7 +719,6 @@ out StringBuilder headerBuilder
702719
)
703720
{
704721
var projectType = ProjectTypeOf(assembly.name);
705-
var analyzers = GetAnalyzers(assembly, responseFilesData, out var rulesetPath);
706722

707723
var projectProperties = new ProjectProperties
708724
{
@@ -711,10 +727,7 @@ out StringBuilder headerBuilder
711727
AssemblyName = assembly.name,
712728
RootNamespace = GetRootNamespace(assembly),
713729
OutputPath = assembly.outputPath,
714-
// Analyzers
715-
RulesetPath = rulesetPath,
716730
// RSP alterable
717-
Analyzers = analyzers,
718731
Defines = assembly.defines.Concat(responseFilesData.SelectMany(x => x.Defines)).Distinct().ToArray(),
719732
Unsafe = assembly.compilerOptions.AllowUnsafeCode | responseFilesData.Any(x => x.Unsafe),
720733
// VSTU Flavoring
@@ -724,6 +737,8 @@ out StringBuilder headerBuilder
724737
FlavoringPackageVersion = VisualStudioIntegration.PackageVersion(),
725738
};
726739

740+
SetAnalyzerAndSourceGeneratorProperties(assembly, responseFilesData, projectProperties);
741+
727742
GetProjectHeader(projectProperties, out headerBuilder);
728743
}
729744

@@ -830,6 +845,23 @@ private void GetProjectHeader(ProjectProperties properties, out StringBuilder he
830845
}
831846
headerBuilder.Append(@" </ItemGroup>").Append(k_WindowsNewline);
832847
}
848+
849+
if (!string.IsNullOrEmpty(properties.AnalyzerConfigPath))
850+
{
851+
headerBuilder.Append(@" <ItemGroup>").Append(k_WindowsNewline);
852+
headerBuilder.Append(@" <EditorConfigFiles Include=""").Append(properties.AnalyzerConfigPath).Append(@""" />").Append(k_WindowsNewline);
853+
headerBuilder.Append(@" </ItemGroup>").Append(k_WindowsNewline);
854+
}
855+
856+
if (properties.AdditionalFilePaths.Any())
857+
{
858+
headerBuilder.Append(@" <ItemGroup>").Append(k_WindowsNewline);
859+
foreach (var additionalFile in properties.AdditionalFilePaths)
860+
{
861+
headerBuilder.Append(@" <AdditionalFiles Include=""").Append(additionalFile).Append(@""" />").Append(k_WindowsNewline);
862+
}
863+
headerBuilder.Append(@" </ItemGroup>").Append(k_WindowsNewline);
864+
}
833865
}
834866

835867
private static string GetProjectFooter()

Packages/com.unity.ide.visualstudio/Editor/ProjectGeneration/ProjectProperties.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ internal class ProjectProperties
1313
// Analyzers
1414
public string[] Analyzers { get; set; } = Array.Empty<string>();
1515
public string RulesetPath { get; set; } = string.Empty;
16+
public string AnalyzerConfigPath { get; set; } = string.Empty;
17+
// Source generators
18+
public string[] AdditionalFilePaths { get; set; } = Array.Empty<string>();
1619

1720
// RSP alterable
1821
public string[] Defines { get; set; } = Array.Empty<string>();

Packages/com.unity.ide.visualstudio/Editor/VisualStudioIntegration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class VisualStudioIntegration
2222
class Client
2323
{
2424
public IPEndPoint EndPoint { get; set; }
25-
public DateTime LastMessage { get; set; }
25+
public double LastMessage { get; set; }
2626
}
2727

2828
private static Messager _messager;
@@ -141,7 +141,8 @@ private static void OnUpdate()
141141
{
142142
foreach (var client in _clients.Values.ToArray())
143143
{
144-
if (DateTime.Now.Subtract(client.LastMessage) > TimeSpan.FromMilliseconds(4000))
144+
// EditorApplication.timeSinceStartup: The time since the editor was started, in seconds, not reset when starting play mode.
145+
if (EditorApplication.timeSinceStartup - client.LastMessage > 4)
145146
_clients.Remove(client.EndPoint);
146147
}
147148
}
@@ -216,14 +217,14 @@ private static void CheckClient(Message message)
216217
client = new Client
217218
{
218219
EndPoint = endPoint,
219-
LastMessage = DateTime.Now
220+
LastMessage = EditorApplication.timeSinceStartup
220221
};
221222

222223
_clients.Add(endPoint, client);
223224
}
224225
else
225226
{
226-
client.LastMessage = DateTime.Now;
227+
client.LastMessage = EditorApplication.timeSinceStartup;
227228
}
228229
}
229230

Packages/com.unity.ide.visualstudio/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"name": "com.unity.ide.visualstudio",
33
"displayName": "Visual Studio Editor",
44
"description": "Code editor integration for supporting Visual Studio as code editor for unity. Adds support for generating csproj files for intellisense purposes, auto discovery of installations, etc.",
5-
"version": "2.0.16",
5+
"version": "2.0.18",
66
"unity": "2019.4",
77
"unityRelease": "25f1",
88
"dependencies": {
99
"com.unity.test-framework": "1.1.9"
1010
},
1111
"relatedPackages": {
12-
"com.unity.ide.visualstudio.tests": "2.0.16"
12+
"com.unity.ide.visualstudio.tests": "2.0.18"
1313
}
1414
}

0 commit comments

Comments
 (0)