Last active
February 27, 2025 21:17
-
-
Save josephschito/96c0e385cb803f3778fb4c40226e64f5 to your computer and use it in GitHub Desktop.
Pack Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
class Ruby2Pack | |
attr_reader :source_path, :ruby_version, :platform, :rel, :output_path | |
def initialize(source_path:, ruby_version:, platform:, rel:) | |
@source_path = source_path | |
@ruby_version = ruby_version | |
@platform = platform | |
@rel = rel | |
@output_path = "#{__dir__}/dist/#{platform}-#{ruby_version}-#{rel}" | |
end | |
def run | |
mk_dist_dir | |
create_application_loader_file | |
copy_files | |
download_traveling_ruby | |
install_gems | |
create_runnable_script | |
end | |
private | |
def mk_dist_dir | |
FileUtils.makedirs output_path unless File.directory? output_path | |
end | |
def create_application_loader_file | |
File.write( | |
"#{output_path}/run.rb", | |
<<~RUBY | |
ENV['BUNDLE_GEMFILE'] ||= File.join("#{output_path}", "Gemfile") | |
require 'bundler/setup' | |
require File.join("#{output_path}", "main.rb") | |
RUBY | |
) | |
end | |
def copy_files | |
Dir.glob("#{source_path}/*").each do |file| | |
FileUtils.cp_r file, output_path | |
end | |
end | |
def download_traveling_ruby | |
url = "https://github.com/YOU54F/traveling-ruby/releases/download/rel-#{rel}/traveling-ruby-#{rel}-#{ruby_version}-#{platform}.tar.gz" | |
`curl -L #{url} | tar xz -C #{output_path}` | |
end | |
def install_gems | |
`BUNDLE_GEMFILE="#{output_path}/Gemfile" #{output_path}/bin/bundle install --path #{output_path}/gems` | |
end | |
def create_runnable_script | |
File.write( | |
"#{output_path}/run", | |
<<~SH | |
#!/usr/bin/env bash | |
DIRNAME=$(dirname -- "$0") | |
GEMFILE_PATH="$DIRNAME/Gemfile" "$DIRNAME/bin/ruby" "$DIRNAME/run.rb" | |
SH | |
) | |
FileUtils.chmod 'a+x', "#{output_path}/run" | |
end | |
end | |
# Usage: | |
# | |
# source_path: The path to the source files | |
# ruby_version: The version of Ruby to use | |
# platform: The platform to target | |
# rel: The release of Ruby to use | |
# Example | |
# Ruby2Pack.new(source_path: 'example', ruby_version: '3.3.0', platform: 'osx-arm64', rel: '20240215').run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment