Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to export user defined XCode build settings to environment variables

Tags:

xcode

ios

iphone

I have made a few build settings for various configurations, e.g.

Build settings e.g.

I can access them in various files (e.g. the info.plist) like so:

${MYTESTSETTING}

However is it possible to get the value in the command line enviroment? e.g. after a xcodebuild from Jenkins

I have tried

echo ${MYTESTSETTING}

And

echo $MYTESTSETTING
like image 694
Robert Avatar asked Dec 08 '22 19:12

Robert


1 Answers

xcodebuild -showBuildSettings

shows all build settings, including the user-defined settings. Example:

$ xcodebuild -configuration Debug -showBuildSettings | grep MYTESTSETTING
    MYTESTSETTING = DebugValue
$ xcodebuild -configuration Release -showBuildSettings | grep MYTESTSETTING
    MYTESTSETTING = ReleaseValue

To get these variables into the environment of your current shell, you have to parse this output. This can for example be done with a Perl script (or many other scripting languages).

Create a Perl script "exportsettings.pl" with the following contents:

#!/usr/bin/perl
open(FH, "xcodebuild -configuration Release -showBuildSettings|");
while(<FH>) {
    if (/\s*(\w+)\s*=\s*(.*)$/) { # Search for <key> = <value>
        $key = $1; $value = $2;
        print "export $key='$value'\n";
    }
}
close(FH);

Now you can run the command

$ eval `perl exportsettings.pl`

from the command line, and (almost) all build settings are in the environment. (There will be some error messages, e.g. "UID: readonly variable").

If you need only your used-defined settings in the environment, you could use a unique prefix (e.g. "MY") and change the line

    if (/\s*(\w+)\s*=\s*(.*)$/) { # Search for <key> = <value>

to

    if (/\s*(MY\w+)\s*=\s*(.*)$/) { # Search for MY<key> = <value>
like image 81
Martin R Avatar answered Dec 11 '22 09:12

Martin R