Skip to content

Commit be3969c

Browse files
authored
fix(tools): require min version or use @latest (#511)
1 parent 73af3da commit be3969c

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

AmplifyTools/amplify-tools.sh

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,121 @@
66
# SPDX-License-Identifier: Apache-2.0
77

88
set -e
9-
export PATH=$PATH:$(npm bin -g)
109

1110
if ! which node >/dev/null; then
1211
echo "warning: Node is not installed. Visit https://nodejs.org/en/download/ to install it"
1312
exit 1
14-
elif ! test -f ./amplifytools.xcconfig; then
15-
npx amplify-app --platform ios
13+
fi
14+
15+
export PATH=$PATH:$(npm bin -g)
16+
17+
# Note the use of tail -1 is important here because when upgrading between versions
18+
# the first time that you run these commands, we have seen this variable take on the value of:
19+
# """
20+
# Scanning for plugins...
21+
# plugin scan successful
22+
# 4.21.0
23+
# """
24+
AMP_APP_VERSION_CURRENT=`npx -q amplify-app --version |tail -1`
25+
AMP_APP_VERSION_MINIMUM="2.17.1"
26+
AMP_APP_VERSION_INVALID=0
27+
28+
AMP_CLI_VERSION_CURRENT=`npx -q amplify --version |tail -1`
29+
AMP_CLI_VERSION_MINIMUM="4.21.0"
30+
AMP_CLI_VERSION_INVALID=0
31+
32+
33+
STRIP_ESCAPE_RESULT=
34+
stripEscapeUtil() {
35+
STRIP_ESCAPE_RESULT=
36+
37+
set +e
38+
HAS_RUBY=0
39+
which ruby > /dev/null 2>&1
40+
if [[ $? -eq 0 ]]; then
41+
HAS_RUBY=1
42+
fi
43+
set -e
44+
45+
if [ ${HAS_RUBY} -eq 1 ]; then
46+
STRIP_ESCAPE_RESULT=`echo "${1}" | ruby -pe 'gsub(/\e\[[0-9;]*m/s, "")'`
47+
else
48+
STRIP_ESCAPE_RESULT=`echo "${1}" | npx -q strip-ansi-cli`
49+
fi
50+
}
51+
52+
VERSION_INVALID=
53+
checkMinVersionCompatibility() {
54+
VERSION_INVALID=0
55+
56+
stripEscapeUtil "${1}"
57+
CURR_VERSION_SAFE="${STRIP_ESCAPE_RESULT}"
58+
CURR_VERSION_MAJOR=`echo "${CURR_VERSION_SAFE}" | tr '.' ' ' | awk '{print $1}'`
59+
CURR_VERSION_MINOR=`echo "${CURR_VERSION_SAFE}" | tr '.' ' ' | awk '{print $2}'`
60+
CURR_VERSION_PATCH=`echo "${CURR_VERSION_SAFE}" | tr '.' ' ' | awk '{print $3}'`
61+
62+
stripEscapeUtil "${2}"
63+
REQU_VERSION_SAFE="${STRIP_ESCAPE_RESULT}"
64+
REQU_VERSION_MAJOR=`echo "${REQU_VERSION_SAFE}" | tr '.' ' ' | awk '{print $1}'`
65+
REQU_VERSION_MINOR=`echo "${REQU_VERSION_SAFE}" | tr '.' ' ' | awk '{print $2}'`
66+
REQU_VERSION_PATCH=`echo "${REQU_VERSION_SAFE}" | tr '.' ' ' | awk '{print $3}'`
67+
68+
if [ -z "${CURR_VERSION_MAJOR}" ] ||
69+
[ -z "${CURR_VERSION_MINOR}" ] ||
70+
[ -z "${CURR_VERSION_PATCH}" ] ||
71+
[ -z "${REQU_VERSION_MAJOR}" ] ||
72+
[ -z "${REQU_VERSION_MINOR}" ] ||
73+
[ -z "${REQU_VERSION_PATCH}" ]; then
74+
VERSION_INVALID=1
75+
return
76+
fi
77+
78+
if [ ${CURR_VERSION_MAJOR} -lt ${REQU_VERSION_MAJOR} ]; then
79+
VERSION_INVALID=1
80+
return
81+
else
82+
if [ ${CURR_VERSION_MINOR} -lt ${REQU_VERSION_MINOR} ]; then
83+
VERSION_INVALID=1
84+
return
85+
else
86+
if [ ${CURR_VERSION_PATCH} -lt ${REQU_VERSION_PATCH} ]; then
87+
VERSION_INVALID=1
88+
return
89+
fi
90+
fi
91+
fi
92+
}
93+
94+
checkMinVersionCompatibility "${AMP_APP_VERSION_CURRENT}" "${AMP_APP_VERSION_MINIMUM}"
95+
AMP_APP_VERSION_INVALID=${VERSION_INVALID}
96+
97+
checkMinVersionCompatibility "${AMP_CLI_VERSION_CURRENT}" "${AMP_CLI_VERSION_MINIMUM}"
98+
AMP_CLI_VERSION_INVALID=${VERSION_INVALID}
99+
100+
if [ ${AMP_CLI_VERSION_INVALID} -eq 1 ]; then
101+
echo "error: Minimum version required of Amplify CLI is not installed."
102+
echo " Min required version: (${AMP_CLI_VERSION_MINIMUM})"
103+
echo " Found Version: (${AMP_CLI_VERSION_CURRENT})"
104+
echo ""
105+
echo "To install the latest version, please run the following command:"
106+
echo " npm install -g @aws-amplify/cli@latest"
107+
exit 1
108+
else
109+
echo "Found amplify-cli version: (${AMP_CLI_VERSION_CURRENT}), Required: >= (${AMP_CLI_VERSION_MINIMUM})"
110+
fi
111+
112+
echo "Found amplify-app version: (${AMP_APP_VERSION_CURRENT}), Required: >= (${AMP_APP_VERSION_MINIMUM})"
113+
if [ ${AMP_APP_VERSION_INVALID} -eq 1 ]; then
114+
echo " Using: npx amplify-app@latest"
115+
NPX_AMP_APPCMD="npx amplify-app@latest"
116+
else
117+
echo " Using: npx amplify-app"
118+
NPX_AMP_APPCMD="npx amplify-app"
119+
fi
120+
121+
122+
if ! test -f ./amplifytools.xcconfig; then
123+
${NPX_AMP_APPCMD} --platform ios
16124
fi
17125

18126
. amplifytools.xcconfig
@@ -28,7 +136,7 @@ if $amplifyModelgen; then
28136
echo "modelgen is set to true, generating Swift models from schema.graphql..."
29137
amplify codegen model
30138
# calls amplify-app again so the Xcode project is updated with the generated models
31-
npx amplify-app --platform ios
139+
${NPX_AMP_APPCMD} --platform ios
32140
fi
33141

34142
if [ -z "$amplifyAccessKey" ] || [ -z "$amplifySecretKey" ] || [ -z "$amplifyRegion" ]; then

0 commit comments

Comments
 (0)