Skip to content

Commit bdc2a8e

Browse files
sailroGitHub Enterprise
authored andcommitted
Merge pull request #164 from unity/sdk-style
LegacyStyle / SdkStyle generators
2 parents fb7dfbc + 69d04b2 commit bdc2a8e

20 files changed

+947
-621
lines changed

Assets/Editor/CSProjAssetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void Start()
4040
public virtual void SetUp()
4141
{
4242
var projectDirectory = Directory.GetParent(Application.dataPath).FullName;
43-
m_ProjectGeneration = new ProjectGeneration(projectDirectory);
43+
m_ProjectGeneration = new LegacyStyleProjectGeneration(projectDirectory);
4444
}
4545

4646
[UnityTearDown]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ public void WontSynchronize_WhenNoFilesChanged()
233233
var synchronizer = m_Builder.Build();
234234

235235
synchronizer.Sync();
236-
Assert.AreEqual(3, m_Builder.WriteTimes, "3 writes for solution + csproj + .vsconfig");
236+
Assert.AreEqual(2, m_Builder.WriteTimes, "2 writes for solution + csproj");
237237

238238
synchronizer.Sync();
239-
Assert.AreEqual(3, m_Builder.WriteTimes, "No more files should be written");
239+
Assert.AreEqual(2, m_Builder.WriteTimes, "No more files should be written");
240240
}
241241

242242
[Test]

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public void ContentWithoutChanges_WhenSynced_DoesNotReSync()
4646
var synchronizer = m_Builder.Build();
4747

4848
synchronizer.Sync();
49-
Assert.AreEqual(3, m_Builder.WriteTimes); // 3 writes for csproj / solution / .vsconfig
49+
Assert.AreEqual(2, m_Builder.WriteTimes); // 2 writes for csproj / solution
5050

5151
synchronizer.Sync();
52-
Assert.AreEqual(3, m_Builder.WriteTimes, "When content doesn't change we shouldn't re-sync");
52+
Assert.AreEqual(2, m_Builder.WriteTimes, "When content doesn't change we shouldn't re-sync");
5353
}
5454

5555
[Test]
@@ -58,12 +58,12 @@ public void AssemblyChanged_AfterSync_PerformsReSync()
5858
var synchronizer = m_Builder.Build();
5959

6060
synchronizer.Sync();
61-
Assert.AreEqual(3, m_Builder.WriteTimes); // 3 writes for csproj / solution / .vsconfig
61+
Assert.AreEqual(2, m_Builder.WriteTimes); // 2 writes for csproj / solution
6262

6363
m_Builder.WithAssemblies(new[] { new Assembly("Another", "path/to/Assembly.dll", new[] { "file.cs" }, new string[0], new Assembly[0], new string[0], AssemblyFlags.None) });
6464

6565
synchronizer.Sync();
66-
Assert.AreEqual(3 + 2, m_Builder.WriteTimes, "Should only re-sync the solution file and the 'Another' csproj");
66+
Assert.AreEqual(2 + 2, m_Builder.WriteTimes, "Should only re-sync the solution file and the 'Another' csproj");
6767
}
6868

6969
[Test]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public SynchronizerBuilder()
5252

5353
public ProjectGeneration Build()
5454
{
55-
return m_ProjectGeneration = new ProjectGeneration(projectDirectory, m_AssemblyProvider.Object, m_FileIoMock, m_GUIDGenerator.Object);
55+
return m_ProjectGeneration = new LegacyStyleProjectGeneration(projectDirectory, m_AssemblyProvider.Object, m_FileIoMock, m_GUIDGenerator.Object);
5656
}
5757

5858
public SynchronizerBuilder WithSolutionText(string solutionText)
@@ -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/Discovery.cs

Lines changed: 16 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -3,162 +3,44 @@
33
* Copyright (c) Microsoft Corporation. All rights reserved.
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
6-
using System;
7-
using System.IO;
86
using System.Collections.Generic;
9-
using System.Diagnostics;
10-
using System.Text.RegularExpressions;
11-
using System.Linq;
12-
using UnityEngine;
7+
using System.IO;
138

