Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RiderPathLocator detects snap on linux, provides GetBuildNumber api #1215

Merged
merged 1 commit into from
Jun 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 62 additions & 33 deletions unity/EditorPlugin/RiderPathLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public static RiderInfo[] GetAllRiderPaths()
{
return CollectRiderInfosWindows();
}

case OperatingSystemFamily.MacOSX:
{
return CollectRiderInfosMac();
}

case OperatingSystemFamily.Linux:
{
return CollectAllRiderPathsLinux();
Expand All @@ -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)
{
Expand Down Expand Up @@ -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"));
Expand All @@ -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
Expand All @@ -136,42 +140,70 @@ 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<string>();
const string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
CollectPathsFromRegistry(registryKey, installPaths);
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<string> installPaths)
{
using (var key = Registry.LocalMachine.OpenSubKey(registryKey))
Expand Down Expand Up @@ -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))
Expand All @@ -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))
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -284,6 +316,7 @@ public static string GetLatestBuildFromJson(string json)
{
Logger.Warn($"Failed to get latest build from json {json}");
}

return null;
}
}
Expand All @@ -293,13 +326,13 @@ class ItemNode
{
public BuildNode item;
}

[Serializable]
class BuildNode
{
public string build;
}

// ReSharper disable once ClassNeverInstantiated.Global
[Serializable]
class ToolboxInstallData
Expand All @@ -325,6 +358,7 @@ public static string GetLatestBuildFromJson(string json)
{
Logger.Warn($"Failed to get latest build from json {json}");
}

return null;
}
}
Expand All @@ -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)";

Expand Down