Created
August 31, 2014 19:50
-
-
Save matthutchinson/f2d1346a62651c5a9b5c to your computer and use it in GitHub Desktop.
Babushka shell command patched to work with rbenv and .ruby-version
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reads a
.ruby-version
file from working shell cmd directory (if it exists), and prepends the shell command with;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;