-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRakefile
354 lines (295 loc) · 6.99 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
require 'faraday'
require 'json'
require 'yaml'
# Classes {{{
# ===========
#
# Global progress indicator.
#
class Progress
#
# Actual count of compiled files.
#
@@actual = 0
#
# The count of files to update.
#
# Get all the output tasks from the main task prerequisites, and only
# keep needed ones to get the final count.
#
def self.count
@@cached_count ||= Rake::Task[SUPPORT].prerequisites
.flat_map { |p| Rake::Task[p].prerequisites.drop 1 }
.map { |p| Rake::Task[p] }
.find_all(&:needed?)
.count
end
#
# Increment the compiled files count.
#
def self.inc
@@actual += 1
end
def self.inc_s
self.inc
self.to_s
end
#
# Text progress.
#
def self.to_s
"(#{@@actual}/#{self.count})"
end
end
# }}}
# Syntaxic sugar {{{
# ==================
class String
def endpoint
match(/\.(.+)\.css$/).captures.first
end
def spec?
start_with?('spec/')
end
def indent(n)
gsub(/^/, ' ' * n)
end
def normalize_encoding
self.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end
def normalize_css
self[/^@charset/]
end
def normalize_libsass_error_messages
self[/^>> /] || strip[/-?\^$/]
end
def normalize_errors_messages
(strip[/^on line/] && strip[/input\.scss$/]) || strip == 'Use --trace for backtrace.'
end
#
# Normalize CSS.
#
def clean
lines
.map(&:normalize_encoding)
.reject(&:normalize_css)
.reject(&:normalize_libsass_error_messages)
.reject(&:normalize_errors_messages)
.join
.gsub(/^Error: /, '')
.gsub(/\s+/, ' ')
.gsub(/ *\{/, " {\n")
.gsub(/([;,]) */, "\\1\n")
.gsub(/ *\} */, " }\n")
.strip
.chomp('.')
end
end
#
# Get YAML without header line.
#
class Hash
def to_partial_yaml
to_yaml.lines.drop(1).join
end
end
# }}}
#
# Supported engines.
#
ENGINES = {
:ruby_sass_3_2 => '3_2',
:ruby_sass_3_3 => '3_3',
:ruby_sass_3_4 => '3_4',
:libsass_3_1 => 'libsass_3_1',
:libsass_3_2 => 'libsass_3_2',
:libsass_3_3 => 'libsass_3_3',
}
#
# Supported engines.
#
DOCKER_ENGINES = {
:ruby_sass_3_2 => 'xzyfer/docker-ruby-sass:3.2',
:ruby_sass_3_3 => 'xzyfer/docker-ruby-sass:3.3',
:ruby_sass_3_4 => 'xzyfer/docker-ruby-sass:3.4',
:libsass_3_1 => 'xzyfer/docker-libsass:3.1.0',
:libsass_3_2 => 'xzyfer/docker-libsass:3.2.5',
:libsass_3_3 => 'xzyfer/docker-libsass:3.3',
}
#
# Engine executable.
#
ENGINE_EXEC = {
:ruby_sass_3_2 => nil,
:ruby_sass_3_3 => nil,
:ruby_sass_3_4 => nil,
:libsass_3_1 => nil,
:libsass_3_2 => nil,
:libsass_3_3 => nil,
}
#
# Init each engine.
#
DOCKER_ENGINES.each do |engine, release|
prefix = if RUBY_PLATFORM[/darwin/]
# Get Boot2Docker environment variables.
`boot2docker shellinit`.split(' ').values_at(1, 3, 5).join(' ')
else
''
end
ENGINE_EXEC[engine] = "#{prefix} docker run --interactive --tty --rm --volume #{ENV['PWD']}:#{ENV['PWD']} --workdir #{ENV['PWD']} #{release}"
end
#
# Specification file.
#
SPEC = YAML.load_file('_data/tests.yml')
#
# Flat array of tests, unaware of the feature names.
#
TESTS = SPEC.flat_map { |name, tests| tests }
#
# Stats file (containing engine stats).
#
STATS = '_data/stats.yml'
#
# SCSS version of the engine stats.
#
STATS_SCSS = '_sass/utils/_stats.scss'
#
# Support file (containing support results).
#
SUPPORT = '_data/support.yml'
task :default => [STATS_SCSS]
#
# Delete intermediate files.
#
task :clean do
TESTS.each do |t|
['expected_output_clean.css', 'output.*.css', 'support.yml'].each do |g|
Dir.glob("#{t}/#{g}").each { |f| File.delete f }
end
end
end
#
# SCSS version of the stats file.
#
file STATS_SCSS => [STATS] do |t|
File.open(t.name, 'w') do |file|
file.puts '$stats: ('
YAML::load_file(STATS).each do |engine, result|
file.puts " '#{engine}': #{result['percentage'].round},"
end
file.puts ');'
end
end
#
# Compute the engine stats.
#
file STATS => [SUPPORT] do |t|
stats = {}
keys = { true => 'passed', false => 'failed' }
#
# Aggregate results for each engine.
#
YAML::load_file(SUPPORT).each do |feature, engines|
engines.each do |engine, result|
stats[engine] ||= { 'passed' => 0, 'failed' => 0 }
result['tests'].each do |test, passed|
stats[engine][keys[passed]] += 1
end
end
end
stats.each do |engine, result|
result['percentage'] = (result['passed'].to_f / TESTS.count * 100).round 2
end
File.write t.name, stats.to_partial_yaml
end
#
# From each individual test support file, build the aggregated YAML
# file.
#
task SUPPORT => TESTS.map { |t| "#{t}/support.yml" } do |t|
File.open(t.name, 'w') do |file|
SPEC.each do |name, tests|
feature = {}
#
# Aggregate tests.
#
tests.each do |test|
YAML::load_file("#{test}/support.yml").each do |engine, support|
feature[engine] ||= { 'support' => [], 'tests' => {} }
feature[engine]['support'] << support
feature[engine]['tests'][test] = support
end
end
#
# Determine `true` (all good), `false` (all fail) or `nil` (mixed)
# support status.
#
feature.each do |_, engine|
engine['support'] = engine['support'].all? || (engine['support'].include?(true) ? nil : false)
end
file << { name => feature }.to_partial_yaml
file << "\n"
end
end
end
TESTS.each do |test|
#
# Ensure sass-spec prerequisite if the test needs it.
#
Rake::Task[SUPPORT].prerequisites.unshift 'spec' if test.spec?
#
# Expected output (normalized).
#
expected = "#{test}/expected_output_clean.css"
#
# Outputs for each engine.
#
outputs = ENGINES.map { |engine, endpoint| "#{test}/output.#{endpoint}.css" }
#
# Build test support file from expected file and outputs.
#
file "#{test}/support.yml" => [expected, *outputs] do |t|
expected_output = File.read expected
support = outputs.map do |source|
name = ENGINES.key(source.endpoint).to_s
[name, File.read(source) == expected_output]
end
File.write t.name, Hash[support].to_partial_yaml
end
#
# Compile output for different engines, from an input CSS file.
#
DOCKER_ENGINES.each do |engine, endpoint|
file "#{test}/output.#{ENGINES[engine]}.css" do |t|
#
# Find the input file.
#
input = ['', '.disabled']
.map { |s| "#{test}/input#{s}.scss" }
.find { |f| File.exist? f }
puts "#{Progress.inc_s} Compiling #{input} for #{engine}"
result = `#{ENGINE_EXEC[engine]} #{input}`
File.write t.name, result.clean
end
end
#
# Clean version of the expectation file.
#
file "#{test}/expected_output_clean.css" => ["#{test}/expected_output.css"] do |t|
File.write t.name, File.read(t.prerequisites.first).clean
end
end
#
# Link `spec` directory.
#
directory 'spec' => 'sass-spec' do
`ln -s sass-spec/spec .`
end
#
# Clone sass-spec repository.
#
directory 'sass-spec' do
`git clone --depth 1 https://github.com/sass/sass-spec.git`
end