Skip to content

Instantly share code, notes, and snippets.

@shenjunru
Last active March 5, 2019 14:54
Show Gist options
  • Save shenjunru/2328b60558c769fe4975378c8eaacc7d to your computer and use it in GitHub Desktop.
Save shenjunru/2328b60558c769fe4975378c8eaacc7d to your computer and use it in GitHub Desktop.
download-realm.sh
#!/bin/sh
# file: node_modules/realm/scripts/download-realm.sh
# cwd: node_modules/realm
cd "$(dirname $0)/.."
source ./dependencies.list
VENDOR_DIR="vendor"
LOCKFILE_NAME="download-realm.lock"
NEW_CORE_SERVER_FOLDER="core/v${REALM_CORE_VERSION}"
NEW_SYNC_SERVER_FOLDER="sync"
TARGET_FLAVOR="Release"
ENABLE_DEBUG=""
ENABLE_FORCE=""
ENABLE_SYNC=""
TARGET_ARCH=""
TARGET_OS="$1"
shift
until [[ $# -eq 0 ]]; do
case "$1" in
--arch*)
TARGET_ARCH=$2
shift
shift
;;
--debug*)
ENABLE_DEBUG=1
TARGET_FLAVOR="Debug"
shift
;;
--force*)
ENABLE_FORCE=1
shift
;;
--sync*)
ENABLE_SYNC=1
shift
;;
*)
shift
;;
esac
done
if [[ -d "$TMPDIR" ]]; then
TEMP_DIR="$TMPDIR"
elif [[ -d "$TMP" ]]; then
TEMP_DIR="$TMP"
elif [[ -d /var/tmp ]]; then
TEMP_DIR="/var/tmp"
else
TEMP_DIR="/tmp"
fi
REALM_DIR="${VENDOR_DIR}/realm-${TARGET_OS}"
if [[ "win" == "$TARGET_OS" ]]; then
if [[ -z "$TARGET_ARCH" ]]; then
echo "Specifying '--arch' is required for platform 'win'"
exit 1
fi
if [[ "ia32" == "$TARGET_ARCH" ]]; then
TARGET_ARCH="Win32"
fi
REALM_DIR="${REALM_DIR}-${TARGET_ARCH}"
fi
if [[ "ios" == "$TARGET_OS" ]]; then
if [[ "Debug" == "$TARGET_FLAVOR" ]]; then
TARGET_FLAVOR="MinSizeDebug"
fi
fi
if [[ "1" == "$ENABLE_DEBUG" ]]; then
REALM_DIR="${REALM_DIR}-dbg"
fi
case "$TARGET_OS" in
mac)
NEW_CORE_SERVER_FOLDER="${NEW_CORE_SERVER_FOLDER}/macos/${TARGET_FLAVOR}"
NEW_CORE_ARCHIVE="realm-core-${TARGET_FLAVOR}-v${REALM_CORE_VERSION}-Darwin-devel.tar.gz"
NEW_SYNC_ARCHIVE="realm-sync-node-cocoa-${REALM_SYNC_VERSION}.tar.gz"
NEW_SYNC_ARCHIVE_ROOT="realm-sync-node-cocoa-${REALM_SYNC_VERSION}"
;;
ios)
NEW_CORE_SERVER_FOLDER="${NEW_CORE_SERVER_FOLDER}/ios/${TARGET_FLAVOR}"
NEW_CORE_ARCHIVE="realm-core-${TARGET_FLAVOR}-v${REALM_CORE_VERSION}-iphoneos.tar.gz"
NEW_SYNC_ARCHIVE="realm-sync-cocoa-${REALM_SYNC_VERSION}.tar.gz"
NEW_SYNC_ARCHIVE_ROOT="core"
;;
win)
NEW_CORE_SERVER_FOLDER="${NEW_CORE_SERVER_FOLDER}/windows/${TARGET_ARCH}/nouwp/${TARGET_FLAVOR}"
NEW_CORE_ARCHIVE="realm-core-${TARGET_FLAVOR}-v${REALM_CORE_VERSION}-Windows-${TARGET_ARCH}-devel.tar.gz"
NEW_SYNC_ARCHIVE="realm-sync-${TARGET_FLAVOR}-v${REALM_SYNC_VERSION}-Windows-${TARGET_ARCH}-devel.tar.gz"
;;
linux)
NEW_CORE_SERVER_FOLDER="core"
NEW_CORE_ARCHIVE="realm-core-${TARGET_FLAVOR}-v${REALM_CORE_VERSION}-Linux-devel.tar.gz"
# flavor is ignored since we only publish Release mode
NEW_SYNC_ARCHIVE="realm-sync-Release-v${REALM_SYNC_VERSION}-Linux-devel.tar.gz"
;;
*)
echo "Unsupported core platform '$TARGET_OS'"
exit 1
;;
esac
download() {
local tmp="${TEMP_DIR}/${2}"
if [[ -e "$tmp" ]] && [[ "1" == "$ENABLE_FORCE" ]]; then
rm -rf "$tmp"
fi
if [[ ! -f "$tmp" ]]; then
local url="https://static.realm.io/downloads/$1/$2"
echo "Downloading $url"
curl "$url" --output "$tmp"
fi
}
extract() {
local tmp="${TEMP_DIR}/${1%%.*}"
local src="$tmp"
if [[ -n "$2" ]]; then
src="$src/$2"
fi
echo "Extracting $1 => $tmp"
rm -rf "$tmp"
mkdir -p "$tmp"
tar -zxf "${TEMP_DIR}/$1" -C "$tmp"
rm -rf "$REALM_DIR"
echo "Moving" $(mv -v "$src" "$REALM_DIR")
}
LOCKFILE="$REALM_DIR/$LOCKFILE_NAME"
if [[ "1" == "$ENABLE_FORCE" ]]; then
echo 'Skipping lockfile check as --force is enabled'
elif [[ -f "$LOCKFILE" ]]; then
source "$LOCKFILE"
if [[ "1" == "$ENABLE_SYNC" ]]; then
if [ "$SYNC_ARCHIVE" = "$NEW_SYNC_ARCHIVE" ] \
&& [ "$SYNC_ARCHIVE_ROOT" = "$NEW_SYNC_ARCHIVE_ROOT" ] \
&& [ "$SYNC_SERVER_FOLDER" = "$NEW_SYNC_SERVER_FOLDER" ];
then
echo 'Matching lockfile already exists at target - nothing to do (use --force to override)'
exit 0
fi
else
if [ "$CORE_ARCHIVE" = "$NEW_CORE_ARCHIVE" ] \
&& [ "$CORE_SERVER_FOLDER" = "$NEW_CORE_SERVER_FOLDER" ];
then
echo 'Matching lockfile already exists at target - nothing to do (use --force to override)'
exit 0
fi
fi
echo 'Target directory has a differing lockfile, overwriting.'
else
echo 'No lockfile found at the target, proceeding.'
fi
if [[ "1" == "$ENABLE_SYNC" ]]; then
download $NEW_SYNC_SERVER_FOLDER $NEW_SYNC_ARCHIVE
extract $NEW_SYNC_ARCHIVE $NEW_SYNC_ARCHIVE_ROOT
echo "SYNC_ARCHIVE=$NEW_SYNC_ARCHIVE" > "$LOCKFILE"
echo "SYNC_ARCHIVE_ROOT=$NEW_SYNC_ARCHIVE_ROOT" >> "$LOCKFILE"
echo "SYNC_SERVER_FOLDER=$NEW_SYNC_SERVER_FOLDER" >> "$LOCKFILE"
else
download $NEW_CORE_SERVER_FOLDER $NEW_CORE_ARCHIVE
extract $NEW_CORE_ARCHIVE
echo "CORE_ARCHIVE=$NEW_CORE_ARCHIVE" > "$LOCKFILE"
echo "CORE_SERVER_FOLDER=$NEW_CORE_SERVER_FOLDER" >> "$LOCKFILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment