forked from opal/opal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
175 lines (140 loc) · 3.85 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
require 'bundler'
Bundler.require
require 'rack'
require 'webrick'
require 'opal-sprockets'
class RunSpec
def initialize(file=nil)
Opal::Processor.arity_check_enabled = true
ENV['OPAL_SPEC'] = file.nil? ? ["#{Dir.pwd}/spec/"].join(',') : file.join(',')
build_specs
server = fork do
app = Rack::Builder.app do
use Rack::ShowExceptions
run Rack::Directory.new('.')
end
Rack::Server.start(:app => app, :Port => 9999, :AccessLog => [],
:Logger => WEBrick::Log.new("/dev/null"))
end
system "phantomjs \"spec/ospec/sprockets.js\" \"http://localhost:9999/spec/index.html\""
success = $?.success?
exit 1 unless success
ensure
Process.kill(:SIGINT, server)
Process.wait
end
def build_specs
env = Opal::Environment.new
env.append_path 'spec'
env.use_gem 'mspec'
FileUtils.mkdir_p 'build'
puts " * build/specs.js"
specs = uglify(env['ospec/main'].to_s)
File.open('build/specs.js', 'w+') { |o| o << specs }
end
# Only if OPAL_UGLIFY is set
def uglify(str)
if ENV['OPAL_UGLIFY']
require 'uglifier'
puts " * uglifying"
Uglifier.compile(str)
else
str
end
end
end
desc "Run tests through mspec"
task :default do
RunSpec.new
end
desc "Build specs to build/specs.js and build/specs.min.js"
task :build_specs do
require 'uglifier'
Opal::Processor.arity_check_enabled = true
ENV['OPAL_SPEC'] = ["#{Dir.pwd}/spec/"].join(',')
env = Opal::Environment.new
env.append_path 'spec'
env.use_gem 'mspec'
FileUtils.mkdir_p 'build'
puts " * build/specs.js"
specs = env['ospec/main'].to_s
puts " * build/specs.min.js"
# min = Uglifier.compile(specs)
File.open('build/specs.js', 'w+') { |o| o << specs }
# File.open('build/specs.min.js', 'w+') { |o| o << min }
end
desc "Run task with spec:dir:file helper"
namespace :spec do
task 'dirs' do
end
rule '' do |task|
#build path for spec files\dirs.
#Example:
#spec:core => spec/core/
#spec:core:array:allocate => spec/core/array/allocate_spec.rb
def path(dirs)
path = "#{Dir.pwd}"
dirs.each do |dir|
base = path + "/#{dir}"
if Dir.exists?(base)
path = base
else
path = Dir.glob("#{base}_spec.rb")
end
end
path = [path].flatten
raise ArgumentError, "File or Dir with task #{dirs.join('/')} not found." if path.empty?
path
end
RunSpec.new(path(task.name.split(":")))
end
end
desc "Build opal.js and opal-parser.js to build/"
task :dist do
Opal::Processor.arity_check_enabled = false
env = Opal::Environment.new
Dir.mkdir 'build' unless File.directory? 'build'
%w[opal opal-parser].each do |lib|
puts "* building #{lib}..."
src = env[lib].to_s
min = uglify src
gzp = gzip min
File.open("build/#{lib}.js", 'w+') { |f| f << src }
File.open("build/#{lib}.min.js", 'w+') { |f| f << min } if min
File.open("build/#{lib}.min.js.gz", 'w+') { |f| f << gzp } if gzp
print "done. (development: #{src.size}B"
print ", minified: #{min.size}B" if min
print ", gzipped: #{gzp.size}Bx" if gzp
puts ")."
puts
end
end
desc "Check file sizes for opal.js runtime"
task :sizes => :dist do
end
desc "Rebuild grammar.rb for opal parser"
task :racc do
%x(racc -l lib/opal/grammar.y -o lib/opal/grammar.rb)
end
# Used for uglifying source to minify
def uglify(str)
IO.popen('uglifyjs', 'r+') do |i|
i.puts str
i.close_write
return i.read
end
rescue Errno::ENOENT
$stderr.puts '"uglifyjs" command not found (install with: "npm install -g uglify-js")'
nil
end
# Gzip code to check file size
def gzip(str)
IO.popen('gzip -f', 'r+') do |i|
i.puts str
i.close_write
return i.read
end
rescue Errno::ENOENT
$stderr.puts '"gzip" command not found, it is required to produce the .gz version'
nil
end