Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running script only for an 'Archive' build in Xcode 4

I have a script that I run using osascript from a Run Script Build Phase in Xcode 4. This script does some checks to try and catch any human-error in plists and suchlike.

However, I only really want to run this script when a developer chooses 'Archive' as opposed to running it every time they build.

Is there any way of testing for this situation? Either in Shell Script or Apple Script?

like image 396
Wex Avatar asked Mar 29 '11 10:03

Wex


People also ask

How do I run a build phase script in Xcode?

Go to the Build Phases section of your project. (Click on the project, then on the main target, then on the “Build Phases” tab along the top.) Click the + at the top left to create a new build phase; choose “New Run Script Phase.” Xcode creates the script at the end of the list, naming it “Run Script.”

How do I turn off archive in Xcode?

On the Xcode menu, select Product -> Edit Scheme... In the Build section, add the new target, then uncheck all the boxes except the one in the Archive column. This ensures the script will only be run on Archive. (See this question for an explanation and a nice screenshot.)

How do I run multiple scripts in Xcode?

you can run as many bash scrips as you like at any point in the build phase. select your target, then either: from the menu: Editor -> Add Build Phase -> Add run script build phase.


2 Answers

In Xcode 4+, you could run this script as a pre- or post-action script on the "Archive" action. However, pre- and post-action scripts are not considered part of the build itself; their output is not part of the build log, and a failing script will not cause the build to fail.

If you want to include this script as a first-class part of the build, such that its failure will cause the build to fail, then you'll want to do something else. Make a new "External Build System" target, and configure it to run your script. The path to your script can be a relative path, relative to the project root.

Finally, open your main project scheme, and in the "Build" tag add the new checker target, and set it to only be included in the "Archive" action. This should cause your checker script to be run as part of the build only when "Archive" is selected, and if the script returns a non-zero value the build will fail.

Here's a step-by-step visual guide to configure a bash script to run only when archiving a target:

  1. File > New > Target > Other > External Build System File > New > Target > Other > External Build System

  2. Name the product accordingly and set /bin/bash as the Build tool Name the product accordingly

  3. Provide the path to the script under Info > Arguments of the newly created target Provide the path to the script under Info > Arguments of the newly created target

  4. Product > Scheme > Edit Scheme…, and edit the scheme of the target where you want this script to run before archiving. Under Build, add the External Build System target you added in step 1. Reorder if needed. Then uncheck everything except Archive. Edit the scheme of the target where you want this script to run before archiving

like image 105
BJ Homer Avatar answered Sep 18 '22 16:09

BJ Homer


  1. Add a "New Run Script Phase" to your project's "Build Phases"
  2. Move (drag&drop) your script in the right position
  3. Check the "Run script only when installing"

If you don't do 3. the script will run all the time, but if you check that box, it will only run when archiving.

like image 39
Mike Avatar answered Sep 20 '22 16:09

Mike