前言
在使用Jenkins打包iOS应用时出现这样一个明显错误:
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/
rubygems/core_ext/kernel_require.rb:55:inrequire': cannot load such file -- xcodeproj
(LoadError)
原因是构建时使用到了Ruby库 - xcodeproj,原来是升级了Mac OS系统后,ruby脚本使用的xcodeproj库没有了,需要重新安装。这个使用xcodeproj的Ruby脚本是别的同事写的,遇到这个问题正好可以去学习一下。
安装Ruby xcodeproj库
首先的使用gem安装库:
$ gem install xcodeproj
xcodeproj这个库很强大,可以让我们通过ruby脚本来修改项目工程的配置,不需要手动打开Xcode修改,下面就来看下一些常见的配置修改。
PS:安装过程中遇到的错误:
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted @ rb_sysopen - /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/gem
网上搜一下,说要升级一下ruby:
$ sudo gem update --system
但还有错误:
You don't have write permissions for the /usr/bin directory.
/usr/bin
/usr/bin
这个目录是受系统完整性保护的,所以不能被任何人甚至root写入。故只能安装到其他目录。
安装到指定目录:
$sudo gem install -n /usr/local/bin xocdeproj
引用网上解决方案:
1,修改目录
SIP
(系统完整性保护,El Capitan 10.11开始有)让 /usr/bin 只读了, 但是 /usr/local 是可读可写的, 将安装目录修改了.
2,关闭SIP(不推荐)
如果不想改目录的话, 只能关了SIP, 在终端里输入:
$ csrutil status # 查看SIP状态
$ csrutil disable # 关闭,启用:enable
$ reboot
想要重新打开的话用enable.
开始使用Ruby编写脚本
如果不懂ruby的,可以先去学习下ruby的基本语法。Ruby教程-菜鸟教程
安装Ruby(Mac)
使用HomeBrew来安装ruby:
$ brew install ruby
检查Ruby是否安装成功:
$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
Ruby脚本
开始编写脚本,以下是一个比较简单的例子,具体可以看需求自行查询API来具体实现。
# 引入库
require 'xcodeproj'
#打开项目工程.xcodeproj
project_path = './RubyEditXcodeProj.xcodeproj'
project = Xcodeproj::Project.open(project_path)
# 查询有多少个target
project.targets.each do |target|
puts target.name
end
# 遍历配置
project.targets[0].build_configurations.each do |config|
puts config.name # Debug / Release
#获得build settings
build_settings = config.build_settings
#build_settings是一个哈希,里面是一个个配置
build_settings.each do |key,value|
print key, " == ", value, "\n"
# 可在这里进行设置证书等操作,常用的如下:
# 比如修改bundle id ,测试
# build_settings[key] = "com.test.demo"
# 设置授权文件
build_settings["CODE_SIGN_ENTITLEMENTS"] = "xxx.entitlements"
# 设置签名 iPhone Distribution / iPhone Developer
build_settings["CODE_SIGN_IDENTITY[sdk=iphoneos*]"] = "iPhone Distribution"
# ..... 其他的视情况(需求)去查找API
end
end
# 在工程中引入Framework .a文件 或 bundele文件
# 找到需要操作的target
targetIndex = 0
project.targets.each_with_index do |target,index|
if target.name == ""
targetIndex = index
end
end
# target
target = project.targets[targetIndex]
# 添加 .framework引用
file_ref = project.frameworks_group.new_file("xxxPath/xx.framework")
target.frameworks_build_phases.add_file_reference(file_ref)
# 新建一个copy files,并把framework添加进去
target.new_copy_files_build_phases("名字")
# 可能有多个copy files build phases,遍历找到对应的
target.copy_files_build_phases.each do |item|
if item.name = "名字"
item.add_file_reference(file_ref)
end
# #设置copy file buldphase中的Destination选项
item.dst_subfolder_spec = "10"
# #勾上code sign on copy选项
item.files[0].settings = Hash.new
item.files[0].settings["ATTRIBUTES"] = ["CodeSignOnCopy"]
end
# 最后保存文件
project.save
使用xcodeproj --help
来查看一些基本的命令,比如比较两个project文件的差异。可以在Xcodeprpj API上查找相关API。
Ruby学习demo及修改project文件的demo在GitHub上:Demo
网友评论