Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically add a run script build phase to an Xcode project?

I am wondering if it is possible to add a run script build phase to another Xcode project from within my Mac utility app. I can see what takes place in an Xcode project when a run script is added (doing a diff), but I don't know how to safely add the equivalent in code.

Am I supposed to parse it manually? Is there documentation or libraries available for this? This would be a useful feature if I can make sure I do it safely and right. I don't want to be messing up people's Xcode projects!

like image 863
Lizza Avatar asked Oct 10 '13 17:10

Lizza


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 run a script in Swift?

Compiling your Swift file and executing it from Xcode is as easy as doing it from the terminal. All you need to do is define a New Run Script Phase and add the commands you executed on the terminal into it. From the Project navigator, select the project file.


2 Answers

For anyone looking for the easy way to do this, there is a great python library that lets you mod all types of things in the .pbxproj file. You can check it out here: https://github.com/kronenthaler/mod-pbxproj.

Now to add a run script build phase you can write a simple python script like:

from pbxproj import XcodeProject
import sys

project = XcodeProject.load('path/to/my-project.xcodeproj/project.pbxproj')
project.add_run_script('bash my_run_script.sh')
project.save()

Now just run the python script and you should be good to go.

like image 108
David Kleiman Avatar answered Sep 24 '22 00:09

David Kleiman


The possibly easiest solution is to add a Run Script Phase to the other Project which defines a command line executing a script which resides as a particular file in some known location.

The first project then may have a Run Script Phase, or execute a program which creates or modifies this script file. When the other project starts, it starts the Run Script Phase and - voilà - executes whatever has been defined.

like image 29
CouchDeveloper Avatar answered Sep 22 '22 00:09

CouchDeveloper