Created
February 8, 2012 15:53
-
-
Save wr0ngway/1770601 to your computer and use it in GitHub Desktop.
Using gist to achieve self contained bundled gems within a single ruby script
This file contains 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 '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) |
This file contains 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
#!/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