-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocketSrv.rb
346 lines (272 loc) · 11.6 KB
/
websocketSrv.rb
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env ruby
$LOAD_PATH << '.'
require "em-websocket"
require "open3"
require 'json'
require 'colorize'
require 'securerandom'
require 'fileutils'
require 'net/http'
require "uri"
require 'timers'
require 'config'
WORKSPACE = File::expand_path("~/Work/Rashim/Rashim.xcworkspace")
PREVIEW_PACKAGES_URL = 'http://6a4df400.ngrok.io'
APPETIZE_API_TOKEN = AppMakerConfig::APPETIZE_API_TOKEN
WORKFOLDER = File::expand_path("~/Work/AppMaker")
def log(ws, status, message)
response = {:command=> 'log', :status => status, :message => message}
ws.send response.to_json
end
def response(ws, status, **args)
resp = {:command=> 'result', :status => status}.merge(args)
ws.send resp.to_json
end
def upload(scheme, previewsLocation, appetizeApiToken, privateKey)
begin
url = "#{previewsLocation}/#{scheme}/Build/Products/Release-iphonesimulator/#{scheme}.zip"
#puts url.yellow
uri = URI('https://api.appetize.io/v1/app/update')
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
if (privateKey.nil?)
body = { :token => appetizeApiToken, :url => url, :platform => 'ios' }.to_json
else
body = { :token => appetizeApiToken, :url => url, :platform => 'ios', :privateKey => privateKey }.to_json
end
req.body = body
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.ssl_version = :SSLv3
http.request req
end
if response.code=='200' then
json = JSON.parse response.body
return true, json #JSON.pretty_generate(json)
else
puts ''
puts '-----------------------------------------------------------------'
puts "Response #{response.code} #{response.message}:#{response.body}".red
puts '-----------------------------------------------------------------'
return false, { :httpCode => response.code, :httpMessage => response.message, :httpBody => response.body }
end
rescue Exception => e
return false, e.message
end
end
def preparePreview(request, ws)
log(ws, 'info', "Got preparePreview command\r")
t = Thread.new() {
scheme = request['scheme']
privateKey = request['privateKey']
curDir = Dir.pwd
tempFolder = File.join(WORKFOLDER, 'Previews', scheme)
FileUtils::mkdir_p tempFolder
print 'Building...'
log(ws, 'info', 'Building.')
progressTimer = EventMachine::PeriodicTimer.new(1) do
log(ws, 'info', '.')
end
#output = `xcodebuild -sdk iphonesimulator -WORKSPACE '#{WORKSPACE}' -scheme '#{scheme}' -configuration Release -derivedDataPath '#{tempFolder}'` # > /dev/null 2>&1
output = ''
exit_status = nil
Open3.popen2e("xcodebuild -sdk iphonesimulator -workspace '#{WORKSPACE}' -scheme '#{scheme}' -configuration Release -derivedDataPath '#{tempFolder}'") do |stdin, stdout_and_stderr, thread|
while outLine=stdout_and_stderr.gets do
#ws.send outLine
output += outLine
end
exit_status = thread.value # Process::Status object returned.
#puts exit_status
end
progressTimer.cancel
#puts output
result = exit_status.to_i
#puts result
if result==0 then
puts "Done"
log(ws, 'info', "Done\r")
tempFolder += '/Build/Products/Release-iphonesimulator'
print 'Making zip...'
log(ws, 'info', 'Making zip...')
cmd = `cd '#{tempFolder}' && zip -r -X '#{scheme}.zip' '#{scheme}.app'`
#puts cmd
#puts system cmd
if result==0 then
puts "Done"
log(ws, 'info', "Done\r")
print 'Uploading for preview...'
log(ws, 'info', 'Uploading for preview.')
progressTimer = EventMachine::PeriodicTimer.new(1) do
log(ws, 'info', '.')
end
res, response = upload(scheme, PREVIEW_PACKAGES_URL, APPETIZE_API_TOKEN, privateKey)
progressTimer.cancel
if res
puts "Done"
log(ws, 'info', "Done\r")
response(ws, 'success', :data => response)
else
puts "Failed"
log(ws, 'error', "Failed\r")
response(ws, 'error', :data => response, :message => 'Failed to upload package')
end
else
puts "Failed"
log(ws, 'error', "Failed\r")
response(ws, 'exception', :message => 'Failed to upload package')
end
else
progressTimer.cancel
puts 'Failed'
log(ws, 'error', "Failed\r")
response(ws, 'exception', :message => 'Failed to build package')
puts output
end
ws.close
}
t.run
end
def submitNewBuild(request, ws)
begin
log(ws, 'info', "Got submitNewBuild command\r")
t = Thread.new {
begin
scheme = request['scheme']
tempFolder = File.join(WORKFOLDER, 'Previews', scheme)
FileUtils::mkdir_p tempFolder
cmd = "xcodebuild -workspace '#{WORKSPACE}' -scheme '#{scheme}' PROFILE=af367fcf-16b5-49c3-9c28-e3d563d7abf9 SIGNING='iPhone Distribution: Mobitti Ltd. (P2WQ65BAA8)' -derivedDataPath '#{tempFolder}' -archivePath '#{tempFolder}/#{scheme}' archive"
exit_status = nil
log(ws, 'info', "Archiving.")
progressTimer = EventMachine::PeriodicTimer.new(1) do
log(ws, 'info', '.')
end
Open3.popen2e(cmd) do |stdin, stdout_and_stderr, thread|
while outLine=stdout_and_stderr.gets do
#ws.send outLine
puts outLine
end
exit_status = thread.value # Process::Status object returned.
end
progressTimer.cancel
puts exit_status
result = exit_status.to_i
#puts result
if result==0 then
log(ws, 'info', "Done\r")
ipa = "#{tempFolder}/#{scheme}.ipa"
FileUtils.rm_f ipa
cmd = "xcodebuild -exportArchive -archivePath '#{tempFolder}/#{scheme}.xcarchive' -exportPath '#{tempFolder}/#{scheme}'" # -exportOptionsPlist '~/Work/AppMaker/exportPList.plist'
exit_status = nil
log(ws, 'info', "Packaging.")
progressTimer = EventMachine::PeriodicTimer.new(1) do
log(ws, 'info', '.')
end
Open3.popen2e(cmd) do |stdin, stdout_and_stderr, thread|
while outLine=stdout_and_stderr.gets do
#ws.send outLine
puts outLine
end
exit_status = thread.value # Process::Status object returned.
end
progressTimer.cancel
puts exit_status
result = exit_status.to_i
if result==0 then
log(ws, 'info', "Done\r")
response(ws, 'success')
else
puts 'Failed'
log(ws, 'error', "Failed\r")
response(ws, 'exception', :message => 'Failed to export archive')
end
else
puts 'Failed'
log(ws, 'error', "Failed\r")
response(ws, 'exception', :message => 'Failed to build archive')
end
ws.close
rescue Exception => e
progressTimer.cancel
response(ws, 'exception', :message => "Failed to clean", :exceptionMessage => e.message)
ws.close
end
}
t.run
rescue Exception => e
response(ws, 'exception', :message => "Failed to clean", :exceptionMessage => e.message)
ws.close
end
end
def clean(request, ws)
begin
log(ws, 'info', "Got clean command\r")
t = Thread.new {
scheme = request['scheme']
tempFolder = File.join(WORKFOLDER, 'Previews', scheme)
FileUtils::mkdir_p tempFolder
cmd = "xcodebuild -sdk iphonesimulator -workspace '#{WORKSPACE}' -scheme '#{scheme}' -configuration Release -derivedDataPath '#{tempFolder}' clean"
exit_status = nil
log(ws, 'info', "Cleaning.")
progressTimer = EventMachine::PeriodicTimer.new(1) do
log(ws, 'info', '.')
end
Open3.popen2e(cmd) do |stdin, stdout_and_stderr, thread|
while outLine=stdout_and_stderr.gets do
#ws.send outLine
end
exit_status = thread.value # Process::Status object returned.
end
progressTimer.cancel
puts exit_status
log(ws, 'info', "Done\r")
response(ws, 'success')
sleep 1
ws.close
}
t.run
rescue Exception => e
progressTimer.cancel
response(ws, 'exception', :message => "Failed to clean", :exceptionMessage => e.message)
ws.close
end
end
EM.run {
EM::WebSocket.run(:host => "0.0.0.0", :port => 9090) do |ws|
ws.onopen { |handshake|
puts 'WebSocket connection open'
# Access properties on the EM::WebSocket::Handshake object, e.g.
# path, query_string, origin, headers
# Publish message to the client
#ws.send "Hello Client, you connected to #{handshake.path}\r"
log(ws, 'info', "Welcome to chuchundra build server...\r\r")
}
ws.onclose { puts 'Connection closed' }
ws.onmessage { |msg|
begin
request = JSON.parse(msg)
rescue Exception => e
response(ws, 'exception', :message => 'Invalid request', :exceptionMessage => e.message)
ws.close
end
begin
if !request.nil?
puts "Recieved message: #{request}"
if (request['command'] == "clean")
clean(request, ws)
elsif (request['command'] == "preparePreview")
preparePreview(request, ws)
elsif (request['command'] == "submitNewBuild")
submitNewBuild(request, ws)
else
puts 'Error: Unknown command'.red
response(ws, 'error', :message => 'Unknown command')
ws.close
end
end
rescue Exception => e
puts "Exception: #{e.message}".red
response(ws, 'exception', :message => 'Unhandled exception', :exceptionMessage => e.message)
ws.close
end
}
end
}