/
build.rb
98 lines (82 loc) · 3.05 KB
/
build.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
module SupportBeeApp
module Build
class << self
def build
move_assets
build_js
end
def move_assets
SupportBeeApp::Base.apps.each do |app|
images_path = app.root.join('assets','images')
next unless Dir.exists?(images_path)
Dir.new(images_path).each do |file|
next if file == '.' or file == '..'
file_path = images_path.join(file)
public_path = Pathname.new(PLATFORM_ROOT).join('public','images',app.slug)
FileUtils.mkpath(public_path)
FileUtils.copy(file_path.to_s, public_path.to_s)
end
end
end
def build_js
output_path = Pathname.new(PLATFORM_ROOT).join('public','javascripts','sb.apps.js').to_s
js = StringIO.new
init_js = 'if(typeof SB === "undefined") { SB = {};} SB.Apps = {};'
js << init_js
js << "\n\n"
js << 'Handlebars.registerHelper(\'ifTicketsCountZero\', function(tickets) {
return tickets.length === 0;
});'
js << "\n"
js << 'Handlebars.registerHelper(\'ifTicketsCountOne\', function(tickets) {
console.log(tickets);
return tickets.length === 1;
});'
js << "\n"
js << 'Handlebars.registerHelper(\'ifTicketsCountMany\', function(tickets) {
return tickets.length > 1;
});'
js << "\n"
SupportBeeApp::Base.apps.each do |app|
app_hash = app.configuration
app_js = "SB.Apps.#{app.slug} = #{JSON.pretty_generate(app_hash)}\n"
app_actions = app_hash['action'].blank? ? {} : app_hash['action']
app_actions.each_pair do |name, options|
app_js << render_button_overlay(app, options) if name == 'button'
end
js << "\n"
js << app_js
js << "\n"
end
screen_hash = {}
SupportBeeApp::Base.apps.each do |app|
app_hash = app.configuration
next unless app_hash['action'] and app_hash['action']['button']
button_config = app_hash['action']['button']
next if button_config['screens'].blank?
button_config['screens'].each do |screen|
if screen_hash[screen]
screen_hash[screen] << app_hash['slug']
else
screen_hash[screen] = [app_hash['slug']]
end
end
end
button_map = JSON.pretty_generate(screen_hash)
button_map = "\n\n SB.Apps.ButtonMap = #{button_map}\n"
js << button_map
output = File.open(output_path, 'w')
output.write(js.string)
output.close
end
private
def render_button_overlay(app, button_config={})
return '' unless button_config['overlay']
button_overlay_path = app.root.join('assets','views','button','overlay.hbs').to_s
template = File.read(button_overlay_path).gsub("'","\'").gsub("\n","\\n")
overlay_js = "Handlebars.compile('#{template}')"
"SB.Apps.#{app.slug}.button_overlay = #{overlay_js}"
end
end
end
end