xcode配置

Running multiple scripts and configuring for projects using CocoaPods

使用CocoaPods运行多个脚本并配置项目

Hi,

你好

In this article, I’ll share how to configure the .yml file of Travis in your Xcode project directory to make it run the test suite of your app automatically, run multiple scripts, and build and test projects that are using CocoaPods.

在本文中,我将分享如何在Xcode项目目录中配置Travis的.yml文件,以使其自动运行应用程序的测试套件,运行多个脚本以及构建和测试使用CocoaPods的项目。

Note: this article assumes that you’ve already set-up travis in your repository and have the .travis.yml file

注意:本文假设您已经在存储库中设置了travis并拥有.travis.yml文件

(1. Starting point)

Initially, your .travis.yml file may look something like this —

最初,您的.travis.yml文件可能看起来像这样-

language: swift
osx_image: xcode11.6
xcode_project: YourApp.xcodeproj # path to your xcodeproj folder
xcode_scheme: YourAppTests
xcode_destination: platform=iOS Simulator,OS=13.6,name=iPhone 8

This Travis configuration would be sufficient to build your Xcode project and run the tests. However, we’d like to consider and tackle some additional use cases such as —

这个Travis配置足以构建您的Xcode项目并运行测试。 但是,我们想考虑并解决其他一些用例,例如-

  1. Making Travis run some other additional script (eg. Swiftlint)
  2. Making Travis work in case the project uses a dependency manager (eg. CocoaPods)

We’ll be tackling both of these two common use-cases one-by-one in the following sections —

在以下各节中,我们将一一解决这两个常见用例—

(2. Running multiple scripts in Travis)

To run multiple scripts, eg. Swiftlint + Build Xcode and Test Suite, we can configure the .travis.yml file like this —

运行多个脚本,例如。 Swiftlint +构建Xcode和测试套件,我们可以像这样配置.travis.yml文件-

install:
 - brew update && brew upgrade swiftlint
language: swift
osx_image: xcode11.6
xcode_project: YourApp.xcodeproj # path to your xcodeproj folder
xcode_scheme: YourAppTests
xcode_destination: platform=iOS Simulator,OS=13.6,name=iPhone 8
script: 
 - swiftlint
 - xcodebuild test -project 'YourApp.xcodeproject' -scheme 'YourAppTests' -destination 'platform=iOS Simulator,OS=13.6,name=iPhone 8'

In the above Travis configuration, we add the ability of running multiple scripts, example: run Swiftlint script along with the default xcodebuild test script.

在上面的Travis配置中,我们添加了运行多个脚本的功能,例如:运行Swiftlint脚本以及默认的xcodebuild测试脚本。

It shall be noted, that while earlier the xcodebuild script was being run by Travis by default, we now need to specify it ourselves (along with Swiftlint, line 9-10) once we explicitly declare ‘script’.

应当指出,虽然Travis以前默认运行xcodebuild脚本,但现在我们需要在明确声明'script'之后自行指定它(以及Swiftlint,第9-10行)。

To be able to use Swiftlint, we install it first. This can be done in the ‘install’ declaration (line 1).

为了能够使用Swiftlint,我们首先安装它。 这可以在“安装”声明(第1行)中完成。

(3. Configuring Travis for project using CocoaPods)

Till now, we’re telling Travis to build the xcode_project (line 5 in above code). However, if we use a dependency manger like CocoaPods in our project, it usually creates a new xcworkspace file which is then used for development instead of the xcodeproj file.

到现在为止,我们告诉Travis构建xcode_project(上面代码中的第5行)。 但是,如果我们在项目中使用像CocoaPods这样的依赖项管理器,它通常会创建一个新的xcworkspace文件,然后将其用于开发而不是xcodeproj文件。

In this case, we also need to tell Travis to use the workspace file instead. This can be done like this —

在这种情况下,我们还需要告诉Travis使用工作区文件。 可以这样做-

before_install:
  - gem install cocoapods
 install:
  - brew update && brew upgrade swiftlint
  - pod install
language: swift
osx_image: xcode11.6
xcode_workspace: YourApp.xcworkspace # path to your xcworkspace file
xcode_scheme: YourAppTests
xcode_destination: platform=iOS Simulator,OS=13.6,name=iPhone 8
script: 
 - swiftlint
 - xcodebuild test -workspace 'YourApp.xcworkspace' -scheme 'YourAppTests' -destination 'platform=iOS Simulator,OS=13.6,name=iPhone 8'

In the above code we make a few changes to enable building of our project using CocoaPods using Travis.

在上面的代码中,我们进行了一些更改,以允许使用Travis使用CocoaPods构建我们的项目。

  1. We change project to workspace (lines 8, 13).
  2. Install CocoaPods and Pods for the project (lines 1-5).