download-core.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. source_root="$(dirname "$0")/.."
  4. readonly source_root
  5. # override this env variable if you need to download from a private mirror
  6. : "${REALM_BASE_URL:="https://static.realm.io/downloads/core"}"
  7. # set to "current" to always use the current build
  8. : "${REALM_CORE_VERSION:=$(sed -n 's/^REALM_CORE_VERSION=\(.*\)$/\1/p' "${source_root}/dependencies.list")}"
  9. # Provide a fallback value for TMPDIR, relevant for Xcode Bots
  10. : "${TMPDIR:=$(getconf DARWIN_USER_TEMP_DIR)}"
  11. readonly dst="$source_root/core"
  12. copy_core() {
  13. local src="$1"
  14. rm -rf "$dst"
  15. mkdir "$dst"
  16. ditto "$src" "$dst"
  17. # XCFramework processing only copies the "realm" headers, so put the third-party ones in a known location
  18. mkdir -p "$dst/include"
  19. find "$src" -name external -exec ditto "{}" "$dst/include/external" \; -quit
  20. # Remove the C API header to avoid accidentally pulling it in
  21. find "$dst" -name realm.h -delete
  22. }
  23. tries_left=3
  24. readonly version="$REALM_CORE_VERSION"
  25. readonly url="${REALM_BASE_URL}/v${version}/cocoa/realm-monorepo-xcframework-v${version}.tar.xz"
  26. # First check if we need to do anything
  27. if [ -e "$dst" ]; then
  28. if [ -e "$dst/version.txt" ]; then
  29. if [ "$(cat "$dst/version.txt")" == "$version" ]; then
  30. echo "Version ${version} already present"
  31. exit 0
  32. else
  33. echo "Switching from version $(cat "$dst/version.txt") to ${version}"
  34. fi
  35. else
  36. if [ "$(find "$dst" -name librealm-monorepo.a)" ]; then
  37. echo 'Using existing custom core build without checking version'
  38. exit 0
  39. fi
  40. fi
  41. fi
  42. # We may already have this version downloaded and just need to set it as
  43. # the active one
  44. readonly versioned_name="realm-core-${version}-xcframework"
  45. readonly versioned_dir="$source_root/$versioned_name"
  46. if [ -e "$versioned_dir/version.txt" ]; then
  47. echo "Setting ${version} as the active version"
  48. copy_core "$versioned_dir"
  49. exit 0
  50. fi
  51. echo "Downloading dependency: ${version} from ${url}"
  52. if [ -z "$TMPDIR" ]; then
  53. TMPDIR='/tmp'
  54. fi
  55. temp_dir=$(dirname "$TMPDIR/waste")/realm-core-tmp
  56. readonly temp_dir
  57. mkdir -p "$temp_dir"
  58. readonly tar_path="${temp_dir}/${versioned_name}.tar.xz"
  59. readonly temp_path="${tar_path}.tmp"
  60. while [ 0 -lt $tries_left ] && [ ! -f "$tar_path" ]; do
  61. if ! error=$(/usr/bin/curl --fail --silent --show-error --location "$url" --output "$temp_path" 2>&1); then
  62. tries_left=$((tries_left-1))
  63. else
  64. mv "$temp_path" "$tar_path"
  65. fi
  66. done
  67. if [ ! -f "$tar_path" ]; then
  68. printf "Downloading core failed:\n\t%s\n\t%s\n" "$url" "$error"
  69. exit 1
  70. fi
  71. (
  72. cd "$temp_dir"
  73. rm -rf core
  74. tar xf "$tar_path" --xz
  75. if [ ! -f core/version.txt ]; then
  76. printf %s "${version}" > core/version.txt
  77. fi
  78. mv core "${versioned_name}"
  79. )
  80. rm -rf "${versioned_dir}"
  81. mv "${temp_dir}/${versioned_name}" "$source_root"
  82. copy_core "$versioned_dir"