2

I have a standard gem scaffold, and in inside the bin directory I have a simple executable (bin/do_things.rb):

#!/usr/bin/env ruby
require 'my_gem'
MyGem::doThings()

The gem hasn't been installed via gem install, so running bin/do_things.rb without bundle exec fails (my Gemfile has the gemspec line in it).

Is there a simple way to have rubygems-bundler execute my script in the bundler context? Should I just modify the $LOAD_PATH in my executable instead?

I could create a wrapper script to execute under Bundler as well, but I'd rather leverage rubygems-bundler somehow.

Or should I just type bundle exec?

Dan
  • 1,729
  • 1
  • 18
  • 25
  • Can you clarify the purpose of your script? Is it something you use only in development or something that users of your gem use? In any case, I don't recommend modifying your load path directly in the executable. – Tim Moore May 27 '13 at 10:18
  • When installed, the purpose of the gem executable would be to do some shell utility stuff--in my case move some files around. It'd be accessible by users after install. – Dan May 27 '13 at 15:53

3 Answers3

4

try:

bundle exec bash -l

it will set you into bundler context - it's an extra shell so running exit will get you back to bundler less context.

mpapis
  • 52,729
  • 14
  • 121
  • 158
  • Thanks! This is exactly what I was looking for. No modifications necessary to the script. This worked for me: `bundle exec zsh -l` – Dan May 27 '13 at 15:55
  • Also, on OSX, I had to take out the first `bash` for it to work: `bundle exec bash -l` – Dan May 27 '13 at 15:56
2

Change the file permission

chmod a+x bin/do_things.rb

and resolve bin path, ignoring symlinks

require "pathname"
bin_file = Pathname.new(__FILE__).realpath

post, add self to libpath

$:.unshift File.expand_path("../../lib", bin_file)
Pravin Mishra
  • 8,298
  • 4
  • 36
  • 49
2

Generating binstubs via bundle install --binstubs creates a wrapper script for all executables listed in my gemspec.

#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'do_things.rb' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('my_gem', 'do_things.rb')

However, by default, the binstubs path is bin, which conflicts with gem's executable path, and will overwrite files in bin.

Running bundle install --binstubs=SOME_DIR and then adding SOME_DIR to .gitignore seem to be the most maintainable way.

Then, I can simple execute SOME_DIR/do_things or any other project-specific executable I add down the line.

Dan
  • 1,729
  • 1
  • 18
  • 25