-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.rake
369 lines (298 loc) · 9.58 KB
/
setup.rake
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
######
# Addon Setup Task
# don't modify this file, it's automatically updated via `rake setup`
######
class AddonTask
def initialize
begin
require "yaml"
require "active_support/all"
require "github/markup"
require "json"
require "rest_client"
require "coffee-script"
require "sass"
require "cssminify"
require "uglifier"
require "erb"
require "ostruct"
rescue Exception => e
puts "\nMissing dependency: #{e.to_s}\n\n"
show_error("Sorry, we're missing some dependencies. Please run `rake setup` to continue.")
end
read_settings
end
def read_settings
addon_settings_path = File.expand_path("./addon.yml", File.dirname(__FILE__))
unless File.exists?(addon_settings_path)
show_error("Could not find ./addon.yml. Please run `rake setup` to continue.")
end
addon_settings = YAML.load_file(addon_settings_path)
# default addon env to development
environment = ENV["ADDON_ENV"].to_s.presence || "development"
# get addon settings
@addon = addon_settings.dup.except("environments")
@addon.merge!(addon_settings["environments"][environment])
if @addon["name"].blank?
show_error("please set a valid addon \"name\" setting in your addon.yml file")
end
# default host if not set
@addon["api_host"] = @addon["api_host"].presence || "https://www.forwardtrail.com"
puts "ForwardTrail Addon: #{@addon['name']}. environment: #{environment}"
puts "host: #{@addon['api_host']}"
# get api key based on environment
# TODO
api_key = @addon["api_key"]
if api_key.blank? or api_key == "YOUR_API_KEY"
puts "********"
puts "Please fill out your 'api_key' in addon.yml (under 'environments' -> '#{environment}')"
puts "api_key: YOUR_API_KEY"
puts
puts "You can get an API Key from your ForwardTrail team settings: "
puts "Click on 'Integrations / Addons'"
puts "********"
puts
exit
end
if @addon["secret"].blank? or @addon["secret"] == "GENERATE_NEW_SECRET"
puts "********"
puts "Please fill out your 'secret' in addon.yml"
puts "secret: GENERATE_NEW_SECRET"
puts
puts "You can generate a new secret by running `rake secret`"
puts "then replace GENERATE_NEW_SECRET with that generated string."
puts "********"
puts
exit
end
@addon
end
def local_port
require 'uri'
URI.parse(@addon["addon_url"]).port
end
def compile_sass(file)
if File.extname(file) == ".sass"
syntax = :sass
elsif File.extname(file) == ".scss"
syntax = :scss
end
if @addon["sass"].try(:[], "style").present?
style = @addon["sass"].try(:[], "style").to_s.to_sym
else
style = :nested
end
engine = Sass::Engine.new(read_file(file), :syntax => syntax, :style => style)
engine.render
end
def compile_css(file)
read_file(file)
end
def compile_coffee(file)
CoffeeScript.compile read_file(file)
end
def compile_js(file)
js = read_file(file)
# wrap in anonymous call
"(function() {\n#{js}\n}).call(this);"
end
# read file content, using ERB
def read_file(file)
namespace = OpenStruct.new({
addon: @addon,
})
ERB.new(File.read(file)).result(namespace.instance_eval { binding }).strip
end
def wrap_http
begin
yield
rescue Errno::ECONNREFUSED => e
show_error("ForwardTrail (#{@addon['api_host']}) is not currently responding: \"#{e.message}\" **\n** Sorry about that! Please email this output to support@forwardtrail.com and we'll get back to you ASAP.")
rescue RestClient::InternalServerError => e
show_error("ForwardTrail ((#{@addon['api_host']}) responded with a server error: \"#{e.message}\" **\n** Sorry about that! We'll look into it ASAP, please email us at support@forwardtrail.com.")
rescue => e
puts
puts e.to_s
puts
show_error("ForwardTrail ((#{@addon['api_host']}) responded with an error. **\n** Sorry about that! Please email this output to support@forwardtrail.com and we'll get back to you ASAP.")
end
end
def compile_embedded_js(base_path)
scripts = []
Dir["#{base_path}/**/*.js"].each do |path|
scripts << compile_js(path)
end
Dir["#{base_path}/**/*.coffee"].each do |path|
scripts << compile_coffee(path)
end
js = scripts.join("\n\n")
if minify?
Uglifier.compile(js, :mangle => false)
else
js
end
end
def compile_embedded_css(base_path)
stylesheets = []
Dir["#{base_path}/**/*.sass"].each do |path|
stylesheets << compile_sass(path)
end
Dir["#{base_path}/**/*.scss"].each do |path|
stylesheets << compile_sass(path)
end
Dir["#{base_path}/**/*.css"].each do |path|
stylesheets << compile_css(path)
end
css = stylesheets.join("\n\n")
if minify?
CSSminify.compress(css)
else
css
end
end
def update_icon
# upload icon
icon_path = File.expand_path("./icon.png", File.dirname(__FILE__))
if File.exists?(icon_path)
wrap_http do
request = RestClient::Request.new({
method: :post,
url: "#{@addon['api_host']}/api/v1/addons/#{@addon['name']}/icon",
headers: {
:accept => :json,
'X-API-KEY' => @addon['api_key']
},
payload: {
multipart: true,
file: File.new(icon_path, "rb")
}
})
result = request.execute
if result.code == 200 and JSON.parse(result)["success"]
puts "Addon: #{@addon['name']} icon uploaded."
end
end
end
end
def install
name = @addon["name"]
# addon base path (current dir)
addon_base = File.dirname(__FILE__)
# verify addon settings
if @addon["title"].blank? or @addon["short_description"].blank?
show_error("please set valid \"title\" and \"short_description\" settings")
end
# generate description
description_path = File.expand_path("./addon.md", addon_base)
if File.exists?(description_path)
# render description markup
@addon["description"] = GitHub::Markup.render("README.md", File.read(description_path))
end
# compile addon options
addon_options = []
(@addon["options"].presence || {}).each do |key, val|
val["name"] = key
if val["title"].blank?
val["title"] = key.to_s.camelize
end
if val["help"].present?
val["help_html"] = GitHub::Markup.render("README.md", val["help"])
end
addon_options << val
end
@addon["settings"] = addon_options
@addon["webhook"] = @addon["addon_url"]
# compile CSS
embedded_css = compile_embedded_css("#{addon_base}/client")
if embedded_css.present?
@addon["embedded_css"] = embedded_css
end
# compile JS
embedded_js = compile_embedded_js("#{addon_base}/client")
if embedded_js.present?
@addon["embedded_js"] = embedded_js
end
# generate JSON
post_json = JSON.dump(@addon)
# upload JSON
# require 'byebug';byebug
result = nil
wrap_http do
result = RestClient.post "#{@addon['api_host']}/api/v1/addons", post_json, :content_type => :json, :accept => :json, 'X-API-KEY' => @addon['api_key']
end
response = {}
response = JSON.parse(result) if result.try(:code) == 200
if response["success"]
# only upload the icon if its blank (to change icon, remove and reinstall addon)
icon_path = File.expand_path("./icon.png", File.dirname(__FILE__))
if File.exists?(icon_path) and response["addon"].try(:[], "icon").blank?
update_icon
end
else
error_msg = ""
if response["msg"]
error_msg << " / Error: #{response["msg"]}"
end
if result.try(:code)
error_msg << " / HTTP Code: #{result.try(:code)}"
end
show_error("Unable to upload addon: #{name}#{error_msg}")
end
puts "Addon: #{name} has been updated."
end
def uninstall
wrap_http do
result = RestClient.delete "#{@addon['api_host']}/api/v1/addons/#{@addon['name']}",:content_type => :json, :accept => :json, 'X-API-KEY' => @addon['api_key']
response = {}
response = JSON.parse(result) if result.try(:code) == 200
if response["success"]
puts "Addon: #{name} has been removed."
end
end
end
def self.setup
puts "UPDATING TOOLS...\n"
require 'open-uri'
# update tools.rake
setup_rake_content = open("https://raw.githubusercontent.com/forwardtrail/addon/master/setup.rake").read
File.open(__FILE__, 'w') { |file| file.write(setup_rake_content) }
# TODO: check dependencies on Gemfile, instruct user to add gem dependencies for anything missing
# bundle gems
system("bundle")
puts "\n\nTools updated!\n\nNext steps:"
puts "- `rake install` to install your addon to ForwardTrail"
puts "- `foreman start` to run your addon server locally"
end
def show_error(msg)
puts "** #{msg} **"
exit
end
def minify?
@addon["minify"]
end
end
desc "Install this Addon onto the ForwardTrail server"
task :install do
AddonTask.new.install
end
desc "Updates the icon for this addon"
task :update_icon do
AddonTask.new.update_icon
end
desc "Uninstall this Addon off of the ForwardTrail server"
task :uninstall do
AddonTask.new.uninstall
end
desc "Pull the latest tools (setup.rake)"
task :setup do
AddonTask.setup
end
desc "Start local dev server"
task :server do
system("shotgun --server=puma --port=#{AddonTask.new.local_port} server/addon.rb")
end
desc "Generate a secret"
task :secret do
require 'securerandom'
puts SecureRandom.hex(64)
end