Xcode 8 add Capabilities from terminal - ios

Have I ability to enable in capabilities Push Notification from bash?
My problem that my builds are using CI.
If I build debug builds with provision, that has app bundle with wildcard *
And I will enable push notification in capabilities debug build will not compile. Because wildcard provision not support Push Notifications
But I need it for release build with normal provision. In this case I need enable push notification in capabilities.
I want to enable it with script if it is real, only for release.
If someone know another approach will be glad to hear.
Will be appreciated for any help.
Thanks

Script I wrote to enable Push Notifications in a Cordova project.
You'll need to modify it to set the DevelopmentTeam to the correct value.
hooks/before_compile/capabilities.sh
#!/bin/sh
# Abort on Error
set -e
APP_NAME=$(sed -e 's/xmlns.*/>/g' config.xml | xmllint --xpath '/widget/name/text()' -)
PROJECT=`find platforms/ios/${APP_NAME} -name project.pbxproj`
# Exit if not required
grep 'TargetAttributes' ${PROJECT} > /dev/null && exit
# Backup
set -x
cp ${PROJECT} ${PROJECT}.orig
# Get ID
ID=`grep -A 1 'Begin PBXNativeTarget section' ${PROJECT} | tail -n 1 | cut -d ' ' -f 1 | tr -d '\t'`
# Inject
sed -i '' -e "/LastUpgradeCheck.*$/a\\
TargetAttributes = {\\
${ID} = {\\
DevelopmentTeam = ABCD1234YZ;\\
SystemCapabilities = {\\
com.apple.Push = {\\
enabled = 1;\\
};\\
};\\
};\\
};" ${PROJECT}
# Compare
diff ${PROJECT}.orig ${PROJECT}

Related

How to read current app version in Xcode 11 with script

