Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40257

Xcode - How to run script just before build starts

I wonder if there is any way to run scripts before build starts in Xcode. There is way from

Build phase -> Run scripts

But this runs after the actual build is finished. I need to run script before Xcode build starts though.

Upvotes: 5

Views: 11542

Answers (4)

Accid Bright
Accid Bright

Reputation: 632

Niether of the above variants helped me. For archive command they are usually lately executed and do not update the version for me. Found another possibility to run script and update version definitely before the build process starts.

  1. Select you current scheme and then select Edit scheme...
  2. Expand Build section in left menu and select Pre-actions item
  3. Select + at the bottom left of the main window and then select New Run Script Action
  4. Check the shell you want to execute your script. Insert script you want to execute

That's all. This script will be executed every time right before build is started.

Manage scheme editor

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122458

The best approach is to create pseudo target in your project.

For example, say you have a shell script that sets the app version by updating an .xcconfig file:

  • Create a target called SetAppVersion of type Other > External Build System.
  • Leave the Build Tool at /bin/bash, set the Arguments to whatever script does the work, for example scripts/set_app_version.sh and set the Directory to $(PROJECT_DIR).

Now for any target affected by this script, simply make the SetAppVersion target a dependency of it, and it will run before any other action is performed on those targets.

Upvotes: 1

user4003752
user4003752

Reputation:

You need to add a build phase. Go to the project file, Build Phases, click Editor on the tool bar menu, add Build Phase, Add Run Script... You can select and drag the script to put it before/after anything.

Upvotes: 9

The_Falcon
The_Falcon

Reputation: 287

If you want to change the code or assets via the script before they get compiled, just move the Run Script phase above the Compile Sources phase.

Upvotes: 4

Related Questions