### Install Gem Bundle with Bundler Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Installs all gem dependencies specified in the Gemfile using Bundler. This command ensures that the gems are available for development and testing before packaging. ```bash bundle install ``` -------------------------------- ### Create Hello World Ruby App (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This snippet demonstrates the initial setup for a basic 'hello world' Ruby application. It creates a directory, defines a Ruby script that prints 'hello world', and executes it to verify functionality. No external dependencies are involved. ```Bash mkdir hello_app cd hello_app echo '#!/usr/bin/env ruby' > hello.rb echo 'puts "hello world"' >> hello.rb ruby hello.rb # => hello world ``` -------------------------------- ### Install macOS SDK Source: https://github.com/phusion/traveling-ruby/blob/main/osx/README.md This command sequence installs the macOS 10.14 SDK, which is necessary for building binaries compatible with older macOS versions. It involves downloading, extracting, and modifying Xcode's platform information. Ensure you have administrator privileges. ```bash sudo tar -xf MacOSX10.14.sdk.tar.xz -C "$(xcode-select -p)/Platforms/MacOSX.platform/Developer/SDKs" ``` -------------------------------- ### Install Gems for Packaging Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Installs gems into a specific directory (`../vendor`) for inclusion in the Traveling Ruby package. It uses a temporary directory and Bundler options (`--path`, `--without development`) to manage installation without altering the main development Bundler configuration. ```bash mkdir packaging/tmp cp Gemfile Gemfile.lock packaging/tmp/ cd packaging/tmp BUNDLE_IGNORE_CONFIG=1 bundle install --path ../vendor --without development cd ../.. rm -rf packaging/tmp ``` -------------------------------- ### Modify Application to Use Faker Gem Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Updates the main application script to utilize functionality from an installed gem. This example shows how to require the 'faker' gem and use it to generate a random name, replacing the placeholder 'world'. ```ruby #!/usr/bin/env ruby require 'faker' puts "hello #{Faker::Name.name}" ``` -------------------------------- ### Automated Packaging with Rake Source: https://context7.com/phusion/traveling-ruby/llms.txt This Rakefile automates the process of packaging a Ruby application for various platforms using Traveling Ruby. It handles gem installation, downloading the Traveling Ruby runtime, and creating the final package. Dependencies include Bundler and the 'rake' gem. ```ruby require 'bundler/setup' PACKAGE_NAME = "hello" VERSION = "1.0.0" TRAVELING_RUBY_VERSION = "20150210-2.1.5" desc "Package your app" task :package => ['package:linux:x86', 'package:linux:x86_64', 'package:osx'] namespace :package do namespace :linux do desc "Package your app for Linux x86_64" task :x86_64 => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz"] do create_package("linux-x86_64") end end desc "Package your app for OS X" task :osx => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz"] do create_package("osx") end desc "Install gems to local directory" task :bundle_install do abort "You can only 'bundle install' using Ruby 2.1" unless RUBY_VERSION =~ /^2\.1\./ sh "rm -rf packaging/tmp && mkdir packaging/tmp" sh "cp Gemfile Gemfile.lock packaging/tmp/" Bundler.with_clean_env do sh "cd packaging/tmp && env BUNDLE_IGNORE_CONFIG=1 bundle install --path ../vendor --without development" end sh "rm -rf packaging/tmp" sh "rm -f packaging/vendor/*/*/cache/*" end end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz" do download_runtime("linux-x86_64") end def create_package(target) package_dir = "#{PACKAGE_NAME}-#{VERSION}-#{target}" sh "rm -rf #{package_dir}" sh "mkdir -p #{package_dir}/lib/app" sh "cp hello.rb #{package_dir}/lib/app/" sh "mkdir #{package_dir}/lib/ruby" sh "tar -xzf packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz -C #{package_dir}/lib/ruby" sh "cp packaging/wrapper.sh #{package_dir}/hello" sh "cp -pR packaging/vendor #{package_dir}/lib/" sh "cp Gemfile Gemfile.lock #{package_dir}/lib/vendor/" sh "mkdir #{package_dir}/lib/vendor/.bundle" sh "cp packaging/bundler-config #{package_dir}/lib/vendor/.bundle/config" unless ENV['DIR_ONLY'] sh "tar -czf #{package_dir}.tar.gz #{package_dir}" sh "rm -rf #{package_dir}" end end def download_runtime(target) sh "cd packaging && curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz" end # Usage: rake package (creates all packages) # Usage: rake package:linux:x86_64 (creates specific platform) # Usage: rake package DIR_ONLY=1 (creates directories without tar.gz) ``` -------------------------------- ### Bundler Configuration Content Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Defines the configuration settings for Bundler when running within the packaged application. It specifies the gem installation path relative to the Gemfile, excludes the 'development' gem group, and disables the use of globally installed gems. ```plaintext BUNDLE_PATH: . BUNDLE_WITHOUT: development BUNDLE_DISABLE_SHARED_GEMS: '1' ``` -------------------------------- ### Package Ruby App with Gem Dependencies (Bash) Source: https://context7.com/phusion/traveling-ruby/llms.txt This snippet shows how to package a Ruby application that includes gem dependencies using Bundler. It covers creating a Gemfile, updating the application to use gems, installing gems for packaging, configuring Bundler, copying gems into the package, and modifying the wrapper script to activate Bundler. ```bash # Create Gemfile cat > Gemfile << 'EOF' source 'https://rubygems.org' gem 'faker' group :development do gem 'rake' end EOF # Update application to use gem cat > hello.rb << 'EOF' #!/usr/bin/env ruby require 'faker' puts "hello #{Faker::Name.name}" EOF # Install gems for packaging (must use Ruby 2.1.x) mkdir packaging/tmp cp Gemfile Gemfile.lock packaging/tmp/ cd packaging/tmp BUNDLE_IGNORE_CONFIG=1 bundle install --path ../vendor --without development cd ../.. rm -rf packaging/tmp rm -f packaging/vendor/*/*/cache/* # Create Bundler config cat > packaging/bundler-config << 'EOF' BUNDLE_PATH: . BUNDLE_WITHOUT: development BUNDLE_DISABLE_SHARED_GEMS: '1' EOF # Copy gems into package cp -pR packaging/vendor hello-1.0.0-linux-x86_64/lib/ cp Gemfile Gemfile.lock hello-1.0.0-linux-x86_64/lib/vendor/ mkdir hello-1.0.0-linux-x86_64/lib/vendor/.bundle cp packaging/bundler-config hello-1.0.0-linux-x86_64/lib/vendor/.bundle/config # Update wrapper script to activate Bundler cat > packaging/wrapper.sh << 'EOF' #!/bin/bash set -e SELFDIR="`dirname \"$0\"`" SELFDIR="`cd \"$SELFDIR\" && pwd`" export BUNDLE_GEMFILE="$SELFDIR/lib/vendor/Gemfile" unset BUNDLE_IGNORE_CONFIG exec "$SELFDIR/lib/ruby/bin/ruby" -rbundler/setup "$SELFDIR/lib/app/hello.rb" EOF ``` -------------------------------- ### Create Package Directories with Rake Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Uses Rake to prepare the directory structure for application packaging. The DIR_ONLY=1 flag prevents the creation of tar.gz files, focusing solely on directory setup for subsequent gem copying. ```bash rake package DIR_ONLY=1 ``` -------------------------------- ### Copy Bundler Gems to Package Directories Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Copies the installed gems from the vendor directory into the `lib` folder of each platform-specific package. This makes the gems available to the application when it runs on the target system. ```bash cp -pR packaging/vendor hello-1.0.0-linux-x86/lib/ cp -pR packaging/vendor hello-1.0.0-linux-x86_64/lib/ cp -pR packaging/vendor hello-1.0.0-osx/lib/ ``` -------------------------------- ### Download and Extract Precompiled Native Extensions for Linux x86_64 Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This shell script downloads and extracts the precompiled sqlite3 native extension for a Linux x86_64 platform into the specified package directory. It utilizes `curl` to retrieve the compressed archive and `tar` to unpack it, then cleans up the downloaded file. This is part of the platform-specific setup for Traveling Ruby packages. ```bash cd hello-1.0.0-linux-x86_64/lib/vendor/ruby curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-gems-20141215-2.1.5-linux-x86_64/sqlite3-1.3.9.tar.gz tar xzf sqlite3-1.3.9.tar.gz rm sqlite3-1.3.9.tar.gz cd ../../../.. ``` -------------------------------- ### Test Packaged Traveling Ruby Application Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This snippet shows how to extract a packaged Traveling Ruby application (specifically for OS X in this example) and test its functionality. It involves extracting the tarball, changing into the application directory, and executing the application, followed by interacting with an SQLite database to verify data modification. ```shell tar xzf hello-1.0.0-osx.tar.gz cd hello-1.0.0-osx ./hello sqlite3 hello.sqlite3 select * from foo; ``` -------------------------------- ### Automate Packaging with Rake Tasks (Ruby) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md This Ruby code defines Rake tasks to automate the application packaging process. It includes tasks for installing gems, creating packages for different platforms, downloading the Traveling Ruby runtime, and performing cleanup operations. The `create_package` function handles the assembly of the final distribution. ```Ruby # For Bundler.with_clean_env require 'bundler/setup' PACKAGE_NAME = "hello" VERSION = "1.0.0" TRAVELING_RUBY_VERSION = "20150210-2.1.5" desc "Package your app" task :package => ['package:linux:x86', 'package:linux:x86_64', 'package:osx'] namespace :package do namespace :linux do desc "Package your app for Linux x86" task :x86 => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86.tar.gz"] do create_package("linux-x86") end desc "Package your app for Linux x86_64" task :x86_64 => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz"] do create_package("linux-x86_64") end end desc "Package your app for OS X" task :osx => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz"] do create_package("osx") end desc "Install gems to local directory" task :bundle_install do if RUBY_VERSION !~ /^2\.1\./ abort "You can only 'bundle install' using Ruby 2.1, because that's what Traveling Ruby uses." end sh "rm -rf packaging/tmp" sh "mkdir packaging/tmp" sh "cp Gemfile Gemfile.lock packaging/tmp/" Bundler.with_clean_env do sh "cd packaging/tmp && env BUNDLE_IGNORE_CONFIG=1 bundle install --path ../vendor --without development" end sh "rm -rf packaging/tmp" sh "rm -f packaging/vendor/*/*/cache/*" end end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86.tar.gz" do download_runtime("linux-x86") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz" do download_runtime("linux-x86_64") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz" do download_runtime("osx") end def create_package(target) package_dir = "#{PACKAGE_NAME}-#{VERSION}-#{target}" sh "rm -rf #{package_dir}" sh "mkdir #{package_dir}" sh "mkdir -p #{package_dir}/lib/app" sh "cp hello.rb #{package_dir}/lib/app/" sh "mkdir #{package_dir}/lib/ruby" sh "tar -xzf packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz -C #{package_dir}/lib/ruby" sh "cp packaging/wrapper.sh #{package_dir}/hello" sh "cp -pR packaging/vendor #{package_dir}/lib/" sh "cp Gemfile Gemfile.lock #{package_dir}/lib/vendor/" sh "mkdir #{package_dir}/lib/vendor/.bundle" sh "cp packaging/bundler-config #{package_dir}/lib/vendor/.bundle/config" if !ENV['DIR_ONLY'] sh "tar -czf #{package_dir}.tar.gz #{package_dir}" sh "rm -rf #{package_dir}" end end def download_runtime(target) sh "cd packaging && curl -L -O --fail " + "https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz" end ``` -------------------------------- ### Ruby Code for SQLite Database Interaction Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This Ruby code demonstrates how to interact with an SQLite database using the sqlite3 gem. It includes creating a table if it doesn't exist, inserting a row, and closing the database connection. This code is part of the example application for the tutorial. ```ruby #!/usr/bin/env ruby require 'faker' require 'sqlite3' db = SQLite3::Database.new("hello.sqlite3") db.execute("create table if not exists foo (name varchar(255))") db.execute("insert into foo values ('hello world')") db.close puts "Hello #{Faker::Name.name}, database file modified." ``` -------------------------------- ### Shell Library Functions for Build Scripts Source: https://context7.com/phusion/traveling-ruby/llms.txt Provides utility functions for build scripts, including colored output and error handling. It checks for Perl to define ANSI color codes and offers functions for headers, running commands, getting absolute paths, and cleaning up background processes. ```bash #!/bin/bash # Source this library in build scripts if perl -v >/dev/null 2>/dev/null; then RESET=`perl -e 'print("\e[0m")'` BOLD=`perl -e 'print("\e[1m")'` YELLOW=`perl -e 'print("\e[33m")'` BLUE_BG=`perl -e 'print("\e[44m")'` fi function header() { local title="$1" echo "${BLUE_BG}${YELLOW}${BOLD}${title}${RESET}" echo "------------------------------------------" } function run() { echo "+ $@" "$@" } function absolute_path() { local dir="`dirname "$1"`" local name="`basename "$1"`" dir="`cd "$dir" && pwd`" echo "$dir/$name" } function cleanup() { set +e local pids=`jobs -p` if [[ "$pids" != "" ]]; then kill $pids 2>/dev/null fi } trap cleanup EXIT # Usage example: # source shared/library.sh # header "Building Ruby binaries" # run make install ``` -------------------------------- ### Create Wrapper Script for Application (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This Bash script serves as a wrapper to launch the 'hello world' application using the bundled Ruby interpreter. It dynamically determines the script's directory and then executes the Ruby interpreter, passing along any arguments. This allows users to run the application with a simple command instead of the full interpreter path. ```Bash #!/bin/bash set -e # Figure out where this script is located. SELFDIR="`dirname \"$0\"`" SELFDIR="`cd \"$SELFDIR\" && pwd`" # Run the actual app using the bundled Ruby interpreter. exec "$SELFDIR/lib/ruby/bin/ruby" "$SELFDIR/lib/app/hello.rb" "$@" ``` -------------------------------- ### Prepare Platform-Specific Package Directories (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This code prepares the necessary directory structure for packaging the 'hello world' application for different target platforms (Linux x86, Linux x86_64, OS X). It creates nested directories and copies the application script into each platform's designated app directory. ```Bash mkdir -p hello-1.0.0-linux-x86/lib/app cp hello.rb hello-1.0.0-linux-x86/lib/app/ mkdir -p hello-1.0.0-linux-x86_64/lib/app cp hello.rb hello-1.0.0-linux-x86_64/lib/app/ mkdir -p hello-1.0.0-osx/lib/app/ cp hello.rb hello-1.0.0-osx/lib/app/ ``` -------------------------------- ### Deploy Wrapper Script to Packages (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This code segment copies the created wrapper script (`packaging/wrapper.sh`) into each platform-specific package directory and renames it to `hello`. It also makes the wrapper script executable, ensuring that the application can be run directly from the package root. ```Bash editor packaging/wrapper.sh ...edit the file as per above... chmod +x packaging/wrapper.sh cp packaging/wrapper.sh hello-1.0.0-linux-x86/hello cp packaging/wrapper.sh hello-1.0.0-linux-x86_64/hello cp packaging/wrapper.sh hello-1.0.0-osx/hello ``` -------------------------------- ### Execute Packaged Application (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This snippet shows how an end-user would execute a packaged Ruby application after downloading and extracting it. It demonstrates navigating to the extracted directory and running the application's executable. This is the final step for the user to interact with the application. ```Bash /path-to/hello-1.0.0-linux-x86/hello # => hello world ``` -------------------------------- ### Run App with Bundled Ruby Interpreter (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This snippet performs a basic sanity check by executing the 'hello world' Ruby application using the bundled Traveling Ruby interpreter. It navigates into a platform-specific package directory and runs the Ruby script, demonstrating that the bundled interpreter is functional. ```Bash cd hello-1.0.0-osx ./lib/ruby/bin/ruby lib/app/hello.rb # => hello world cd .. ``` -------------------------------- ### Download and Extract Packaged Application (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This snippet illustrates the end-user process of obtaining and preparing a Traveling Ruby application for execution. It involves downloading the application tarball using wget, extracting its contents, and then navigating into the newly created application directory. ```Bash wget hello-1.0.0-linux-x86_64.tar.gz ... tar xzf hello-1.0.0-linux-x86_64.tar.gz cd hello-1.0.0-linux-x86_64 ./hello # => hello world ``` -------------------------------- ### Remove Test and Spec Files from Gems Source: https://github.com/phusion/traveling-ruby/blob/main/REDUCING_PACKAGE_SIZE.md Deletes test, spec, and feature directories from gem installations within the Traveling Ruby package. This helps reduce package size by removing development and testing artifacts. ```shell rm -rf lib/vendor/ruby/*/gems/*/test rm -rf lib/vendor/ruby/*/gems/*/tests rm -rf lib/vendor/ruby/*/gems/*/spec rm -rf lib/vendor/ruby/*/gems/*/features rm -rf lib/vendor/ruby/*/gems/*/benchmark ``` -------------------------------- ### Automate Packaging with Rake (Ruby) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This Rakefile automates the process of packaging a Ruby application for different platforms using Traveling Ruby. It defines tasks for creating packages, downloading the necessary Traveling Ruby runtimes, and assembling the final tar.gz files. Environment variables like DIR_ONLY can modify the build process. ```Ruby PACKAGE_NAME = "hello" VERSION = "1.0.0" TRAVELING_RUBY_VERSION = "20150210-2.1.5" desc "Package your app" task :package => ['package:linux:x86', 'package:linux:x86_64', 'package:osx'] namespace :package do namespace :linux do desc "Package your app for Linux x86" task :x86 => "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86.tar.gz" do create_package("linux-x86") end desc "Package your app for Linux x86_64" task :x86_64 => "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz" do create_package("linux-x86_64") end end desc "Package your app for OS X" task :osx => "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz" do create_package("osx") end end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86.tar.gz" do download_runtime("linux-x86") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz" do download_runtime("linux-x86_64") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz" do download_runtime("osx") end def create_package(target) package_dir = "#{PACKAGE_NAME}-#{VERSION}-#{target}" sh "rm -rf #{package_dir}" sh "mkdir -p #{package_dir}/lib/app" sh "cp hello.rb #{package_dir}/lib/app/" sh "mkdir #{package_dir}/lib/ruby" sh "tar -xzf packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz -C #{package_dir}/lib/ruby" sh "cp packaging/wrapper.sh #{package_dir}/hello" if !ENV['DIR_ONLY'] sh "tar -czf #{package_dir}.tar.gz #{package_dir}" sh "rm -rf #{package_dir}" end end def download_runtime(target) sh "cd packaging && curl -L -O --fail " + "https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz" end ``` -------------------------------- ### Remove Documentation Files from Gems Source: https://github.com/phusion/traveling-ruby/blob/main/REDUCING_PACKAGE_SIZE.md Removes various documentation files like READMEs, ChangeLogs, and entire documentation directories from gem installations. This significantly reduces package size by eliminating non-essential text files. ```shell rm -f lib/vendor/ruby/*/gems/*/README* rm -f lib/vendor/ruby/*/gems/*/CHANGE* rm -f lib/vendor/ruby/*/gems/*/Change* rm -f lib/vendor/ruby/*/gems/*/COPYING* rm -f lib/vendor/ruby/*/gems/*/LICENSE* rm -f lib/vendor/ruby/*/gems/*/MIT-LICENSE* rm -f lib/vendor/ruby/*/gems/*/TODO rm -f lib/vendor/ruby/*/gems/*/*.txt rm -f lib/vendor/ruby/*/gems/*/*.md rm -f lib/vendor/ruby/*/gems/*/*.rdoc rm -rf lib/vendor/ruby/*/gems/*/doc rm -rf lib/vendor/ruby/*/gems/*/docs rm -rf lib/vendor/ruby/*/gems/*/example rm -rf lib/vendor/ruby/*/gems/*/examples rm -rf lib/vendor/ruby/*/gems/*/sample rm -rf lib/vendor/ruby/*/gems/*/doc-api find lib/vendor/ruby -name '*.md' | xargs rm -f ``` -------------------------------- ### Package Application Directories using Tar (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This snippet demonstrates how to create compressed tar archives (.tar.gz) for packaged application directories. It involves creating archives for different platforms (Linux x86, Linux x86_64, OS X) and then removing the original directories. This is a manual step before automation. ```Bash tar -czf hello-1.0.0-linux-x86.tar.gz hello-1.0.0-linux-x86 tar -czf hello-1.0.0-linux-x86_64.tar.gz hello-1.0.0-linux-x86_64 tar -czf hello-1.0.0-osx.tar.gz hello-1.0.0-osx rm -rf hello-1.0.0-linux-x86 rm -rf hello-1.0.0-linux-x86_64 rm -rf hello-1.0.0-osx ``` -------------------------------- ### Remove Miscellaneous Unnecessary Files from Gems Source: https://github.com/phusion/traveling-ruby/blob/main/REDUCING_PACKAGE_SIZE.md Deletes common version control and CI configuration files like .gitignore and .travis.yml from gem installations. These files are not needed for runtime and contribute to unnecessary package bloat. ```shell rm -rf lib/vendor/ruby/*/gems/*/.gitignore rm -rf lib/vendor/ruby/*/gems/*/.travis.yml ``` -------------------------------- ### Download and Extract Traveling Ruby Binaries (Bash) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-1.md This script downloads the Traveling Ruby binaries for Linux x86, Linux x86_64, and OS X from a specified CloudFront URL. It then extracts these binaries into the respective platform-specific package directories, including the necessary Ruby interpreter and libraries. ```Bash mkdir packaging cd packaging curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-20141215-2.1.5-linux-x86.tar.gz curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-20141215-2.1.5-linux-x86_64.tar.gz curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-20141215-2.1.5-osx.tar.gz cd .. mkdir hello-1.0.0-linux-x86/lib/ruby && tar -xzf packaging/traveling-ruby-20141215-2.1.5-linux-x86.tar.gz -C hello-1.0.0-linux-x86/lib/ruby mkdir hello-1.0.0-linux-x86_64/lib/ruby && tar -xzf packaging/traveling-ruby-20141215-2.1.5-linux-x86_64.tar.gz -C hello-1.0.0-linux-x86_64/lib/ruby mkdir hello-1.0.0-osx/lib/ruby && tar -xzf packaging/traveling-ruby-20141215-2.1.5-osx.tar.gz -C hello-1.0.0-osx/lib/ruby ``` -------------------------------- ### Test Windows Package Execution Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-4.md These commands demonstrate how to test a generated Windows package. After extracting the .zip archive to a specified location (e.g., C:\), this sequence shows navigating to the extracted directory in a Windows command prompt and executing the application's wrapper script to verify its functionality. ```Bash C:\Users\Test> cd C:\hello-1.0.0-win32 C:\hello-1.0.0-win32> hello hello Mrs. Mellie Ebert ``` -------------------------------- ### Define Gem Dependencies in Gemfile Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Specifies the Ruby gems required for the application and its development environment. This file is used by Bundler to install and manage gem dependencies. It lists the primary gem ('faker') and development-specific gems ('rake'). ```ruby source 'https://rubygems.org' gem 'faker' group :development do gem 'rake' end ``` -------------------------------- ### Clean Up Ruby Project Files Source: https://context7.com/phusion/traveling-ruby/llms.txt Removes test, spec, documentation, native extension source files, unused encodings, and RDoc from a Ruby project to reduce package size. This script targets specific directories and file patterns commonly found in Ruby gem installations. ```shell # Remove tests and specs rm -rf lib/vendor/ruby/*/gems/*/test rm -rf lib/vendor/ruby/*/gems/*/tests rm -rf lib/vendor/ruby/*/gems/*/spec rm -rf lib/vendor/ruby/*/gems/*/features # Remove documentation rm -f lib/vendor/ruby/*/gems/*/README* rm -f lib/vendor/vendor/ruby/*/gems/*/CHANGE* rm -f lib/vendor/ruby/*/gems/*/LICENSE* rm -f lib/vendor/ruby/*/gems/*/*.md rm -rf lib/vendor/ruby/*/gems/*/doc rm -rf lib/vendor/ruby/*/gems/*/examples # Remove native extension source files rm -f lib/vendor/ruby/*/gems/*/ext/Makefile find lib/vendor/ruby -name '*.c' | xargs rm -f find lib/vendor/ruby -name '*.cpp' | xargs rm -f find lib/vendor/ruby -name '*.h' | xargs rm -f find lib/vendor/ruby -name 'extconf.rb' | xargs rm -f # Remove unused encodings (keep only ASCII/UTF-8) rm -f lib/ruby/lib/ruby/*/*/enc/cp949* rm -f lib/ruby/lib/ruby/*/*/enc/euc_* rm -f lib/ruby/lib/ruby/*/*/enc/shift_jis* rm -f lib/ruby/lib/ruby/*/*/enc/windows* rm -f lib/ruby/lib/ruby/*/*/enc/utf_16* rm -rf lib/ruby/lib/ruby/*/*/enc/trans # Remove RDoc rm -rf lib/ruby/lib/ruby/*/rdoc* # Result: Reduces compressed package size from ~20MB to ~6MB ``` -------------------------------- ### Create Basic Ruby Package (Bash) Source: https://context7.com/phusion/traveling-ruby/llms.txt This snippet demonstrates how to package a simple Ruby application without gem dependencies for Linux. It involves creating a basic Ruby script, setting up the directory structure, downloading and extracting Traveling Ruby binaries, creating a wrapper script for execution, and finally packaging the application into a tar.gz archive. ```bash # Create application mkdir hello_app && cd hello_app echo '#!/usr/bin/env ruby' > hello.rb echo 'puts "hello world"' >> hello.rb # Create package directories for each platform mkdir -p hello-1.0.0-linux-x86_64/lib/app cp hello.rb hello-1.0.0-linux-x86_64/lib/app/ # Download and extract Traveling Ruby binaries mkdir packaging && cd packaging curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-20141215-2.1.5-linux-x86_64.tar.gz cd .. mkdir hello-1.0.0-linux-x86_64/lib/ruby tar -xzf packaging/traveling-ruby-20141215-2.1.5-linux-x86_64.tar.gz -C hello-1.0.0-linux-x86_64/lib/ruby # Create wrapper script cat > packaging/wrapper.sh << 'EOF' #!/bin/bash set -e SELFDIR="`dirname \"$0\"`" SELFDIR="`cd \"$SELFDIR\" && pwd`" exec "$SELFDIR/lib/ruby/bin/ruby" "$SELFDIR/lib/app/hello.rb" "$@" EOF chmod +x packaging/wrapper.sh cp packaging/wrapper.sh hello-1.0.0-linux-x86_64/hello # Package the application tar -czf hello-1.0.0-linux-x86_64.tar.gz hello-1.0.0-linux-x86_64 ``` -------------------------------- ### Automate Traveling Ruby Packaging with Rake Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This snippet outlines the Ruby code for a Rakefile, designed to automate the packaging process for Traveling Ruby applications. It includes tasks for bundling dependencies, handling native extensions, and creating platform-specific package archives. The primary goal is to streamline the build and packaging workflow. ```ruby # Rakefile content would go here, demonstrating task definitions like: task :package => [:package_bundle_install, :package_native_extensions, :package_archives] task :package_bundle_install do # Remove locally compiled native extensions end task :package_native_extensions do # Extract platform-specific native extension binaries end task :package_archives do # Create tar.gz archives for different platforms end ``` -------------------------------- ### Modify Rakefile for Windows Packaging Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-4.md These Ruby code snippets demonstrate modifications to a Rakefile to support Windows packaging. It includes adding a new Rake task for creating Windows (.zip) packages, defining a dependency to download Windows-specific Traveling Ruby binaries, and updating the package creation logic to handle differences between Unix and Windows platforms. ```Ruby namespace :package do ... desc "Package your app for Windows x86" task :win32 => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-win32.tar.gz"] create_package("win32", :windows) end ``` ```Ruby file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-win32.tar.gz" do download_runtime("win32") end ``` ```Ruby def create_package(target, os_type = :unix) ``` ```Ruby # Look for: sh "cp packaging/wrapper.sh #{package_dir}/hello" # Replace it with: if os_type == :unix sh "cp packaging/wrapper.sh #{package_dir}/hello" else sh "cp packaging/wrapper.bat #{package_dir}/hello.bat" end ``` ```Ruby # Look for: sh "tar -czf #{package_dir}.tar.gz #{package_dir}" # Replace it with: if os_type == :unix sh "tar -czf #{package_dir}.tar.gz #{package_dir}" else sh "zip -9r #{package_dir}.zip #{package_dir}" end ``` ```Ruby task :package => ['package:linux:x86', 'package:linux:x86_64', 'package:osx', 'package:win32'] ``` -------------------------------- ### Build Binary Packages with Rake Source: https://github.com/phusion/traveling-ruby/blob/main/osx/README.md This command initiates the build process for binary packages on macOS. It assumes you are in the 'osx' directory and have all system requirements met. No specific inputs are required, and the output is the compiled binary package. ```bash cd osx rake ``` -------------------------------- ### Download and Extract Precompiled Native Extensions for Linux x86 Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This sequence of commands downloads and extracts the precompiled sqlite3 native extension for a Linux x86 platform into the package's vendor directory. It uses `curl` to fetch the tarball and `tar` to extract its contents, followed by removing the downloaded archive. ```bash cd hello-1.0.0-linux-x86/lib/vendor/ruby curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-gems-20141215-2.1.5-linux-x86/sqlite3-1.3.9.tar.gz tar xzf sqlite3-1.3.9.tar.gz rm sqlite3-1.3.9.tar.gz cd ../../../.. ``` -------------------------------- ### Create Windows Wrapper Script (Batch) Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-4.md This Batch script serves as the Windows equivalent of a Unix shell script for your Ruby application. It sets necessary environment variables for Bundler and executes the bundled Ruby interpreter with your application, ensuring proper gem and application path resolution on Windows. ```Batch @echo off :: Tell Bundler where the Gemfile and gems are. set "BUNDLE_GEMFILE=%~dp0\lib\vendor\Gemfile" set BUNDLE_IGNORE_CONFIG= :: Run the actual app using the bundled Ruby interpreter, with Bundler activated. @"%~dp0\lib\ruby\bin\ruby.bat" -rbundler/setup "%~dp0\lib\app\hello.rb" ``` -------------------------------- ### List Available Rake Tasks Source: https://github.com/phusion/traveling-ruby/blob/main/osx/README.md This command displays all available tasks within the Rake build system. It's useful for understanding the full scope of build operations. The output is a list of task names and their descriptions. ```bash rake -T ``` -------------------------------- ### Download and Extract Precompiled Native Extensions for OS X Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This command sequence downloads and extracts the precompiled sqlite3 native extension for macOS (OS X) into the package's vendor directory. It uses `curl` to fetch the archive and `tar` to extract its contents, then removes the downloaded tarball. This ensures the native extension is correctly placed for the target OS. ```bash cd hello-1.0.0-osx/lib/vendor/ruby curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-gems-20141215-2.1.5-osx/sqlite3-1.3.9.tar.gz tar xzf sqlite3-1.3.9.tar.gz rm sqlite3-1.3.9.tar.gz cd ../../../.. ``` -------------------------------- ### Define Ruby Functions for Package Creation Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md These Ruby functions are used by the Rake tasks to create application packages. They handle creating directories, copying files, downloading runtime environments, and including native extensions. ```ruby def create_package(target) package_dir = "#{PACKAGE_NAME}-#{VERSION}-#{target}" sh "rm -rf #{package_dir}" sh "mkdir #{package_dir}" sh "mkdir -p #{package_dir}/lib/app" sh "cp hello.rb #{package_dir}/lib/app/" sh "mkdir #{package_dir}/lib/ruby" sh "tar -xzf packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz -C #{package_dir}/lib/ruby" sh "cp packaging/wrapper.sh #{package_dir}/hello" sh "cp -pR packaging/vendor #{package_dir}/lib/" sh "cp Gemfile Gemfile.lock #{package_dir}/lib/vendor/" sh "mkdir #{package_dir}/lib/vendor/.bundle" sh "cp packaging/bundler-config #{package_dir}/lib/vendor/.bundle/config" sh "tar -xzf packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}-sqlite3-#{SQLITE3_VERSION}.tar.gz " + "-C #{package_dir}/lib/vendor/ruby" if !ENV['DIR_ONLY'] sh "tar -czf #{package_dir}.tar.gz #{package_dir}" sh "rm -rf #{package_dir}" end end def download_runtime(target) sh "cd packaging && curl -L -O --fail " + "https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz" end def download_native_extension(target, gem_name_and_version) sh "curl -L --fail -o packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}-#{gem_name_and_version}.tar.gz " + "https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-gems-#{TRAVELING_RUBY_VERSION}-#{target}/#{gem_name_and_version}.tar.gz" end ``` -------------------------------- ### Define Rake Tasks for Packaging Application Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-3.md This Rakefile defines tasks for packaging a Ruby application using Traveling Ruby. It includes tasks for different operating systems (Linux x86, x86_64, OS X) and dependencies like bundler and native extensions. ```ruby require 'bundler/setup' PACKAGE_NAME = "hello" VERSION = "1.0.0" TRAVELING_RUBY_VERSION = "20150210-2.1.5" SQLITE3_VERSION = "1.3.9" # Must match Gemfile desc "Package your app" task :package => ['package:linux:x86', 'package:linux:x86_64', 'package:osx'] namespace :package do namespace :linux do desc "Package your app for Linux x86" task :x86 => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86.tar.gz", "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86-sqlite3-#{SQLITE3_VERSION}.tar.gz" ] do create_package("linux-x86") end desc "Package your app for Linux x86_64" task :x86_64 => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz", "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64-sqlite3-#{SQLITE3_VERSION}.tar.gz" ] do create_package("linux-x86_64") end end desc "Package your app for OS X" task :osx => [:bundle_install, "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz", "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx-sqlite3-#{SQLITE3_VERSION}.tar.gz" ] do create_package("osx") end desc "Install gems to local directory" task :bundle_install do if RUBY_VERSION !~ /^2\.1\./ abort "You can only 'bundle install' using Ruby 2.1, because that's what Traveling Ruby uses." end sh "rm -rf packaging/tmp" sh "mkdir packaging/tmp" sh "cp Gemfile Gemfile.lock packaging/tmp/" Bundler.with_clean_env do sh "cd packaging/tmp && env BUNDLE_IGNORE_CONFIG=1 bundle install --path ../vendor --without development" end sh "rm -rf packaging/tmp" sh "rm -f packaging/vendor/*/*/cache/*" sh "rm -rf packaging/vendor/ruby/*/extensions" sh "find packaging/vendor/ruby/*/gems -name '*.so' | xargs rm -f" sh "find packaging/vendor/ruby/*/gems -name '*.bundle' | xargs rm -f" sh "find packaging/vendor/ruby/*/gems -name '*.o' | xargs rm -f" end end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86.tar.gz" do download_runtime("linux-x86") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64.tar.gz" do download_runtime("linux-x86_64") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx.tar.gz" do download_runtime("osx") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86-sqlite3-#{SQLITE3_VERSION}.tar.gz" do download_native_extension("linux-x86", "sqlite3-#{SQLITE3_VERSION}") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-linux-x86_64-sqlite3-#{SQLITE3_VERSION}.tar.gz" do download_native_extension("linux-x86_64", "sqlite3-#{SQLITE3_VERSION}") end file "packaging/traveling-ruby-#{TRAVELING_RUBY_VERSION}-osx-sqlite3-#{SQLITE3_VERSION}.tar.gz" do download_native_extension("osx", "sqlite3-#{SQLITE3_VERSION}") end ``` -------------------------------- ### Execute Ruby Application with Bundler Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Runs the Ruby application using the Bundler-managed gem environment. This verifies that the application executes correctly and that gem dependencies are loaded as expected. ```bash bundle exec ruby hello.rb ``` -------------------------------- ### Create Bundler Configuration for Packaged Gems Source: https://github.com/phusion/traveling-ruby/blob/main/TUTORIAL-2.md Sets up the Bundler configuration file (`.bundle/config`) within the packaged gem directories. This configuration tells Bundler where to find gems (`BUNDLE_PATH: .`), which groups to ignore (`BUNDLE_WITHOUT: development`), and to disable shared gems (`BUNDLE_DISABLE_SHARED_GEMS: '1'`). ```bash mkdir hello-1.0.0-linux-x86/lib/vendor/.bundle mkdir hello-1.0.0-linux-x86_64/lib/vendor/.bundle mkdir hello-1.0.0-osx/lib/vendor/.bundle cp packaging/bundler-config hello-1.0.0-linux-x86/lib/vendor/.bundle/config cp packaging/bundler-config hello-1.0.0-linux-x86_64/lib/vendor/.bundle/config cp packaging/bundler-config hello-1.0.0-osx/lib/vendor/.bundle/config ```