Building on the Command Line

It turns out you don’t have to be running Xcode to build an Xcode project. The command-line tools installed with Xcode include xcodebuild, which lets you perform build actions on the command line or as part of a shell script.

This style of build is particularly important when you’re doing continuous integration, in which your codebase is being automatically built on a regular basis. One common technique for this is to set up Jenkins,[9] which can scan for commits to a source control system like Git (to be introduced later, in Chapter 10, Source Control Management), check out the project, build it, and then package it for distribution or send emails if the build failed. For this to work, the CI script has to have the ability to perform builds programmatically, and that’s what xcodebuild provides.

xcodebuild has a lot of options you can pass as arguments, which are shown on its manual page, by entering man xcodebuild on the command line. To do a build, you call either xcodebuild -project Project-Name.xcodeproj or xcodebuild -workspace Project-Name.xcworkspace, depending on whether you’re working with a regular Xcode project or a workspace. You can also use -target to choose specific targets, -scheme to pick a scheme, -configuration for configurations, -sdk to choose between an iOS device or simulator, and several other options.

In fact, sometimes the first step to using xcodebuild is figuring out how to call it. The -list command inspects a project or workspace, and shows you its configurations, targets, custom schemes, and more. For example, the UpcomingConferences project back in Creating App Extensions and Frameworks had an app, an app extension, and a supporting framework, so it provides some interesting output. cd to that directory and use the -list command to inspect it:

=> xcodebuild -list -project UpcomingConferences.xcodeproj/
<= Information about project "UpcomingConferences":
  Targets:
  UpcomingConferences
  UpcomingConferencesTests
  UpcomingConferencesUITests
  UpcomingConferencesToday
  UpcomingConferencesFramework
  UpcomingConferencesFrameworkTests
 
  Build Configurations:
  Debug
  Release
 
  If no build configuration is specified and -scheme is not passed then
  "Release" is used.
 
  This project contains no schemes.

So, as you might suspect, you could build just the framework with xcodebuild -target UpcomingConferencesFramework -project UpcomingConferences.xcodeproj, or the whole app with xcodebuild -target UpcomingConferences -project UpcomingConferences.xcodeproj (since the app builds the framework as a dependency). Actually, you don’t even need to specify the app as the target, since it’s the default. You could do a debug build by appending -configuration Debug, or even build for the simulator with -sdk iphonesimulator11.2 (or the version of whatever Simulator is current at the time you read this), although just building for the Simulator and not actually immediately running it in the Simulator may not be particularly useful, which is why the default is to do a Release-configuration build for the device.

Building with AppleScript

images/aside-icons/tip.png

If Automator and AppleScript are more your speed, you’ll be pleased to know that Xcode supports both of them. Automator comes with a “Build Xcode Project” action that you can make part of a workflow. Meanwhile, the AppleScript dictionary for Xcode offers a deep look into a project or workspace and its build settings, along with the ability to build and run from an AppleScript.

images/building/script-editor-open-dictionary-xcode.png
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.19.251