Get an xcode project's bundle id from bash - xcode

How can I get the bundle identifier of the project from bash?
I've got a couple of similar xcode projects for iOS. I'm trying to get some thing done via scripts but can't figure out how to get/change project configurations (like bundle id and version) from bash.
How can I get the bundle identifier of the project from bash?

Run the following command inside the code directory:
$ grep -R PRODUCT_BUNDLE_IDENTIFIER *
$ grep -R PRODUCT_BUNDLE_IDENTIFIER *|awk -F= '{print $2}'|sed 's/ //g'|sed 's/;//g'

Related

sed fails with error "\1 not defined in the RE" when running in Gitlab CI

I am trying to update contents of a file from a variable with sed during Gitlab CI job. The variable comes from artifacts of the previous stage version. If simplified, my job looks something like this:
build-android-dev:
stage: build
dependencies:
- version
only:
- mybranch
before_script:
- PUBSPEC_VERSION="`cat app-pubspec-version`"
- echo "Pubspec version - $PUBSPEC_VERSION"
script:
- >
sed -i -E "s/^(version: )(.+)$/\1${PUBSPEC_VERSION}/g" pubspec.yaml
- cat pubspec.yaml | grep version
interruptible: true
tags:
- macmini-flutter
Unfortunatelly, the job fails with the following error message:
$ sed -i -E "s/^(version: )(.+)$/\1${PUBSPEC_VERSION}/g" pubspec.yaml
sed: 1: "s/^(version: )(.+)$/\14 ...": \1 not defined in the RE
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit status 1
PUBSPEC_VERSION coming from artifacts is the following:
$ echo "Pubspec version - $PUBSPEC_VERSION"
Pubspec version - 4.0.0+2
I am able to execute the command on my local Ubuntu (Linux) machine without any issues:
$ export PUBSPEC_VERSION=4.0.0+2
$ sed -i -E "s/^(version: )(.+)$/\1${PUBSPEC_VERSION}/g" pubspec.yaml
$ cat pubspec.yaml | grep version
version: 4.0.0+2
The remote machine where Gitlab Runner is started is MacOS. Not sure whether it matters.
As you can see, I also use folding style in my CI configuration like proposed here in order to avoid inproper colon interpretation.
I googled for solutions to solve the issue but it seems that I don't need to escape (though I also tried) group parentheses in my regular expression because I use extended regular expression.
So I'm stuck on it...
P.S. I don't have access to the shell of remote MacOS.
is MacOS.
-i takes a suffix argument, so -E is the backup suffix to create. Yuo would want:
- sed -i '' -E 's/...'

Finding latest version of anaconda automatically from bashrc

I'm trying to create a code which will fetch the latest version of anaconda and install it.
Currently we can do this to install the latest version:
mkdir tmp
cd tmp
wget https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh
bash Anaconda3-2020.11-Linux-x86_64.sh
I want the script to be more generalized such that the code would automatically find the latest version of anaconda, download the shell script file and install it.
You can use this to get the latest version:
wget https://repo.anaconda.com/archive/ -q -O- |\
grep 'Anaconda3'| \
sed -n 's|.*>Anaconda3-\([0-9]\{4\}\.[0-9]\{2\}\)-.*|\1|p'
uniq |\
sort -r |\
head -1
This solution works only for those versions that use the year format (e.g. 2020-07), but since the latest version will presumably be of that format that should be fine.
Some explanation:
wget to fetch the contents of the archive page, which gives us the HTML content containing all the download URLs. -q quiets the output, -O- prints to stdout. Alternatively, you can use curl -s to the same effect.
grep 'Anaconda3' gives us the lines containing Anaconda, which contain the download links.
Use sed to select the version strings from the download links, e.g. 2020-11. That gives you a list of all versions (of the format YYYY-MM).
Sort that lists and select the first entry, which is the latest version.
Use the version in the rest of your script and you are done. A complete solution would be:
version=$(wget https://repo.anaconda.com/archive/ -q -O- |\
grep 'Anaconda3'|\
sed -n 's|.*>Anaconda3-\([0-9]\{4\}\.[0-9]\{2\}\)-.*|\1|p' |\
uniq |\
sort -r |\
head -1)
wget "https://repo.anaconda.com/archive/Anaconda3-$version-Linux-x86_64.sh"
I'm sure fetching the latest version could be made more efficient, but this should be sufficient for your use case.

How do I collect the stdout and std error from the .xcresult bundle generated by my XCUI Unit Tests?

In Xcode 10 and below, the stdout and stderror of the test suite run could be found and extracted from inside the specified -resultBundlePath using this shell command:
cp -fv $(find $resultBundlePath/1_Test -print | grep TestSuite1 | grep 'StandardOutputAndStandardError.txt' | xargs) TestSuite1Xctestrun.log"
With Xcode 11+, this file no longer is found in the bundle.
Where and how do I extract it from the xcbundle?
xcrun xcresulttool --formatDescription shows that there is a logRef key with a unique value pointing to the log that we can query for if we know the item's ID.
Using jq, I was able accomplish this task.
First we get the id of the logRef, then we extract its value from the .xcresult bundle into a text file.
# find the id that points to the location of the encoded file in the .xcresult bundle
id=$(xcrun xcresulttool get --format json --path Tests.xcresult | jq '.actions._values[]' | jq -r '.actionResult.logRef.id._value')
# export the log found at the the id in the .xcresult bundle
xcrun xcresulttool export --path Tests.xcresult --id $id --output-path TestsStdErrorStdout.log --type file

Not able to use gsed and grep command on jenkins

I made a script on OSX terminal which utilizes the gsed and grep commands. I tried running this on a jenkins job but it seems that these were incompatible in this unix environment. Here is an example of the code I'm trying to run:
line_formatted=$(gsed -r '/name="adhoc.display"/{s#(.*value=")([^"]*)(".*)#\1$R{\2.LABEL}\3#g;}' <<<"$line")
sed -i bak -e 's|'"${line}"'|'"${line_formatted}"'|g' $topicJRXML_source
adhoc_value=$(grep -oE '{[^/]+}' <<<"$line_formatted" | cut -c2- | rev | cut -c2- | rev)
I am able to assign my formatted lines I'm reading from a file into the variables line_formatted and adhoc_value on the OSX terminal but when these are run on jenkins it seems to fail with the error: gsed: command not found
I was wondering if there is a way to run these commands on jenkins, or if there is an equivalent way to express these lines on jenkins?
You have to have the gsed command installed and in the PATH on the jenkins machine. Is the jenkins machine a linux box? You aren't running these commands in Jenkins. Jenkins runs the commands on the local machine in a shell.

Read Xcode Build Settings from terminal

I found you can get a list of all environment variables from my project`s build settings by doing:
xcodebuild -showBuildSettings -project <project>.xcodeproj
It also prints out the PROVISIONING_PROFILE, which I want to use for a build script
PROVISIONING_PROFILE = d0eff791-6b39-4d9b-a164-3e768f63b333
however if I do a
echo $PROVISIONING_PROFILE
or
sudo echo $PROVISIONING_PROFILE
it prints nothing.
How can I access the ${PROVISIONING_PROFILE} variable from outside XCode, like in terminal or a build script?
Probably not the most elegant solution...
export PROVISIONING_PROFILE=$(xcodebuild -showBuildSettings -project <project>.xcodeproj | grep PROVISIONING_PROFILE | cut -d' ' -c3)

Resources