-
Notifications
You must be signed in to change notification settings - Fork 1
/
codegen.sh
executable file
·110 lines (91 loc) · 3.3 KB
/
codegen.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
# Exit on all errors, undeclared variables and pipefailures.
set -euo pipefail
# Get the path to the script directory
SCRIPT_DIR="$(dirname "$0")"
# Get the SHASUM of the tarball
ZIP_FILE="${SCRIPT_DIR}/apollo.tar.gz"
ZIP_FILE_DOWNLOAD_URL="https://install.apollographql.com/legacy-cli/darwin/2.33.6"
SHASUM_FILE="${SCRIPT_DIR}/apollo/.shasum"
APOLLO_DIR="${SCRIPT_DIR}"/apollo
IS_RETRY="false"
SHASUM=""
# Helper functions
download_apollo_cli_if_needed() {
echo "Checking if CLI needs to be downloaded..."
if [ -f "${ZIP_FILE}" ]; then
echo "Zip file already downloaded!"
else
download_cli
fi
}
download_cli() {
echo "Downloading zip file with the CLI..."
curl --silent --retry 3 --fail --show-error -L "${ZIP_FILE_DOWNLOAD_URL}" -o "${ZIP_FILE}"
}
force_cli_download() {
rm -r "${ZIP_FILE}"
remove_existing_apollo
download_cli
IS_RETRY="true"
validate_codegen_and_extract_if_needed
}
update_shasum() {
SHASUM="$(/usr/bin/shasum -a 256 "${ZIP_FILE}" | /usr/bin/awk '{ print $1 }')"
}
remove_existing_apollo() {
if [[ -f "${APOLLO_DIR}" ]]; then
rm -r "${APOLLO_DIR}"
fi
}
extract_cli() {
tar xzf "${SCRIPT_DIR}"/apollo.tar.gz -C "${SCRIPT_DIR}"
echo "${SHASUM}" | tee "${SHASUM_FILE}"
}
validate_codegen_and_extract_if_needed() {
# Make sure the SHASUM matches the release for this version
EXPECTED_SHASUM="496b4de6a4a1f5a1c4a093c8d2378054ebf0dc19361a7dad847f82feeccad2be"
update_shasum
if [[ ${SHASUM} = ${EXPECTED_SHASUM}* ]]; then
echo "Correct version of the CLI tarball is downloaded locally, checking if it's already been extracted..."
else
if [[ "${IS_RETRY}" == "true" ]]; then
#This was a retry, and the SHASUM still doesn't match.
echo "Error: The SHASUM of this zip file does not match the official released version from Apollo! This may present security issues. Terminating code generation." >&2
exit 1
else
# This was the first attempt, the version may have changed.
echo "Incorrect version of the CLI tarball is downloaded locally, redownloading the zip from the server."
force_cli_download
fi
fi
# Check if the SHASUM file has already been written for this version
if [ -f "${SHASUM_FILE}" ]; then
# The file exists, let's see if it's the same SHASUM
FILE_CONTENTS="$(cat "${SHASUM_FILE}")"
if [[ ${FILE_CONTENTS} = ${EXPECTED_SHASUM}* ]]; then
echo "Current verson of CLI is already extracted!"
else
echo "Extracting updated version of the Apollo CLI. This may take a minute..."
remove_existing_apollo
extract_cli
fi
else
# The file doesn't exist, unzip the CLI
echo "Extracting the Apollo CLI. This may take a minute..."
extract_cli
fi
}
# Download the current version of the CLI if it doesn't exist
download_apollo_cli_if_needed
# Make sure we're using an up-to-date and valid version of the Apollo CLI
validate_codegen_and_extract_if_needed
# Add the binary directory to the beginning of PATH so included binary verson of node is used.
PATH="${SCRIPT_DIR}/apollo/bin:${PATH}"
# Use the bundled executable of the Apollo CLI to generate code
APOLLO_CLI="${SCRIPT_DIR}/apollo/bin/run"
# Print version
echo "Apollo CLI Information: $("${APOLLO_CLI}" --version)"
# Print commands before executing them (useful for troubleshooting)
set -x
"$APOLLO_CLI" "$@"