-
Notifications
You must be signed in to change notification settings - Fork 10
/
build
executable file
·144 lines (109 loc) · 3.21 KB
/
build
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
#!/usr/bin/env ruby
require 'ftools'
require 'fileutils'
require 'rubygems'
require 'RMagick'
include Magick
require 'open3'
def merge( files = [] )
merged_file = ""
files.each do |file|
File.open( file, "r") { |f|
merged_file += f.read + "\n"
}
end
merged_file
end
def minify( string = '', type = 'js' )
cmd = "java -jar bin/yuicompressor-2.4.2.jar --type #{type}"
stdin, stdout, stderr = Open3.popen3( cmd )
stdin.write string
stdin.close
stdout.read
end
def minify_to_file( string, type, output )
cmd = "java -jar bin/yuicompressor-2.4.2.jar -v --type #{type} -o #{output}"
# puts cmd
stdin, stdout, stderr = Open3.popen3( cmd )
stdin.write string
stdin.close
puts stderr.read
end
def string2png( string, output )
image = Magick::Image.new 1,string.length
for i in 0 .. string.length
color = '#%02X0000' % string[i]
pixel = Magick::Pixel.from_color color
image.pixel_color 0, i, pixel
end
image.compression = ZipCompression
image.write("png8:"+ output)
image = nil
end
release_path = 'app'
# Clean up
puts "Clean up release folder (#{release_path})"
FileUtils.rm_rf release_path
File.makedirs release_path + '/img'
File.makedirs release_path + '/js'
files_to_copy = {
'index.app.html' => 'index.html',
'img/dir.gif' => 'img/dir.gif',
'img/la.gif' => 'img/la.gif',
'img/la_h.gif' => 'img/la_h.gif',
'img/txt.gif' => 'img/txt.gif',
}
puts "Copy files"
files_to_copy.each do |src,dest|
dest = release_path + '/' + dest
puts " cp #{src} => #{dest}"
File.copy src, dest
end
js_files = [
'javascripts/vendors/jsonp.js',
'javascripts/util.js',
'javascripts/f.js',
'javascripts/p.js',
'javascripts/gh.js',
'javascripts/plugins/base.js',
# 'javascripts/diff.js',
# 'javascripts/keyboard.js',
'javascripts/plugins/code_highlighter.js',
'javascripts/vendors/code_highlighter.js',
# 'javascripts/plugins/resizeable_panel.js',
# 'javascripts/vendors/difflib.js',
# 'javascripts/vendors/diffview.js',
'javascripts/app.js'
]
puts "bundling js and css:"
puts " minifying js..."
js = minify merge( js_files )
puts " minifying css..."
css = minify(merge([
'styles.css',
'css/code_highlighter.css'
]), 'css')
puts " bundling..."
string2png js + "~10K~" + css, release_path + '/c.png' # bundle both js + css into 1 image
files_to_minify = [
{ :files => [ 'javascripts/bootstrap.js'], :output => 'js/bootstrap.js', :type => 'js' }
# { :files => [ 'styles.css'], :output => 'styles.css', :type => 'css' }
]
files_to_minify.each do |group|
puts "minify #{group[:output]}"
minify_to_file merge(group[:files]), group[:type], release_path + '/' + group[:output]
end
puts "------ Size Report ------"
files_to_skip_counting = [
'prototype.js',
'styles.css',
'img'
]
total_size = 0
Dir.glob( [release_path + '/*', release_path + '/img/*',release_path + '/js/*' ] ).each do |file|
next if files_to_skip_counting.include? File.basename(file)
puts "#{"%-20.20s" % file} #{"%30s" % File.stat( file ).size}"
total_size += File.stat( file ).size
end
puts "Total size: #{total_size}"
puts "10K Limit: #{- total_size + 10240}"