From 16721cf34e2ab0c3efbca642c8cbf60c4174812f Mon Sep 17 00:00:00 2001 From: Ivan Shakhov Date: Wed, 12 Jun 2019 10:26:18 +0200 Subject: [PATCH] RiderPathLocator detects snap on linux, provides GetBuildNumber api (#1215) --- unity/EditorPlugin/RiderPathLocator.cs | 95 +++++++++++++++++--------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/unity/EditorPlugin/RiderPathLocator.cs b/unity/EditorPlugin/RiderPathLocator.cs index 7d16898aed..419292522d 100644 --- a/unity/EditorPlugin/RiderPathLocator.cs +++ b/unity/EditorPlugin/RiderPathLocator.cs @@ -27,10 +27,12 @@ public static RiderInfo[] GetAllRiderPaths() { return CollectRiderInfosWindows(); } + case OperatingSystemFamily.MacOSX: { return CollectRiderInfosMac(); } + case OperatingSystemFamily.Linux: { return CollectAllRiderPathsLinux(); @@ -45,7 +47,7 @@ public static RiderInfo[] GetAllRiderPaths() return new RiderInfo[0]; } #endif - + #if RIDER_EDITOR_PLUGIN // can't be used in com.unity.ide.rider internal static RiderInfo[] GetAllFoundInfos(OperatingSystemFamilyRider operatingSystemFamily) { @@ -86,12 +88,12 @@ private static RiderInfo[] CollectAllRiderPathsLinux() var home = Environment.GetEnvironmentVariable("HOME"); if (string.IsNullOrEmpty(home)) return new RiderInfo[0]; - var pathToBuildTxt = "../../build.txt"; + //$Home/.local/share/JetBrains/Toolbox/apps/Rider/ch-0/173.3994.1125/bin/rider.sh //$Home/.local/share/JetBrains/Toolbox/apps/Rider/ch-0/.channel.settings.json var toolboxRiderRootPath = Path.Combine(home, @".local/share/JetBrains/Toolbox/apps/Rider"); var paths = CollectPathsFromToolbox(toolboxRiderRootPath, "bin", "rider.sh", false) - .Select(a=>new RiderInfo(GetBuildNumber(Path.Combine(a, pathToBuildTxt)), a, true)).ToList(); + .Select(a => new RiderInfo(a, true)).ToList(); //$Home/.local/share/applications/jetbrains-rider.desktop var shortcut = new FileInfo(Path.Combine(home, @".local/share/applications/jetbrains-rider.desktop")); @@ -103,31 +105,33 @@ private static RiderInfo[] CollectAllRiderPathsLinux() { if (!line.StartsWith("Exec=\"")) continue; - var path = line.Split('"').Where((item, index)=>index==1).SingleOrDefault(); + var path = line.Split('"').Where((item, index) => index == 1).SingleOrDefault(); if (string.IsNullOrEmpty(path)) continue; - var buildTxtPath = Path.Combine(path, pathToBuildTxt); - var buildNumber = GetBuildNumber(buildTxtPath); + if (paths.Any(a => a.Path == path)) // avoid adding similar build as from toolbox continue; - paths.Add(new RiderInfo(buildNumber, path, false)); + paths.Add(new RiderInfo(path, false)); } } + // snap install + var snapInstallPath = "/snap/rider/current/bin/rider.sh"; + if (new FileInfo(snapInstallPath).Exists) + paths.Add(new RiderInfo(snapInstallPath, false)); + return paths.ToArray(); } private static RiderInfo[] CollectRiderInfosMac() { - var pathToBuildTxt = "Contents/Resources/build.txt"; - // "/Applications/*Rider*.app" var folder = new DirectoryInfo("/Applications"); if (!folder.Exists) return new RiderInfo[0]; var results = folder.GetDirectories("*Rider*.app") - .Select(a=> new RiderInfo(GetBuildNumber(Path.Combine(a.FullName, pathToBuildTxt)), a.FullName, false)) + .Select(a => new RiderInfo(a.FullName, false)) .ToList(); // /Users/user/Library/Application Support/JetBrains/Toolbox/apps/Rider/ch-1/181.3870.267/Rider EAP.app @@ -136,29 +140,30 @@ private static RiderInfo[] CollectRiderInfosMac() { var toolboxRiderRootPath = Path.Combine(home, @"Library/Application Support/JetBrains/Toolbox/apps/Rider"); var paths = CollectPathsFromToolbox(toolboxRiderRootPath, "", "Rider*.app", true) - .Select(a => new RiderInfo(GetBuildNumber(Path.Combine(a, pathToBuildTxt)), a, true)); + .Select(a => new RiderInfo(a, true)); results.AddRange(paths); } return results.ToArray(); } - private static string GetBuildNumber(string path) + internal static string GetBuildNumber(string path) { - var file = new FileInfo(path); - if (file.Exists) - return File.ReadAllText(file.FullName); + var file = new FileInfo(Path.Combine(path, GetRelativePathToBuildTxt())); + if (!file.Exists) + return string.Empty; + var text = File.ReadAllText(file.FullName); + if (text.Length > 3) + return text.Substring(3); return string.Empty; } private static RiderInfo[] CollectRiderInfosWindows() { - var pathToBuildTxt = "../../build.txt"; - var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); var toolboxRiderRootPath = Path.Combine(localAppData, @"JetBrains\Toolbox\apps\Rider"); var installPathsToolbox = CollectPathsFromToolbox(toolboxRiderRootPath, "bin", "rider64.exe", false).ToList(); - var installInfosToolbox = installPathsToolbox.Select(a => new RiderInfo(GetBuildNumber(Path.Combine(a, pathToBuildTxt)), a, true)).ToList(); + var installInfosToolbox = installPathsToolbox.Select(a => new RiderInfo(a, true)).ToList(); var installPaths = new List(); const string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; @@ -166,12 +171,39 @@ private static RiderInfo[] CollectRiderInfosWindows() const string wowRegistryKey = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; CollectPathsFromRegistry(wowRegistryKey, installPaths); - var installInfos = installPaths.Select(a => new RiderInfo(GetBuildNumber(Path.Combine(a, pathToBuildTxt)), a, false)).ToList(); + var installInfos = installPaths.Select(a => new RiderInfo(a, false)).ToList(); installInfos.AddRange(installInfosToolbox); return installInfos.ToArray(); } + + private static string GetRelativePathToBuildTxt() + { +#if RIDER_EDITOR_PLUGIN + switch (PluginSettings.SystemInfoRiderPlugin.operatingSystemFamily) + { + case OperatingSystemFamilyRider.Windows: + case OperatingSystemFamilyRider.Linux: + return "../../build.txt"; + case OperatingSystemFamilyRider.MacOSX: + return "Contents/Resources/build.txt"; + } + throw new Exception("Unknown OS"); +#else + switch (SystemInfo.operatingSystemFamily) + { + case OperatingSystemFamily.Windows: + case OperatingSystemFamily.Linux: + return "../../build.txt"; + case OperatingSystemFamily.MacOSX: + return "Contents/Resources/build.txt"; + } + throw new Exception("Unknown OS"); + #endif + } + + private static void CollectPathsFromRegistry(string registryKey, List installPaths) { using (var key = Registry.LocalMachine.OpenSubKey(registryKey)) @@ -201,7 +233,7 @@ private static string[] CollectPathsFromToolbox(string toolboxRiderRootPath, str var paths = channelDirs.SelectMany(channelDir => { try - { + { // use history.json - last entry stands for the active build https://jetbrains.slack.com/archives/C07KNP99D/p1547807024066500?thread_ts=1547731708.057700&cid=C07KNP99D var historyFile = Path.Combine(channelDir, ".history.json"); if (File.Exists(historyFile)) @@ -216,7 +248,7 @@ private static string[] CollectPathsFromToolbox(string toolboxRiderRootPath, str return executablePaths; } } - + var channelFile = Path.Combine(channelDir, ".channel.settings.json"); if (File.Exists(channelFile)) { @@ -230,10 +262,10 @@ private static string[] CollectPathsFromToolbox(string toolboxRiderRootPath, str return executablePaths; } } - + // changes in toolbox json files format may brake the logic above, so return all found Rider installations return Directory.GetDirectories(channelDir) - .SelectMany(buildDir=> GetExecutablePaths(dirName, searchPattern, isMac, buildDir)); + .SelectMany(buildDir => GetExecutablePaths(dirName, searchPattern, isMac, buildDir)); } catch (Exception e) { @@ -284,6 +316,7 @@ public static string GetLatestBuildFromJson(string json) { Logger.Warn($"Failed to get latest build from json {json}"); } + return null; } } @@ -293,13 +326,13 @@ class ItemNode { public BuildNode item; } - + [Serializable] class BuildNode { public string build; } - + // ReSharper disable once ClassNeverInstantiated.Global [Serializable] class ToolboxInstallData @@ -325,6 +358,7 @@ public static string GetLatestBuildFromJson(string json) { Logger.Warn($"Failed to get latest build from json {json}"); } + return null; } } @@ -344,16 +378,11 @@ public struct RiderInfo public string BuildVersion; public string Path; - public RiderInfo(string buildVersion, string path, bool isToolbox) + public RiderInfo(string path, bool isToolbox) { - BuildVersion = buildVersion; + BuildVersion = GetBuildNumber(path); Path = new FileInfo(path).FullName; // normalize separators - - var version = string.Empty; - if (buildVersion.Length > 3) - version = buildVersion.Substring(3); - - var presentation = "Rider " + version; + var presentation = "Rider " + BuildVersion; if (isToolbox) presentation += " (JetBrains Toolbox)";