require 'bundler' PACKAGE_NAME = '<%= options[:name] %>' VERSION = '0.1.0' RUBY_V = '2.2' TRAVELING_RUBY_VERSION = "20150715-#{RUBY_V}.2" 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 do create_package('osx') end end def create_package(target) if RUBY_VERSION !~ /^#{RUBY_V}\./ abort "You can only use Ruby #{RUBY_V}, because that's what Traveling Ruby uses." end package_dir = "#{PACKAGE_NAME}-#{VERSION}-#{target}" sh "rm -rf #{package_dir}" sh "rm -rf tmp/" sh "mkdir tmp" sh "mkdir -p #{package_dir}/lib/ruby" sh "rsync -a src #{package_dir}/lib" download_runtime('osx') sh "tar -xzf tmp/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz -C #{package_dir}/lib/ruby" create_wrapper("#{package_dir}/#{PACKAGE_NAME}") sh "chmod 755 #{package_dir}/#{PACKAGE_NAME}" bundle_install sh "cp -pR tmp/vendor #{package_dir}/lib/" sh "cp src/Gemfile src/Gemfile.lock #{package_dir}/lib/vendor/" sh "mkdir #{package_dir}/lib/vendor/.bundle" sh "cp tmp/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 sh "rm -rf tmp/" end def download_runtime(target) sh "cd tmp && curl -L -O --fail https://d6r77u77i8pq3.cloudfront.net/releases/traveling-ruby-#{TRAVELING_RUBY_VERSION}-#{target}.tar.gz" end def bundle_install sh "cp src/Gemfile src/Gemfile.lock tmp/" Bundler.with_clean_env do sh "cd tmp && env BUNDLE_IGNORE_CONFIG=1 bundle install --path vendor --without development" <% if options[:development] -%> sh "cd tmp && env BUNDLE_IGNORE_CONFIG=1 bundle package --all" sh "mv tmp/vendor/cache/neutron tmp/vendor/ruby/#{RUBY_V}.0/gems" <% end -%> end sh "rm -rf tmp/vendor/cache" sh "rm -rf tmp/vendor/ruby/#{RUBY_V}.0/cache" bundle_config = <<-CONFIG BUNDLE_PATH: . BUNDLE_WITHOUT: development BUNDLE_DISABLE_SHARED_GEMS: '1' CONFIG File.open('tmp/bundler-config', 'w') {|f| f << bundle_config.gsub(/^\s{4}/, '') } end def create_wrapper(out) wrapper = <<-WRAPPER #!/bin/bash set -e SELFDIR="`dirname \"$0\"`" SELFDIR="`cd \"$SELFDIR\" && pwd`" export BUNDLE_GEMFILE="$SELFDIR/lib/vendor/Gemfile" unset BUNDLE_IGNORE_CONFIG exec "NODE_ENV=production $SELFDIR/lib/ruby/bin/ruby" -rbundler/setup "$SELFDIR/lib/src/main.rb" WRAPPER File.open(out, 'w') {|f| f << wrapper.gsub(/^\s{4}/, '') } end