forked from jaykz52/mechanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
119 lines (101 loc) · 3.36 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
require 'rake/packagetask'
MECHANIC_VERSION = "0.1.0"
MECHANIC_ROOT = File.expand_path(File.dirname(__FILE__))
MECHANIC_SRC_DIR = File.join(MECHANIC_ROOT, 'src')
MECHANIC_DIST_DIR = File.join(MECHANIC_ROOT, 'dist')
MECHANIC_PKG_DIR = File.join(MECHANIC_ROOT, 'pkg')
MECHANIC_COMPONENTS = [
'mechanic-core',
'logging',
'data',
'events'
]
task :default => [:clean, :concat, :dist]
# MECHANIC_TESTS = %w[
# test/mechanic.html
# test/ajax.html
# test/data.html
# test/detect.html
# test/form.html
# test/fx.html
# test/polyfill.html
# ]
desc "Clean the distribution directory."
task :clean do
rm_rf MECHANIC_DIST_DIR
mkdir MECHANIC_DIST_DIR
end
def normalize_whitespace(filename)
contents = File.readlines(filename)
contents.each { |line| line.sub!(/\s+$/, "") }
File.open(filename, "w") do |file|
file.write contents.join("\n").sub(/(\n+)?\Z/m, "\n")
end
end
desc "Strip trailing whitespace and ensure each file ends with a newline"
task :whitespace do
Dir["*", "src/**/*", "test/**/*", "examples/**/*"].each do |filename|
normalize_whitespace(filename) if File.file?(filename)
end
end
desc "Concatenate source files to build for release"
task :concat, [:addons] => :whitespace do |task, args|
# colon-separated arguments such as `concat[foo:bar:-baz]` specify
# which components to add or exclude, depending on if it starts with "-"
add, exclude = args[:addons].to_s.split(':').partition {|c| c !~ /^-/ }
exclude.each {|c| c.sub!('-', '') }
components = (MECHANIC_COMPONENTS | add) - exclude
unless components == MECHANIC_COMPONENTS
puts "Building release by including: #{components.join(', ')}"
end
File.open(File.join(MECHANIC_DIST_DIR, 'mechanic.js'), 'w') do |f|
f.puts components.map { |component|
File.read File.join(MECHANIC_SRC_DIR, "#{component}.js")
}
end
end
def google_compiler(src, target)
puts "Minifying #{src} with Google Closure Compiler..."
`java -jar vendor/google-compiler/compiler.jar --js #{src} --summary_detail_level 3 --js_output_file #{target}`
end
def process_minified(src, target)
cp target, File.join(MECHANIC_DIST_DIR,'temp.js')
msize = File.size(File.join(MECHANIC_DIST_DIR,'temp.js'))
`gzip -9 #{File.join(MECHANIC_DIST_DIR,'temp.js')}`
osize = File.size(src)
dsize = File.size(File.join(MECHANIC_DIST_DIR,'temp.js.gz'))
rm_rf File.join(MECHANIC_DIST_DIR,'temp.js.gz')
puts "Original version: %.3fk" % (osize/1024.0)
puts "Minified: %.3fk" % (msize/1024.0)
puts "Minified and gzipped: %.3fk, compression factor %.3f" % [dsize/1024.0, osize/dsize.to_f]
end
desc "Generates a minified version for distribution using the Google Closure compiler."
task :dist do
src, target = File.join(MECHANIC_DIST_DIR,'mechanic.js'), File.join(MECHANIC_DIST_DIR,'mechanic.min.js')
google_compiler src, target
process_minified src, target
end
Rake::PackageTask.new('mechanic', MECHANIC_VERSION) do |package|
package.need_tar_gz = true
package.need_zip = true
package.package_dir = MECHANIC_PKG_DIR
package.package_files.include(
'README.md',
'LICENSE',
'dist/**/*',
'src/**/*',
'test/**/*',
'vendor/evidence.js',
'examples/**/*'
).exclude(*`git ls-files -o test src examples -z`.split("\0"))
end
def silence_warnings
require 'stringio'
begin
old_stderr = $stderr
$stderr = StringIO.new
yield
ensure
$stderr = old_stderr
end
end