Is it possible to run an Xcode Build Phase Script when running xcodebuild from the command line? - xcode

I have a build phase Run Script in my Xcode project that is required for ensuring the module bundle is up to date. The script runs when actions are executed through the Xcode GUI, and the Swift Package is able to build and run tests successfully. But when I try to run xcodebuild test from the command line for CI/CD, the tests fail because the Run Script did not get executed during the build process.
Is there a way to tell xcodebuild to use the Run Scripts from my Xcode project? Or is there a flag I can use to specify a script for it to run?

Judging by the radio silence, I'm guessing this isn't possible using an xcodebuild flag or changing any Xcode settings.
What I ended up doing as a workaround was splitting build and test commands and then running my script in between.
# build-for-test is used bcus normal build wont build the test bundle, which was important in my use case
xcodebuild build-for-test ...
bash path/to/run-script.sh
xcodebuild test ...
The xcodebuild test command will use the files compiled by the earlier build command, so it won't clean any changes made to the xcode build dir by the run script (which is what I was doing).

Related

XCode: How to link auto generated xcframework

I have a XCode project depends on a xcframework, which would be generated by a external shell script (by xcodebuild -create-xcframework)
the question is: How to make XCode run the shell script before build?
NOTE:
I have added a New Run Script Phase in the Build Phases, but before the shell script being run, the error occurred: There is no XCFramework found at xxx
for auto generated source file, it works fine if it was added to the Output Files in the New Run Script Phase
however, for the xcframework, it does not work, even if the xcframework has been added to Output Files
XCode version: 13.4.1

Replicate Xcode Cmd + R from a command line

What I am trying to achieve is editing code in vim then running make and that would invoke the same functionality as Cmd+R in Xcode: build and launch a simulator with latest changes.
What actual commands are invoked and what parameters are used in this process? Is there a way to view this from Xcode?
Try running xcodebuild from project's directory. Running with --help parameter will reveal all the stuff that you need - which project to build, which target to run, which sdk to use (iphoneos or iphonesimulator). Usually the call looks like this:
xcodebuild -project MyProject -target MyApplication -configuration Release -sdk iphonesimulator5.1

Xcode Run script Build Phase "run script only when installing" option

There is a "run script only when installing" option in Xcode Run script Build Phase, I am not able to find documentation on this. What does it do?
With Run script only when installing checked, the script only runs when do Product Archive.
In the Xcode Build System Guide (Xcode 3.2.x), it says:
Run script only when installing. Runs the script only during install builds, that is, when using the install option of xcodebuild or when the build settings Deployment Location (DEPLOYMENT_LOCATION) and Deployment Postprocessing (DEPLOYMENT_POSTPROCESSING) are on.

Can anyone give an example of how to configure Bamboo to build an Xcode project?

I've been searching for how to configure Bamboo (a continuous integration system) to build an Xcode project. This should be pretty simple, as it is just getting it to run a shell command such as:
xcodebuild -project ProjectName -target TargetName -configuration ConfigType
However this is proving more difficult than expected. I've investigated creating a "custom builder" for the xcodbuild command (tells Bamboo about the command) but then it only seems to let you pass ONE argument to the command not the multiple that the xcodebuild command requires. Any help or pointers would be much appreciated, including links to any appropriate examples (I couldn't find any.) Thanks.
OK, I got a "HelloWorld" example working by choosing the "script" option with a shell script rather than a "custom builder" and trying to tell Bamboo how to use xcodebuild command directly. Just specified a script like below.
#!/bin/bash
/usr/bin/xcodebuild -project TestProject/TestProject.xcodeproj -target TestProject -configuration Release build
That might have done the trick but there is a more generic way to do it.
You can define in your agent capabilities as a Command to execute /usr/bin/xcodebuild. Then in your tasks after you checkout your code, you can define a task Command, and you can select Xcode from the drop down list and provide the arguments needed for the project in the relevant input -workspace YourProject.xcworkspace -scheme YourProject. This approach provides more flexibility since Xcode installation can differ from agent to agent but bamboo will still be able to forward your builds properly where they can be build.

Run a bash script in Xcode only when "Build & Run"

Is there a way, in Xcode, to run a bash script in a build phase, only when I "Build & Run" and not in all other occasions?
Actually, what I would like to do, is to write a script to update the build number every time I build and run a new version on the device.
Xcode does not tell your script why it's building — that is, if it's going to run the app afterwards or not —; only that it is building. Also note that selecting 'Run' as opposed to 'Build & Run' does not run scripts in Run Script phases.
If you build in other occasions, you can pass build settings to xcodebuild (if building from the command line) or by selecting a different build configuration (if building from the Xcode application), which you can refer to in your build scripts as environment variables. For example:
if [ "$CONFIGURATION" == "Debug" ]; then
# increment build number
fi
(This script uses the CONFIGURATION built-in build setting, but you can create custom build settings if needed.)

Resources