Skip to content

Commit cb2baf7

Browse files
committed
use installation generator
1 parent 5121db3 commit cb2baf7

File tree

3 files changed

+50
-36
lines changed

3 files changed

+50
-36
lines changed

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

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
using System;
77
using System.IO;
88
using System.Linq;
9-
using System.Runtime.InteropServices;
109
using System.Runtime.CompilerServices;
1110
using UnityEditor;
1211
using UnityEngine;
1312
using Unity.CodeEditor;
14-
using System.Collections.Concurrent;
1513

1614
[assembly: InternalsVisibleTo("Unity.VisualStudio.EditorTests")]
1715
[assembly: InternalsVisibleTo("Unity.VisualStudio.Standalone.EditorTests")]
@@ -31,8 +29,6 @@ public class VisualStudioEditor : IExternalCodeEditor
3129

3230
private static readonly AsyncOperation<IVisualStudioInstallation[]> _discoverInstallations;
3331

34-
private readonly IGenerator _generator = new LegacyStyleProjectGeneration();
35-
3632
static VisualStudioEditor()
3733
{
3834
if (!UnityInstallation.IsMainUnityEditorProcess)
@@ -54,7 +50,7 @@ private static IVisualStudioInstallation[] DiscoverInstallations()
5450
}
5551
catch (Exception ex)
5652
{
57-
UnityEngine.Debug.LogError($"Error detecting Visual Studio installations: {ex}");
53+
Debug.LogError($"Error detecting Visual Studio installations: {ex}");
5854
return Array.Empty<IVisualStudioInstallation>();
5955
}
6056
}
@@ -65,8 +61,12 @@ private static IVisualStudioInstallation[] DiscoverInstallations()
6561
// keeping it for now given it is public, so we need a major bump to remove it
6662
public void CreateIfDoesntExist()
6763
{
68-
if (!_generator.HasSolutionBeenGenerated())
69-
_generator.Sync();
64+
if (!TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, true, out var installation))
65+
return;
66+
67+
var generator = installation.ProjectGenerator;
68+
if (!generator.HasSolutionBeenGenerated())
69+
generator.Sync();
7070
}
7171

7272
public void Initialize(string editorInstallationPath)
@@ -94,7 +94,7 @@ internal virtual bool TryGetVisualStudioInstallationForPath(string editorPath, b
9494
public virtual bool TryGetInstallationForPath(string editorPath, out CodeEditor.Installation installation)
9595
{
9696
var result = TryGetVisualStudioInstallationForPath(editorPath, searchInstallations: false, out var vsi);
97-
installation = vsi == null ? default : vsi.ToCodeEditorInstallation();
97+
installation = vsi?.ToCodeEditorInstallation() ?? default;
9898
return result;
9999
}
100100

@@ -128,29 +128,39 @@ public void OnGUI()
128128
EditorGUI.indentLevel--;
129129
}
130130

131-
void RegenerateProjectFiles()
131+
private void RegenerateProjectFiles()
132132
{
133-
var rect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(new GUILayoutOption[] { }));
133+
var rect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect());
134134
rect.width = 252;
135135
if (GUI.Button(rect, "Regenerate project files"))
136136
{
137-
_generator.Sync();
137+
if (TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, true, out var installation))
138+
{
139+
installation.ProjectGenerator.Sync();
140+
}
138141
}
139142
}
140143

141-
void SettingsButton(ProjectGenerationFlag preference, string guiMessage, string toolTip)
144+
private void SettingsButton(ProjectGenerationFlag preference, string guiMessage, string toolTip)
142145
{
143-
var prevValue = _generator.AssemblyNameProvider.ProjectGenerationFlag.HasFlag(preference);
144-
var newValue = EditorGUILayout.Toggle(new GUIContent(guiMessage, toolTip), prevValue);
145-
if (newValue != prevValue)
146+
if (TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, true, out var installation))
146147
{
147-
_generator.AssemblyNameProvider.ToggleProjectGeneration(preference);
148+
var generator = installation.ProjectGenerator;
149+
var prevValue = generator.AssemblyNameProvider.ProjectGenerationFlag.HasFlag(preference);
150+
var newValue = EditorGUILayout.Toggle(new GUIContent(guiMessage, toolTip), prevValue);
151+
if (newValue != prevValue)
152+
{
153+
generator.AssemblyNameProvider.ToggleProjectGeneration(preference);
154+
}
148155
}
149156
}
150157

151158
public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] movedFiles, string[] movedFromFiles, string[] importedFiles)
152159
{
153-
_generator.SyncIfNeeded(addedFiles.Union(deletedFiles).Union(movedFiles).Union(movedFromFiles), importedFiles);
160+
if (TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, true, out var installation))
161+
{
162+
installation.ProjectGenerator.SyncIfNeeded(addedFiles.Union(deletedFiles).Union(movedFiles).Union(movedFromFiles), importedFiles);
163+
}
154164

