0

I'm trying to use rbenv (with plugins) & bundler to bulletproof some of our rake tasks. However, the rbenv shimmed versions of commands (of e.g. bundle) depend on first running this (often found in .bashrc or its equal):

eval "$(rbenv init -)"

This expands to shell commands which makes rbenv and plugins work correctly in the shell. It also means that rake tasks that work correctly in an interactive bash shell on my machine, won't work generally, e.g. evoked from /bin/sh or on another machine.

What I'd like to do is invoke this once for a shell in rake, and then continue to use that shell to invoke other commands in rake. Is that possible?

If not, can I copy envariables & shell functions resulting from one shell to a subsequent shells, in series?

Or am I left with either something like

eval "$(rbenv init -)" && other_command

or

rbenv exec bundle exec other_command

(The latter of which seemed to have its own problems).

I'm also open to "you're doing it wrong" strategic resets, modulo dodging rvm vs. rbenv jihads.

Update:

In answer to @konsolebox's question below, a ruby function I might want to use in my rake file might look something like this:

def rbenv_sh(command)
    %x(eval "$(rbenv init -)" && #{command})
end

That would insure that command would run in the correct rbenv context, without depending on dot-initialization files. I'm just looking for something better than that.

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118

1 Answers1

0

Do you perhaps need a script to run other comands?

#!/bin/sh
eval "$(rbenv init -)"
other commands

Or do you want to have a script that would accept arguments to execute:

#!/bin/sh
eval "$(rbenv init -)"
"$@"

Which you could run as

sh script.sh other commands

Or

chmod 755 script.sh
./script.sh other commands

And even place that script in /usr/local/bin to be searchable with $PATH if you like.

At your own preference you could also place eval "$(rbenv init -)" in startup files like ~/.profile or ~/.bashrc but I'm not sure if that would be safe to other non-ruby related applications.

Update:

How about storing the output on a variable first:

@@rbenv_init = %x(rbenv init -)

def rbenv_sh(command)
     %x(#{@@rbenv_init}\n#{command})
end
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Is that more-or-less equivalent to the `&&` option I mentioned? Going that route of running the `rbenv init` on each shell, I could wrap it with a ruby fn, e.g. `def rbenv_sh(command)`, and save the extra script file. – Clay Bridges Sep 19 '13 at 17:00
  • I'm sorry but I don't understand how the ruby fn could help. Aren't you running commands from a shell? Or perhaps I don't follow much. Can you elaborate? If `eval` run multiple commands that doesn't return quickly on error, using `&&` won't give much difference for it would only mind about the last command that changes `$?`. Note that `&&` only executes the command in front of it if `$?` is 0. – konsolebox Sep 19 '13 at 17:04
  • @ClayBridges I made a suggestion as well. – konsolebox Sep 20 '13 at 20:55
  • Good suggestion, though `&&` parses better for me. – Clay Bridges Sep 30 '13 at 22:56