Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using gist to achieve self contained bundled gems within a single ruby script
require 'tmpdir'
require 'fileutils'
# scope bundling for this script to a directory based on its path
tmpdir = "#{Dir.tmpdir}/script_bundler/#{File.expand_path(__FILE__).gsub(/\W+/, '_')}"
FileUtils.mkdir_p(tmpdir)
gemfile = "#{tmpdir}/Gemfile"
gemfile_lock = "#{gemfile}.lock"
FileUtils.rm_f(gemfile_lock) if ENV["FORCE_BUNDLE_INSTALL"]
needs_bundle_install ||= ! File.exist?(gemfile_lock)
needs_bundle_install ||= File.read(gemfile) != gemfile_contents
if needs_bundle_install
File.open(gemfile, "w") do |f|
f.write(gemfile_contents)
end
end
ENV['BUNDLE_GEMFILE'] = gemfile
begin
require "bundler"
rescue LoadError
puts "Installing bundler..."
system "gem install --quiet bundler"
exec $0, *ARGV
end
if needs_bundle_install
puts "Bundle installing gems..."
Bundler::Installer.install(Bundler.root, Bundler.definition, :system => true)
end
Bundler.setup(:default)
Bundler.require(:default)
#!/usr/bin/env ruby
# Add your gems here
#
gemfile_contents = <<-EOF
source "http://rubygems.org"
gem "some-gem"
EOF
require 'open-uri'
eval open('https://raw.github.com/gist/1770601/gist-bundler.rb').read, binding
# Your code goes here
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.