Vendoring, unpacking, freezing gems in ruby not rails no bundler

I’ve been writing a lot of utility scripts lately using CocoaDialog and its ruby wrapper to produce nice dialogs and Platypus to package them as simple Mac droplets.

Since the users of the droplets will not have any gems installed, they need to be unpacked and added to your project:

gem unpack NAME

Be sure to do this for all dependencies of the given gem(s). Bundler helps quite a bit in figuring out what those are (e.g. bundle package), however bundle package provides you the .gem files and not the actual ruby scripts, so I use gem unpack

Next, put the unpacked gems within your project, e.g. vendor/gems and add this to your script:

1
2
3
4
Dir.glob(File.join(File.dirname(__FILE__), "vendor", "gems", "*", "lib")).each do |lib|
$LOAD_PATH.unshift File.expand_path(lib)
end
require 'GEMNAME' # they are in your load path now!</code></p>

I do NOT require rubygems in these scripts, however if you will be doing this, or you suspect a gem might be doing this, you’ll want to prepare in advance, with a slight mod to the above:

1
2
3
4
5
6
require 'rubygems'
Dir.glob(File.join(File.dirname(__FILE__), "vendor", "gems", "*", "lib")).each do |lib|
$LOAD_PATH.unshift File.expand_path(lib)
Gem.path.unshift File.expand_path(lib)
end
require 'GEMNAME' # they are in your load path now! They'll also be known to rubygems within the context of your application</code></p>