Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VisualStudioForMacInstallation.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. #if UNITY_EDITOR_OSX
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Runtime.InteropServices;
  11. using System.Text.RegularExpressions;
  12. using Unity.CodeEditor;
  13. using IOPath = System.IO.Path;
  14. namespace Microsoft.Unity.VisualStudio.Editor
  15. {
  16. internal class VisualStudioForMacInstallation : VisualStudioInstallation
  17. {
  18. // C# language version support for Visual Studio for Mac
  19. private static readonly VersionPair[] OSXVersionTable =
  20. {
  21. // VisualStudio for Mac 2022
  22. new VersionPair(17,4, /* => */ 11,0),
  23. new VersionPair(17,0, /* => */ 10,0),
  24. // VisualStudio for Mac 8.x
  25. new VersionPair(8,8, /* => */ 9,0),
  26. new VersionPair(8,3, /* => */ 8,0),
  27. new VersionPair(8,0, /* => */ 7,3),
  28. };
  29. private static readonly IGenerator _generator = new LegacyStyleProjectGeneration();
  30. public override bool SupportsAnalyzers
  31. {
  32. get
  33. {
  34. return Version >= new Version(8, 3);
  35. }
  36. }
  37. public override Version LatestLanguageVersionSupported
  38. {
  39. get
  40. {
  41. return GetLatestLanguageVersionSupported(OSXVersionTable);
  42. }
  43. }
  44. private string GetExtensionPath()
  45. {
  46. const string addinName = "MonoDevelop.Unity";
  47. const string addinAssembly = addinName + ".dll";
  48. // user addins repository
  49. var localAddins = IOPath.Combine(
  50. Environment.GetFolderPath(Environment.SpecialFolder.Personal),
  51. $"Library/Application Support/VisualStudio/${Version.Major}.0" + "/LocalInstall/Addins");
  52. // In the user addins repository, the addins are suffixed by their versions, like `MonoDevelop.Unity.1.0`
  53. // When installing another local user addin, MD will remove files inside the folder
  54. // So we browse all VSTUM addins, and return the one with an addin assembly
  55. if (Directory.Exists(localAddins))
  56. {
  57. foreach (var folder in Directory.GetDirectories(localAddins, addinName + "*", SearchOption.TopDirectoryOnly))
  58. {
  59. if (File.Exists(IOPath.Combine(folder, addinAssembly)))
  60. return folder;
  61. }
  62. }
  63. // Check in Visual Studio.app/
  64. // In that case the name of the addin is used
  65. var addinPath = IOPath.Combine(Path, $"Contents/Resources/lib/monodevelop/AddIns/{addinName}");
  66. if (File.Exists(IOPath.Combine(addinPath, addinAssembly)))
  67. return addinPath;
  68. addinPath = IOPath.Combine(Path, $"Contents/MonoBundle/Addins/{addinName}");
  69. if (File.Exists(IOPath.Combine(addinPath, addinAssembly)))
  70. return addinPath;
  71. return null;
  72. }
  73. public override string[] GetAnalyzers()
  74. {
  75. var vstuPath = GetExtensionPath();
  76. if (string.IsNullOrEmpty(vstuPath))
  77. return Array.Empty<string>();
  78. return GetAnalyzers(vstuPath);
  79. }
  80. public override IGenerator ProjectGenerator
  81. {
  82. get
  83. {
  84. return _generator;
  85. }
  86. }
  87. private static bool IsCandidateForDiscovery(string path)
  88. {
  89. return Directory.Exists(path) && Regex.IsMatch(path, "Visual\\s?Studio(?!.*Code.*).*.app$", RegexOptions.IgnoreCase);
  90. }
  91. public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioInstallation installation)
  92. {
  93. installation = null;
  94. if (string.IsNullOrEmpty(editorPath))
  95. return false;
  96. if (!IsCandidateForDiscovery(editorPath))
  97. return false;
  98. // On Mac we use the .app folder, so we need to access to main assembly
  99. var fvi = IOPath.Combine(editorPath, "Contents/Resources/lib/monodevelop/bin/VisualStudio.exe");
  100. if (!File.Exists(fvi))
  101. fvi = IOPath.Combine(editorPath, "Contents/MonoBundle/VisualStudio.exe");
  102. if (!File.Exists(fvi))
  103. fvi = IOPath.Combine(editorPath, "Contents/MonoBundle/VisualStudio.dll");
  104. if (!File.Exists(fvi))
  105. return false;
  106. // VS preview are not using the isPrerelease flag so far
  107. // On Windows FileDescription contains "Preview", but not on Mac
  108. var vi = FileVersionInfo.GetVersionInfo(fvi);
  109. var version = new Version(vi.ProductVersion);
  110. var isPrerelease = vi.IsPreRelease || string.Concat(editorPath, "/" + vi.FileDescription).ToLower().Contains("preview");
  111. installation = new VisualStudioForMacInstallation()
  112. {
  113. IsPrerelease = isPrerelease,
  114. Name = $"{vi.FileDescription}{(isPrerelease ? " Preview" : string.Empty)} [{version.ToString(3)}]",
  115. Path = editorPath,
  116. Version = version
  117. };
  118. return true;
  119. }
  120. public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations()
  121. {
  122. var candidates = Directory.EnumerateDirectories("/Applications", "*.app");
  123. foreach (var candidate in candidates)
  124. {
  125. if (TryDiscoverInstallation(candidate, out var installation))
  126. yield return installation;
  127. }
  128. }
  129. [DllImport("AppleEventIntegration")]
  130. private static extern bool OpenVisualStudio(string appPath, string solutionPath, string filePath, int line);
  131. public override void CreateExtraFiles(string projectDirectory)
  132. {
  133. }
  134. public override bool Open(string path, int line, int column, string solution)
  135. {
  136. string absolutePath = "";
  137. if (!string.IsNullOrWhiteSpace(path))
  138. {
  139. absolutePath = IOPath.GetFullPath(path);
  140. }
  141. return OpenVisualStudio(CodeEditor.CurrentEditorInstallation, solution, absolutePath, line);
  142. }
  143. public static void Initialize()
  144. {
  145. }
  146. }
  147. }
  148. #endif