1
1
require 'rake'
2
2
require 'rake/packagetask'
3
+ require 'rbconfig'
3
4
require 'yaml'
4
5
5
6
module PrototypeHelper
7
+ extend Rake ::DSL
8
+
6
9
ROOT_DIR = File . expand_path ( File . dirname ( __FILE__ ) )
7
10
SRC_DIR = File . join ( ROOT_DIR , 'src' )
8
11
DIST_DIR = File . join ( ROOT_DIR , 'dist' )
@@ -13,9 +16,12 @@ module PrototypeHelper
13
16
TEST_UNIT_DIR = File . join ( TEST_DIR , 'unit' )
14
17
TMP_DIR = File . join ( TEST_UNIT_DIR , 'tmp' )
15
18
VERSION = YAML . load ( IO . read ( File . join ( SRC_DIR , 'constants.yml' ) ) ) [ 'PROTOTYPE_VERSION' ]
16
-
19
+
17
20
DEFAULT_SELECTOR_ENGINE = 'sizzle'
18
-
21
+
22
+ host = RbConfig ::CONFIG [ 'host' ]
23
+ IS_WINDOWS = host . include? ( 'mswin' ) || host . include? ( 'mingw32' )
24
+
19
25
# Possible options for PDoc syntax highlighting, in order of preference.
20
26
SYNTAX_HIGHLIGHTERS = [ :pygments , :coderay , :none ]
21
27
@@ -27,43 +33,43 @@ module PrototypeHelper
27
33
begin
28
34
`git --version`
29
35
return true
30
- rescue Error => e
36
+ rescue Error
31
37
return false
32
38
end
33
39
end
34
-
40
+
35
41
def self . require_git
36
42
return if has_git?
37
43
puts "\n Prototype requires Git in order to load its dependencies."
38
44
puts "\n Make sure you've got Git installed and in your path."
39
45
puts "\n For more information, visit:\n \n "
40
- puts " http://book. git-scm.com/2_installing_git.html "
46
+ puts " http://git-scm.com/book/en/v2/Getting-Started-Installing-Git "
41
47
exit
42
48
end
43
-
49
+
44
50
def self . sprocketize ( options = { } )
45
51
options = {
46
52
:destination => File . join ( DIST_DIR , options [ :source ] ) ,
47
53
:strip_comments => true
48
54
} . merge ( options )
49
-
55
+
50
56
require_sprockets
51
57
load_path = [ SRC_DIR ]
52
-
58
+
53
59
if selector_path = get_selector_engine ( options [ :selector_engine ] )
54
60
load_path << selector_path
55
61
end
56
-
62
+
57
63
secretary = Sprockets ::Secretary . new (
58
64
:root => File . join ( ROOT_DIR , options [ :path ] ) ,
59
65
:load_path => load_path ,
60
66
:source_files => [ options [ :source ] ] ,
61
67
:strip_comments => options [ :strip_comments ]
62
68
)
63
-
69
+
64
70
secretary . concatenation . save_to ( options [ :destination ] )
65
71
end
66
-
72
+
67
73
def self . build_doc_for ( file )
68
74
rm_rf ( DOC_DIR )
69
75
mkdir_p ( DOC_DIR )
96
102
:assets => 'doc_assets'
97
103
} )
98
104
end
99
-
105
+
106
+ def self . require_package ( name )
107
+ begin
108
+ require name
109
+ rescue LoadError
110
+ puts "You need the #{ name } package. Try installing it with:\n "
111
+ puts " $ gem install #{ name } "
112
+ exit
113
+ end
114
+ end
115
+
116
+ def self . require_phantomjs
117
+ cmd = IS_WINDOWS ? "phantomjs.cmd -v" : "phantomjs -v > /dev/null 2>&1"
118
+ success = system ( cmd )
119
+ if !success
120
+ puts "\n You need phantomjs installed to run this task. Find out how at:"
121
+ puts " http://phantomjs.org/download.html"
122
+ exit
123
+ end
124
+ end
125
+
100
126
def self . syntax_highlighter
101
127
if ENV [ 'SYNTAX_HIGHLIGHTER' ]
102
128
highlighter = ENV [ 'SYNTAX_HIGHLIGHTER' ] . to_sym
103
129
require_highlighter ( highlighter , true )
104
130
return highlighter
105
131
end
106
-
132
+
107
133
SYNTAX_HIGHLIGHTERS . detect { |n | require_highlighter ( n ) }
108
134
end
109
-
135
+
110
136
def self . require_highlighter ( name , verbose = false )
111
137
case name
112
138
when :pygments
121
147
when :coderay
122
148
begin
123
149
require 'coderay'
124
- rescue LoadError => e
150
+ rescue LoadError
125
151
if verbose
126
152
puts "\n You asked to use CodeRay, but I can't find the 'coderay' gem. Just run:\n \n "
127
153
puts " $ gem install coderay"
@@ -139,48 +165,42 @@ EOF
139
165
exit
140
166
end
141
167
end
142
-
168
+
143
169
def self . require_sprockets
144
170
require_submodule ( 'Sprockets' , 'sprockets' )
145
171
end
146
-
172
+
147
173
def self . require_pdoc
148
174
require_submodule ( 'PDoc' , 'pdoc' )
149
175
end
150
-
176
+
151
177
def self . require_unittest_js
152
178
require_submodule ( 'UnittestJS' , 'unittest_js' )
153
179
end
154
-
180
+
155
181
def self . require_caja_builder
156
182
require_submodule ( 'CajaBuilder' , 'caja_builder' )
157
183
end
158
-
184
+
159
185
def self . get_selector_engine ( name )
160
186
return if !name
161
- # If the submodule exists, we should use it, even if we're using the
162
- # default engine; the user might have fetched it manually, and thus would
163
- # want to build a distributable with the most recent version of that
164
- # engine.
187
+ # If the submodule exists, we should use it.
165
188
submodule_path = File . join ( ROOT_DIR , "vendor" , name )
166
189
return submodule_path if File . exist? ( File . join ( submodule_path , "repository" , ".git" ) )
167
190
return submodule_path if name === "legacy_selector"
168
-
169
- # If it doesn't exist, we should fetch it, _unless_ it's the default
170
- # engine. We've already got a known version of the default engine in our
171
- # load path.
172
- return if name == DEFAULT_SELECTOR_ENGINE
191
+
192
+ # If it doesn't exist, we should fetch it.
173
193
get_submodule ( 'the required selector engine' , "#{ name } /repository" )
174
194
unless File . exist? ( submodule_path )
175
195
puts "The selector engine you required isn't available at vendor/#{ name } .\n \n "
176
196
exit
177
197
end
178
198
end
179
-
199
+
180
200
def self . get_submodule ( name , path )
181
201
require_git
182
202
puts "\n You seem to be missing #{ name } . Obtaining it via git...\n \n "
183
-
203
+
184
204
Kernel . system ( "git submodule init" )
185
205
return true if Kernel . system ( "git submodule update vendor/#{ path } " )
186
206
# If we got this far, something went wrong.
@@ -189,16 +209,18 @@ EOF
189
209
puts " $ git submodule update vendor/#{ path } "
190
210
false
191
211
end
192
-
212
+
193
213
def self . require_submodule ( name , path )
194
214
begin
195
- require path
215
+ full_path = File . join ( PrototypeHelper ::ROOT_DIR , 'vendor' , path , 'lib' , path )
216
+ # We need to require the explicit version in the submodule.
217
+ require full_path
196
218
rescue LoadError => e
197
219
# Wait until we notice that a submodule is missing before we bother the
198
220
# user about installing git. (Maybe they brought all the files over
199
221
# from a different machine.)
200
- missing_file = e . message . sub ( 'no such file to load -- ' , '' )
201
- if missing_file == path
222
+ missing_file = e . message . sub ( 'no such file to load -- ' , '' ) . sub ( 'cannot load such file -- ' , '' )
223
+ if missing_file == full_path
202
224
# Missing a git submodule.
203
225
retry if get_submodule ( name , path )
204
226
else
210
232
exit
211
233
end
212
234
end
213
-
235
+
214
236
def self . current_head
215
237
`git show-ref --hash HEAD` . chomp [ 0 ..6 ]
216
238
end
@@ -232,7 +254,7 @@ namespace :doc do
232
254
task :build => [ :require ] do
233
255
PrototypeHelper . build_doc_for ( ENV [ 'SECTION' ] ? "#{ ENV [ 'SECTION' ] } .js" : 'prototype.js' )
234
256
end
235
-
257
+
236
258
task :require do
237
259
PrototypeHelper . require_pdoc
238
260
end
@@ -261,83 +283,37 @@ task :clean_package_source do
261
283
rm_rf File . join ( PrototypeHelper ::PKG_DIR , "prototype-#{ PrototypeHelper ::VERSION } " )
262
284
end
263
285
264
- task :test => [ 'test:build ' , 'test:run ' ]
286
+ task :test => [ 'test:require ' , 'test:start ' ]
265
287
namespace :test do
266
- desc 'Runs all the JavaScript unit tests and collects the results'
267
- task :run => [ :require ] do
268
- testcases = ENV [ 'TESTCASES' ]
269
- browsers_to_test = ENV [ 'BROWSERS' ] && ENV [ 'BROWSERS' ] . split ( ',' )
270
- tests_to_run = ENV [ 'TESTS' ] && ENV [ 'TESTS' ] . split ( ',' )
271
- runner = UnittestJS ::WEBrickRunner ::Runner . new ( :test_dir => PrototypeHelper ::TMP_DIR )
272
-
273
- Dir [ File . join ( PrototypeHelper ::TMP_DIR , '*_test.html' ) ] . each do |file |
274
- file = File . basename ( file )
275
- test = file . sub ( '_test.html' , '' )
276
- unless tests_to_run && !tests_to_run . include? ( test )
277
- runner . add_test ( file , testcases )
278
- end
279
- end
280
-
281
- UnittestJS ::Browser ::SUPPORTED . each do |browser |
282
- unless browsers_to_test && !browsers_to_test . include? ( browser )
283
- runner . add_browser ( browser . to_sym )
284
- end
285
- end
286
-
287
- trap ( 'INT' ) { runner . teardown ; exit }
288
- runner . run
289
- end
290
-
291
- task :build => [ :clean , :dist ] do
292
- builder = UnittestJS ::Builder ::SuiteBuilder . new ( {
293
- :input_dir => PrototypeHelper ::TEST_UNIT_DIR ,
294
- :assets_dir => PrototypeHelper ::DIST_DIR
295
- } )
296
- selected_tests = ( ENV [ 'TESTS' ] || '' ) . split ( ',' )
297
- builder . collect ( *selected_tests )
298
- builder . render
299
- end
300
-
301
- task :clean => [ :require ] do
302
- UnittestJS ::Builder . empty_dir! ( PrototypeHelper ::TMP_DIR )
288
+ desc 'Starts the test server.'
289
+ task :start => [ :require ] do
290
+ path_to_app = File . join ( PrototypeHelper ::ROOT_DIR , 'test' , 'unit' , 'server.rb' )
291
+ require path_to_app
292
+
293
+ puts "Starting unit test server..."
294
+ puts "Unit tests available at <http://127.0.0.1:4567/test/>\n \n "
295
+ UnitTests . run!
303
296
end
304
-
297
+
305
298
task :require do
306
- PrototypeHelper . require_unittest_js
299
+ PrototypeHelper . require_package ( 'sinatra' )
307
300
end
308
- end
309
301
310
- task :test_units do
311
- puts '"rake test_units" is deprecated. Please use "rake test" instead.'
312
- end
313
-
314
- task :build_unit_tests do
315
- puts '"rake test_units" is deprecated. Please use "rake test:build" instead.'
316
- end
317
-
318
- task :clean_tmp do
319
- puts '"rake clean_tmp" is deprecated. Please use "rake test:clean" instead.'
320
- end
302
+ desc "Opens the test suite in several different browsers. (Does not start or stop the server; you should do that separately.)"
303
+ task :run => [ :require ] do
304
+ browsers , tests , grep = ENV [ 'BROWSERS' ] , ENV [ 'TESTS' ] , ENV [ 'GREP' ]
305
+ path_to_runner = File . join ( PrototypeHelper ::ROOT_DIR , 'test' , 'unit' , 'runner.rb' )
306
+ require path_to_runner
321
307
322
- namespace :caja do
323
- task :test => [ 'test:build' , 'test:run' ]
324
-
325
- namespace :test do
326
- task :run => [ 'rake:test:run' ]
327
-
328
- task :build => [ :require , 'rake:test:clean' , :dist ] do
329
- builder = UnittestJS ::CajaBuilder ::SuiteBuilder . new ( {
330
- :input_dir => PrototypeHelper ::TEST_UNIT_DIR ,
331
- :assets_dir => PrototypeHelper ::DIST_DIR ,
332
- :whitelist_dir => File . join ( PrototypeHelper ::TEST_DIR , 'unit' , 'caja_whitelists' ) ,
333
- :html_attrib_schema => 'html_attrib.json'
334
- } )
335
- selected_tests = ( ENV [ 'TESTS' ] || '' ) . split ( ',' )
336
- builder . collect ( *selected_tests )
337
- builder . render
338
- end
308
+ Runner ::run ( browsers , tests , grep )
339
309
end
340
- task :require => [ 'rake:test:require' ] do
341
- PrototypeHelper . require_caja_builder
310
+
311
+ desc "Runs the tests in PhantomJS. (Does not start or stop the server; you should do that separately.)"
312
+ task :phantom do
313
+ PrototypeHelper . require_phantomjs
314
+ tests , grep = ENV [ 'TESTS' ] , ENV [ 'GREP' ]
315
+ url = "http://127.0.0.1:4567/test/#{ tests } "
316
+ url << "?grep=#{ grep } " if grep
317
+ system ( %Q[phantomjs ./test/unit/phantomjs/mocha-phantomjs.js "#{ url } "] )
342
318
end
343
- end
319
+ end
0 commit comments