build.sh 6.7 KB

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