Skip to content

Commit

Permalink
Merge pull request #50 from browserstack/LOC-5824-linux-check-missing
Browse files Browse the repository at this point in the history
Support Linux Compatibility for Binary Download
  • Loading branch information
kamal-kaur04 authored Jan 7, 2025
2 parents a29ce33 + 0d13cfa commit 915ec7c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
59 changes: 50 additions & 9 deletions BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand All @@ -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)
Expand Down Expand Up @@ -79,7 +120,7 @@ public virtual void fallbackPaths()

public void modifyBinaryPermission()
{
if (isDarwin())
if (!IsWindows())
{
try
{
Expand Down
52 changes: 52 additions & 0 deletions BrowserStackLocal/BrowserStackLocal/Util.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 915ec7c

Please sign in to comment.