Skip to content

Commit 43fcd51

Browse files
committed
Refactor
1 parent 1317e37 commit 43fcd51

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public override bool TryGetInstallationForPath(string editorPath, out CodeEditor
248248
return true;
249249
}
250250

251-
internal override bool TryGetVisualStudioInstallationForPath(string editorPath, bool searchInstallations, out IVisualStudioInstallation installation)
251+
internal override bool TryGetVisualStudioInstallationForPath(string editorPath, bool lookupDiscoveredInstallations, out IVisualStudioInstallation installation)
252252
{
253253
var mock = new Mock<IVisualStudioInstallation>();
254254
mock.Setup(x => x.SupportsAnalyzers).Returns(true);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal static string GetInstallationDetails(IVisualStudioInstallation installa
2323

2424
internal static void GenerateSolutionWith(VisualStudioEditor vse, string installationPath)
2525
{
26-
if (vse != null && vse.TryGetVisualStudioInstallationForPath(installationPath, searchInstallations: true, out var vsi))
26+
if (vse != null && vse.TryGetVisualStudioInstallationForPath(installationPath, lookupDiscoveredInstallations: true, out var vsi))
2727
{
2828
Log($"Using {GetInstallationDetails(vsi)}");
2929
vse.SyncAll();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private static bool ShouldSyncOnReimportedAsset(string asset)
185185
private void RefreshCurrentInstallation()
186186
{
187187
var editor = CodeEditor.CurrentEditor as VisualStudioEditor;
188-
editor?.TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, searchInstallations: true, out m_CurrentInstallation);
188+
editor?.TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, lookupDiscoveredInstallations: true, out m_CurrentInstallation);
189189
}
190190

191191
static ProfilerMarker solutionSyncMarker = new ProfilerMarker("SolutionSynchronizerSync");

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66
using System;
7+
using System.Collections.Generic;
78
using System.IO;
89
using System.Linq;
910
using System.Runtime.CompilerServices;
@@ -23,11 +24,13 @@ public class VisualStudioEditor : IExternalCodeEditor
2324
internal static bool IsOSX => Application.platform == RuntimePlatform.OSXEditor;
2425
internal static bool IsWindows => !IsOSX && Path.DirectorySeparatorChar == FileUtility.WinSeparator && Environment.NewLine == "\r\n";
2526

26-
CodeEditor.Installation[] IExternalCodeEditor.Installations => _discoverInstallations.Result
27-
.Select(i => i.ToCodeEditorInstallation())
27+
CodeEditor.Installation[] IExternalCodeEditor.Installations => _discoverInstallations
28+
.Result
29+
.Values
30+
.Select(v => v.ToCodeEditorInstallation())
2831
.ToArray();
2932

30-
private static readonly AsyncOperation<IVisualStudioInstallation[]> _discoverInstallations;
33+
private static readonly AsyncOperation<Dictionary<string, IVisualStudioInstallation>> _discoverInstallations;
3134

3235
static VisualStudioEditor()
3336
{
@@ -37,21 +40,21 @@ static VisualStudioEditor()
3740
Discovery.Initialize();
3841
CodeEditor.Register(new VisualStudioEditor());
3942

40-
_discoverInstallations = AsyncOperation<IVisualStudioInstallation[]>.Run(DiscoverInstallations);
43+
_discoverInstallations = AsyncOperation<Dictionary<string, IVisualStudioInstallation>>.Run(DiscoverInstallations);
4144
}
4245

43-
private static IVisualStudioInstallation[] DiscoverInstallations()
46+
private static Dictionary<string, IVisualStudioInstallation> DiscoverInstallations()
4447
{
4548
try
4649
{
4750
return Discovery
4851
.GetVisualStudioInstallations()
49-
.ToArray();
52+
.ToDictionary(i => Path.GetFullPath(i.Path), i => i);
5053
}
5154
catch (Exception ex)
5255
{
5356
Debug.LogError($"Error detecting Visual Studio installations: {ex}");
54-
return Array.Empty<IVisualStudioInstallation>();
57+
return new Dictionary<string, IVisualStudioInstallation>();
5558
}
5659
}
5760

@@ -73,27 +76,20 @@ public void Initialize(string editorInstallationPath)
7376
{
7477
}
7578

76-
internal virtual bool TryGetVisualStudioInstallationForPath(string editorPath, bool searchInstallations, out IVisualStudioInstallation installation)
79+
internal virtual bool TryGetVisualStudioInstallationForPath(string editorPath, bool lookupDiscoveredInstallations, out IVisualStudioInstallation installation)
7780
{
78-
if (searchInstallations)
79-
{
80-
// lookup for well known installations
81-
foreach (var candidate in _discoverInstallations.Result)
82-
{
83-
if (!string.Equals(Path.GetFullPath(editorPath), Path.GetFullPath(candidate.Path), StringComparison.OrdinalIgnoreCase))
84-
continue;
81+
editorPath = Path.GetFullPath(editorPath);
8582

86-
installation = candidate;
87-
return true;
88-
}
89-
}
83+
// lookup for well known installations
84+
if (lookupDiscoveredInstallations && _discoverInstallations.Result.TryGetValue(editorPath, out installation))
85+
return true;
9086

9187
return Discovery.TryDiscoverInstallation(editorPath, out installation);
9288
}
9389

9490
public virtual bool TryGetInstallationForPath(string editorPath, out CodeEditor.Installation installation)
9591
{
96-
var result = TryGetVisualStudioInstallationForPath(editorPath, searchInstallations: false, out var vsi);
92+
var result = TryGetVisualStudioInstallationForPath(editorPath, lookupDiscoveredInstallations: false, out var vsi);
9793
installation = vsi?.ToCodeEditorInstallation() ?? default;
9894
return result;
9995
}

0 commit comments

Comments
 (0)