Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created August 31, 2014 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthutchinson/f2d1346a62651c5a9b5c to your computer and use it in GitHub Desktop.
Save matthutchinson/f2d1346a62651c5a9b5c to your computer and use it in GitHub Desktop.
Babushka shell command patched to work with rbenv and .ruby-version
# rbenv shell helper patch
module Babushka
module ShellHelpers
# Run +cmd+ via #shell, raising an exception if it doesn't exit
# with success.
def shell! *cmd, &block
opts = cmd.extract_options!
cmd = cmd.first if cmd.map(&:class) == [Array]
if opts[:cd]
if !opts[:cd].p.exists?
if opts[:create]
opts[:cd].p.mkdir
else
raise Errno::ENOENT, opts[:cd]
end
elsif !opts[:cd].p.dir?
raise Errno::ENOTDIR, opts[:cd]
end
# prefix cmd with rbenv init if ./ruby-version exists in path
cmd[0] = "#{rbenv_prefix(opts[:cd])} #{cmd[0]}"
end
shell_method = (opts[:as] || opts[:sudo]) ? :sudo : :shell_cmd
send shell_method, *cmd.dup.push(opts), &block
end
private
def rbenv_version_file(path)
"#{path}/.ruby-version".p
end
def rbenv_prefix(path)
if rbenv_version_file(path).exists?
"eval \"$(rbenv init -)\" && RBENV_VERSION=#{rbenv_version_file(path).read}"
end
end
end
end
@matthutchinson
Copy link
Author

Reads a .ruby-version file from working shell cmd directory (if it exists), and prepends the shell command with;

eval "$(rbenv init -)" && RBENV_VERSION=2.1.2

When running the shell will be properly setup to use the ruby (or gems) that have been installed with rbenv. This can be required in deps with something like;

require_relative 'lib/babushka_ext/shell_helpers'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment