-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRakefile
489 lines (393 loc) · 13.1 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# frozen_string_literal: true
require 'rake/clean'
task default: %i[install clean]
task install: %w[cli.rb] do
Rake::Task['embedded_sass_pb.rb'].invoke unless File.exist?('embedded_sass_pb.rb')
end
CLEAN.include %w[protoc.exe ruby *.proto *.tar.gz *.zip]
CLOBBER.include %w[dart-sass cli.rb embedded_sass_pb.rb node_modules package-lock.json]
file 'protoc.exe' do |t|
fetch(ENV.fetch('PROTOC_BIN') { SassConfig.default_protoc }, t.name)
chmod 'a+x', t.name
end
file 'dart-sass/sass' do |t|
raise if ENV.key?('DART_SASS')
gem_install 'sass-embedded', SassConfig.gem_version, SassConfig.gem_platform do |dir|
mv File.absolute_path("ext/sass/#{t.name}", dir), t.name
end
rescue StandardError
archive = fetch(ENV.fetch('DART_SASS') { SassConfig.default_dart_sass })
unarchive archive
rm archive
end
file 'node_modules/sass' do
sh 'npm', 'install'
end
task 'dart-sass' do
Rake::Task['dart-sass/sass'].invoke
rescue NotImplementedError
Rake::Task['node_modules/sass'].invoke
end
file 'cli.rb' => %w[dart-sass] do |t|
require_relative '../../lib/sass/elf'
begin
exe = 'dart-sass/sass'
exe = "#{exe}#{['', '.bat', '.exe'].find { |ext| File.exist?("#{exe}#{ext}") }}"
raise Errno::ENOENT, exe unless File.exist?(exe)
runtime = 'dart-sass/src/dart'
runtime = "#{runtime}#{['', '.exe'].find { |ext| File.exist?("#{runtime}#{ext}") }}"
snapshot = 'dart-sass/src/sass.snapshot'
command = if File.exist?(runtime) && File.exist?(snapshot)
[runtime, snapshot]
else
[exe]
end
interpreter = File.open(command[0], 'rb') do |file|
Sass.const_get(:ELF).new(file).interpreter
rescue ArgumentError
nil
end
command_source = command.map do |argument|
"File.absolute_path('#{argument}', __dir__).freeze"
end.join(',
')
rescue Errno::ENOENT
package = 'node_modules/sass'
script = File.join(package, SassConfig.package_json(package)['bin']['sass'])
interpreter = nil
command_source = [
"'node'",
"File.absolute_path('#{script}', __dir__).freeze"
].join(',
')
end
if interpreter.nil?
File.write(t.name, <<~CLI_RB)
# frozen_string_literal: true
module Sass
module CLI
COMMAND = [
#{command_source}
].freeze
end
private_constant :CLI
end
CLI_RB
else
File.write(t.name, <<~CLI_RB)
# frozen_string_literal: true
require_relative '../../lib/sass/elf'
module Sass
module CLI
INTERPRETER = '#{interpreter}'
INTERPRETER_SUFFIX = '/#{File.basename(interpreter)}'
COMMAND = [
*(ELF::INTERPRETER if ELF::INTERPRETER != INTERPRETER && ELF::INTERPRETER&.end_with?(INTERPRETER_SUFFIX)),
#{command_source}
].freeze
end
private_constant :CLI
end
CLI_RB
end
end
file 'embedded_sass.proto' => %w[cli.rb] do |t|
fetch(ENV.fetch('EMBEDDED_SASS_PROTOCOL') { SassConfig.default_embedded_sass_protocol }, t.name)
end
rule '_pb.rb' => %w[.proto protoc.exe] do |t|
sh './protoc.exe', '--proto_path=.', '--ruby_out=.', t.source
end
# This is a FileUtils extension that defines several additional commands to be
# added to the FileUtils utility functions.
module FileUtils
# PowerShell quirks:
# - `powershell -Command -`:
# Arguments must be part of command, thus cannot pass arguments safely without escaping.
# - `powershell -Command <script-block> [-args <arg-array>]`:
# This only works when invoking powershell subshell in powershell.
# - `powershell -Command <string> [<CommandParameters>]`:
# CommandParameters are joined with command and then parsed, thus cannot pass arguments safely without escaping.
# - `powershell -File -`:
# Arguments must be part of file, thus cannot pass arguments safely without escaping.
# - `powershell -File <filePath> <args>`:
# This is the only way to pass arguments safely without escaping.
def powershell(file, *args)
sh 'powershell', '-NoLogo', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', file, *args
end
def junzip(archive, dest = '.')
require 'java'
Rake.rake_output_message "Archive: #{archive}" if Rake::FileUtilsExt.verbose_flag
current_directory = java.nio.file.Paths.get(org.jruby.Ruby.getGlobalRuntime.getCurrentDirectory)
zip_file = java.util.zip.ZipFile.new(current_directory.resolve(archive).toFile)
dest_path = current_directory.resolve(dest).normalize
entries = zip_file.entries
while entries.hasMoreElements
entry = entries.nextElement
name = entry.getName
path = dest_path.resolve(name).normalize
raise unless path.startsWith(dest_path)
Rake.rake_output_message " inflating: #{name}" if Rake::FileUtilsExt.verbose_flag
if entry.isDirectory
java.nio.file.Files.createDirectories(path)
else
java.nio.file.Files.createDirectories(path.getParent)
java.nio.file.Files.copy(zip_file.getInputStream(entry), path)
end
end
ensure
zip_file&.close
end
def unarchive(archive, dest = '.')
case archive.downcase
when ->(name) { name.include?('.tar.') || name.end_with?('.tar') }
mkdir_p dest
sh 'tar', '-vxC', dest, '-f', archive, '--no-same-owner', '--no-same-permissions'
when ->(name) { name.end_with?('.zip') }
if RUBY_PLATFORM == 'java'
junzip archive, dest
elsif Gem.win_platform?
powershell 'expand-archive.ps1', '-Force', '-LiteralPath', archive, '-DestinationPath', dest
else
sh 'unzip', '-od', dest, archive
end
else
raise ArgumentError, "Unknown archive format #{archive}"
end
end
def fetch(source_uri, dest_path = nil)
require 'rubygems/remote_fetcher'
source_uri = "/#{source_uri}" if !source_uri.start_with?('/') && File.absolute_path?(source_uri)
source_uri = begin
Gem::Uri.parse!(source_uri)
rescue NoMethodError
begin
URI.parse(source_uri)
rescue StandardError
URI.parse(URI::DEFAULT_PARSER.escape(source_uri.to_s))
end
end
scheme = source_uri.scheme
source_path = begin
Gem::URI::DEFAULT_PARSER
rescue NameError
URI::DEFAULT_PARSER
end.unescape(source_uri.path)
dest_path = File.basename(source_path) if dest_path.nil?
fetcher = Gem::RemoteFetcher.fetcher
symbol = :"fetch_#{scheme.nil? ? 'file' : scheme}"
raise ArgumentError, "Unsupported URI scheme #{scheme}" unless fetcher.respond_to?(symbol)
Rake.rake_output_message "fetch #{Gem::Uri.new(source_uri).redacted}" if Rake::FileUtilsExt.verbose_flag
unless Rake::FileUtilsExt.nowrite_flag
data = fetcher.public_send(symbol, source_uri)
Gem.write_binary(dest_path, data)
end
dest_path
end
def gem_install(name, version, platform)
require 'rubygems/remote_fetcher'
install_dir = File.absolute_path('ruby')
if Rake::FileUtilsExt.verbose_flag
Rake.rake_output_message [
'gem', 'install',
'--force',
'--install-dir', install_dir,
'--no-document', '--ignore-dependencies',
'--platform', platform,
'--version', version,
'sass-embedded'
].join(' ')
end
dependency = Gem::Dependency.new(name, version)
dependency_request = Gem::Resolver::DependencyRequest.new(dependency, nil)
resolver_spec = Gem::Resolver::BestSet.new.find_all(dependency_request).find do |s|
s.platform == platform
end
raise Gem::UnsatisfiableDependencyError, dependency_request if resolver_spec.nil?
options = { force: true, install_dir: }
if Rake::FileUtilsExt.nowrite_flag
installer = Gem::Installer.for_spec(resolver_spec.spec, options)
else
path = resolver_spec.download(options)
installer = Gem::Installer.at(path, options)
installer.install
end
yield installer.dir
ensure
rm_rf install_dir unless Rake::FileUtilsExt.nowrite_flag
end
end
# The {SassConfig} module.
module SassConfig
module Platform
OS = case RbConfig::CONFIG['host_os'].downcase
when /darwin/
'darwin'
when /linux-android/
'linux-android'
when /linux-musl/
'linux-musl'
when /linux-uclibc/
'linux-uclibc'
when /linux/
'linux'
when *Gem::WIN_PATTERNS
'windows'
else
RbConfig::CONFIG['host_os'].downcase
end
CPU = case RbConfig::CONFIG['host_cpu'].downcase
when /amd64|x86_64|x64/
'x86_64'
when /i\d86|x86|i86pc/
'x86'
when /arm64|aarch64/
'aarch64'
when /arm/
'arm'
when /ppc64le|powerpc64le/
'powerpc64le'
else
RbConfig::CONFIG['host_cpu']
end
ARCH = "#{CPU}-#{OS}".freeze
end
private_constant :Platform
module_function
def package_json(path = '.')
require 'json'
JSON.parse(File.read(File.absolute_path('package.json', path)))
end
def dart_sass_version
package_json['dependencies']['sass']
end
def default_dart_sass
repo = 'https://github.com/sass/dart-sass'
tag_name = dart_sass_version
message = "dart-sass for #{Platform::ARCH} not available at #{repo}/releases/tag/#{tag_name}"
env = ''
os = case Platform::OS
when 'darwin'
'macos'
when 'linux'
'linux'
when 'linux-android'
'android'
when 'linux-musl'
env = '-musl'
'linux'
when 'windows'
'windows'
else
raise NotImplementedError, message
end
cpu = case Platform::CPU
when 'x86'
'ia32'
when 'x86_64'
'x64'
when 'aarch64'
'arm64'
when 'arm'
'arm'
when 'riscv64'
'riscv64'
else
raise NotImplementedError, message
end
ext = Platform::OS == 'windows' ? 'zip' : 'tar.gz'
"#{repo}/releases/download/#{tag_name}/dart-sass-#{tag_name}-#{os}-#{cpu}#{env}.#{ext}"
end
def default_protoc
require 'rubygems/remote_fetcher'
repo = 'https://repo.maven.apache.org/maven2/com/google/protobuf/protoc'
dependency = Gem::Dependency.new('google-protobuf')
spec = dependency.to_spec
version = spec.version
message = "protoc for #{Platform::ARCH} not available at #{repo}/#{version}"
os = case Platform::OS
when 'darwin'
'osx'
when 'linux'
'linux'
when 'windows'
'windows'
else
raise NotImplementedError, message
end
cpu = case Platform::CPU
when 'x86'
'x86_32'
when 'x86_64'
'x86_64'
when 'aarch64'
'aarch_64'
when 'powerpc64le'
'ppcle_64'
when 's390x'
's390_64'
else
raise NotImplementedError, message
end
uri = "#{repo}/#{version}/protoc-#{version}-#{os}-#{cpu}.exe"
Gem::RemoteFetcher.fetcher.fetch_https(Gem::Uri.new("#{uri}.sha1"))
uri
rescue Gem::RemoteFetcher::FetchError
dependency_request = Gem::Resolver::DependencyRequest.new(dependency, nil)
versions = Gem::Resolver::BestSet.new.find_all(dependency_request).filter_map do |s|
s.version if s.platform == Gem::Platform::RUBY
end
versions.sort.reverse_each do |v|
uri = "#{repo}/#{v}/protoc-#{v}-#{os}-#{cpu}.exe"
Gem::RemoteFetcher.fetcher.fetch_https(Gem::Uri.new("#{uri}.sha1"))
return uri
rescue Gem::RemoteFetcher::FetchError
next
end
raise NotImplementedError, message
end
def default_embedded_sass_protocol
require 'json'
require 'open3'
stdout, stderr, status = Open3.capture3(RbConfig.ruby,
File.absolute_path('../../exe/sass', __dir__),
'--embedded',
'--version')
raise stderr unless status.success?
tag_name = JSON.parse(stdout)['protocolVersion']
"https://github.com/sass/sass/raw/embedded-protocol-#{tag_name}/spec/embedded_sass.proto"
end
def development?
File.exist?('../../Gemfile')
end
def gem_version
require_relative '../../lib/sass/embedded/version'
development? ? dart_sass_version : Sass::Embedded::VERSION
end
def gem_platform
platform = Gem::Platform.new("#{Platform::CPU}-#{RbConfig::CONFIG['host_os']}")
case Platform::OS
when 'darwin'
case platform.cpu
when 'aarch64'
Gem::Platform.new(['arm64', platform.os])
else
platform
end
when 'linux'
if platform.version&.start_with?('gnu')
platform
else
Gem::Platform.new([platform.cpu, platform.os, "gnu#{platform.version}"])
end
when 'windows'
case platform.cpu
when 'x86_64'
Gem::Platform.new('x64-mingw-ucrt')
else
Gem::Platform.new([platform.cpu, 'mingw', 'ucrt'])
end
else
platform
end
end
end