Until Xcode 11, I used a script that reads the current app version (for the AppStore) and help me change the LaunchScreen since we can't use swift for that.
sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
versionNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
sed -i .bak -e "/userLabel=\"APP_VERSION_LABEL\"/s/text=\"[^\"]*\"/text=\"v$versionNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
But in Xcode 11 there is a new section inside the project's build settings called Versioning
And CFBundleShortVersionString automatically changed to $(MARKETING_VERSION). Xcode automatically handles that and I don't want to change it manually to an static number and let Xcode do it's work.
So the question is how can I access this new MARKETING_VERSION and set it to my launchScreen label using run script?
Xcode 11/12
In terminal or bash script in your project you can use:
App version
xcodebuild -showBuildSettings | grep MARKETING_VERSION | tr -d 'MARKETING_VERSION =' // will be displayed 1.1.6
Build version
xcodebuild -showBuildSettings | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION =' // will be displayed 7
Or (don't forget to change YouProjectName to your project name):
App version
cat YouProjectName.xcodeproj/project.pbxproj | grep -m1 'MARKETING_VERSION' | cut -d'=' -f2 | tr -d ';' | tr -d ' '
Build version
cat YouProjectName.xcodeproj/project.pbxproj | grep -m1 'CURRENT_PROJECT_VERSION' | cut -d'=' -f2 | tr -d ';' | tr -d ' '
Or slower method (Thx Joshua Kaden):
App version
xcodebuild -project YouProjectName.xcodeproj -showBuildSettings | grep "MARKETING_VERSION" | sed 's/[ ]*MARKETING_VERSION = //'
Build version
xcodebuild -project YouProjectName.xcodeproj -showBuildSettings | grep "CURRENT_PROJECT_VERSION" | sed 's/[ ]*CURRENT_PROJECT_VERSION = //'
Couldn't find right answer on the internet, so I started digging.
Version and build numbers are displayed in ./PROJECTNAME.xcodeproj/project.pbxproj as MARKETING VERSION (MV) and CURRENT PROJECT VERSION (CPV).
I used sed to get the numbers. It finds first occurrence of MV or CPV, removes everything except the number, and returns result. In order for this to work, you need to do 2 things:
navigate to projects root folder
change PROJECTNAME to your project's name
Commands:
version_number=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./PROJECTNAME.xcodeproj/project.pbxproj`
build_number=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./PROJECTNAME.xcodeproj/project.pbxproj`
Result:
Note: If you have more targets in your workspace with different version and build numbers, this might or might not work for you, because it stops on first occurrence. In that case, good luck :)
You can use it like any other project variable:
sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
versionNumber="$MARKETING_VERSION"
buildNumber="$CURRENT_PROJECT_VERSION"
sed -i .bak -e "/userLabel=\"APP_VERSION_LABEL\"/s/text=\"[^\"]*\"/text=\"v$versionNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
I miss here a solution for multiple targets and configurations:
xcodebuild -target <target> -configuration <configuaration> -showBuildSettings | grep -i 'MARKETING_VERSION' | sed 's/[ ]*MARKETING_VERSION = //'
target: the name of the target
configuration: Release, Debug
If you use Bitrise then the following script will save you:
Extracting App Marketing Version
envman add --key=APP_VERSION_NO --value=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./${PATH_TO_YOUR_PROJECT_XCODEPROJ_FILE}/project.pbxproj`
Extracting App Build Number
Extract the Build Number from xcodeproj file only if you use Apple Generic versioning system otherwise extract the Build Number from the XCode project info.plist file.
Note: The following script extracts the Build Number from the xcodeproj file.
envman add --key=APP_BUILD_NO --value=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./${PATH_TO_YOUR_PROJECT_XCODEPROJ_FILE}/project.pbxproj`
Printing to console:
envman run bash -c 'echo "App Version: $APP_VERSION_NO"'
envman run bash -c 'echo "App Build No: $APP_BUILD_NO"'
Thanks to the answer by #babac
Most voted answers by now show how to extract the value through sed and further tools in the chain. Thought to provide a (simpler?) solution just through awk.
Just to give a little bit of context, following one-liner shows the culprit row:
xcodebuild -project MyProj/MyProj.xcodeproj -showBuildSettings | awk '/MARKETING_VERSION/ { print }'
# output
MARKETING_VERSION = 0.0.1
Default awk field separator should be the space, and so it's just a matter of extracting the third field (MARKETING_VERSION is first, the equal sign is second):
xcodebuild -project MyProj/MyProj.xcodeproj -showBuildSettings | awk '/MARKETING_VERSION/ { print $3 }'
# output
0.0.1
HTH
How about saving a value to CURRENT_PROJECT_VERSION ? did anyone managed to do this?
I can get the value like
buildNumber=$CURRENT_PROJECT_VERSION
but this doesn't work:
CURRENT_PROJECT_VERSION="" or $CURRENT_PROJECT_VERSION=""
In my case I'm trying to set it to ""
This line doesn't set the CURRENT_PROJECT_VERSION field too
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$appInfoPlist"
If your project is set up to use Apple Generic Versioning then you may use this command:
agvtool what-marketing-version -terse1
More info on how to set up AGV can be found here.
For Node.js: there is xcode package. Example of usage:
const xcode = require('xcode');
const project = xcode.project('ios/PROJECT_NAME.xcodeproj/project.pbxproj').parse(() => {
const config = project.pbxXCBuildConfigurationSection();
const releaseScheme = Object.keys(config).find(key => config[key].name === 'Release');
const version = config[releaseScheme].buildSettings.MARKETING_VERSION;
});
Previously I used the plist package, but with latest xCode changes it became outdated, since I'm not able to extract a version from Info.plist for React Native projects.
I had similar issue and made it work by displaying MARKETING_VERSION itself:
version="$MARKETING_VERSION"
version+=" ("
version+=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $SRCROOT/MyApp/Info.plist`
version+=")"
/usr/libexec/PlistBuddy "$SRCROOT/MyApp/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:1:DefaultValue $version"
I'm developing a framework with this scenario:
A workspace
A framework target
An aggregate target with 2 external scripts:
one for build a fat framework
other for prepare the release framework
The key is that in XCode 11 the aggregate framework script doesn't get run environment variables for other workspace targets so it's impossible to read the $MARKETING_VERSION from my framework target.
So the solution that works for me has been use PlistBuddy specifying the Info.plist result of the framework target build in this way:
FAT_FRAMEWORK="${SRCROOT}/build/${FRAMEWORK_NAME}.framework"
BUILD_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${FAT_FRAMEWORK}/Info.plist")
VERSION_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${FAT_FRAMEWORK}/Info.plist")

Trigger specific job on push to specific directory

We have 12 different projects inside the same repository and have a different job to run for each of these.
I want to know how I can trigger a job only when a change has happened in a specific folder, since running all 12 on every push takes too long to finish.
Well I have hacked a solution that works for us.
First, add an Execute Shell Build Step:
#!/bin/bash
export DIRS="api objects"
DIFF=`git diff --name-only develop`
echo "export RUN_TEST=0" > "$WORKSPACE/RUN_TEST"
for DIR in $DIRS; do
for LINE in $DIFF; do
# Is this file inside an interesting directory?
echo $LINE | grep -e "^$DIR/"
# Checking if it is inside
if [ $? -eq 0 ]; then
echo "export RUN_TEST=1" > "$WORKSPACE/RUN_TEST"
fi
done
done
Here:
api and objects are the 2 directories I want to trigger this Job
develop is the main branch we use, so I want to know how my directories compare to that branch in particular
I create a file $WORKSPACE/RUN_TEST to set a variable if I should or not run it
Then in the time-consuming build steps add:
#!/bin/sh
. "$WORKSPACE/RUN_TEST"
if [ $RUN_TEST -eq 1 ]; then
# Time consuming code here
fi
That way the job is triggered but runs as fast as if it wasn't triggered.
Now I modified it to:
#!/bin/bash
export DIRS="api objects"
DIFF=`git diff --name-only origin/develop`
RUN_TEST=111
for DIR in $DIRS; do
for LINE in $DIFF; do
# Is this file inside an interesting directory?
echo $LINE | grep -e "^$DIR/"
# Checking if it is inside
if [ $? -eq 0 ]; then
RUN_TEST=0
fi
done
done
echo "RUN_TEST=$RUN_TEST"
echo "return $RUN_TEST" > "$WORKSPACE/RUN_TEST"
exit $RUN_TEST
And set Exit code to set build unstable to 111 on all build steps. Then, in all following build steps I did:
#!/bin/bash
# Exit on any error
set -euo pipefail
. "$WORKSPACE/RUN_TEST"
# Rest of build step

Best practice for using Fabric/Crashlytics with open-source app?

I have an iOS app that I'm about to open source. I don't want to include my key and secret in the Run Script code when the the app is live for everyone to look at, fork, download etc. for obvious reasons.
What is the best way to still use Fabric/Crashlytics but also keep those keys secure so that only those who can deploy the app have access to those credentials?
Here's a way:
1 - Store fabric keys in a local file.
<apiKey>
<secretKey>
2 - In your cocoa pods run script phase (under Build Phases in Xcode), have your script grab the api key and secret key from the local file.
apiKey=$(sed -n '1p' < localFile.txt)
secretKey=$(sed -n '2p' < localFile.txt)
3 - Use PlistBuddy in the cocoa pods run script phase to set the API Key into your Info.plist file. Something like this:
/usr/libexec/PlistBuddy -c "Set :Fabric:APIKey string $apiKey" $(PROJECT_DIR)/Info.plist
4 - Call the cocoa pods run command.
"${PODS_ROOT}/Fabric/run" $apiKey $secretKey
Edit: full script
apiKey=$(sed -n '1p' < localFile.txt)
secretKey=$(sed -n '2p' < localFile.txt)
/usr/libexec/PlistBuddy -c "Set :Fabric:APIKey string $apiKey" $(PROJECT_DIR)/Info.plist
"${PODS_ROOT}/Fabric/run" $apiKey $secretKey
NOTE -
$(PROJECT_DIR)/Info.plist may not be the correct path for your particular project.
Jake's answer didn't work for me. Apparently $(PROJECT_DIR)/Info.plist returns an incorrect path.
But here's a working example for a project that uses both Fabric and Google sign in. (This works perfectly in Xcode 9 as of July 2018)
FabricApiKey=$(sed -n '2p' < app_config.txt)
FabricSecretKey=$(sed -n '4p' < app_config.txt)
GoogleReversedClientId=$(sed -n '6p' < app_config.txt)
INFO_PLIST="$BUILT_PRODUCTS_DIR/$INFOPLIST_PATH"
/usr/libexec/PlistBuddy -c "Set :Fabric:APIKey $FabricApiKey" "$INFO_PLIST"
/usr/libexec/PlistBuddy -c "Set :CFBundleURLTypes:0:CFBundleURLSchemes:0 $GoogleReversedClientId" "$INFO_PLIST"
"${PODS_ROOT}/Fabric/run" $FabricApiKey $FabricSecretKey
and here's the structure of app_config.txt file that must be present at the project root:
FabricApiKey:
your_key_here
FabricSecretKey:
your_secret_here
GoogleReversedClientId:
google_reversed_client_id_here
Excellent post by #Jake - thank-you! This is my variation which checks for the existence of the secrets file, and uses some environment variables provided by Xcode 9.4.
#1/usr/bin/env sh
secretsFile=$PROJECT_DIR/scripts/fabric.secrets
if [ ! -f $secretsFile ]; then
echo "warning: '$secretsFile' not found"
exit 0
fi
apiKey=$(sed -n '1p' < $secretsFile)
secretKey=$(sed -n '2p' < $secretsFile)
/usr/libexec/PlistBuddy -c "Set :Fabric:APIKey $apiKey" $PRODUCT_SETTINGS_PATH
$PROJECT_DIR/Fabric.framework/run $apiKey $secretKey
Note: the echo "warning: " part will be detected by Xcode, and put into the build log as a yellow warning.
Finally, here's a pre-commit git hook to check for a 40-character hex string accidentally being added to the Info.plist:
#!/usr/bin/env sh
files=$(ls */Info.plist)
git diff --cached --name-status | while read modificationtype thisfile; do
if [ "$modificationtype" == 'D' ]; then continue; fi
for file in $files
do
if [ ! "$thisfile" == "$file" ]; then continue; fi
if egrep '[0-9a-fA-F]{40}' $file ; then
echo "ERROR: API key in file: ${file}"
exit 1
fi
done
done || exit $?

Finding bundle identifier in XCode project using shell script

A Bundle identifier in Info.plist of an Xcode project could have various forms, for e.g.
com.company.$(PRODUCT_NAME:rfc1034identifier)
$(PRODUCT_BUNDLE_IDENTIFIER)
Someone could design their own product bundle identifier for debug, release etc types of build and write a variable against Bundle identifier, e.g. com.company.$(PRODUCT_NAME:rfc1034identifier).$(someRandomVariable)
I want to write a shell script that just reads the bundle identifier properly.
However, if you only know shell script - I know, how to figure out values of variables in $(), but want a shell script that should give me all such variables in the string and then I will have code to figure out their values, post which I will create string back with the variables replaced with values.
function getBundleIdentifier
{
cfBundleIdentifier=${PRODUCT_BUNDLE_IDENTIFIER}
if [ ${#cfBundleIdentifier} -lt 1 ]; then
SOURCE="rfc1034identifier"
cfBundleIdentifier=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${PROJECT_DIR}/${INFOPLIST_FILE}")
if echo "$cfBundleIdentifier" | grep -q "$SOURCE"; then
echo `eval echo $cfBundleIdentifier``eval echo ${PRODUCT_NAME:rfc1034identifier}`
else
echo `eval echo $cfBundleIdentifier`
fi
else
echo $cfBundleIdentifier
fi
}
This is what I have written, but it does not cover all the cases.
It's very easy, just run this command:
BUNDLE_ID=`xcodebuild -showBuildSettings | grep PRODUCT_BUNDLE_IDENTIFIER`
echo $BUNDLE_ID
you can use this
xcodebuild -project Myproject.xcodeproj \-showBuildSettings | grep PRODUCT_BUNDLE_IDENTIFIER | awk -F ' = ' '{print $2}'
or just consolidated the non-xcode portion into :
xcodebuild……. |
{m,g}awk '$!NF = $(NF=2*/PRODUCT_BUNDLE_IDENTIFIER/)' FS=' = '
To get get the bundle identifier in a shell script, I found this to be the simplest way:
xcodebuild -showBuildSettings | awk -F ' = ' '/PRODUCT_BUNDLE_IDENTIFIER/ { print $2 }'
If your script works currently in a different path, use -project option:
xcodebuild -project Myproject.xcodeproj -showBuildSettings | awk -F ' = ' '/PRODUCT_BUNDLE_IDENTIFIER/ { print $2 }'

Running iOS UIAutomation as a post-action build script is return as a posix spawn error

I'm entirely new to using bash and Xcode build scripts and so my code is probably a jungle full of errors.
The idea here is to trigger the script below which will scrape the directory that it is saved in for any .js automation scripts. It will then send these scripts to instruments to be run one at a time. I found some nifty code that created time stamped files and so I used that to create a more meaningful storage system.
#!/bin/bash
# This script should run all (currently only one) tests, independently from
# where it is called from (terminal, or Xcode Run Script).
# REQUIREMENTS: This script has to be located in the same folder as all the
# UIAutomation tests. Additionally, a *.tracetemplate file has to be present
# in the same folder. This can be created with Instruments (Save as template...)
# The following variables have to be configured:
#EXECUTABLE="Plans.app"
# Find the test folder (this script has to be located in the same folder).
ROOT="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Prepare all the required args for instruments.
TEMPLATE=`find $ROOT -name '*.tracetemplate'`
#EXECUTABLE=`find ~/Library/Application\ Support/iPhone\ Simulator | grep "${EXECUTABLE}$"`
echo "$BUILT_PRODUCTS_DIR"
echo "$PRODUCT_NAME"
EXECUTABLE="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/"
SCRIPTS=`find $ROOT -name '*.js'`
# Prepare traces folder
TRACES="${ROOT}/Traces/`date +%Y-%m-%d_%H-%M-%S`"
mkdir -p "$TRACES"
printf "\n" >> "$ROOT/results.log"
echo `date +%Y-%m-%d_%H-%M-%S` >> "$ROOT/results.log"
# Get the name of the user we should use to run Instruments.
# Currently this is done, by getting the owner of the folder containing this script.
USERNAME=`ls -l "${ROOT}/.." | grep \`basename "$ROOT"\` | awk '{print $3}'`
# Bring simulator window to front. Depending on the localization, the name is different.
osascript -e 'try
tell application "iPhone Simulator" to activate
on error
tell application "iOS Simulator" to activate
end try'
# Prepare an Apple Script that promts for the password.
PASS_SCRIPT="tell application \"System Events\"
activate
display dialog \"Password for user $USER:\" default answer \"\" with hidden answer
text returned of the result
end tell"
# Run all the tests.
for SCRIPT in $SCRIPTS; do
echo -e "\nRunning test script $SCRIPT"
TESTC="sudo -u ${USER} xcrun instruments -l -c -t ${TEMPLATE} ${EXECUTABLE} -e UIARESULTSPATH ${TRACES}/${TRACENAME} -e UIASCRIPT ${SCRIPT} >> ${ROOT}/results.log"
#echo "$COMMAND"
echo "Executing command $TESTC" >> "$ROOT/results.log"
echo "here $TESTC" >> "$ROOT/results.log"
OUTPUT=$(TESTC)
echo $OUTPUT >> "$ROOT/results.log"
echo "Finished logging" >> "$ROOT/results.log"
SCRIPTNAME=`basename "$SCRIPT"`
TRACENAME=`echo "$SCRIPTNAME" | sed 's_\.js$_.trace_g'`
for i in $(ls -A1t $PWD | grep -m 1 '.trace')
do
TRACEFILE="$PWD/$i"
done
if [ -e $TRACEFILE ]; then
mv "$TRACEFILE" "${TRACES}/${TRACENAME}"
fi
if [ `grep " Fail: " results.log | wc -l` -gt 0 ]; then
echo "Test ${SCRIPTNAME} failed. See trace for details."
open "${TRACES}/${TRACENAME}"
exit 1
break
fi
done
rm results.log
A good portion of this was taken from another Stack Overflow answer but because of the repository setup that I'm working with I needed to keep the paths abstract and separate from the root folder of the script. Everything seems to work (although probably not incredibly efficiently) except for the actual xcrun command to launch instruments.
TESTC="sudo -u ${USER} xcrun instruments -l -c -t ${TEMPLATE} ${EXECUTABLE} -e UIARESULTSPATH ${TRACES}/${TRACENAME} -e UIASCRIPT ${SCRIPT} >> ${ROOT}/results.log"
echo "Executing command $TESTC" >> "$ROOT/results.log"
OUTPUT=$(TESTC)
This is turned into the following by whatever black magic Bash runs on:
sudo -u Braains xcrun instruments -l -c -t
/Users/Braains/Documents/Automation/AppName/TestCases/UIAutomationTemplate.tracetemplate
/Users/Braains/Library/Developer/Xcode/DerivedData/AppName-
ekqevowxyipndychtscxwgqkaxdk/Build/Products/Debug-iphoneos/AppName.app/ -e UIARESULTSPATH
/Users/Braains/Documents/Automation/AppName/TestCases/Traces/2014-07-17_16-31-49/ -e
UIASCRIPT /Users/Braains/Documents/Automation/AppName/TestCases/Test-Case_1js
(^ Has inserted line breaks for clarity of the question ^)
The resulting error that I am seeing is:
posix spawn failure; aborting launch (binary ==
/Users/Braains/Library/Developer/Xcode/DerivedData/AppName-
ekqevowxyipndychtscxwgqkaxdk/Build/Products/Debug-iphoneos/AppName.app/AppName).
I have looked all over for a solution to this but I can't find anything because Appium has a similar issue. Unfortunately I don't understand the systems well enough to know how to translate the fixes to Appium to my own code but I imagine it's a similar issue.
I do know that the posix spawn failure is related to threading, but I don't know enough about xcrun to say what's causing the threading issue.
Related info:
- I'm building for the simulator but it'd be great to work on real devices too
- I'm using xCode 5.1.1 and iOS Simulator 7.1
- This script is meant to be run as a build post action script in xCode
- I did get it briefly working once before I broke it and couldn't get it back to the working state. So I think that means all of my permissions are set correctly.
UPDATE: So I've gotten to the root of this problem although I have not found a fix yet. First of all I have no idea what xcrun is for and so I dropped it. Then after playing around I found that my Xcode environment variables are returning the wrong path, probably because of some project setting somewhere. If you copy the Bash command from above but replace Debug-iphoneos with Debug-iphonesimulator the script can be run from the command line and will work as expected.
So for anyone who happens across this the only solution I could find was to hardcode the script for the simulator.
I changed EXECUTABLE="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/" to be EXECUTABLE="${SYMROOT}/Debug-iphonesimulator/${EXECUTABLE_PATH}". This is obviously not a great solution but it works for now.

Resources