Skip to content

Commit 2e8db69

Browse files
committed
Use Platform defines instead of run-time conditions
1 parent 4819ba3 commit 2e8db69

File tree

8 files changed

+70
-63
lines changed

8 files changed

+70
-63
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ internal static class Discovery
1313
{
1414
public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations()
1515
{
16+
#if UNITY_EDITOR_WIN
1617
foreach (var installation in VisualStudioForWindowsInstallation.GetVisualStudioInstallations())
1718
yield return installation;
18-
19+
#elif UNITY_EDITOR_OSX
1920
foreach (var installation in VisualStudioForMacInstallation.GetVisualStudioInstallations())
2021
yield return installation;
22+
#endif
2123

2224
foreach (var installation in VisualStudioCodeInstallation.GetVisualStudioInstallations())
2325
yield return installation;
@@ -27,12 +29,13 @@ public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioI
2729
{
2830
try
2931
{
32+
#if UNITY_EDITOR_WIN
3033
if (VisualStudioForWindowsInstallation.TryDiscoverInstallation(editorPath, out installation))
3134
return true;
32-
35+
#elif UNITY_EDITOR_OSX
3336
if (VisualStudioForMacInstallation.TryDiscoverInstallation(editorPath, out installation))
3437
return true;
35-
38+
#endif
3639
if (VisualStudioCodeInstallation.TryDiscoverInstallation(editorPath, out installation))
3740
return true;
3841
}
@@ -46,8 +49,11 @@ public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioI
4649

4750
public static void Initialize()
4851
{
52+
#if UNITY_EDITOR_WIN
4953
VisualStudioForWindowsInstallation.Initialize();
54+
#elif UNITY_EDITOR_OSX
5055
VisualStudioForMacInstallation.Initialize();
56+
#endif
5157
VisualStudioCodeInstallation.Initialize();
5258
}
5359
}

Packages/com.unity.ide.visualstudio/Editor/Messaging/UdpSocket.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ public void Bind(IPAddress address, int port = 0)
2727

2828
private void SetIOControl()
2929
{
30-
if (!VisualStudioEditor.IsWindows)
31-
return;
32-
30+
#if UNITY_EDITOR_WIN
3331
try
3432
{
3533
const int SIO_UDP_CONNRESET = -1744830452;
@@ -40,6 +38,7 @@ private void SetIOControl()
4038
{
4139
// fallback
4240
}
41+
#endif
4342
}
4443

4544
public static byte[] BufferFor(IAsyncResult result)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,11 @@ public string ProjectFile(Assembly assembly)
597597
return Path.Combine(ProjectDirectory, $"{m_AssemblyNameProvider.GetAssemblyName(assembly.outputPath, assembly.name)}.csproj");
598598
}
599599

600-
private static readonly Regex InvalidCharactersRegexPattern = new Regex(@"\?|&|\*|""|<|>|\||#|%|\^|;" + (VisualStudioEditor.IsWindows ? "" : "|:"));
600+
#if UNITY_EDITOR_WIN
601+
private static readonly Regex InvalidCharactersRegexPattern = new Regex(@"\?|&|\*|""|<|>|\||#|%|\^|;", RegexOptions.Compiled);
602+
#else
603+
private static readonly Regex InvalidCharactersRegexPattern = new Regex(@"\?|&|\*|""|<|>|\||#|%|\^|;|:"), RegexOptions.Compiled);
604+
#endif
601605

602606
public string SolutionFile()
603607
{

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

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public override IGenerator ProjectGenerator
6666

6767
private static bool IsCandidateForDiscovery(string path)
6868
{
69-
if (VisualStudioEditor.IsOSX)
70-
return Directory.Exists(path) && Regex.IsMatch(path, ".*Code.*.app$", RegexOptions.IgnoreCase);
71-
72-
if (VisualStudioEditor.IsWindows)
73-
return File.Exists(path) && Regex.IsMatch(path, ".*Code.*.exe$", RegexOptions.IgnoreCase);
74-
69+
#if UNITY_EDITOR_OSX
70+
return Directory.Exists(path) && Regex.IsMatch(path, ".*Code.*.app$", RegexOptions.IgnoreCase);
71+
#elif UNITY_EDITOR_WIN
72+
return File.Exists(path) && Regex.IsMatch(path, ".*Code.*.exe$", RegexOptions.IgnoreCase);
73+
#else
7574
return File.Exists(path) && path.EndsWith("code", StringComparison.OrdinalIgnoreCase);
75+
#endif
7676
}
7777

7878
[Serializable]
@@ -99,21 +99,23 @@ public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioI
9999
{
100100
var manifestBase = GetRealPath(editorPath);
101101

102-
if (VisualStudioEditor.IsWindows) // on Windows, editorPath is a file, resources as subdirectory
103-
manifestBase = IOPath.GetDirectoryName(manifestBase);
104-
else if (VisualStudioEditor.IsOSX) // on Mac, editorPath is a directory
105-
manifestBase = IOPath.Combine(manifestBase, "Contents");
106-
else {
107-
// on Linux, editorPath is a file, in a bin sub-directory
108-
var parent = Directory.GetParent(manifestBase);
109-
// but we can link to [vscode]/code or [vscode]/bin/code
110-
manifestBase = parent?.Name == "bin" ? parent.Parent?.FullName : parent?.FullName;
111-
}
102+
#if UNITY_EDITOR_WIN
103+
// on Windows, editorPath is a file, resources as subdirectory
104+
manifestBase = IOPath.GetDirectoryName(manifestBase);
105+
#elif UNITY_EDITOR_OSX
106+
// on Mac, editorPath is a directory
107+
manifestBase = IOPath.Combine(manifestBase, "Contents");
108+
#else
109+
// on Linux, editorPath is a file, in a bin sub-directory
110+
var parent = Directory.GetParent(manifestBase);
111+
// but we can link to [vscode]/code or [vscode]/bin/code
112+
manifestBase = parent?.Name == "bin" ? parent.Parent?.FullName : parent?.FullName;
113+
#endif
112114

113115
if (manifestBase == null)
114116
return false;
115117

116-
var manifestFullPath = IOPath.Combine(manifestBase, @"resources", "app", "package.json");
118+
var manifestFullPath = IOPath.Combine(manifestBase, "resources", "app", "package.json");
117119
if (File.Exists(manifestFullPath))
118120
{
119121
var manifest = JsonUtility.FromJson<VisualStudioCodeManifest>(File.ReadAllText(manifestFullPath));
@@ -142,32 +144,27 @@ public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallation
142144
{
143145
var candidates = new List<string>();
144146

145-
if (VisualStudioEditor.IsWindows)
146-
{
147-
var localAppPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs");
148-
var programFiles = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
147+
#if UNITY_EDITOR_WIN
148+
var localAppPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs");
149+
var programFiles = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
149150

150-
foreach (var basePath in new[] {localAppPath, programFiles})
151-
{
152-
candidates.Add(IOPath.Combine(basePath, "Microsoft VS Code", "Code.exe"));
153-
candidates.Add(IOPath.Combine(basePath, "Microsoft VS Code Insiders", "Code - Insiders.exe"));
154-
}
155-
}
156-
else if (VisualStudioEditor.IsOSX)
151+
foreach (var basePath in new[] {localAppPath, programFiles})
157152
{
158-
var appPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
159-
candidates.AddRange(Directory.EnumerateDirectories(appPath, "Visual Studio Code*.app"));
160-
}
161-
else
162-
{
163-
// Well known locations
164-
candidates.Add("/usr/bin/code");
165-
candidates.Add("/bin/code");
166-
candidates.Add("/usr/local/bin/code");
167-
168-
// Preference ordered base directories relative to which desktop files should be searched
169-
candidates.AddRange(GetXdgCandidates());
153+
candidates.Add(IOPath.Combine(basePath, "Microsoft VS Code", "Code.exe"));
154+
candidates.Add(IOPath.Combine(basePath, "Microsoft VS Code Insiders", "Code - Insiders.exe"));
170155
}
156+
#elif UNITY_EDITOR_OSX
157+
var appPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
158+
candidates.AddRange(Directory.EnumerateDirectories(appPath, "Visual Studio Code*.app"));
159+
#elif UNITY_EDITOR_LINUX
160+
// Well known locations
161+
candidates.Add("/usr/bin/code");
162+
candidates.Add("/bin/code");
163+
candidates.Add("/usr/local/bin/code");
164+
165+
// Preference ordered base directories relative to which desktop files should be searched
166+
candidates.AddRange(GetXdgCandidates());
167+
#endif
171168

172169
foreach (var candidate in candidates.Distinct())
173170
{
@@ -176,6 +173,7 @@ public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallation
176173
}
177174
}
178175

176+
#if UNITY_EDITOR_LINUX
179177
private static readonly Regex DesktopFileExecEntry = new Regex(@"Exec=(\S+)", RegexOptions.Singleline | RegexOptions.Compiled);
180178

181179
private static IEnumerable<string> GetXdgCandidates() {
@@ -207,7 +205,6 @@ private static IEnumerable<string> GetXdgCandidates() {
207205
}
208206
}
209207

210-
#if UNITY_EDITOR_LINUX
211208
[System.Runtime.InteropServices.DllImport ("libc")]
212209
private static extern int readlink(string path, byte[] buffer, int buflen);
213210

@@ -514,13 +511,14 @@ public override bool Open(string path, int line, int column, string solution)
514511

515512
private static ProcessStartInfo ProcessStartInfoFor(string application, string arguments)
516513
{
517-
if (!VisualStudioEditor.IsOSX)
518-
return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect: false);
519-
514+
#if UNITY_EDITOR_OSX
520515
// wrap with built-in OSX open feature
521516
arguments = $"-n \"{application}\" --args {arguments}";
522517
application = "open";
523518
return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect:false, shell: true);
519+
#else
520+
return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect: false);
521+
#endif
524522
}
525523

526524
public static void Initialize()

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ namespace Microsoft.Unity.VisualStudio.Editor
2121
[InitializeOnLoad]
2222
public class VisualStudioEditor : IExternalCodeEditor
2323
{
24-
internal static bool IsOSX => Application.platform == RuntimePlatform.OSXEditor;
25-
internal static bool IsWindows => !IsOSX && Path.DirectorySeparatorChar == FileUtility.WinSeparator && Environment.NewLine == "\r\n";
26-
2724
CodeEditor.Installation[] IExternalCodeEditor.Installations => _discoverInstallations
2825
.Result
2926
.Values

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
#if UNITY_EDITOR_OSX
7+
68
using System;
79
using System.Collections.Generic;
810
using System.Diagnostics;
@@ -178,3 +180,5 @@ public static void Initialize()
178180
}
179181
}
180182
}
183+
184+
#endif

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
#if UNITY_EDITOR_WIN
7+
68
using System;
79
using System.Collections.Concurrent;
810
using System.Collections.Generic;
@@ -126,7 +128,7 @@ public override IGenerator ProjectGenerator
126128

127129
private static bool IsCandidateForDiscovery(string path)
128130
{
129-
return File.Exists(path) && VisualStudioEditor.IsWindows && Regex.IsMatch(path, "devenv.exe$", RegexOptions.IgnoreCase);
131+
return File.Exists(path) && Regex.IsMatch(path, "devenv.exe$", RegexOptions.IgnoreCase);
130132
}
131133

132134
public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioInstallation installation)
@@ -169,9 +171,6 @@ public static string FormatProductName(string productName)
169171

170172
public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations()
171173
{
172-
if (!VisualStudioEditor.IsWindows)
173-
yield break;
174-
175174
foreach (var installation in QueryVsWhere())
176175
yield return installation;
177176
}
@@ -373,8 +372,9 @@ private static void OnOutputReceived(string data, BlockingCollection<COMIntegrat
373372

374373
public static void Initialize()
375374
{
376-
if (VisualStudioEditor.IsWindows)
377-
_vsWherePath = FileUtility.GetPackageAssetFullPath("Editor", "VSWhere", "vswhere.exe");
375+
_vsWherePath = FileUtility.GetPackageAssetFullPath("Editor", "VSWhere", "vswhere.exe");
378376
}
379377
}
380378
}
379+
380+
#endif

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ private static void RunOnceOnUpdate(Action action)
106106
private static void RunOnShutdown(Action action)
107107
{
108108
// Mono on OSX has all kinds of quirks on AppDomain shutdown
109-
if (!VisualStudioEditor.IsWindows)
110-
return;
111-
109+
#if !UNITY_EDITOR_WIN
112110
AppDomain.CurrentDomain.DomainUnload += (_, __) => action();
111+
#endif
113112
}
114113

115114
private static int DebuggingPort()

0 commit comments

Comments
 (0)