From b9efe5920016f01f3cd330f1ae479cdd5b25655f Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Sun, 10 Apr 2016 09:48:18 -0700 Subject: [PATCH] installation info: Refactor OS detection, support Ubuntu and GNOME --- SparkleShare/Common/BaseController.cs | 7 +-- SparkleShare/Common/SparkleShare.cs | 4 +- Sparkles/Configuration.cs | 7 ++- Sparkles/Git/GitFetcher.cs | 2 +- Sparkles/InstallationInfo.cs | 64 +++++++++++++++------------ Sparkles/Logger.cs | 2 +- 6 files changed, 47 insertions(+), 39 deletions(-) diff --git a/SparkleShare/Common/BaseController.cs b/SparkleShare/Common/BaseController.cs index 4ce194f9..7b03ee11 100644 --- a/SparkleShare/Common/BaseController.cs +++ b/SparkleShare/Common/BaseController.cs @@ -197,7 +197,7 @@ namespace SparkleShare { { string app_data_path = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); - if (InstallationInfo.Platform == PlatformID.Unix) + if (InstallationInfo.OperatingSystem != OS.Windows && InstallationInfo.OperatingSystem != OS.Mac) app_data_path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".config"); string config_path = Path.Combine (app_data_path, "org.sparkleshare.SparkleShare"); @@ -216,8 +216,9 @@ namespace SparkleShare { { Logger.LogInfo ("Environment", "SparkleShare " + InstallationInfo.Version); Logger.LogInfo ("Environment", "Git " + Sparkles.Git.GitCommand.GitVersion); - Logger.LogInfo ("Environment", InstallationInfo.Platform + " (" + Environment.OSVersion + ")"); - + Logger.LogInfo ("Environment", InstallationInfo.OperatingSystem + " (" + Environment.OSVersion + ")"); + // todo tostring with nice os version names + Preset.PresetsPath = PresetsPath; InstallProtocolHandler (); diff --git a/SparkleShare/Common/SparkleShare.cs b/SparkleShare/Common/SparkleShare.cs index d949ef1d..122dca27 100644 --- a/SparkleShare/Common/SparkleShare.cs +++ b/SparkleShare/Common/SparkleShare.cs @@ -39,8 +39,8 @@ namespace SparkleShare { Arguments = args; if (args.Length != 0 && (args [0].Equals ("help") || args [0].Equals ("version")) && - InstallationInfo.Platform != PlatformID.MacOSX && - InstallationInfo.Platform != PlatformID.Win32NT) { + InstallationInfo.OperatingSystem != OS.Mac && + InstallationInfo.OperatingSystem != OS.Windows) { string n = Environment.NewLine; diff --git a/Sparkles/Configuration.cs b/Sparkles/Configuration.cs index e8af0c65..395f84f5 100644 --- a/Sparkles/Configuration.cs +++ b/Sparkles/Configuration.cs @@ -35,7 +35,7 @@ namespace Sparkles { public string HomePath { get { - if (InstallationInfo.Platform == PlatformID.Win32NT) + if (InstallationInfo.OperatingSystem == OS.Windows) return Environment.GetFolderPath (Environment.SpecialFolder.UserProfile); return Environment.GetFolderPath (Environment.SpecialFolder.Personal); @@ -113,12 +113,11 @@ namespace Sparkles { { string user_name = Environment.UserName; - if (InstallationInfo.Platform == PlatformID.Unix || - InstallationInfo.Platform == PlatformID.MacOSX) { - + if (InstallationInfo.OperatingSystem != OS.Windows) { if (string.IsNullOrEmpty (user_name)) user_name = "Unknown"; else + // On Unix systems the user name may have commas appended user_name = user_name.TrimEnd (','); } diff --git a/Sparkles/Git/GitFetcher.cs b/Sparkles/Git/GitFetcher.cs index 0b6637fa..ea923060 100644 --- a/Sparkles/Git/GitFetcher.cs +++ b/Sparkles/Git/GitFetcher.cs @@ -252,7 +252,7 @@ namespace Sparkles.Git { "push.default matching" }; - if (InstallationInfo.Platform == PlatformID.Win32NT) + if (InstallationInfo.OperatingSystem == OS.Windows) settings [0] = "core.autocrlf true"; foreach (string setting in settings) { diff --git a/Sparkles/InstallationInfo.cs b/Sparkles/InstallationInfo.cs index 9f0c930c..67713d0e 100644 --- a/Sparkles/InstallationInfo.cs +++ b/Sparkles/InstallationInfo.cs @@ -17,44 +17,52 @@ using System; using System.Reflection; -using System.Runtime.InteropServices; namespace Sparkles { + public enum OS + { + Mac, + Windows, + Ubuntu, + GNOME + } + + public partial class InstallationInfo { + static OS operating_system = OS.Windows; + + public static OS OperatingSystem { + get { + if (Environment.OSVersion.Platform == PlatformID.Win32NT) + return operating_system; + + var uname = new Command ("uname", "-a"); + string output = uname.StartAndReadStandardOutput (); + + // Environment.OSVersion.Platform.PlatformID.MacOSX is broken in Mono + // for historical reasons, so check manually + if (output.StartsWith ("Darwin", StringComparison.InvariantCulture)) { + operating_system = OS.Mac; + + } else if (output.Contains ("Ubuntu")) { + operating_system = OS.Ubuntu; + + } else { + operating_system = OS.GNOME; + } + + return operating_system; + } + } + + public static string Version { get { string version = "" + Assembly.GetExecutingAssembly ().GetName ().Version; return version.Substring (0, version.Length - 2); } } - - - // This fixes the PlatformID enumeration for MacOSX in Environment.OSVersion.Platform, - // which is intentionally broken in Mono for historical reasons - public static PlatformID Platform { - get { - IntPtr buf = IntPtr.Zero; - - try { - buf = Marshal.AllocHGlobal (8192); - - if (uname (buf) == 0 && Marshal.PtrToStringAnsi (buf) == "Darwin") - return PlatformID.MacOSX; - - } catch { - } finally { - if (buf != IntPtr.Zero) - Marshal.FreeHGlobal (buf); - } - - return Environment.OSVersion.Platform; - } - } - - - [DllImport ("libc")] - static extern int uname (IntPtr buf); } } diff --git a/Sparkles/Logger.cs b/Sparkles/Logger.cs index eeb2964a..b37918ef 100644 --- a/Sparkles/Logger.cs +++ b/Sparkles/Logger.cs @@ -65,7 +65,7 @@ namespace Sparkles { { string home_path = Environment.GetFolderPath (Environment.SpecialFolder.Personal); - if (InstallationInfo.Platform == PlatformID.Win32NT) + if (InstallationInfo.OperatingSystem == OS.Windows) home_path = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile); string crash_report_file_path = Path.Combine (home_path, "SparkleShare", "crash_report.txt");