diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackLocal.csproj b/BrowserStackLocal/BrowserStackLocal/BrowserStackLocal.csproj index 2fd4d10..2e3a514 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackLocal.csproj +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackLocal.csproj @@ -31,4 +31,4 @@ --> - \ No newline at end of file + diff --git a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs index f454995..40ed6c1 100644 --- a/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs +++ b/BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs @@ -14,11 +14,11 @@ public enum LocalState { Idle, Connecting, Connected, Error, Disconnected }; public class BrowserStackTunnel : IDisposable { - static readonly string binaryName = isDarwin() ? "BrowserStackLocal-darwin-x64" : "BrowserStackLocal.exe"; - static readonly string downloadURL = isDarwin() ? - "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64" : - "https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe"; - static readonly string homepath = isDarwin() ? + static readonly string uname = Util.GetUName(); + static readonly string binaryName = GetBinaryName(); + static readonly string downloadURL = "https://www.browserstack.com/local-testing/downloads/binaries/" + binaryName; + + static readonly string homepath = !IsWindows() ? Environment.GetFolderPath(Environment.SpecialFolder.Personal) : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); public static readonly string[] basePaths = new string[] { @@ -37,10 +37,51 @@ public class BrowserStackTunnel : IDisposable Process process = null; - static Boolean isDarwin() + static bool IsDarwin(string osName) + { + return osName.Contains("darwin"); + } + + + static bool IsWindows() + { + return Environment.OSVersion.VersionString?.ToLower().Contains("windows") ?? false; + } + + static bool IsLinux(string osName) + { + return osName.Contains("linux"); + } + + static bool IsAlpine() { - OperatingSystem os = Environment.OSVersion; - return os.Platform.ToString() == "Unix"; + try + { + string[] output = Util.RunShellCommand("grep", "-w \'NAME\' /etc/os-release"); + return output[0]?.ToLower()?.Contains("alpine") ?? false; + } + catch (System.Exception ex) + { + Console.WriteLine("Exception while check isAlpine " + ex); + } + return false; + } + + static string GetBinaryName() + { + if (IsWindows()) return "BrowserStackLocal.exe"; + if (IsDarwin(uname)) return "BrowserStackLocal-darwin-x64"; + + if (IsLinux(uname)) + { + if (Util.Is64BitOS()) + { + return IsAlpine() ? "BrowserStackLocal-alpine" : "BrowserStackLocal-linux-x64"; + } + return "BrowserStackLocal-linux-ia32"; + } + + return "BrowserStackLocal.exe"; } public virtual void addBinaryPath(string binaryAbsolute) @@ -79,7 +120,7 @@ public virtual void fallbackPaths() public void modifyBinaryPermission() { - if (isDarwin()) + if (!IsWindows()) { try { diff --git a/BrowserStackLocal/BrowserStackLocal/Util.cs b/BrowserStackLocal/BrowserStackLocal/Util.cs new file mode 100644 index 0000000..56b2ddd --- /dev/null +++ b/BrowserStackLocal/BrowserStackLocal/Util.cs @@ -0,0 +1,52 @@ +using System; +using System.Diagnostics; + +namespace BrowserStack +{ + public class Util { + + // Only Unix Support + public static string[] RunShellCommand(string command, string args = "") + { + ProcessStartInfo psi = new ProcessStartInfo { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + FileName = command, + Arguments = args + }; + + Process process = new Process { StartInfo = psi }; + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + string error = process.StandardError.ReadToEnd(); + process.WaitForExit(); + return new string[]{output, error}; + } + + public static string GetUName() + { + string osName = ""; + try + { + string[] output = RunShellCommand("uname"); + osName = output[0]?.ToLower(); + } + catch (System.Exception) {} + return osName; + } + + // Using for Linux Only + public static bool Is64BitOS() + { + #if NET48_OR_GREATER || NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_0_OR_GREATER + return Environment.Is64BitOperatingSystem; + #endif + // https://learn.microsoft.com/en-gb/dotnet/standard/choosing-core-framework-server?WT.mc_id=dotnet-35129-website + // linux won't be supported in .NET Framework and fallback to 64 bit + return true; + } + } +} +