运行脚本时如何让 xcode 使用正确版本的 ruby​​?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15776319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:06:49  来源:igfitidea点击:

How to make xcode use the correct version of ruby when running a script?

rubyxcodervm

提问by Mark Wheeler

In xcode I have a 'run script' build phase that runs a ruby script. However, it seems that xcode is trying to run it using the default mac 1.8 version of ruby rather than the latest version. Given that the script requires a gem, it's failing with a require error and the path in the error points to /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/

在 xcode 中,我有一个运行 ruby​​ 脚本的“运行脚本”构建阶段。但是,似乎 xcode 正在尝试使用默认的 mac 1.8 版本的 ruby​​ 而不是最新版本来运行它。鉴于脚本需要一个 gem,它失败并出现 require 错误,并且错误中的路径指向 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/

Obviously the 1.8 in that path is making me suspicious.

显然,该路径中的 1.8 使我怀疑。

If I open up terminal and run the command ruby -vthen it correctly returns 2.0.0p0which I installed and set as default using RVM.

如果我打开终端并运行命令,ruby -v那么它会正确返回2.0.0p0我使用 RVM 安装并设置为默认值的内容。

How might I get Xcode to look in the right place? Or am I mis-interpreting this error?

我怎样才能让 Xcode 找到正确的位置?还是我误解了这个错误?

Update:

更新:

To give a little more info, here is the exact error that the compiler is throwing:

