-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
159 lines (146 loc) · 4.38 KB
/
Rakefile
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
#encoding: utf-8
require 'zlib'
require 'nokogiri'
require 'uglifier'
require 'cssminify'
require 'bindata'
task :default => [:flatten]
$ugly_options = {
:comments => :none
}
$input_dir = 'src'
$output_dir = 'out'
$dist_dir = 'dist'
def add_timestamp(input)
return input.gsub('{{timestamp}}', Time.now.strftime('%Y%m%d%H%M'))
end
def squish_file(input, options = {})
# Don't squish site survey because this removes the tag we want
return input if input =~ /CONFIG_VAR\(survey_cmd, CFG_SURVEY\)/
doc = Nokogiri::HTML(input)
# Compress all of the inline scripts
doc.xpath('//script').each do |s|
unless (s.content.include?('/*<%') && s.content.include?('%>*/'))
src = (options[:flatten] && s['src'] ? File.read("src/#{s['src']}") : s.content)
s.content = Uglifier.new($ugly_options).compile(src)
s.delete('src') if options[:flatten]
end
end
# Remove whitespace from the html
doc.xpath('//text()').each do |node|
node.remove unless node.content=~/\S/
end
# Minify the CSS
doc.xpath('//style').each do |c|
c.content = CSSminify.compress(c.content)
end
doc.to_s
end
task :clean_output do
if Dir.exists? $output_dir
Dir.glob("#{$output_dir}/*").each do |f|
File.delete(f)
end
else
Dir.mkdir($output_dir)
end
end
task :flatten do
puts "Building single sumorobot html file"
Rake::Task["clean_output"].execute
# Combine all of the html and javascript into one file
# Find all of the html files to compress
Dir.glob('src/*.html').each do |f|
# Read the file
file = File.read(f)
# Add the timestamp for versioning
file = add_timestamp(file)
# Deside where to put it
outfile = f.gsub($input_dir, $output_dir)
# Flatten and compress it
output = squish_file(file, :flatten => true)
# Write to file
File.open(outfile, 'w') { |file|
file.write(output)
}
end
end
task :dist do
Rake::Task["flatten"].execute
Rake::Task["create_bin"].execute
end
task :build do
puts "Building sumorobot UI files"
Rake::Task["clean_output"].execute
Dir.glob('src/*').each do |f|
# Read the file
file = File.read(f)
# Add the timestamp for versioning
file = add_timestamp(file)
# Deside where to put it
outfile = f.gsub($input_dir, $output_dir)
# Flatten and compress it
if f =~ /.*\.html\z/
output = squish_file(file)
elsif f =~ /.*\.js\z/
output = Uglifier.new($ugly_options).compile(file)
elsif f =~ /.*\.css\z/
output = CSSminify.compress(file)
end
# Write to file
File.open(outfile, 'w') { |file|
file.write(output)
}
end
end
class FileHeader < BinData::Record
int32le :next_file
string :file_name, :length => 32, :pad_byte => 0
int32le :file_loc
int32le :file_len
int32le :dyn_addr, :initial_value => 0
int32le :dyn_length, :initial_value => 0
int8le :file_type
array :reserved, :initial_length => 3 do
int8le :initial_value => 0
end
end
def file_type(name)
types = {'HTML' => 1, 'CSS' => 2, 'JS' => 3, 'JPG' => 4, 'PNG' => 5, 'GIF' => 6}
types[File.extname(name).gsub('.', '').upcase]
end
task :create_bin do
puts "Generating bin file"
outfile = "#{$dist_dir}/sumorobot.bin"
headers = []
file_contents = []
files = Dir.glob("#{$output_dir}/*")
files.each_with_index do |f, i|
type = file_type(f)
if type
file_contents << File.read(f).force_encoding('ASCII-8BIT')
headers << FileHeader.new(:next_file => (i+1) * 56, :file_name => f.gsub($output_dir, ''), :file_len => file_contents.last.length, :file_type => type)
headers.last.file_loc = (56 * files.length) + file_contents.join.length - file_contents.last.length
if pos = file_contents.last =~ /(\/\*<%(.*)%>\*\/)/m
headers.last.dyn_addr = pos
headers.last.dyn_length = $2.length + 8
end
end
end
headers.last.next_file = 0
output = headers.map(&:to_binary_s).join + file_contents.join
output.force_encoding('ASCII-8BIT')
crc = output.scan(/.{1,4}/m).map{|x|
x.length < 4 ? (x + "\0" * (4 - x.length)).unpack('L').first : x.unpack('L').first
}.inject(0){ |sum, n|
(sum + n) & 0xFFFFFFFF
}
output += "\xAA\x88".force_encoding('ASCII-8BIT')
output += [crc].pack('V')
output += "\xAA\x88".force_encoding('ASCII-8BIT')
Dir.mkdir($dist_dir) unless Dir.exists?($dist_dir)
File.delete(outfile) if File.exists?(outfile)
File.open(outfile, 'w') { |file|
file.write(output)
}
end