简体   繁体   中英

Using rbenv & bundler in rake tasks, outside bash

I'm trying to use rbenv (with plugins) & bundler to bulletproof some of our rake tasks. However, the rbenv shimmed versions of commands (of eg 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, eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM