build.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. set -o pipefail
  3. set -e
  4. usage() {
  5. cat <<EOF
  6. Usage: sh $0 command [argument]
  7. command:
  8. test-all: tests all projects in this repo.
  9. test-ios-objc-static: tests iOS Objective-C static example.
  10. test-ios-objc-dynamic: tests iOS Objective-C dynamic example.
  11. test-ios-objc-cocoapods: tests iOS Objective-C CocoaPods example.
  12. test-ios-objc-cocoapods-dynamic: tests iOS Objective-C CocoaPods Dynamic example.
  13. test-ios-objc-carthage: tests iOS Objective-C Carthage example.
  14. test-ios-swift-dynamic: tests iOS Swift dynamic example.
  15. test-ios-swift-cocoapods: tests iOS Swift CocoaPods example.
  16. test-ios-swift-carthage: tests iOS Swift Carthage example.
  17. test-ios-spm: tests iOS Swift Package Manager example.
  18. test-osx-objc-dynamic: tests macOS Objective-C dynamic example.
  19. test-osx-objc-cocoapods: tests macOS Objective-C CocoaPods example.
  20. test-osx-objc-carthage: tests macOS Objective-C Carthage example.
  21. test-osx-swift-dynamic: tests macOS Swift dynamic example.
  22. test-osx-swift-cocoapods: tests macOS Swift CocoaPods example.
  23. test-osx-swift-carthage: tests macOS Swift Carthage example.
  24. test-osx-spm: tests macOS Swift Package Manager example.
  25. test-watchos-objc-dynamic: tests watchOS Objective-C dynamic example.
  26. test-watchos-objc-cocoapods: tests watchOS Objective-C CocoaPods example.
  27. test-watchos-objc-carthage: tests watchOS Objective-C Carthage example.
  28. test-watchos-swift-dynamic: tests watchOS Swift dynamic example.
  29. test-watchos-swift-cocoapods: tests watchOS Swift CocoaPods example.
  30. test-watchos-swift-carthage: tests watchOS Swift Carthage example.
  31. test-watchos-spm: tests watchOS Swift Package Manager example.
  32. test-tvos-spm: tests tvOS Swift Package Manager example.
  33. EOF
  34. }
  35. COMMAND="$1"
  36. # https://github.com/CocoaPods/CocoaPods/issues/7708
  37. export EXPANDED_CODE_SIGN_IDENTITY=''
  38. download_zip_if_needed() {
  39. LANG="$1"
  40. DIRECTORY=realm-$LANG-latest
  41. if [ ! -d $DIRECTORY ]; then
  42. curl -o $DIRECTORY.zip -L https://static.realm.io/downloads/$LANG/latest
  43. unzip $DIRECTORY.zip
  44. rm $DIRECTORY.zip
  45. mv realm-$LANG-* $DIRECTORY
  46. fi
  47. }
  48. xcode_version_major() {
  49. echo "${REALM_XCODE_VERSION%%.*}"
  50. }
  51. xctest() {
  52. PLATFORM="$1"
  53. LANG="$2"
  54. NAME="$3"
  55. DIRECTORY="$PLATFORM/$LANG/$NAME"
  56. if [[ ! -d "$DIRECTORY" ]]; then
  57. DIRECTORY="${DIRECTORY/swift/swift-$REALM_SWIFT_VERSION}"
  58. fi
  59. PROJECT="$DIRECTORY/$NAME.xcodeproj"
  60. WORKSPACE="$DIRECTORY/$NAME.xcworkspace"
  61. if [[ $PLATFORM == ios ]]; then
  62. sh "$(dirname "$0")/../../scripts/reset-simulators.sh"
  63. fi
  64. if [[ $NAME == CocoaPods* ]]; then
  65. pod install --project-directory="$DIRECTORY"
  66. elif [[ $NAME == Carthage* ]]; then
  67. (
  68. cd "$DIRECTORY"
  69. if [ -n "$REALM_BUILD_USING_LATEST_RELEASE" ]; then
  70. echo "github \"realm/realm-cocoa\"" > Cartfile
  71. else
  72. echo "github \"realm/realm-cocoa\" \"${sha:-master}\"" > Cartfile
  73. fi
  74. if [[ $PLATFORM == ios ]]; then
  75. carthage update --platform iOS
  76. elif [[ $PLATFORM == osx ]]; then
  77. carthage update --platform Mac
  78. elif [[ $PLATFORM == watchos ]]; then
  79. carthage update --platform watchOS
  80. fi
  81. )
  82. elif [[ $LANG == swift* ]]; then
  83. download_zip_if_needed swift
  84. else
  85. download_zip_if_needed $LANG
  86. fi
  87. DESTINATION=""
  88. if [[ $PLATFORM == ios ]]; then
  89. simulator_id="$(xcrun simctl list devices | grep -v unavailable | grep -m 1 -o '[0-9A-F\-]\{36\}')"
  90. xcrun simctl boot $simulator_id
  91. DESTINATION="-destination id=$simulator_id"
  92. elif [[ $PLATFORM == watchos ]]; then
  93. if xcrun simctl list devicetypes | grep -q 'iPhone Xs'; then
  94. DESTINATION="-destination id=$(xcrun simctl list devices | grep -v unavailable | grep 'iPhone Xs' | grep -m 1 -o '[0-9A-F\-]\{36\}')"
  95. fi
  96. fi
  97. CMD="-project $PROJECT"
  98. if [ -d $WORKSPACE ]; then
  99. CMD="-workspace $WORKSPACE"
  100. fi
  101. ACTION=""
  102. if [[ $PLATFORM == watchos ]]; then
  103. ACTION="build"
  104. else
  105. ACTION="build test"
  106. fi
  107. if [[ $PLATFORM == ios ]]; then
  108. xcodebuild $CMD -scheme $NAME clean $ACTION $DESTINATION CODE_SIGN_IDENTITY=
  109. else
  110. xcodebuild $CMD -scheme $NAME clean $ACTION $DESTINATION CODE_SIGN_IDENTITY= CODE_SIGNING_REQUIRED=NO
  111. fi
  112. }
  113. swiftpm() {
  114. PLATFORM="$1"
  115. cd SwiftPMExample
  116. xcrun swift build
  117. }
  118. source "$(dirname "$0")/../../scripts/swift-version.sh"
  119. set_xcode_and_swift_versions # exports REALM_SWIFT_VERSION, REALM_XCODE_VERSION, and DEVELOPER_DIR variables if not already set
  120. PLATFORM=$(echo $COMMAND | cut -d - -f 2)
  121. LANGUAGE=$(echo $COMMAND | cut -d - -f 3)
  122. case "$COMMAND" in
  123. "test-all")
  124. for target in ios-swift-dynamic ios-swift-cocoapods osx-swift-dynamic ios-swift-carthage osx-swift-carthage; do
  125. ./build.sh test-$target || exit 1
  126. done
  127. if (( $(xcode_version_major) >= 11 )); then
  128. for target in ios osx watchos tvos; do
  129. ./build.sh test-$target-spm || exit 1
  130. done
  131. fi
  132. ;;
  133. test-*-*-static)
  134. xctest $PLATFORM $LANGUAGE StaticExample
  135. ;;
  136. test-*-*-dynamic)
  137. xctest $PLATFORM $LANGUAGE DynamicExample
  138. ;;
  139. test-*-*-cocoapods)
  140. xctest $PLATFORM $LANGUAGE CocoaPodsExample
  141. ;;
  142. test-*-*-cocoapods-dynamic)
  143. xctest $PLATFORM $LANGUAGE CocoaPodsDynamicExample
  144. ;;
  145. test-*-*-carthage)
  146. xctest $PLATFORM $LANGUAGE CarthageExample
  147. ;;
  148. test-*-spm)
  149. swiftpm $PLATFORM
  150. ;;
  151. *)
  152. echo "Unknown command '$COMMAND'"
  153. usage
  154. exit 1
  155. ;;
  156. esac