155165
foreach (var file in importedFiles.Where(a => Path.GetExtension(a) == ".pdb"))
156166
{
@@ -173,10 +183,13 @@ public void SyncIfNeeded(string[] addedFiles, string[] deletedFiles, string[] mo
173183

174184
public void SyncAll()
175185
{
176-
_generator.Sync();
186+
if (TryGetVisualStudioInstallationForPath(CodeEditor.CurrentEditorInstallation, true, out var installation))
187+
{
188+
installation.ProjectGenerator.Sync();
189+
}
177190
}
178191

179-
bool IsSupportedPath(string path)
192+
private static bool IsSupportedPath(string path, IGenerator generator)
180193
{
181194
// Path is empty with "Open C# Project", as we only want to open the solution without specific files
182195
if (string.IsNullOrEmpty(path))
@@ -185,10 +198,7 @@ bool IsSupportedPath(string path)
185198
// cs, uxml, uss, shader, compute, cginc, hlsl, glslinc, template are part of Unity builtin extensions
186199
// txt, xml, fnt, cd are -often- par of Unity user extensions
187200
// asdmdef is mandatory included
188-
if (_generator.IsSupportedFile(path))
189-
return true;
190-
191-
return false;
201+
return generator.IsSupportedFile(path);
192202
}
193203

194204
public bool OpenProject(string path, int line, int column)
@@ -200,13 +210,14 @@ public bool OpenProject(string path, int line, int column)
200210
return false;
201211
}
202212

203-
if (!IsSupportedPath(path))
213+
var generator = installation.ProjectGenerator;
214+
if (!IsSupportedPath(path, generator))
204215
return false;
205216

206-
if (!IsProjectGeneratedFor(path, out var missingFlag))
217+
if (!IsProjectGeneratedFor(path, generator, out var missingFlag))
207218
Debug.LogWarning($"You are trying to open {path} outside a generated project. This might cause problems with IntelliSense and debugging. To avoid this, you can change your .csproj preferences in Edit > Preferences > External Tools and enable {GetProjectGenerationFlagDescription(missingFlag)} generation.");
208219

209-
var solution = GetOrGenerateSolutionFile(path);
220+
var solution = GetOrGenerateSolutionFile(generator);
210221
return installation.Open(path, line, column, solution);
211222
}
212223

@@ -235,7 +246,7 @@ private static string GetProjectGenerationFlagDescription(ProjectGenerationFlag
235246
}
236247
}
237248

238-
private bool IsProjectGeneratedFor(string path, out ProjectGenerationFlag missingFlag)
249+
private static bool IsProjectGeneratedFor(string path, IGenerator generator, out ProjectGenerationFlag missingFlag)
239250
{
240251
missingFlag = ProjectGenerationFlag.None;
241252

@@ -248,9 +259,9 @@ private bool IsProjectGeneratedFor(string path, out ProjectGenerationFlag missin
248259
return true;
249260

250261
// Even on windows, the package manager requires relative path + unix style separators for queries
251-
var basePath = _generator.ProjectDirectory;
252-
var relativePath = FileUtility
253-
.NormalizeWindowsToUnix(path)
262+
var basePath = generator.ProjectDirectory;
263+
var relativePath = path
264+
.NormalizeWindowsToUnix()
254265
.Replace(basePath, string.Empty)
255266
.Trim(FileUtility.UnixSeparator);
256267

@@ -262,18 +273,18 @@ private bool IsProjectGeneratedFor(string path, out ProjectGenerationFlag missin
262273
if (!Enum.TryParse<ProjectGenerationFlag>(source.ToString(), out var flag))
263274
return true;
264275

265-
if (_generator.AssemblyNameProvider.ProjectGenerationFlag.HasFlag(flag))
276+
if (generator.AssemblyNameProvider.ProjectGenerationFlag.HasFlag(flag))
266277
return true;
267278

268279
// Return false if we found a source not flagged for generation
269280
missingFlag = flag;
270281
return false;
271282
}
272283

273-
private string GetOrGenerateSolutionFile(string path)
284+
private static string GetOrGenerateSolutionFile(IGenerator generator)
274285
{
275-
_generator.Sync();
276-
return _generator.SolutionFile();
286+
generator.Sync();
287+
return generator.SolutionFile();
277288
}
278289
}
279290
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal class VisualStudioForMacInstallation : VisualStudioInstallation
2929
new VersionPair(8,0, /* => */ 7,3),
3030
};
3131

32+
private static readonly IGenerator _generator = new LegacyStyleProjectGeneration();
33+
3234
public override bool SupportsAnalyzers
3335
{
3436
get
@@ -93,7 +95,7 @@ public override IGenerator ProjectGenerator
9395
{
9496
get
9597
{
96-
return new LegacyStyleProjectGeneration();
98+
return _generator;
9799
}
98100
}
99101

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ internal class VisualStudioForWindowsInstallation : VisualStudioInstallation
4040
};
4141

4242
private static string _vsWherePath = null;
43+
private static readonly IGenerator _generator = new LegacyStyleProjectGeneration();
4344

4445
public override bool SupportsAnalyzers
4546
{
@@ -119,7 +120,7 @@ public override IGenerator ProjectGenerator
119120
{
120121
get
121122
{
122-
return new LegacyStyleProjectGeneration();
123+
return _generator;
123124
}
124125
}
125126

0 commit comments

Comments
 (0)