如何从 ruby 与 RVM 交互?
我正在尝试获取有关 RVM 内已安装的 ruby 版本以及关联的 gemset 和 gems 的信息。
我的第一个想法是使用对 rvm list
的系统调用来获取已安装的 Rubies,并且 rvm use #{ruby_version} &&每个 Ruby 的 rvm gemset 列表
。但这有一个问题。看起来 rvm use #{ruby_version}
已被 RVM 执行并确认(输出 Using #{path_to_ruby
),但列出的 gemset 是运行的 Ruby 版本中的 gemset脚本。
有没有办法通过 CLI 或 API 从 Rubyscript 与 RVM 进行通信?
I am trying to get informations about the installed versions of ruby inside RVM and the associated gemsets and gems.
My first idea was to use a system call to rvm list
to get the installed Rubies and rvm use #{ruby_version} && rvm gemset list
for every Ruby. But there is an issue with that. It seems that rvm use #{ruby_version}
is executed and confirmed by RVM (output Using #{path_to_ruby
), but the gemsets listed are the one from the Ruby version that runs the script.
Is there any way to communicate with RVM from a Rubyscript via CLI or an API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试一下 - 我认为你的问题是 rvm 主要通过修改你的环境变量来将你的 shell 指向不同的 gemset 和 ruby 版本。
但是,当您在子shell中运行
rvm use
时,环境变量的更改不会传播到父shell。还没有对此进行太多研究,我最初的想法是让您运行
rvm use
东西 + 然后在同一个子 shell 会话中运行列出所有这些更新的环境变量的内容的东西(请参阅此处您需要查看哪些内容:http://beginrescueend.com/rvm/info/ ) ...然后在您的 ruby 脚本需要设置环境以匹配子 shell 中的环境。在 shellspeak 中,在这种情况下您通常所做的是“获取”脚本而不是执行它。即
源“the_script_that_sets_environment_variables”
。但是,当您在 ruby 脚本中并使用反引号在子 shell 中运行内容时,如果不手动执行,则无法将环境传播回父级。另一种解决方案可能是查看RVM Ruby API。我还没有找到太多关于它的文档,但它也可能适合您的情况:
http://www.rubyflow.com/items/4285
I'll take a stab at this - I think your problem is that rvm works mostly by mucking around with your environmental variables to point your shell to the different gemsets and ruby versions.
But when you run
rvm use
in a subshell the changes of the env variables are not propagated up to the parent shell.Without having looked into this too much yet, my initial idea would for you to run the
rvm use
thing + then in that same subshell session run something that lists the contents of all these updated env vars (see here for which ones you need to look at: http://beginrescueend.com/rvm/info/ )...then in your ruby script you need to set the environment to match the environment in your subshell.In shellspeak what you usually do in a case like this is "source" the script instead of executing it. I.e.
source "the_script_that_sets_environment_variables"
. But when you are in a ruby script and use backticks to run stuff in a subshell you can't propagate the environment back up to the parent without doing it manually.Another solution might be to take a look at the RVM Ruby API. I didn't find much documentation on it yet, but it might also do the trick for your case:
http://www.rubyflow.com/items/4285