Skip to content

Instantly share code, notes, and snippets.

@usagimaru
Last active October 29, 2023 03:24
Show Gist options
  • Save usagimaru/60a9840fa0ad43a8d59f3182aa814ff4 to your computer and use it in GitHub Desktop.
Save usagimaru/60a9840fa0ad43a8d59f3182aa814ff4 to your computer and use it in GitHub Desktop.
A shell script to add some environment variables to Xcode's scheme to suppress useless console outputs. (e.g. os_log messages)
#!/bin/sh
function printHelp () {
printBlueBold "===================="
printBlueBold "Add useful environment variables to a scheme of Xcode."
printBlueBold "Usage:"
printBlueBold " %% THIS_SCRIPT.sh AnyProject.xcodeproj"
printBlueBold "===================="
}
function printRed () {
local input=$1
printf "\e[31m${input}\e[m\n"
}
function printBlueBold () {
local input=$1
printf "\e[34;1m${input}\e[m\n"
}
# ヘルプを出力
printHelp
# 入力引数が不正の場合はエラーとして終了
if [ $# != 1 ]; then
printRed "Input error."
exit 1
fi
# 入力
inputfile=$1
# "ファイル名.拡張子"
filename_ext=${inputfile##*/}
# ファイル名から拡張子を取り除く
filename=${filename_ext%.*}
# schemeファイルのパス
schemefile="$inputfile/xcshareddata/xcschemes/$filename.xcscheme"
# 操作対象ノード
targetNode='//LaunchAction[contains(@buildConfiguration,"Debug")]'
if xmlstarlet sel -Q -t -c "$targetNode/EnvironmentVariables" $schemefile; then
echo "Node \"EnvironmentVariables\" already exists... pass"
else
echo "Node \"EnvironmentVariables\" does not exist and add it"
xmlstarlet ed --inplace \
--subnode $targetNode \
--type elem -n "EnvironmentVariables" \
$schemefile
fi
if xmlstarlet sel -Q -t -c "$targetNode/EnvironmentVariables/"'EnvironmentVariable[contains(@key,"OS_ACTIVITY_MODE")]' $schemefile; then
echo "EnvironmentVariable \"OS_ACTIVITY_MODE\" already exists... pass"
else
echo "EnvironmentVariable \"OS_ACTIVITY_MODE\" does no exist and add it"
xmlstarlet ed --inplace \
--subnode "$targetNode/EnvironmentVariables" \
--type elem -n "EnvironmentVariable_" \
--insert "$targetNode/EnvironmentVariables/EnvironmentVariable_" \
--type attr -n "key" -v "OS_ACTIVITY_MODE" \
--insert "$targetNode/EnvironmentVariables/EnvironmentVariable_" \
--type attr -n "value" -v "disable" \
--insert "$targetNode/EnvironmentVariables/EnvironmentVariable_" \
--type attr -n "isEnabled" -v "YES" \
$schemefile
sed -i -E 's/EnvironmentVariable_/EnvironmentVariable/' $schemefile
fi
if xmlstarlet sel -Q -t -c "$targetNode/EnvironmentVariables/"'EnvironmentVariable[contains(@key,"IDELogRedirectionPolicy")]' $schemefile; then
echo "EnvironmentVariable \"IDELogRedirectionPolicy\" already exists... pass"
else
echo "EnvironmentVariable \"IDELogRedirectionPolicy\" does not exist and add it"
xmlstarlet ed --inplace \
--subnode "$targetNode/EnvironmentVariables" \
--type elem -n "EnvironmentVariable_" \
--insert "$targetNode/EnvironmentVariables/EnvironmentVariable_" \
--type attr -n "key" -v "IDELogRedirectionPolicy" \
--insert "$targetNode/EnvironmentVariables/EnvironmentVariable_" \
--type attr -n "value" -v "oslogToStdio" \
--insert "$targetNode/EnvironmentVariables/EnvironmentVariable_" \
--type attr -n "isEnabled" -v "YES" \
$schemefile
sed -i -E 's/EnvironmentVariable_/EnvironmentVariable/' $schemefile
fi
echo "Result:"
xmlstarlet sel -t -c "$targetNode/EnvironmentVariables" $schemefile | xmllint --format - | sed 's/<?xml version="1.0"?>//' | sed -E '/^$/d' | highlight --out-format=ansi --syntax=xml
#highlight for XML syntax
#http://www.andre-simon.de/doku/highlight/en/highlight.php
@usagimaru
Copy link
Author

usagimaru commented Oct 29, 2023

Introduction

This script adds the environment variables OS_ACTIVITY_MODE=disable and IDELogRedirectionPolicy=oslogToStdio, which is much easier than working with the GUI.
When using the script, close the target Xcode project before running it.

More info of IDELogRedirectionPolicy:
https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes

Usage

% ./add_xcode_envs.sh /path/to/AnyProject.xcodeproj

Close the Xcode project before running it.

Required

You can install these with Homebrew.

  • xmlstarlet
  • xmllint
  • highlight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment