xcode命令行工具



(Introduction)

In this brief tutorial, I’ll introduce you to some useful Xcode command-line tools and present useful use cases where you can leverage their potential.

在这个简短的教程中,我将向您介绍一些有用的Xcode命令行工具,并介绍一些有用的用例,您可以在其中利用它们的潜力。

(1. Use Case: Build Workflow)

CI/CD are trendy nowadays, and one of the basic checks that you can set to your repository is to ensure all your targets build. Let’s take a look at some useful commands. But first, open your terminal and run cd path_to_your_project_folder .

CI / CD现在很流行,可以对存储库设置的基本检查之一是确保构建所有目标。 让我们看一些有用的命令。 但是首先,打开终端并运行cd path_to_your_project_folder

(Show build settings)

The following command will output a list with all your project settings:

以下命令将输出包含所有项目设置的列表:

xcodebuild -project MyProject.xcodeproj -showBuildSettings

If you’re looking for one in particular, use grep to filter. For example, to look up your build directory (BUILD_DIR):

如果您要特别寻找一个,请使用grep进行过滤。 例如,要查找您的构建目录( BUILD_DIR ):

xcodebuild -project MyProject.xcodeproj -showBuildSettings | grep -m 1 "BUILD_DIR" | grep -oEi "\/.*"

NOTE: You can also use -workspace MyWorkspace.xcworkspace instead of -project MyProject.xcproject. This also applies to all the following commands.

注意:您也可以使用-workspace MyWorkspace.xcworkspace而不是-workspace MyWorkspace.xcworkspace -project MyProject.xcproject 。 这也适用于以下所有命令。

(Build a scheme)

To build your scheme, you can run:

要构建方案,可以运行:

xcodebuild clean -scheme MyScheme \
                 -project MyProject.xcodeproj \
                 -destination SomeDeviceOrSimulator

If you want a fresh build, add clean before build:

如果您想重新构建,请在build之前添加clean

xcodebuild clean build  -scheme MyScheme \
                        -project MyProject.xcodeproj \
                        -destination "platform=macOS"

Here are some destinations you can choose:

您可以选择以下目的地:

  • "platform=macOS" "platform=macOS"
  • "platform=macOS,variant=Mac Catalyst" "platform=macOS,variant=Mac Catalyst"
  • "platform=iOS" "platform=iOS"
  • "platform=tvOS" "platform=tvOS"
  • "platform=watchOS" "platform=watchOS"
  • "platform=iOS Simulator" "platform=iOS Simulator"

You can also choose a specific architecture: "platform=macOS,arch=arm64".

您还可以选择特定的体系结构: "platform=macOS,arch=arm64"

(Use case in action)

This is how you could leverage xcodebuild in a CI workflow to build all your targets:

这是在CI工作流程中利用xcodebuild来构建所有目标的方式:

SCHEMES=( Target1_iOS Target1_macOS Target2 )
PLATFORMS=( "platform=iOS" "generic/platform=macOS" "generic/platform=iOS" )DIR=$(xcodebuild -project MyProject.xcodeproj -showBuildSettings | grep -m 1 "BUILD_DIR" | grep -oEi "\/.*")for i in ${!SCHEMES[@]}; do
  xcodebuild clean build  -scheme ${SCHEMES[$i]} \
                          -project MyProject.xcodeproj \
                          -destination "${PLATFORMS[$i]}"  COUNT=$(find $DIR -maxdepth 1 -type d | wc -l)  if [ $COUNT -eq 2 ]; then
    echo "$(tput setaf 2)Build succeded for target ${SCHEMES[$i]}$(tput sgr0)"
  else
    echo "$(tput setaf 1)Build failed for target ${SCHEMES[$i]}$(tput sgr0)"
  exit 1
  fidone

This script executes a for loop to build all your targets and validates that the build is successful.

该脚本执行for循环以构建所有目标并验证构建是否成功。

(2. Use Case: Generate an XCFramework)

Another common action to do in a CI workflow is to generate an XCFramework once you’ve published your release and attached the generated binary to it, to support manual installations (learn more in the article Continuous Integration With GitHub Actions).

CI工作流中另一个常见的操作是在发布发行版并将其附加到生成的二进制文件后生成XCFramework,以支持手动安装(有关更多信息,请参见Continuous Integration With GitHub Actions中的内容 )。

(Archive schemes)

To archive a scheme, use xcodebuild :

要归档方案,请使用xcodebuild

xcodebuild archive -scheme MyScheme \
                   -destination "platform=macOS" \
                   -archivePath "./MyScheme.macOS.xcarchive" \
                   SKIP_INSTALL=NO \
                   BUILD_LIBRARY_FOR_DISTRIBUTION=YES

This command will generate an .archive file in the selected path compatible with the selected destination. As before, you can also add clean :

此命令将在与所选目标位置兼容的所选路径中生成一个.archive文件。 和以前一样,您还可以添加clean

xcodebuild clean archive -scheme MyScheme \
                         -destination "platform=macOS" \
                         -archivePath "./MyScheme.macOS.xcarchive" \
                         SKIP_INSTALL=NO \
                         BUILD_LIBRARY_FOR_DISTRIBUTION=YES

(Create an XCFramework)

To create an XCFramework, use -create-xcframework after archiving your schemes, and pass the archive path:

要创建XCFramework, 在归档方案后使用-create-xcframework ,并传递归档路径:

xcodebuild -create-xcframework \
           -framework "path_to_archived_framework_1" \
           -framework "path_to_archived_framework_2"
           -output "MyProject.xcframework"

You can very easily create another script to run in your CI workflow with these two commands, similar to the one seen before.

您可以使用这两个命令轻松地创建另一个脚本以在CI工作流程中运行,类似于之前看到的脚本。

(3. Use Case: Run Tests)

Similar to the previous use cases, Xcode lets you run tests from the command line.

与以前的用例相似,Xcode允许您从命令行运行测试。

(Run a test scheme)

If your project or workspace has a specific scheme for tests, then you can run them from the command line:

如果您的项目或工作区具有特定的测试方案,则可以从命令行运行它们:

xcodebuild test -scheme MyScheme \
                -project MyProject.xcodeproj \
                -destination 'platform=OS X,arch=x86_64'

(Run tests on your Swift package)

If you don’t have a project file but a Swift package, you can run:

如果您没有项目文件而是Swift包,则可以运行:

swift test

(4. Use Case: Simulate Deeplinks and Notifications)

Deeplinks and notifications are some of the features that are more difficult to test in a device. Fortunately, Xcode command-line tools have a solution.

深层链接和通知是一些较难在设备中进行测试的功能。 幸运的是,Xcode命令行工具提供了解决方案。

(Deeplinks)

Anywhere in your code, you can run:

您可以在代码中的任何位置运行:

xcrun simctl openurl booted <INSERT_URL_HERE>

NOTE: The url should contain a scheme that’s recognized by your app. See how to set up your deeplink (or universal link) in the article Deep Links, Universal Links, and the SwiftUI App Life Cycle.

注意:网址应包含您的应用可以识别的方案。 在文章深层链接,通用链接和SwiftUI应用生命周期中,了解如何设置深层链接(或通用链接)。

(Notifications)

Available from 11.4, Xcode adds a way to simulate notifications. You just have to create an APNS file with your notification’s payload:

Xcode从11.4开始提供,它增加了一种模拟通知的方法。 您只需使用通知的有效负载创建一个APNS文件:

{    
  "aps": {        
    "alert": {
      "title": "Test",
      "body": "Testing notifications in the simulator"
    }
    "sound": "default"
     "badge": 1
}

And run:

并运行:

xcrun simctl push <device> <bundle identifier> <APNS file>

To get all available devices you can run:

要获取所有可用的设备,可以运行:

instruments -s devices

Or if it’s booted, run:

或者,如果已启动,请运行:

xcrun simctl list | grep 'Booted' # Phone: iPhone Xs (AB165068-87DC-43EC-A513-893860CF0615) (Booted)

And copy the ID between brackets.

并在方括号之间复制ID。

xcrun simctl push AB165068-87DC-43EC-A513-893860CF0615 com.example.app push-notification.apns



翻译自: https://medium.com/better-programming/xcode-command-line-tools-use-cases-6eb331eda99d

xcode命令行工具