将运行脚本构建阶段从 podspec 添加到 Xcode 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20072937/
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 04:18:37  来源:igfitidea点击:

Add run script build phase to Xcode project from podspec

iosobjective-crubyxcodecocoapods

提问by opedge

I'm trying to write Cocoapods specification for my library which must modify Xcode project and add "Run Script Build Phase" to project's target. I thought I can use post_installhook. But "pod spec lint" says that this hook is deprecated:

我正在尝试为我的库编写 Cocoapods 规范,该规范必须修改 Xcode 项目并将“运行脚本构建阶段”添加到项目的目标。我以为我可以使用post_install钩子。但是“ pod spec lint”说这个钩子已被弃用:

- WARN  | [iOS] The post install hook of the specification DSL has been deprecated, use the `resource_bundles` or the `prepare_command` attributes.

I have no idea how I can replace post_install hook with *resource_bundles* or *prepare_command*. Who knows any other approach to solve my problem? Is it possible?

我不知道如何用 *resource_bundles* 或 *prepare_command* 替换 post_install 钩子。谁知道解决我的问题的任何其他方法?是否可以?

And another problem is how to modify Xcode project to add build phase, but it is actual only when "post_hook problem" is solved.

另一个问题是如何修改Xcode项目添加构建阶段,但只有在“post_hook问题”解决后才实际存在。

回答by Onato

Use the Xcodeproj ruby gem (which is part of Cocoapods) to write a script that modifies your Xcode project.

使用 Xcodeproj ruby​​ gem(它是 Cocoapods 的一部分)编写一个脚本来修改您的 Xcode 项目。

You then call this script using the prepare_command.

然后使用prepare_command调用此脚本。

require 'xcodeproj'
path_to_project = "${SOURCE_ROOT}/${PROJECT_NAME}.xcodeproj"
project = Xcodeproj::Project.open(path_to_project)
main_target = project.targets.first
phase = main_target.new_shell_script_build_phase("Name of your Phase")
phase.shell_script = "do sth with ${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/your.file"
project.save()

Documentation: http://rubydoc.info/gems/xcodeproj/frames

文档:http: //rubydoc.info/gems/xcodeproj/frames

You can get a list of build setting variables and their values by running…

您可以通过运行来获取构建设置变量及其值的列表...

xcodebuild -project [ProjectName].xcodeproj -target "[TargetName]" -showBuildSettings

UPDATE:A few things have changed since this answer was written. The issue of accessing environment variables is currently being discussed here: https://github.com/CocoaPods/CocoaPods/issues/2115

更新:自从写下这个答案以来,一些事情发生了变化。目前正在讨论访问环境变量的问题:https: //github.com/CocoaPods/CocoaPods/issues/2115