0%

Ruby相关命令收集

RVM是一个命令行工具,它能帮你安装多个版本的Ruby,并切换不同的版本。

安装RVM步骤

1.安装rvm

1
2
3
4
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
$ curl -sSL https://get.rvm.io | bash -s stable
# 如果上面的连接失败,可以尝试:
$ curl -L https://raw.githubusercontent.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable

2.载入rvm环境(新开Terminal,会自动重新载入)

1
$ source ~/.rvm/scripts/rvm

3.修改rvm下载的Ruby源,到RubyChina的镜像:

1
$ echo "ruby_url=https://cache.ruby-china.com/pub/ruby" > ~/.rvm/user/db

4.检查rvm是否安装正确

1
2
$ rvm -v
rvm 1.22.17 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

RVM常用指令

1
2
3
4
5
6
7
8
9
10
11
12
13
$ ruby -v # 查看ruby 版本
$ rvm list known # 列出已知的 ruby 版本
$ rvm install 2.3.0 # 选择指定 ruby 版本进行更新
$ rvm get stable # 更新 rvm
$ rvm use 2.2.2 # 切换到指定 ruby 版本
$ rvm use 2.2.2 --default # 设置指定 ruby 版本为默认版本
$ rvm list # 查询已安装的 ruby 版本
$ rvm remove 1.9.2 # 卸载移除 指定 ruby 版本

$ curl -L https://get.rvm.io | bash -s stable # 安装 rvm 环境
$ curl -sSL https://get.rvm.io | bash -s stable --ruby # 默认安装 rvm 最新版本
$ curl -sSL https://get.rvm.io | bash -s stable --ruby=2.3.0 # 安装 rvm 指定版本
$ source ~/.rvm/scripts/rvm # 载入 rvm

Gem常用指令

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
$ gem -v # 查看 gem 版本
$ gem source # 查看 gem 配置源
$ gem source -l # 查看 gem 配置源目录
$ gem sources -a url # 添加 gem 配置源(url 需换成网址)
$ gem sources --add url # 添加 gem 配置源(url 需换成网址)
$ gem sources -r url # 删除 gem 配置源(url 需换成网址)
$ gem sources --remove url # 删除 gem 配置源(url 需换成网址)
$ gem update # 更新 所有包
$ gem update --system # 更新 Ruby Gems 软件

$ gem install rake # 安装 rake,从本地或远程服务器
$ gem install rake --remote # 安装 rake,从远程服务器
$ gem install watir -v 1.6.2 # 安装 指定版本的 watir
$ gem install watir --version 1.6.2 # 安装 指定版本的 watir
$ gem uninstall rake # 卸载 rake 包
$ gem list d # 列出 本地以 d 打头的包
$ gem query -n ''[0-9]'' --local # 查找 本地含有数字的包
$ gem search log --both # 查找 从本地和远程服务器上查找含有 log 字符串的包
$ gem search log --remoter # 查找 只从远程服务器上查找含有 log 字符串的包
$ gem search -r log # 查找 只从远程服务器上查找含有log字符串的包

$ gem help # 提醒式的帮助
$ gem help install # 列出 install 命令 帮助
$ gem help examples # 列出 gem 命令使用一些例子
$ gem build rake.gemspec # 把 rake.gemspec 编译成 rake.gem
$ gem check -v pkg/rake-0.4.0.gem # 检测 rake 是否有效
$ gem cleanup # 清除 所有包旧版本,保留最新版本
$ gem contents rake # 显示 rake 包中所包含的文件
$ gem dependency rails -v 0.10.1 # 列出 与 rails 相互依赖的包
$ gem environment # 查看 gem 的环境

$ sudo gem -v # 查看 gem 版本(以管理员权限)
$ sudo gem install cocoa pods # 安装 CocoaPods(以管理员权限)
$ sudo gem install cocoapods # 安装 CocoaPods(以管理员权限)
$ sudo gem install cocoapods --pre # 安装 CocoaPods 至预览版(以管理员权限)
$ sudo gem install cocoapods -v 1.4.0 # 安装 CocoaPods 指定版本(以管理员权限)
$ sudo gem update cocoapods # 更新 CocoaPods 至最新版(以管理员权限)
$ sudo gem update cocoapods --pre # 更新 CocoaPods 至预览版(以管理员权限)
$ sudo gem uninstall cocoapods -v 1.4.0 # 移除 CocoaPods 指定版本(以管理员权限)

# 在Catalina系统上执行上述命令会报无权限访问错误,需要使用下面的命令
$ sudo gem install -n /usr/local/bin cocoapods -v 1.9.1

# 如果安装失败的话,可以把需要安装的gem文件下载到本地然后在本地安装,使用命令
$ sudo gem install -n /usr/local/bin ~/Downloads/cocoapods-1.9.1.gem


# Cocoapods相关命令
# 更新Pod
$ pod update --verbose --no-repo-update

# 清除Pod的缓存
$ pod cache clean --all

# 清除Pod库AAA的缓存
$ pod cache clean AAA

如果命令无法修改Gem源,可以手动修改Gem源,Gem的配置文件位置在~/.gemrc,编辑~/.gemrc文件,在:sources:配置行下添加https://gems.ruby-china.com/,这样就配置了ruby-china的Gem源。最终的配置文件如下:

1
2
3
4
5
6
7
8
9
---
:backtrace: false
:bulk_threshold: 1000
:sources:
- https://gems.ruby-china.com
:update_sources: true
:verbose: true
:ssl_verify_mode: 0
concurrent_downloads: 8

mac系统升级到Catalina时,使用cocoa pod时报错:-bash: /usr/local/bin/pod: /
System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby: bad interpreter: No such file or directory”.

解决方案:sudo gem install -n /usr/local/bin cocoapods -v 1.4.0

Fastlane安装步骤

指定版本安装fastlane

1
2
3
$ set -ex
$ gem uninstall fastlane --all --executables
$ gem install fastlane --version 2.28.3 --no-document

mac系统升级到Catalina时,安装Fastlane会报错,请尝试:

1
2
3
$ set -ex
$ gem uninstall -n /usr/local/bin fastlane --all --executables
$ gem install -n /usr/local/bin fastlane --version 2.28.3 --no-document

如果尝试多次仍然无法安装Fastlane,可以尝试:

1
2
3
#brew cask安装fastlane的执行命令:
$ brew cask uninstall fastlane
$ brew cask install fastlane

Fastlane基本命令

初始化fastlane

1
$ fastlane init

给fastlane安装插件

1
2
$ fastlane add_plugin git_pull_branch
$ fastlane add_plugin fastlane-plugin-ipa_install_plist_generate

使用fastlane进行重签名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
desc "重签名"
lane :ad_hoc_resign do

# 包的地址
ipa_path = "/Users/kris/Desktop/wantong/Payload.ipa"
signing_identity = "iPhone Distribution: XXXXX Asia Limited"
provision_profile = "/Users/kris/Desktop/wantong/embedded.mobileprovision"

result = resign(
ipa: ipa_path,
signing_identity: signing_identity,
provisioning_profile: provision_profile,
)

puts result

end

将homebrew的源换成阿里云,提高下载速度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1.替换brew.git:
$ cd "$(brew --repo)"
$ git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

# 2.替换homebrew-core.git:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 3.替换homebrew-cask.git:
$ cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

# 4.替换homebrew-bottles:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
$ source ~/.zshrc

给Git添加代理访问,提高Git的访问速度

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1.编辑PAC用户自定义规则,添加以下地址:
github.com
githubusercontent.com
githubassets.com

# 2.编辑~/.ssh/config文件,在文件中添加以下命令:
Host github.com
HostName github.com
User git
ProxyCommand nc -v -x 127.0.0.1:1086 %h %p

# 3.让配置立即生效:
$ source ~/.ssh/config

给终端添加代理访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1.编辑~/.bash_profile文件
$ vim ~/.bash_profile

# 2.在配置文件中加入如下内容,并保存
export http_proxy=http://127.0.0.1:1087
export https_proxy=http://127.0.0.1:1087

# 3.让配置立即生效
$ source ~/.bash_profile

# 4.测试代理是否成功
$ curl cip.cc
IP : 192.3.249.219
地址 : 美国 华盛顿州 西雅图
运营商 : colocrossing.com

数据二 : 美国

数据三 : 美国华盛顿西雅图

URL : http://www.cip.cc/192.3.249.219

解决真机连接异常的问题

1
2
# 杀掉USB服务
$ sudo killall -STOP -c usbd

参考链接

安装配置RVM环境
安装指定版本的cocoapods
手动安装RVM
解决Homebrew下载更新极慢的问题
Mac上的各种代理设置
Mac安装Ruby版本管理器RVM