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!
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.”
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With