怎么修改 Xcode Run Script 使用 RVM 管理的 Ruby 版本?

Xcode Run Script 使用的 Ruby 版本是 macOS 自带的 Ruby,如果你用 RVM 切换了别的 Ruby 版本,Xcode 使用的 Ruby 还是系统自带的,不会有变化。
那么这会造成什么影响呢?

比如你用 RVM 管理的 Ruby 通过 gem 安装了在 Xcode Run Script 中需要使用的 Sass,那么在 Xcode 中 Sass 命令将执行失败,你会得到 command not found: sass 或如下错误:

1
2
3
4
5
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe': can't find gem sass (>= 0.a) with executable sass (Gem::GemNotFoundException)
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
from /usr/local/bin/sass:23:in `<main>'
from /usr/local/bin/ruby_executable_hooks:22:in `eval'
from /usr/local/bin/ruby_executable_hooks:22:in `<main>'

如果你不清楚这些,你就很难搞清楚你明明安装了 Sass,而且在终端可以执行,却为什么在 Xcode 中却不行。

怎么知道 Xcode 默认只会使用系统自带的 Ruby?

  1. 先通过 RVM切换 Ruby 版本。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:11:43] 
    $ rvm list
    ruby-2.3.8 [ x86_64 ]
    ruby-2.5.8 [ x86_64 ]
    ruby-2.6.6 [ x86_64 ]
    ruby-2.7.2 [ x86_64 ]

    # Default ruby not set. Try 'rvm alias create default <ruby>'.

    # => - current
    # =* - current && default
    # * - default

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:14:31]
    $ rvm use 2.7.2 --default
    Using /Users/ganzhixiong/.rvm/gems/ruby-2.7.2

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:16:33]
    $ ruby -v
    ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin18]

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:16:41]
    $ gem -v
    3.1.4
  2. 再编辑 Xcode Shell 脚本如下:

  3. 你会发现在 Build 里面输出的 Ruby 和 gem 版本不是刚切换的版本。

    1
    2
    ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]
    3.0.3.1

怎么让 Xcode 可以执行 Sass 命令?

假设你的系统从未安装过 Sass。

  1. 你应该会尝试先在 Ruby 2.7 下安装 Sass。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:35:08] C:1
    $ ruby -v
    ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin18]

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:36:13]
    $ gem install sass
    Fetching sass-3.7.4.gem

    Ruby Sass has reached end-of-life and should no longer be used.

    * If you use Sass as a command-line tool, we recommend using Dart Sass, the new
    primary implementation: https://sass-lang.com/install

    * If you use Sass as a plug-in for a Ruby web framework, we recommend using the
    sassc gem: https://github.com/sass/sassc-ruby#readme

    * For more details, please refer to the Sass blog:
    https://sass-lang.com/blog/posts/7828841

    Successfully installed sass-3.7.4
    Parsing documentation for sass-3.7.4
    Installing ri documentation for sass-3.7.4
    Done installing documentation for sass after 2 seconds
    1 gem installed

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:36:50]
    $ sass -v
    Ruby Sass 3.7.4
  2. 然后你在 Xcode 中执行 sass -v,会发现 Build 输出提示 sass 找不动。

    1
    xxx/ScriptEC87903024D805310043B186.sh:2: command not found: sass
  3. 其实这是正常的。上面我已经说过了 Xcode 只会使用系统自带的 Ruby,而每个 Ruby 版本都对应各自的 gem,所以你仅仅是在 Ruby 2.7 下安装了 Sass,Xcode 使用的 Ruby 版本是 2.6,自然就找不到 Sass了。

  4. 此时你只需要使用 rvm use system --default 将 Ruby 切换到系统自带的 Ruby 版本,再用安装 Sass,即可解决。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:50:46] 
    $ ruby -v
    ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin18]

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:50:51]
    $ rvm use system --default
    Now using system ruby.
    Now using system ruby.

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:51:00]
    $ ruby -v
    ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.x86_64-darwin21]

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:51:07]
    $ sass -v
    zsh: command not found: sass

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:51:15] C:127
    $ sudo gem install sass
    Password:

    Ruby Sass has reached end-of-life and should no longer be used.

    * If you use Sass as a command-line tool, we recommend using Dart Sass, the new
    primary implementation: https://sass-lang.com/install

    * If you use Sass as a plug-in for a Ruby web framework, we recommend using the
    sassc gem: https://github.com/sass/sassc-ruby#readme

    * For more details, please refer to the Sass blog:
    https://sass-lang.com/blog/posts/7828841

    Successfully installed sass-3.7.4
    Parsing documentation for sass-3.7.4
    Done installing documentation for sass after 2 seconds
    1 gem installed

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~ [16:51:57]
    $ sass -v
    Ruby Sass 3.7.4

怎么修改 Xcode Run Script 使用 RVM 管理的 Ruby 版本?

Shell 脚本直接写在 Xcode 中

在 Xcode Run Script 脚本最前面添加如下脚本,即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash

# Xcode scripting does not invoke rvm. To get the correct ruby,
# we must invoke rvm manually. This requires loading the rvm
# *shell function*, which can manipulate the active shell-script
# environment.
# cf. http://rvm.io/workflow/scripting

# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then

# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"

elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then

# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else

printf "ERROR: An RVM installation was not found.\n"
exit 128
fi

# rvm will use the controlling versioning (e.g. .ruby-version) for the
# pwd using this function call.
rvm use .

ruby -v
gem -v
sass -v

在 Build 输出中可以看到版本的变化。

1
2
3
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin18]
3.1.4
Ruby Sass 3.7.4

如果没有变化,请将界面中 Shell 对应的输入框,修改为默认的 Shell 或其他能调用 rvm 的 Shell,我这里默认是修改为默认 Shell /bin/zsh/bin/bash 都可以,但是 /bin/sh 却不可以。

Xcode 调用 Shell 脚本文件

将上面写的 Shell 脚本放到 Shell 脚本文件中。然后 Xcode 直接调用脚本文件。

image-20220110005118259

而且必须在脚本文件的开头写上能使 Xcode 调用 rvm 的 Shell。比如我这里写 #!/bin/zsh#!/bin/bash 都可以。
否者将会提示 rvm: command not found

#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。

怎么修改 Xcode Run Script 使用 RVM 管理的 Ruby 版本?

https://ganzhixiong.com/p/1c1fcd81/

Author

干志雄

Posted on

2022-01-07

Updated on

2022-01-07

Licensed under

Comments