-
Notifications
You must be signed in to change notification settings - Fork 640
/
Rakefile
executable file
·319 lines (276 loc) · 9.74 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
require 'rake'
require 'rake/packagetask'
require 'rbconfig'
require 'yaml'
module PrototypeHelper
extend Rake::DSL
ROOT_DIR = File.expand_path(File.dirname(__FILE__))
SRC_DIR = File.join(ROOT_DIR, 'src')
DIST_DIR = File.join(ROOT_DIR, 'dist')
DOC_DIR = File.join(ROOT_DIR, 'doc')
TEMPLATES_DIR = File.join(ROOT_DIR, 'templates')
PKG_DIR = File.join(ROOT_DIR, 'pkg')
TEST_DIR = File.join(ROOT_DIR, 'test')
TEST_UNIT_DIR = File.join(TEST_DIR, 'unit')
TMP_DIR = File.join(TEST_UNIT_DIR, 'tmp')
VERSION = YAML.load(IO.read(File.join(SRC_DIR, 'constants.yml')))['PROTOTYPE_VERSION']
DEFAULT_SELECTOR_ENGINE = 'sizzle'
host = RbConfig::CONFIG['host']
IS_WINDOWS = host.include?('mswin') || host.include?('mingw32')
# Possible options for PDoc syntax highlighting, in order of preference.
SYNTAX_HIGHLIGHTERS = [:pygments, :coderay, :none]
%w[sprockets pdoc unittest_js caja_builder].each do |name|
$:.unshift File.join(PrototypeHelper::ROOT_DIR, 'vendor', name, 'lib')
end
def self.has_git?
begin
`git --version`
return true
rescue Error
return false
end
end
def self.require_git
return if has_git?
puts "\nPrototype requires Git in order to load its dependencies."
puts "\nMake sure you've got Git installed and in your path."
puts "\nFor more information, visit:\n\n"
puts " http://git-scm.com/book/en/v2/Getting-Started-Installing-Git"
exit
end
def self.sprocketize(options = {})
options = {
:destination => File.join(DIST_DIR, options[:source]),
:strip_comments => true
}.merge(options)
require_sprockets
load_path = [SRC_DIR]
if selector_path = get_selector_engine(options[:selector_engine])
load_path << selector_path
end
secretary = Sprockets::Secretary.new(
:root => File.join(ROOT_DIR, options[:path]),
:load_path => load_path,
:source_files => [options[:source]],
:strip_comments => options[:strip_comments]
)
secretary.concatenation.save_to(options[:destination])
end
def self.build_doc_for(file)
rm_rf(DOC_DIR)
mkdir_p(DOC_DIR)
hash = current_head
index_header = <<EOF
<h1 style="margin-top: 31px; height: 75px; padding: 1px 0; background: url(images/header-stripe-small.png) repeat-x;">
<a href="http://prototypejs.org" style="padding-left: 120px;">
<img src="images/header-logo-small.png" alt="Prototype JavaScript Framework API" />
</a>
</h1>
EOF
PDoc.run({
:source_files => Dir[File.join('src', 'prototype', '**', '*.js')],
:destination => DOC_DIR,
:index_page => 'README.markdown',
:syntax_highlighter => syntax_highlighter,
:markdown_parser => :bluecloth,
:src_code_text => "View source on GitHub →",
:src_code_href => proc { |obj|
"https://github.com/sstephenson/prototype/blob/#{hash}/#{obj.file}#L#{obj.line_number}"
},
:pretty_urls => false,
:bust_cache => false,
:name => 'Prototype JavaScript Framework',
:short_name => 'Prototype',
:home_url => 'http://prototypejs.org',
:version => PrototypeHelper::VERSION,
:index_header => index_header,
:footer => 'This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0 Unported License</a>.',
:assets => 'doc_assets'
})
end
def self.require_package(name)
begin
require name
rescue LoadError
puts "You need the #{name} package. Try installing it with:\n"
puts " $ gem install #{name}"
exit
end
end
def self.require_phantomjs
cmd = IS_WINDOWS ? "phantomjs.cmd -v" : "phantomjs -v > /dev/null 2>&1"
success = system(cmd)
if !success
puts "\nYou need phantomjs installed to run this task. Find out how at:"
puts " http://phantomjs.org/download.html"
exit
end
end
def self.syntax_highlighter
if ENV['SYNTAX_HIGHLIGHTER']
highlighter = ENV['SYNTAX_HIGHLIGHTER'].to_sym
require_highlighter(highlighter, true)
return highlighter
end
SYNTAX_HIGHLIGHTERS.detect { |n| require_highlighter(n) }
end
def self.require_highlighter(name, verbose=false)
case name
when :pygments
success = system("pygmentize -V > /dev/null")
if !success && verbose
puts "\nYou asked to use Pygments, but I can't find the 'pygmentize' binary."
puts "To install, visit:\n"
puts " http://pygments.org/docs/installation/\n\n"
exit
end
return success # (we have pygments)
when :coderay
begin
require 'coderay'
rescue LoadError
if verbose
puts "\nYou asked to use CodeRay, but I can't find the 'coderay' gem. Just run:\n\n"
puts " $ gem install coderay"
puts "\nand you should be all set.\n\n"
exit
end
return false
end
return true # (we have CodeRay)
when :none
return true
else
puts "\nYou asked to use a syntax highlighter I don't recognize."
puts "Valid options: #{SYNTAX_HIGHLIGHTERS.join(', ')}\n\n"
exit
end
end
def self.require_sprockets
require_submodule('Sprockets', 'sprockets')
end
def self.require_pdoc
require_submodule('PDoc', 'pdoc')
end
def self.require_unittest_js
require_submodule('UnittestJS', 'unittest_js')
end
def self.require_caja_builder
require_submodule('CajaBuilder', 'caja_builder')
end
def self.get_selector_engine(name)
return if !name
# If the submodule exists, we should use it.
submodule_path = File.join(ROOT_DIR, "vendor", name)
return submodule_path if File.exist?(File.join(submodule_path, "repository", ".git"))
return submodule_path if name === "legacy_selector"
# If it doesn't exist, we should fetch it.
get_submodule('the required selector engine', "#{name}/repository")
unless File.exist?(submodule_path)
puts "The selector engine you required isn't available at vendor/#{name}.\n\n"
exit
end
end
def self.get_submodule(name, path)
require_git
puts "\nYou seem to be missing #{name}. Obtaining it via git...\n\n"
Kernel.system("git submodule init")
return true if Kernel.system("git submodule update vendor/#{path}")
# If we got this far, something went wrong.
puts "\nLooks like it didn't work. Try it manually:\n\n"
puts " $ git submodule init"
puts " $ git submodule update vendor/#{path}"
false
end
def self.require_submodule(name, path)
begin
full_path = File.join(PrototypeHelper::ROOT_DIR, 'vendor', path, 'lib', path)
# We need to require the explicit version in the submodule.
require full_path
rescue LoadError => e
# Wait until we notice that a submodule is missing before we bother the
# user about installing git. (Maybe they brought all the files over
# from a different machine.)
missing_file = e.message.sub('no such file to load -- ', '').sub('cannot load such file -- ', '')
if missing_file == full_path
# Missing a git submodule.
retry if get_submodule(name, path)
else
# Missing a gem.
puts "\nIt looks like #{name} is missing the '#{missing_file}' gem. Just run:\n\n"
puts " $ gem install #{missing_file}"
puts "\nand you should be all set.\n\n"
end
exit
end
end
def self.current_head
`git show-ref --hash HEAD`.chomp[0..6]
end
end
task :default => [:dist, :dist_helper, :package, :clean_package_source]
desc "Builds the distribution."
task :dist do
PrototypeHelper.sprocketize(
:path => 'src',
:source => 'prototype.js',
:selector_engine => ENV['SELECTOR_ENGINE'] || PrototypeHelper::DEFAULT_SELECTOR_ENGINE
)
end
namespace :doc do
desc "Builds the documentation."
task :build => [:require] do
PrototypeHelper.build_doc_for(ENV['SECTION'] ? "#{ENV['SECTION']}.js" : 'prototype.js')
end
task :require do
PrototypeHelper.require_pdoc
end
end
task :doc => ['doc:build']
desc "Builds the updating helper."
task :dist_helper do
PrototypeHelper.sprocketize(:path => 'ext/update_helper', :source => 'prototype_update_helper.js')
end
Rake::PackageTask.new('prototype', PrototypeHelper::VERSION) do |package|
package.need_tar_gz = true
package.package_dir = PrototypeHelper::PKG_DIR
package.package_files.include(
'[A-Z]*',
'dist/prototype.js',
'lib/**',
'src/**',
'test/**'
)
end
task :clean_package_source do
rm_rf File.join(PrototypeHelper::PKG_DIR, "prototype-#{PrototypeHelper::VERSION}")
end
task :test => ['test:require', 'test:start']
namespace :test do
desc 'Starts the test server.'
task :start => [:require] do
path_to_app = File.join(PrototypeHelper::ROOT_DIR, 'test', 'unit', 'server.rb')
require path_to_app
puts "Starting unit test server..."
puts "Unit tests available at <http://127.0.0.1:4567/test/>\n\n"
UnitTests.run!
end
task :require do
PrototypeHelper.require_package('sinatra')
end
desc "Opens the test suite in several different browsers. (Does not start or stop the server; you should do that separately.)"
task :run => [:require] do
browsers, tests, grep = ENV['BROWSERS'], ENV['TESTS'], ENV['GREP']
path_to_runner = File.join(PrototypeHelper::ROOT_DIR, 'test', 'unit', 'runner.rb')
require path_to_runner
Runner::run(browsers, tests, grep)
end
desc "Runs the tests in PhantomJS. (Does not start or stop the server; you should do that separately.)"
task :phantom do
PrototypeHelper.require_phantomjs
tests, grep = ENV['TESTS'], ENV['GREP']
url = "http://127.0.0.1:4567/test/#{tests}"
url << "?grep=#{grep}" if grep
system(%Q[phantomjs ./test/unit/phantomjs/mocha-phantomjs.js "#{url}"])
end
end