UNPKG

6.99 kBPlain TextView Raw
1require 'rubygems'
2require 'rake'
3require 'date'
4
5#############################################################################
6#
7# Helper functions
8#
9#############################################################################
10
11def name
12 @name ||= Dir['*.gemspec'].first.split('.').first
13end
14
15def version
16 line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17 line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18end
19
20def date
21 Date.today.to_s
22end
23
24def gemspec_file
25 "#{name}.gemspec"
26end
27
28def gem_file
29 "#{name}-#{version}.gem"
30end
31
32def replace_header(head, header_name)
33 head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
34end
35
36#############################################################################
37#
38# Standard tasks
39#
40#############################################################################
41
42desc "Open an irb session preloaded with this library"
43task :console do
44 sh "irb -rubygems -r ./lib/#{name}.rb"
45end
46
47#############################################################################
48#
49# Custom tasks (add your own tasks here)
50#
51#############################################################################
52
53require 'rake/clean'
54
55$LOAD_PATH.unshift File.dirname(__FILE__) + "/lib"
56require 'webfontloader'
57
58#
59# Setup
60#
61
62# Build targets (remove with `rake clean`)
63CLEAN.include("target")
64CLEAN.include("tmp")
65
66# JsCompiler
67JsCompilerJar = "tools/compiler/compiler.jar"
68
69# JS Source dependencies
70AllJs = FileList["{src,src-test}/**/*"]
71SourceJs = FileList["src/**/*"]
72
73# JS Source loader
74@modules = WebFontLoader::Modules.new
75
76#
77# Build
78#
79
80directory "target"
81directory "tmp"
82
83desc "Compile the JavaScript into target/webfont.js"
84task :compile, [:modules] => "target/webfont.js"
85
86file "webfontloader.js" => "target/webfont.js" do
87 cp "target/webfont.js", "webfontloader.js"
88end
89
90file "target/webfont.js", [:modules] => SourceJs + ["target"] do |t, args|
91 args.with_defaults(:modules => 'custom google typekit monotype fontdeck')
92
93 modules = args[:modules].split ' '
94
95 output_marker = "%output%"
96 output_wrapper = @modules.js_output_wrapper(output_marker, version)
97
98 args = [
99 ["-jar", JsCompilerJar],
100 ["--compilation_level", "ADVANCED_OPTIMIZATIONS"],
101 ["--js_output_file", t.name],
102 ["--output_wrapper", %("#{output_wrapper}")],
103 ["--warning_level", "VERBOSE"],
104 ["--summary_detail_level", "3"],
105 ["--externs", "externs.js"],
106 "--define goog.DEBUG=false"
107 ]
108
109 args.concat modules.map { |m| "--define INCLUDE_" + m.upcase + "_MODULE" }
110
111 # Extra args to add warnings.
112 args.concat([
113 ["--warning_level", "VERBOSE"],
114 ["--summary_detail_level", "1"]
115 ])
116
117 source = @modules.all_source_files
118 args.concat source.map { |f| ["--js", f] }
119
120 output = `java #{args.flatten.join(' ')} 2>&1`
121 $?.success? ? (puts output) : (fail output)
122end
123
124desc "Creates debug version into target/webfont.js"
125task :debug, [:modules] => "target/webfont_debug.js"
126
127file "target/webfont_debug.js", [:modules] => SourceJs + ["target"] do |t, args|
128 args.with_defaults(:modules => 'custom google typekit monotype fontdeck')
129
130 modules = args[:modules].split ' '
131
132 output_marker = "%output%"
133 output_wrapper = @modules.js_output_wrapper(output_marker, version)
134
135 args = [
136 ["-jar", JsCompilerJar],
137 ["--compilation_level", "ADVANCED_OPTIMIZATIONS"],
138 ["--js_output_file", t.name],
139 ["--output_wrapper", %("#{output_wrapper}")],
140 ["--warning_level", "VERBOSE"],
141 ["--summary_detail_level", "3"],
142 ["--externs", "externs.js"],
143 "--debug=true",
144 "--formatting=PRETTY_PRINT",
145 "--formatting=PRINT_INPUT_DELIMITER"
146 ]
147
148 args.concat modules.map { |m| "--define INCLUDE_" + m.upcase + "_MODULE" }
149
150 # Extra args to add warnings.
151 args.concat([
152 ["--warning_level", "VERBOSE"],
153 ["--summary_detail_level", "1"]
154 ])
155
156 source = @modules.all_source_files
157 args.concat source.map { |f| ["--js", f] }
158
159 output = `java #{args.flatten.join(' ')} 2>&1`
160 $?.success? ? (puts output) : (fail output)
161end
162
163#
164# Run
165#
166desc "BrowserStack tests"
167task :bstest do |t|
168 exec "browserstack-test -u $BROWSERSTACK_USERNAME -p $BROWSERSTACK_PASSWORD -k $BROWSERSTACK_KEY -b browsers.json -t 300 http://localhost:9999/spec/index.html"
169end
170
171desc "Test everything"
172task :default => [:clean, :gzipbytes, :test]
173
174desc "Run all tests"
175task :test do |t|
176 exec "phantomjs tools/jasmine-phantomjs/jasmine-phantomjs.js spec/index.html"
177end
178
179desc "Start the demo server"
180task :demo => "target/webfont.js" do |t|
181 js = t.prerequisites.first
182 exec "bin/webfontloader-demos -F --compiled_js #{js}"
183end
184
185desc "Start the demo server for development"
186task :demodev do
187 exec "bin/webfontloader-demos -F -L --modules"
188end
189
190desc "Find out how many bytes the source is"
191task :bytes => [:clean, "target/webfont.js"] do |t|
192 js = t.prerequisites.last
193 bytes = File.read(js).size
194 puts "#{bytes} bytes uncompressed"
195end
196
197desc "Find out how many bytes the source is when gzipped"
198task :gzipbytes => [:clean, "target/webfont.js"] do |t|
199 require 'zlib'
200 js = t.prerequisites.last
201 bytes = Zlib::Deflate.deflate(File.read(js)).size
202 puts "#{bytes} bytes gzipped"
203end
204
205
206#############################################################################
207#
208# Packaging tasks
209#
210#############################################################################
211
212task :release => [:build] do
213 unless `git branch` =~ /^\* master$/
214 puts "You must be on the master branch to release!"
215 exit!
216 end
217 sh "git add webfontloader.js"
218 sh "git commit --allow-empty -a -m 'Release #{version}'"
219 sh "npm version #{version}"
220 sh "git push --tags origin master"
221 sh "gem push pkg/#{name}-#{version}.gem"
222 sh "npm publish"
223end
224
225task :build => :gemspec do
226 Rake::Task["target/webfont.js"].execute
227 Rake::Task["webfontloader.js"].execute
228 sh "mkdir -p pkg"
229 sh "gem build #{gemspec_file}"
230 sh "mv #{gem_file} pkg"
231end
232
233task :gemspec => :validate do
234 # read spec file and split out manifest section
235 spec = File.read(gemspec_file)
236 head, manifest, tail = spec.split(" # = MANIFEST =\n")
237
238 # replace name version and date
239 replace_header(head, :name)
240 replace_header(head, :version)
241 replace_header(head, :date)
242
243 # determine file list from git ls-files
244 files = `git ls-files`.
245 split("\n").
246 sort.
247 reject { |file| file =~ /^\./ }.
248 reject { |file| file =~ /^(rdoc|pkg)/ }.
249 map { |file| " #{file.gsub(/\s/, '\ ')}" }.
250 join("\n")
251
252 # piece file back together and write
253 manifest = " s.files = %w[\n#{files}\n ]\n"
254 spec = [head, manifest, tail].join(" # = MANIFEST =\n")
255 File.open(gemspec_file, 'w') { |io| io.write(spec) }
256 puts "Updated #{gemspec_file}"
257end
258
259task :validate do
260 libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
261 unless libfiles.empty?
262 puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
263 exit!
264 end
265 unless Dir['VERSION*'].empty?
266 puts "A `VERSION` file at root level violates Gem best practices."
267 exit!
268 end
269end