为了提供更多信息,这里是编译器抛出的确切错误:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- json (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /Volumes/Macintosh HD/Documents/Projects/WesternMusicElements/WesternMusicElements/Ruby/NoteCollectionParser.rb:9
Command /bin/sh failed with exit code 1

回答by zadr

If you have Xcode print out the environment variables, IIRC, the $PATHset by Xcode is different from the $PATHthat you load in a regular terminal session.

如果您让 Xcode 打印出环境变量 IIRC,则$PATHXcode 设置的设置与$PATH您在常规终端会话中加载的不同。

This leaves you with (at least) two ways to go about fixing the problem:

这为您提供了(至少)两种解决问题的方法:

  1. You can edit your script to point to the rvm-installed ruby directly. This might be better if your script will have to work on multiple systems (even if it does go against how rvm promises to work).

  2. According to Peter Hosey's comment on Chris Hanson's answer in thisquestion, you can set environment variables in ~/.MacOSX/environment.plistthat will apply to all processes you launch.

  1. 您可以编辑脚本以直接指向 rvm 安装的 ruby​​。如果您的脚本必须在多个系统上工作,这可能会更好(即使它违背了 rvm 承诺的工作方式)。

  2. 根据 Peter Hosey 对 Chris Hanson 在这个问题中的回答的评论,您可以设置环境变量,~/.MacOSX/environment.plist这将适用于您启动的所有进程。

回答by Clay Bridges

Assuming you are specifying your rvm ruby versioning in your project (and I think you should be), I think this is a more generally applicable answer than the present ones.

假设您在项目中指定了 rvm ruby​​ 版本控制(我认为您应该这样做),我认为这是一个比目前更普遍适用的答案。

In broad strokes, in your shell script, you should

概括地说,在你的 shell 脚本中,你应该

# provides the rvm *function* to your shell script 
source "${SOMETHING}/scripts/rvm"

# then, for this script, the rvm function will change to
# the controlling ruby version for the pwd when this is run
rvm use .

I cover this in more production-ready detail in this answer.

我在这个答案中更详细地介绍了这一点。

回答by Cyril Cermak

You can also do rvm use systemwhich switches to default mac os ruby and then install the gems with sudo. Seemed to be the easiest for me.

您还rvm use system可以切换到默认的 mac os ruby​​,然后使用 sudo 安装 gem。对我来说似乎是最简单的。

回答by amacleod

I had a similar situation, but the script being run had a shebang line referring to the system-installed Ruby.

我遇到了类似的情况,但是正在运行的脚本有一行指向系统安装的 Ruby。

#!/usr/bin/ruby

My solution, which might be dangerous, was to replace /usr/bin/rubywith a symlink to the Ruby installed by RVM.

我的解决方案可能很危险,是/usr/bin/ruby用指向 RVM 安装的 Ruby 的符号链接替换。

Here are the steps I took:

以下是我采取的步骤:

  1. Install multi-user RVM (the sudooption from https://rvm.io/rvm/install)
  2. Add myself to the rvmgroup with the Settings app (Settings > Users and Groups).
  3. Log out and back in to ensure group membership is respected by the OS.
  4. Install the gems I needed from the command line. Since I'm now a RVM manager by virtue of being in the rvmgroup, this affects the stuff under /usr/local/rvm.
  5. Move /usr/bin/rubyout of the way and symlink the RVM default Ruby in its place. (This is the dangerous part. It might lead to issues if programs assume they'll be using the system-installed version.)
  1. 安装多用户 RVM(sudo来自https://rvm.io/rvm/install的选项)
  2. rvm使用设置应用程序(设置 > 用户和组)将我自己添加到组中。
  3. 注销并重新登录以确保操作系统尊重组成员身份。
  4. 从命令行安装我需要的 gem。由于我现在是组中的 RVM 经理rvm,这会影响/usr/local/rvm.
  5. 移动/usr/bin/ruby出的方式和符号链接默认RVM红宝石在它的位置。(这是危险的部分。如果程序假定它们将使用系统安装的版本,则可能会导致问题。)

To replace the symlink, I did:

为了替换符号链接,我做了:

sudo mv /usr/bin/ruby /usr/bin/system_ruby
sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby

After doing this (while Xcode was not running), the next time I built the project in Xcode, the script used the Ruby I had installed with RVM.

执行此操作后(当 Xcode 未运行时),下次我在 Xcode 中构建项目时,脚本使用了我与 RVM 一起安装的 Ruby。

回答by chb

I used this to run Xcode with an interpreter other than the default system interpreter:

我用它来运行带有默认系统解释器以外的解释器的 Xcode:

  1. Quit Xcode if it's open and go to your terminal.
  2. Activate the rvm you want to use.
    rvm use 2.0.0p0
  3. Then open Xcode from the terminal:
    open -a Xcode
  1. 如果 Xcode 处于打开状态,请退出并转到您的终端。
  2. 激活您要使用的 rvm。
    rvm use 2.0.0p0
  3. 然后从终端打开 Xcode:
    open -a Xcode

To test this, you can include something in your build that logs the interpreter's version, or available gems.

要对此进行测试,您可以在构建中包含一些记录解释器版本或可用 gem 的内容。

回答by RicardoDuarte

I had this problem also, as i wanted to use xcpretty. I end up doing the following below to point at the correct gem folder

我也有这个问题,因为我想使用 xcpretty。我最终执行以下操作以指向正确的 gem 文件夹

$RUBYPATH=$(eval ~/.rvm/bin/rvm info | grep "GEM_HOME:" | awk -F '"' '{print }')

whatever gem you want will be in this folder.

你想要的任何 gem 都会在这个文件夹中。

Also if you just want you ruby home path:

另外,如果您只想要 ruby​​ 主路径:

$RUBYPATH=$(eval ~/.rvm/bin/rvm info | grep "MY_RUBY_HOME:" | awk -F '"' '{print }')

xcpretty was under the first option in bin/ in my scenario as bellow. So for me i end up doing the following:

在我的场景中,xcpretty 在 bin/ 中的第一个选项下,如下所示。所以对我来说,我最终做了以下事情:

$RUBYPATH=$(eval ~/.rvm/bin/rvm info | grep "GEM_HOME:" | awk -F '"' '{print }')
RUBY_BIN_PATH=${RUBY_PATH}"/bin/" 
XCPRETTY=$RUBY_BIN_PATH"xcpretty"
eval $XCPRETTY

You can find out where your ruby command is beforehand by doing which

您可以通过执行 which 预先找出您的 ruby​​ 命令的位置