Is there a way to access variables inside of a .xcconfigfile from the terminal?

466 views Asked by At

I have an .xcconfig file that I want to access via the terminal to access variables in the file. Is there a command or is there some way to do this? For example I have a variable called Build_Code = 1234. How would I access that?

2

There are 2 answers

0
Carl Lindberg On

If it's part of an Xcode project, you can run xcodebuild -showBuildSettings to see a list of the system defaults, plus what is in the default target's config values with variables replaced by values). You can also pass -json to get the results as a json file. For an explicit file, you can use the -xcconfig <path> argument as well. If in a project, it shows that file, then the project. If standalone, it just shows the values in that file. Unfortunately, -json does not seem to work when using that option. You would still have to parse the result a bit, but those values are more normalized, with comments stripped, from the original xcconfig. includes are processed but variables are not replaced in the -xcconfig mode.

0
Nic3500 On

Create a script to read the value of a variable.

Ex: .xconfig

var1 = value1
var2 = value2

get_value.bash

#!/bin/bash
#
# get_value.bash <file> <variable>
#
usage()
{
    echo "Usage: get_value.bash <file> <variable>"
    exit 1
}

#################################################

# Arguments
if [[ $# -eq 2 ]]
then
    file="$1"
    var="$2"
else
    usage
fi

# Check if the file exists
if [[ ! -f "$file" ]]
then
    echo "ERROR: file $file does not exist."
    exit 2
fi

# Get the variable's value
grep -w "$var" "$file" | cut -d'=' -f2 | tr -d ' '
  • This simple version assumes the format of the lines is VARIABLE\s*=\s*VALUE.
  • The tr is to remove spaces around the value.
  • The VALUE cannot contain spaces.
  • The <file> argument could be hard coded if you will only ever check .xconfig

Many other solutions could be conceived, depending on the exact requirements, but this does the basic need you put in your question.