149
namespace Microsoft.Unity.VisualStudio.Editor
1510
{
1611
internal static class Discovery
1712
{
18-
internal const string ManagedWorkload = "Microsoft.VisualStudio.Workload.ManagedGame";
19-
20-
internal static string _vsWherePath;
21-
22-
public static void FindVSWhere()
23-
{
24-
_vsWherePath = FileUtility.GetPackageAssetFullPath("Editor", "VSWhere", "vswhere.exe");
25-
}
26-
2713
public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations()
2814
{
29-
if (VisualStudioEditor.IsWindows)
30-
{
31-
foreach (var installation in QueryVsWhere())
32-
yield return installation;
33-
}
34-
35-
if (VisualStudioEditor.IsOSX)
36-
{
37-
var candidates = Directory.EnumerateDirectories("/Applications", "*.app");
38-
foreach (var candidate in candidates)
39-
{
40-
if (TryDiscoverInstallation(candidate, out var installation))
41-
yield return installation;
42-
}
43-
}
44-
}
45-
46-
private static bool IsCandidateForDiscovery(string path)
47-
{
48-
if (File.Exists(path) && VisualStudioEditor.IsWindows && Regex.IsMatch(path, "devenv.exe$", RegexOptions.IgnoreCase))
49-
return true;
50-
51-
if (Directory.Exists(path) && VisualStudioEditor.IsOSX && Regex.IsMatch(path, "Visual\\s?Studio(?!.*Code.*).*.app$", RegexOptions.IgnoreCase))
52-
return true;
15+
foreach (var installation in VisualStudioForWindowsInstallation.GetVisualStudioInstallations())
16+
yield return installation;
5317

54-
return false;
18+
foreach (var installation in VisualStudioForMacInstallation.GetVisualStudioInstallations())
19+
yield return installation;
5520
}
5621

5722
public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioInstallation installation)
5823
{
59-
installation = null;
60-
61-
if (string.IsNullOrEmpty(editorPath))
62-
return false;
63-
64-
if (!IsCandidateForDiscovery(editorPath))
65-
return false;
66-
67-
// On windows we use the executable directly, so we can query extra information
68-
var fvi = editorPath;
69-
70-
// On Mac we use the .app folder, so we need to access to main assembly
71-
if (VisualStudioEditor.IsOSX)
24+
try
7225
{
73-
fvi = Path.Combine(editorPath, "Contents/Resources/lib/monodevelop/bin/VisualStudio.exe");
74-
75-
if (!File.Exists(fvi))
76-
fvi = Path.Combine(editorPath, "Contents/MonoBundle/VisualStudio.exe");
26+
if (VisualStudioForWindowsInstallation.TryDiscoverInstallation(editorPath, out installation))
27+
return true;
7728

78-
if (!File.Exists(fvi))
79-
fvi = Path.Combine(editorPath, "Contents/MonoBundle/VisualStudio.dll");
29+
if (VisualStudioForMacInstallation.TryDiscoverInstallation(editorPath, out installation))
30+
return true;
8031
}
81-
82-
if (!File.Exists(fvi))
83-
return false;
84-
85-
// VS preview are not using the isPrerelease flag so far
86-
// On Windows FileDescription contains "Preview", but not on Mac
87-
var vi = FileVersionInfo.GetVersionInfo(fvi);
88-
var version = new Version(vi.ProductVersion);
89-
var isPrerelease = vi.IsPreRelease || string.Concat(editorPath, "/" + vi.FileDescription).ToLower().Contains("preview");
90-
91-
installation = new VisualStudioInstallation()
32+
catch (IOException)
9233
{
93-
IsPrerelease = isPrerelease,
94-
Name = $"{vi.FileDescription}{(isPrerelease && VisualStudioEditor.IsOSX ? " Preview" : string.Empty)} [{version.ToString(3)}]",
95-
Path = editorPath,
96-
Version = version
97-
};
98-
return true;
99-
}
100-
101-
#region VsWhere Json Schema
102-
#pragma warning disable CS0649
103-
[Serializable]
104-
internal class VsWhereResult
105-
{
106-
public VsWhereEntry[] entries;
107-
108-
public static VsWhereResult FromJson(string json)
109-
{
110-
return JsonUtility.FromJson<VsWhereResult>("{ \"" + nameof(VsWhereResult.entries) + "\": " + json + " }");
34+
installation = null;
11135
}
11236

113-
public IEnumerable<VisualStudioInstallation> ToVisualStudioInstallations()
114-
{
115-
foreach (var entry in entries)
116-
{
117-
yield return new VisualStudioInstallation()
118-
{
119-
Name = $"{entry.displayName} [{entry.catalog.productDisplayVersion}]",
120-
Path = entry.productPath,
121-
IsPrerelease = entry.isPrerelease,
122-
Version = Version.Parse(entry.catalog.buildVersion)
123-
};
124-
}
125-
}
126-
}
127-
128-
[Serializable]
129-
internal class VsWhereEntry
130-
{
131-
public string displayName;
132-
public bool isPrerelease;
133-
public string productPath;
134-
public VsWhereCatalog catalog;
135-
}
136-
137-
[Serializable]
138-
internal class VsWhereCatalog
139-
{
140-
public string productDisplayVersion; // non parseable like "16.3.0 Preview 3.0"
141-
public string buildVersion;
37+
return false;
14238
}
143-
#pragma warning restore CS3021
144-
#endregion
14539

146-
private static IEnumerable<VisualStudioInstallation> QueryVsWhere()
40+
public static void Initialize()
14741
{
148-
var progpath = _vsWherePath;
149-
150-
if (string.IsNullOrWhiteSpace(progpath))
151-
return Enumerable.Empty<VisualStudioInstallation>();
152-
153-
var result = ProcessRunner.StartAndWaitForExit(progpath, "-prerelease -format json -utf8");
154-
155-
if (!result.Success)
156-
throw new Exception($"Failure while running vswhere: {result.Error}");
157-
158-
// Do not catch any JsonException here, this will be handled by the caller
159-
return VsWhereResult
160-
.FromJson(result.Output)
161-
.ToVisualStudioInstallations();
42+
VisualStudioForWindowsInstallation.Initialize();
43+
VisualStudioForMacInstallation.Initialize();
16244
}
16345
}
16446
}

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Diagnostics;
88
using System.Text;
9+
using System.Threading;
910
using System.Threading.Tasks;
1011

