-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.rb
150 lines (116 loc) · 3.45 KB
/
service.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
#!/usr/bin/env ruby
$LOAD_PATH << '.'
require 'sinatra'
require 'json'
require 'securerandom'
require 'fileutils'
require 'net/http'
require "uri"
require 'colorize'
require 'config'
set :port, 1234
$stdout.sync = true
workspace = File::expand_path("~/Work/Rashim/Rashim.xcworkspace")
previewsLocation = 'http://cc673dd2.ngrok.io'
appetizeApiToken = AppMakerConfig::APPETIZE_API_TOKEN
workFolder = File::expand_path("~/Work/AppMaker")
def camel_case(str)
words = str.downcase.split
words.shift + words.map(&:capitalize).join
end
#use Rack::Auth::Basic do |username, password|
# username == 'admin' && password == 'secret'
#end
get '/' do
'Welcome to app maker service'
end
def upload(scheme, previewsLocation, appetizeApiToken)
url = "#{previewsLocation}/#{scheme}/Build/Products/Release-iphonesimulator/#{scheme}.zip"
uri = URI('https://api.appetize.io/v1/app/update')
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
body = { :token => appetizeApiToken, :url => url, :platform => 'ios' }.to_json
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
JSON.pretty_generate(json)
else
puts ''
puts '-----------------------------------------------------------------'
puts "Response #{response.code} #{response.message}:#{response.body}".red
puts '-----------------------------------------------------------------'
500
end
end
get '/upload/:scheme' do
scheme = params['scheme']
upload (scheme)
end
get '/build/:scheme' do
scheme = params['scheme']
curDir = Dir.pwd
tempFolder = File.join(workFolder, "Previews", scheme)
FileUtils::mkdir_p tempFolder
print 'Building...'.green
output = `xcodebuild -sdk iphonesimulator -workspace '#{workspace}' -scheme '#{scheme}' -configuration Release -derivedDataPath '#{tempFolder}'` # > /dev/null 2>&1
#puts output
result = $?.to_i
# puts result
if result==0 then
puts "Done".green
tempFolder += '/Build/Products/Release-iphonesimulator'
print 'Making zip...'.green
cmd = `cd '#{tempFolder}' && zip -r -X '#{scheme}.zip' '#{scheme}.app'`
#puts cmd
#puts system cmd
if result==0 then
puts "Done".green
print 'Uploading for preview...'.green
upload(scheme, previewsLocation, appetizeApiToken)
else
puts "Failed".red
500
end
else
puts 'Failed'.red
puts output
500
end
end
get '/clean/:scheme' do
scheme = params['scheme']
output = `xcodebuild -workspace "#{workspace}" -scheme '#{scheme}' clean`
puts output
end
get '/schemes' do
schemesPattern = "Schemes:"
output = `xcodebuild -workspace '#{workspace}' -list`
index = output.index(schemesPattern)
if !index.nil?
headers \
"Content-Type" => "application/json;charset=utf-8"
output = output[index + schemesPattern.length, output.length - schemesPattern.length].strip
output.gsub! ' ', ''
schemes = output.lines
schemes.map! { |s|
s.gsub "\n", ''
}
JSON.pretty_generate(schemes)
else
404
end
end
#get '/hi' do
# 'Hello World!'
#end
#get '/hello/:name/:prefix' do
# # matches "GET /hello/foo" and "GET /hello/bar"
# # params['name'] is 'foo' or 'bar'
# name = params['name']
# name = camel_case(name)
# "Hello #{params['prefix']}.#{name}!"
#end