-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlocalbinary.rb
More file actions
112 lines (97 loc) · 2.8 KB
/
localbinary.rb
File metadata and controls
112 lines (97 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'net/http'
require 'net/https'
require 'rbconfig'
require 'openssl'
require 'tmpdir'
require 'browserstack/localexception'
module BrowserStack
class LocalBinary
def initialize
host_os = RbConfig::CONFIG['host_os']
@http_path = case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
@windows = true
"https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe"
when /darwin|mac os/
"https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64"
when /linux-musl/
"https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-alpine"
when /linux/
if 1.size == 8
"https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-x64"
else
"https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-ia32"
end
end
@ordered_paths = [
File.join(File.expand_path('~'), '.browserstack'),
Dir.pwd,
Dir.tmpdir
]
end
def download(dest_parent_dir)
unless File.exist? dest_parent_dir
Dir.mkdir dest_parent_dir
end
uri = URI.parse(@http_path)
binary_path = File.join(dest_parent_dir, "BrowserStackLocal#{".exe" if @windows}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = http.get(uri.path)
file = open(binary_path, 'wb')
file.write(res.body)
file.close
FileUtils.chmod 0755, binary_path
binary_path
end
def verify_binary(binary_path)
binary_response = IO.popen(binary_path + " --version").readline
binary_response =~ /BrowserStack Local version \d+\.\d+/
rescue Exception => e
false
end
def binary_path
dest_parent_dir = get_available_dirs
binary_path = File.join(dest_parent_dir, "BrowserStackLocal#{".exe" if @windows}")
if File.exist? binary_path
binary_path
else
binary_path = download(dest_parent_dir)
end
valid_binary = verify_binary(binary_path)
if valid_binary
binary_path
else
binary_path = download(dest_parent_dir)
valid_binary = verify_binary(binary_path)
if valid_binary
binary_path
else
raise BrowserStack::LocalException.new('BrowserStack Local binary is corrupt')
end
end
end
private
def get_available_dirs
i = 0
while i < @ordered_paths.size
path = @ordered_paths[i]
if make_path(path)
return path
else
i += 1
end
end
raise BrowserStack::LocalException.new('Error trying to download BrowserStack Local binary')
end
def make_path(path)
begin
FileUtils.mkdir_p path if !File.directory?(path)
return true
rescue Exception
return false
end
end
end
end