forked from realm/realm-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
find Xcode preferentially without spotlight - v2 (realm#4652)
fixes realm#4534 and realm#4503 by looking in `/Applications` for Xcode before using Spotlight, and skipping the current default Xcode when running under Jenkins.
- Loading branch information
Showing
3 changed files
with
136 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,130 @@ | ||
#!/usr/bin/env bash | ||
|
||
get_swift_version() { | ||
swift_command=$@ | ||
$swift_command --version 2>/dev/null | sed -ne 's/^Apple Swift version \([^\b ]*\).*/\1/p' | ||
"$1" --version 2>/dev/null | sed -ne 's/^Apple Swift version \([^\b ]*\).*/\1/p' | ||
} | ||
|
||
get_xcode_version() { | ||
xcodebuild_command=$@ | ||
$xcodebuild_command -version 2>/dev/null | sed -ne 's/^Xcode \([^\b ]*\).*/\1/p' | ||
"$1" -version 2>/dev/null | sed -ne 's/^Xcode \([^\b ]*\).*/\1/p' | ||
} | ||
|
||
find_xcode_with_version() { | ||
local xcodes dev_dir version | ||
|
||
# First check if the currently active one is fine | ||
version="$(get_xcode_version xcodebuild)" | ||
if [[ "$version" = "$1" ]]; then | ||
export DEVELOPER_DIR="$(xcode-select -p)" | ||
export REALM_SWIFT_VERSION=$(get_swift_version xcrun swift) | ||
local path required_version | ||
|
||
if [ -z "$1" ]; then | ||
echo "find_xcode_with_version requires an Xcode version" >&2 | ||
exit 1 | ||
fi | ||
required_version=$1 | ||
|
||
# First check if the currently active one is fine, unless we are in a CI run | ||
if [ -z "$JENKINS_HOME" ] && [ $(get_xcode_version xcodebuild) = "$required_version" ]; then | ||
DEVELOPER_DIR=$(xcode-select -p) | ||
return 0 | ||
fi | ||
|
||
# Check all installed copies of Xcode for the desired version | ||
xcodes=() | ||
dev_dir="Contents/Developer" | ||
for dir in $(mdfind "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'" 2>/dev/null); do | ||
[[ -d "$dir" && -n "$(ls -A "$dir/$dev_dir")" ]] && xcodes+=("$dir/$dev_dir") | ||
|
||
# Check all of the items in /Applications that look promising per #4534 | ||
for path in /Applications/Xcode*.app/Contents/Developer; do | ||
if [ $(get_xcode_version "$path/usr/bin/xcodebuild") = "$required_version" ]; then | ||
DEVELOPER_DIR=$path | ||
return 0 | ||
fi | ||
done | ||
|
||
for xcode in "${xcodes[@]}"; do | ||
version="$(get_xcode_version "$xcode/usr/bin/xcodebuild")" | ||
if [[ "$version" = "$1" ]]; then | ||
export DEVELOPER_DIR="$xcode" | ||
export REALM_SWIFT_VERSION=$(get_swift_version xcrun swift) | ||
|
||
# Use Spotlight to see if we can find others installed copies of Xcode | ||
for path in $(/usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'" 2>/dev/null); do | ||
path="$path/Contents/Developer" | ||
if [ ! -d "$path" ]; then | ||
continue | ||
fi | ||
if [ $(get_xcode_version "$path/usr/bin/xcodebuild") = "$required_version" ]; then | ||
DEVELOPER_DIR=$path | ||
return 0 | ||
fi | ||
done | ||
|
||
>&2 echo "No Xcode found with version $1" | ||
echo "No Xcode found with version $required_version" >&2 | ||
exit 1 | ||
} | ||
|
||
test_xcode_for_swift_version() { | ||
if [ -z "$1" ] || [ -z "$2" ]; then | ||
echo "test_xcode_for_swift_version called with empty parameter(s): '$1' or '$2'" >&2 | ||
exit 1 | ||
fi | ||
local path=$1 | ||
local required_version=$2 | ||
|
||
for swift in "$path"/Toolchains/*.xctoolchain/usr/bin/swift; do | ||
if [ $(get_swift_version "$swift") = "$required_version" ]; then | ||
return 0 | ||
fi | ||
done | ||
return 1 | ||
} | ||
|
||
find_xcode_for_swift() { | ||
local xcodes dev_dir version | ||
|
||
# First check if the currently active one is fine | ||
version="$(get_swift_version xcrun swift || true)" | ||
if [[ "$version" = "$1" ]]; then | ||
export DEVELOPER_DIR="$(xcode-select -p)" | ||
local path required_version | ||
|
||
if [ -z "$1" ]; then | ||
echo "find_xcode_for_swift requires a Swift version" >&2 | ||
exit 1 | ||
fi | ||
required_version=$1 | ||
|
||
# First check if the currently active one is fine, unless we are in a CI run | ||
if [ -z "$JENKINS_HOME" ] && test_xcode_for_swift_version "$(xcode-select -p)" "$required_version"; then | ||
DEVELOPER_DIR=$(xcode-select -p) | ||
return 0 | ||
fi | ||
|
||
# Check all installed copies of Xcode for the desired Swift version | ||
xcodes=() | ||
dev_dir="Contents/Developer" | ||
for dir in $(mdfind "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'" 2>/dev/null); do | ||
[[ -d "$dir" && -n "$(ls -A "$dir/$dev_dir")" ]] && xcodes+=("$dir/$dev_dir") | ||
# Check all of the items in /Applications that look promising per #4534 | ||
for path in /Applications/Xcode*.app/Contents/Developer; do | ||
if test_xcode_for_swift_version "$path" "$required_version"; then | ||
DEVELOPER_DIR=$path | ||
return 0 | ||
fi | ||
done | ||
|
||
for xcode in "${xcodes[@]}"; do | ||
for swift in "$xcode"/Toolchains/*.xctoolchain/usr/bin/swift; do | ||
version="$(get_swift_version $swift)" | ||
if [[ "$version" = "$1" ]]; then | ||
export DEVELOPER_DIR="$xcode" | ||
return 0 | ||
fi | ||
done | ||
|
||
# Use Spotlight to see if we can find others installed copies of Xcode | ||
for path in $(/usr/bin/mdfind "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'" 2>/dev/null); do | ||
path="$path/Contents/Developer" | ||
if [ ! -d "$path" ]; then | ||
continue | ||
fi | ||
if test_xcode_for_swift_version "$path" "$required_version"; then | ||
DEVELOPER_DIR=$path | ||
return 0 | ||
fi | ||
done | ||
|
||
>&2 echo "No version of Xcode found that supports Swift $1" | ||
return 1 | ||
echo "No version of Xcode found that supports Swift $required_version" >&2 | ||
exit 1 | ||
} | ||
|
||
if [[ "$REALM_XCODE_VERSION" ]]; then | ||
find_xcode_with_version $REALM_XCODE_VERSION | ||
elif [[ "$REALM_SWIFT_VERSION" ]]; then | ||
find_xcode_for_swift $REALM_SWIFT_VERSION | ||
else | ||
REALM_SWIFT_VERSION=$(get_swift_version xcrun swift) | ||
if [[ -z "$DEVELOPER_DIR" ]]; then | ||
export DEVELOPER_DIR="$(xcode-select -p)" | ||
set_xcode_and_swift_versions() { | ||
if [ -n "$REALM_XCODE_VERSION" ]; then | ||
find_xcode_with_version $REALM_XCODE_VERSION | ||
|
||
if [ -n "$REALM_SWIFT_VERSION" ] && ! test_xcode_for_swift_version "$DEVELOPER_DIR" "$REALM_SWIFT_VERSION"; then | ||
echo "The version of Xcode specified ($REALM_XCODE_VERSION) does not support the Swift version required: $REALM_SWIFT_VERSION" | ||
exit 1 | ||
fi | ||
elif [ -n "$REALM_SWIFT_VERSION" ]; then | ||
find_xcode_for_swift $REALM_SWIFT_VERSION | ||
elif [ -z "$DEVELOPER_DIR" ]; then | ||
DEVELOPER_DIR="$(xcode-select -p)" | ||
fi | ||
export DEVELOPER_DIR | ||
export REALM_XCODE_VERSION | ||
|
||
if [ -z "$REALM_SWIFT_VERSION" ]; then | ||
REALM_SWIFT_VERSION=$(get_swift_version "$(xcrun -f swift)") | ||
fi | ||
fi | ||
export REALM_SWIFT_VERSION | ||
} | ||
|
||
return 2>/dev/null || { # only run if called directly | ||
set_xcode_and_swift_versions | ||
echo "Found Swift version $REALM_SWIFT_VERSION in $DEVELOPER_DIR" | ||
} |