Skip to content

Instantly share code, notes, and snippets.

@josephschito
Last active April 16, 2024 09:54
Show Gist options
  • Save josephschito/96c0e385cb803f3778fb4c40226e64f5 to your computer and use it in GitHub Desktop.
Save josephschito/96c0e385cb803f3778fb4c40226e64f5 to your computer and use it in GitHub Desktop.
Pack Ruby
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 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
# Ruby2Exe.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