简体   繁体   English

如何使用Podfile的安装后部分添加文件引用并自动设置该文件的目标?

[英]How can add a file reference and set that file's target automatically using the post install portion of the Podfile?

I have a project employing CocoaPods in which one of my pods requires a .framework file to be added as a reference to the Pods project after it is generated by pod update . 我有一个使用CocoaPods的项目,其中我的一个pod要求通过pod update生成后,添加一个.framework文件作为对Pods项目的引用。 The .framework file is located in the main project directory for now, and I'm adding a reference to it. .framework文件目前位于主项目目录中,我将添加对它的引用。 Currently, I can go into the project navigator in Xcode after building the pods, right click and add the file to the project. 当前,在构建Pod之后,我可以进入Xcode中的项目导航器,右键单击并将文件添加到项目中。 I then have to select the framework file and set it's target to the pod that needs to make use of it. 然后,我必须选择框架文件并将其目标设置为需要利用它的pod。

In order to allow my CI server to build automatically, I'm attempting to automate the process of adding the file and selecting its target by using the post_install hook in the Podfile. 为了使我的CI服务器能够自动构建,我试图通过使用Podfile中的post_install挂钩来自动化添加文件并选择其目标的过程。 That's where I'm running into issues. 那就是我遇到问题的地方。

I've managed to add the file reference to the group for the actual Pod, but this seems to not work as the files in that pod seem to expect to be importing a framework from outside its group. 我已经设法将文件引用添加到实际Pod的组中,但是这似乎不起作用,因为该Pod中的文件似乎希望从其组外部导入框架。 When I add the file reference manually to the Pods project but not within the actual group for the Pod in question, it works fine. 当我手动将文件引用添加到Pods项目中,但不在该Pod的实际组中时,它可以正常工作。

First question: 第一个问题:
Is there a proper way to add a file reference to the Pods project itself without having to add it to a specific Pod's group? 是否有适当的方法可以添加对Pods项目本身的文件引用,而不必将其添加到特定的Pod组中? Or perhaps a way to create a new group for the file reference when adding it? 还是在添加文件时为文件引用创建新组的一种方法?

Second question: 第二个问题:
How would I go about setting the target for the file once it is added to the Pods project automatically? 将文件自动添加到Pods项目后,如何设置文件目标?

This is the code that adds the file reference to the Pod group itself: 这是将文件引用添加到Pod组本身的代码:

post_install do |installer|
    dir = Dir.pwd + '/app/VPX.framework'
    group = installer.pods_project.pod_group('OGVKit')
    installer.pods_project.add_file_reference(dir, group)
end

You can achieve the second by getting a reference to the target and adding the file reference to it. 您可以通过获取对目标的引用并向其添加文件引用来实现第二个目标。 Here is the tl;dr; 这是tl; dr; solution

post_install do |installer|

    dir = Dir.pwd + '/app/VPX.framework'

    project = installer.pods_project

    group_name = 'OGVKit'

    group     = project.pod_group(group_name)
    reference = project.add_file_reference(dir, group)

    project.targets.select { |target| 
      target.add_file_references([ reference ]) if target.display_name.eql? group_name
    }

end

here's a little breakdown: 这里有一些故障:

project.add_file_reference(dir, group)

This returns a PBXFileReference, this just represents a reference to a file in the file system. 这将返回PBXFileReference,它仅表示对文件系统中文件的引用。

We can use this reference to add it to the build target. 我们可以使用此引用将其添加到构建目标。

project.targets.select { |target| 

Loop and iterate over the available targets 循环并遍历可用目标

target.add_file_references([ reference ]) if target.display_name.eql? group_name

The target add_file_references accepts an array of PBXFileReference objects, great, we can use the one we have previously obtained. 目标add_file_references接受一个PBXFileReference对象数组,很好,我们可以使用先前获得的对象。

Cocoapods target name is the same as the group name, so we just need to perform a conditional check validating this. Cocoapods目标名称与组名称相同,因此我们只需要执行条件检查来验证此名称。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不支持指定多个 post_install 挂钩的无效 Podfile 文件 - Invalid Podfile file specifying multiple post_install hooks is unsupported 如何在 React Native 中结合 Podfile 中的 Post Install? - How to Combine Post Install in Podfile in React Native? 如何解决此错误,例如无效的“ Podfile”文件:[!]目标不支持的选项{{:exclusive => true}` - How to solve this error like Invalid `Podfile` file: [!] Unsupported options `{:exclusive=>true}` for target 当安装pod时,ERROR“无效的`Podfile`文件:未定义的方法`plateform' - when install pod ,ERROR "Invalid `Podfile` file: undefined method `plateform' 无效的“ Podfile”文件 - Invalid 'Podfile' file 在Podfile中包含文件 - Include file in Podfile ios 创建新文件并自动添加到所有目标 - ios Create new file and add to all target automatically Cocoapods post_install,如何在 Pods 项目中添加目标成员资格 - Cocoapods post_install, how to add target membership in Pods project 使用多个框架时,如何在不“重复”我的 `Podfile` 的情况下添加用于测试的 pod? - How can I add pods for testing without “repeating” them in my `Podfile` when using multiple Frameworks? 如何正确设置具有框架目标和使用该框架的应用程序目标的Podfile - How to properly set up a Podfile that has both a framework target and an application target that uses that framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM