简体   繁体   English

为 Xcode 中的多个目标重用构建阶段

[英]Reusing build phase for multiple targets in Xcode

Can I reuse the same build phase for multiple targets?我可以为多个目标重用相同的构建阶段吗?

I have multiple targets on my project and I want to run SwiftLint for all the targets as a build phase.我的项目有多个目标,我想在构建阶段为所有目标运行 SwiftLint。

Is there a way I can achieve this without duplicating the build phase?有没有一种方法可以在不重复构建阶段的情况下实现这一目标?

You can.你可以。 Although is a bit of a manual endeavor.虽然有点手动。

When you create build phases, Xcode creates a unique reference id for the build phase.当您创建构建阶段时,Xcode 会为构建阶段创建一个唯一的引用 ID。 Xcode has an array for your build phases with names and reference ids. Xcode 有一个包含名称和引用 ID 的构建阶段数组。 This reference id is used to reference the content of the build phase.此引用 id 用于引用构建阶段的内容。

If you open up your .pbxproj file, you can manually change the reference id's to be the same.如果您打开.pbxproj文件,您可以手动将引用 ID 更改为相同。 The file is found inside your .xcodeproj .该文件位于您的.xcodeproj中。 Show the project in finder -> Right click on the .xcodeproj file -> Click on Show Package Contents -> Open project.pbxproj file.在 finder 中显示项目 -> 右键单击.xcodeproj文件 -> 单击Show Package Contents -> 打开project.pbxproj文件。

In the project file you can find build phases "array" inside every target.在项目文件中,您可以在每个目标中找到构建阶段“数组”。 The section will look like something like this:该部分将如下所示:

buildPhases = (
                1232BB9B27B275C300A05A1E /* Sources */,
                1232BB9C27B275C300A05A1E /* Frameworks */,
                1232BB9D27B275C300A05A1E /* Resources */,
                346E52AF28EC321F00CB6A61 /* SwiftLint */,
            );

The first item is the reference id, the comment tells the name of the referenced build phase for clarity.第一项是引用 id,为了清楚起见,注释会告诉引用的构建阶段的名称。 Later in the file you can find the actual implementation of the build phases.在文件的后面,您可以找到构建阶段的实际实现。 My SwiftLint phase looks like this:我的SwiftLint阶段如下所示:

/* Begin PBXShellScriptBuildPhase section */
        346E52AF28EC321F00CB6A61 /* SwiftLint */ = {
            isa = PBXShellScriptBuildPhase;
            alwaysOutOfDate = 1;
            buildActionMask = 2147483647;
            files = (
            );
            inputFileListPaths = (
            );
            inputPaths = (
            );
            name = SwiftLint;
            outputFileListPaths = (
            );
            outputPaths = (
            );
            runOnlyForDeploymentPostprocessing = 0;
            shellPath = /bin/sh;
            shellScript = "# This workflow is shared between all targets.\nexport PATH=\"$PATH:$HOME/.mint/bin\"\nif which swiftlint >/dev/null; then\n    swiftlint\nelse\n    echo \"warning: SwiftLint not installed. Follow the instructions located in docs/styleGuide.md.\"\nfi\n";
        };
/* End PBXShellScriptBuildPhase section */

If you make the build phase multiple times by copy pasting the contents, you will have multiples of the same build phase with different id's.如果您通过复制粘贴内容多次创建构建阶段,您将拥有多个具有不同 ID 的相同构建阶段。

So you can delete the duplicate phases and replace the reference id for all the targets with the remaining build phase id.因此,您可以删除重复的阶段并将所有目标的引用 ID 替换为剩余的构建阶段 ID。 In this case 346E52AF28EC321F00CB6A61 .在这种情况下346E52AF28EC321F00CB6A61

Took me a bit to find this out, so hopefully this helps other people searching for the same solution.我花了一点时间才发现这一点,希望这能帮助其他人寻找相同的解决方案。

Note: Tested with Xcode 14.0.1注意:使用 Xcode 14.0.1 测试

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM