2

I have an iOS for which I want to run a build phase that reads a value from a JSON file, export that as an environment variable and then read that in my Info.plist.

I currently have:

enter image description here enter image description here

# Build Scripts/SetAPIVersion
set -e

if ! which jq > /dev/null; then
echo "error: jq is missing. Be sure to git pull `dev-box` and run apply.sh"
exit 1
fi

export API_VERSION =$(cat ../src/version.json | jq .api)

echo "Set API Version to $(API_VERSION)!"

My application will build however the value does not appear to be set. What am I doing wrong here?

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263

2 Answers2

2

You can use this:

plutil -replace APIVersion -string <VERSION> <PATH TO INFO>.plist

Also you can use PlistBuddy:

/usr/libexec/PlistBuddy -c "Set :APIVersion string <VERSION>" <PATH TO INFO>.plist

If versions are static numbers depending on environment, you can use project build settings user defined variable to:

user defined variable`

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • Does it work in the latest XCode? I am trying to make it work but it doesn't put any value in the plist. – denis_lor Jan 28 '19 at 14:41
  • I tried with the latest XCode but since it runs in parallel the Build Phase > Run Script it is like the plist is not yet created when it launch the command. I am not sure if I have to put anything else apart from the script, maybe some Input File? – denis_lor Jan 28 '19 at 14:59
  • I actually had to put two reference files: $(SRCROOT)/${INFOPLIST_PATH} and $(SRCROOT)/${TARGET_BUILD_DIR} in Input Files under the Shell bin/shell script window. it's like saying to XCode don't run the script untile those files exists – denis_lor Jan 28 '19 at 15:02
1

The shell interpreter is run as a subprocess. When it exports an environment variable, that only affects that shell interpreter process and its subprocesses, but doesn't affect the parent process (i.e. Xcode) nor its sibling processes (other build phases).

You can make the shell script build phase take an input file, say Info.plist.in, and produce Info.plist from that. It would transform the input to the output however you like. For example, it could use sed to replace a special string with the value it should have. Be sure to configure the inputs and outputs of the run-script build phase as appropriate.

Alternatively, you could have the run-script build phase produce a header file that defines a macro, say api_version.h which #defines API_VERSION, #include that header file in your Info.plist, and enable preprocessing of Info.plist in the build settings. Again, make sure the inputs and outputs of the run-script phase are correct.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154