简体   繁体   English

在bash之外的rake任务中使用rbenv和bundler

[英]Using rbenv & bundler in rake tasks, outside bash

I'm trying to use rbenv (with plugins) & bundler to bulletproof some of our rake tasks. 我正在尝试使用rbenv(带有插件)和捆绑程序来防弹一些我们的耙子任务。 However, the rbenv shimmed versions of commands (of eg bundle ) depend on first running this (often found in .bashrc or its equal): 但是,命令的rbenv匀场版本(例如bundle )取决于首先运行此命令(通常在.bashrc或其等同版本中找到):

eval "$(rbenv init -)"

This expands to shell commands which makes rbenv and plugins work correctly in the shell. 这扩展到了shell命令,这使rbenv和插件在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. 这也意味着在我的计算机上的交互式bash shell中正常运行的rake任务通常无法正常工作,例如从/ bin / sh或另一台计算机上触发。

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. 我想做的是在rake中为shell调用一次,然后继续使用该shell在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. 我也乐于接受“您做错了”的战略重设,以模块方式避开rvm与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: 为了回答以下@konsolebox的问题,我可能想在rake文件中使用的ruby函数可能看起来像这样:

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. 这样可以确保command可以在正确的rbenv上下文中运行,而不必依赖于点初始化文件。 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. 如果愿意,甚至可以将该脚本放置在/ usr / local / bin中,以使用$ PATH进行搜索。

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. 您也可以根据自己的喜好,将eval "$(rbenv init -)"放在〜/ .profile或〜/ .bashrc之类的启动文件中,但是我不确定这对于其他与非Ruby相关的应用程序是否安全。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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