Skip to content

Commit 23a073d

Browse files
sailroGitHub Enterprise
authored andcommitted
Merge pull request #186 from unity/platform-defines
Use Platform defines instead of run-time conditions
2 parents d687665 + 1edfe70 commit 23a073d

File tree

8 files changed

+71
-68
lines changed

8 files changed

+71
-68
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 & 45 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,22 +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-
{
108-
// on Linux, editorPath is a file, in a bin sub-directory
109-
var parent = Directory.GetParent(manifestBase);
110-
// but we can link to [vscode]/code or [vscode]/bin/code
111-
manifestBase = parent?.Name == "bin" ? parent.Parent?.FullName : parent?.FullName;
112-
}
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
113114

114115
if (manifestBase == null)
115116
return false;
116117

117-
var manifestFullPath = IOPath.Combine(manifestBase, @"resources", "app", "package.json");
118+
var manifestFullPath = IOPath.Combine(manifestBase, "resources", "app", "package.json");
118119
if (File.Exists(manifestFullPath))
119120
{
120121
var manifest = JsonUtility.FromJson<VisualStudioCodeManifest>(File.ReadAllText(manifestFullPath));
@@ -143,32 +144,27 @@ public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallation
143144
{
144145
var candidates = new List<string>();
145146

146-
if (VisualStudioEditor.IsWindows)
147-
{
148-
var localAppPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs");
149-
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));
150150

151-
foreach (var basePath in new[] {localAppPath, programFiles})
152-
{
153-
candidates.Add(IOPath.Combine(basePath, "Microsoft VS Code", "Code.exe"));
154-
candidates.Add(IOPath.Combine(basePath, "Microsoft VS Code Insiders", "Code - Insiders.exe"));
155-
}
156-
}
157-
else if (VisualStudioEditor.IsOSX)
151+
foreach (var basePath in new[] {localAppPath, programFiles})
158152
{
159-
var appPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
160-
candidates.AddRange(Directory.EnumerateDirectories(appPath, "Visual Studio Code*.app"));
161-
}
162-
else
163-
{
164-
// Well known locations
165-
candidates.Add("/usr/bin/code");
166-
candidates.Add("/bin/code");
167-
candidates.Add("/usr/local/bin/code");
168-
169-
// Preference ordered base directories relative to which desktop files should be searched
170-
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"));
171155
}
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
172168

173169
foreach (var candidate in candidates.Distinct())
174170
{
@@ -177,6 +173,7 @@ public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallation
177173
}
178174
}
179175

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

182179
private static IEnumerable<string> GetXdgCandidates()
@@ -212,7 +209,6 @@ private static IEnumerable<string> GetXdgCandidates()
212209
}
213210
}
214211

215-
#if UNITY_EDITOR_LINUX
216212
[System.Runtime.InteropServices.DllImport ("libc")]
217213
private static extern int readlink(string path, byte[] buffer, int buflen);
218214

@@ -519,13 +515,14 @@ public override bool Open(string path, int line, int column, string solution)
519515

520516
private static ProcessStartInfo ProcessStartInfoFor(string application, string arguments)
521517
{
522-
if (!VisualStudioEditor.IsOSX)
523-
return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect: false);
524-
518+
#if UNITY_EDITOR_OSX
525519
// wrap with built-in OSX open feature
526520
arguments = $"-n \"{application}\" --args {arguments}";
527521
application = "open";
528522
return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect:false, shell: true);
523+
#else
524+
return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect: false);
525+
#endif
529526
}
530527

531528
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: 5 additions & 4 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;
@@ -101,7 +103,7 @@ public override IGenerator ProjectGenerator
101103

102104
private static bool IsCandidateForDiscovery(string path)
103105
{
104-
return Directory.Exists(path) && VisualStudioEditor.IsOSX && Regex.IsMatch(path, "Visual\\s?Studio(?!.*Code.*).*.app$", RegexOptions.IgnoreCase);
106+
return Directory.Exists(path) && Regex.IsMatch(path, "Visual\\s?Studio(?!.*Code.*).*.app$", RegexOptions.IgnoreCase);
105107
}
106108

107109
public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioInstallation installation)
@@ -144,9 +146,6 @@ public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioI
144146

145147
public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations()
146148
{
147-
if (!VisualStudioEditor.IsOSX)
148-
yield break;
149-
150149
var candidates = Directory.EnumerateDirectories("/Applications", "*.app");
151150
foreach (var candidate in candidates)
152151
{
@@ -178,3 +177,5 @@ public static void Initialize()
178177
}
179178
}
180179
}
180+
181+
#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)