1112
namespace Microsoft.Unity.VisualStudio.Editor
@@ -21,19 +22,34 @@ internal static class ProcessRunner
2122
{
2223
public const int DefaultTimeoutInMilliseconds = 300000;
2324

24-
public static ProcessStartInfo ProcessStartInfoFor(string filename, string arguments)
25+
public static ProcessStartInfo ProcessStartInfoFor(string filename, string arguments, bool redirect = true, bool shell = false)
2526
{
2627
return new ProcessStartInfo
2728
{
28-
UseShellExecute = false,
29+
UseShellExecute = shell,
2930
CreateNoWindow = true,
30-
RedirectStandardOutput = true,
31-
RedirectStandardError = true,
31+
RedirectStandardOutput = redirect,
32+
RedirectStandardError = redirect,
3233
FileName = filename,
3334
Arguments = arguments
3435
};
3536
}
3637

38+
public static void Start(string filename, string arguments)
39+
{
40+
Start(ProcessStartInfoFor(filename, arguments, false));
41+
}
42+
43+
public static void Start(ProcessStartInfo processStartInfo)
44+
{
45+
var process = new Process { StartInfo = processStartInfo };
46+
47+
using (process)
48+
{
49+
process.Start();
50+
}
51+
}
52+
3753
public static ProcessRunnerResult StartAndWaitForExit(string filename, string arguments, int timeoutms = DefaultTimeoutInMilliseconds, Action<string> onOutputReceived = null)
3854
{
3955
return StartAndWaitForExit(ProcessStartInfoFor(filename, arguments), timeoutms, onOutputReceived);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class AssemblyNameProvider : IAssemblyNameProvider
4040

4141
public string ProjectGenerationRootNamespace => EditorSettings.projectGenerationRootNamespace;
4242

43-
public ProjectGenerationFlag ProjectGenerationFlag
43+
public virtual ProjectGenerationFlag ProjectGenerationFlag
4444
{
4545
get => m_ProjectGenerationFlag;
4646
private set

0 commit comments

Comments
 (0)