|
2 | 2 |
|
3 | 3 | require 'bundler/gem_tasks' |
4 | 4 | require 'rspec/core/rake_task' |
| 5 | +require 'rubygems/package_task' |
| 6 | +require 'open-uri' |
| 7 | +require 'digest' |
5 | 8 |
|
6 | 9 | RSpec::Core::RakeTask.new(:spec) |
7 | 10 |
|
8 | 11 | require 'rubocop/rake_task' |
9 | 12 |
|
10 | 13 | RuboCop::RakeTask.new |
11 | 14 |
|
12 | | -task default: %i[spec rubocop] |
| 15 | +task default: %i[prepare_build spec rubocop] |
| 16 | + |
| 17 | +require_relative 'lib/triangulum/bun' |
| 18 | + |
| 19 | +TRIANGULUM_GEMSPEC = Bundler.load_gemspec('triangulum.gemspec') |
| 20 | + |
| 21 | +task prepare_build: %i[download build:entrypoints] |
| 22 | + |
| 23 | +task package: %i[clobber prepare_build] |
| 24 | + |
| 25 | +desc 'Clean downloaded bun binaries' |
| 26 | +task 'clobber:bun' do |
| 27 | + Triangulum::Bun::NATIVE_PLATFORMS.each_key do |platform| |
| 28 | + bun_path = File.join('exe', platform, 'bun') |
| 29 | + rm bun_path if File.exist?(bun_path) |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +task clobber: %i[clobber:bun] |
| 34 | + |
| 35 | +exepaths = [] |
| 36 | + |
| 37 | +Triangulum::Bun::NATIVE_PLATFORMS.each do |platform, (zip_filename, expected_checksum)| |
| 38 | + TRIANGULUM_GEMSPEC.dup.tap do |gemspec| |
| 39 | + exedir = File.join('exe', platform) |
| 40 | + exepath = File.join(exedir, 'bun') |
| 41 | + |
| 42 | + exepaths << exepath |
| 43 | + |
| 44 | + gemspec.platform = platform |
| 45 | + gemspec.files += [exepath] |
| 46 | + |
| 47 | + gem_path = Gem::PackageTask.new(gemspec).define |
| 48 | + desc "Build the #{platform} gem" |
| 49 | + task "gem:#{platform}" => [gem_path] |
| 50 | + |
| 51 | + directory exedir |
| 52 | + file exepath => [exedir] do |
| 53 | + url = Triangulum::Bun.download_url(zip_filename) |
| 54 | + warn "Downloading #{exepath} from #{url} ..." |
| 55 | + |
| 56 | + URI.open(url) do |remote| # rubocop:disable Security/Open |
| 57 | + zip_data = remote.read |
| 58 | + |
| 59 | + actual_checksum = Digest::SHA256.hexdigest(zip_data) |
| 60 | + unless actual_checksum == expected_checksum |
| 61 | + raise "Checksum mismatch for #{zip_filename}: expected #{expected_checksum}, got #{actual_checksum}" |
| 62 | + end |
| 63 | + |
| 64 | + # Bun zips contain a directory with the binary inside |
| 65 | + require 'zip' |
| 66 | + Zip::File.open_buffer(zip_data) do |zip| |
| 67 | + bun_entry = zip.find { |e| File.basename(e.name) == 'bun' } |
| 68 | + raise "bun binary not found in #{zip_filename}" unless bun_entry |
| 69 | + |
| 70 | + File.binwrite(exepath, bun_entry.get_input_stream.read) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + FileUtils.chmod(0o755, exepath, verbose: true) |
| 75 | + end |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +desc 'Download all bun binaries' |
| 80 | +task download: exepaths |
| 81 | + |
| 82 | +desc 'Bundle the TypeScript entrypoints into single JS files' |
| 83 | +task 'build:entrypoints' do |
| 84 | + rm_rf 'lib/triangulum/js' |
| 85 | + directory 'lib/triangulum/js' |
| 86 | + |
| 87 | + entrypoints = %w[single-validation] |
| 88 | + |
| 89 | + entrypoints.each do |entrypoint| |
| 90 | + entrypoint_src = File.expand_path("../entrypoint/#{entrypoint}.ts", __dir__) |
| 91 | + entrypoint_dst = File.expand_path("lib/triangulum/js/#{entrypoint}.js", __dir__) |
| 92 | + |
| 93 | + sh 'bun', 'build', entrypoint_src, '--outfile', entrypoint_dst, '--target', 'bun' |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +desc 'Push all platform gems to RubyGems' |
| 98 | +task 'push:all' do |
| 99 | + Dir['pkg/*.gem'].each do |gem_file| |
| 100 | + sh 'gem', 'push', gem_file |
| 101 | + end |
| 102 | +end |
0 commit comments