Skip to content

Instantly share code, notes, and snippets.

@ivanstnsk
Last active June 15, 2021 10:24
Show Gist options
  • Save ivanstnsk/824fedc7db089cae6d5784a80642405d to your computer and use it in GitHub Desktop.
Save ivanstnsk/824fedc7db089cae6d5784a80642405d to your computer and use it in GitHub Desktop.
React Native update project version on mac
#!/bin/bash
COLOR_OK='\033[1;32m'
COLOR_HEADING='\033[0;36m'
COLOR_ERROR='\033[0;31m'
NC='\033[0m'
# Set your project directory
APP_REPO_DIR=~/dev/project-dir
ANDROID_RELEASE_KEYSTORE="${APP_REPO_DIR}/android/app/release.keystore"
ANDROID_RELEASE_PROPERTIES="${APP_REPO_DIR}/android/release.properties"
ANDROID_GRADLE_PROPERTIES="${APP_REPO_DIR}/android/gradle.properties"
IOS_PBXPROJ="${APP_REPO_DIR}/ios/MobileEcommerce.xcodeproj/project.pbxproj"
ANDROID_TEMP_RELEASE_KEYSTORY="/tmp/release.keystore"
IOS_TEMP_PBXPROJ="/tmp/project.pbxproj"
ENV_FILE="${APP_REPO_DIR}/.env"
printf "$COLOR_HEADING"
cat << "EOF"
_____ _ _ __ __ _
| __ \ | \ | | \ \ / / (_)
| |__) | | \| | \ \ / / ___ _ __ ___ _ ___ _ __
| _ / | . ` | \ \/ / / _ \ | '__| / __| | | / _ \ | '_ \
| | \ \ | |\ | \ / | __/ | | \__ \ | | | (_) | | | | |
|_| \_\ |_| \_| \/ \___| |_| |___/ |_| \___/ |_| |_|
_ _ _ _
| | | | | | | |
| | | | _ __ __| | __ _ | |_ ___ _ __
| | | | | '_ \ / _` | / _` | | __| / _ \ | '__|
| |__| | | |_) | | (_| | | (_| | | |_ | __/ | |
\____/ | .__/ \__,_| \__,_| \__| \___| |_|
| |
|_|
EOF
printf "$NC"
#-------------------------------------------------------------------------------------
# CHECK
#-------------------------------------------------------------------------------------
printf "${COLOR_HEADING}Checking requirements...\n\n$NC"
if test -f "$ANDROID_RELEASE_KEYSTORE"; then
printf "${COLOR_OK}${ANDROID_RELEASE_KEYSTORE} exists. OK\n$NC"
else
printf "${COLOR_ERROR}${ANDROID_RELEASE_KEYSTORE} is not exists. ERROR\n$NC"
printf "Exit..."
exit 1
fi
if test -f "$ANDROID_RELEASE_PROPERTIES"; then
printf "${COLOR_OK}${ANDROID_RELEASE_PROPERTIES} exists. OK\n$NC"
else
printf "${COLOR_ERROR}${ANDROID_RELEASE_PROPERTIES} is not exists. ERROR\n$NC"
printf "Exit..."
exit 1
fi
if test -f "$IOS_PBXPROJ"; then
printf "${COLOR_OK}${IOS_PBXPROJ} exists. OK\n$NC"
else
printf "${COLOR_ERROR}${IOS_PBXPROJ} is not exists. ERROR\n$NC"
printf "Exit..."
exit 1
fi
if test -f "$ENV_FILE"; then
printf "${COLOR_OK}${ENV_FILE} exists. OK\n$NC"
else
printf "${COLOR_ERROR}${ENV_FILE} is not exists. ERROR\n$NC"
printf "Exit..."
exit 1
fi
read_var() {
VAR=$(grep $1 $2 | xargs)
IFS="=" read -ra VAR <<< "$VAR"
echo ${VAR[1]}
}
APP_ENV=$(read_var APP_ENV $ENV_FILE)
if [ "$APP_ENV" == "production" ]; then
printf "${COLOR_OK}.env is set to production. OK\n$NC"
else
printf "${COLOR_ERROR}.env is set to ${APP_ENV}. ERROR\n$NC"
printf "Exit..."
exit 1
fi
#-------------------------------------------------------------------------------------
# PREPARE VERSION
#-------------------------------------------------------------------------------------
VERSION_CODE=$(read_var VERSION_CODE $ANDROID_GRADLE_PROPERTIES)
VERSION_NAME=$(read_var VERSION_NAME $ANDROID_GRADLE_PROPERTIES)
NEXT_VERSION_CODE=$(($VERSION_CODE + 1))
NEXT_VERSION_NAME="${VERSION_NAME}"
printf "\n"
printf "${COLOR_HEADING}Found last build ${VERSION_NAME}_${VERSION_CODE}\n\n$NC"
input_valid=false
answer=0
while [ "$input_valid" == false ];
do
printf "${COLOR_HEADING}Would you like to increment this version (${VERSION_NAME}_${VERSION_CODE} ==> ${NEXT_VERSION_NAME}_${NEXT_VERSION_CODE})? (y/n) $NC"
read answer
if [ "$answer" == "y" ] || [ "$answer" == "n" ]; then
input_valid=true
else
printf "${COLOR_ERROR}Invalid option: ${answer}\n$NC"
fi
done
printf "\n"
if [ "$answer" == "n" ]; then
input_valid=false
while [ "$input_valid" == false ];
do
is_correct=false
printf "${COLOR_HEADING}Please enter the version in format MAJOR.MINOR.PATH: $NC"
read NEXT_VERSION_NAME
printf "${COLOR_HEADING}Please enter the build number: $NC"
read NEXT_VERSION_CODE
while [ "$is_correct" == false ];
do
printf "${COLOR_HEADING}Is it correct (${NEXT_VERSION_NAME}_${NEXT_VERSION_CODE})? (y/n) $NC"
read is_correct
if [ "$is_correct" == "y" ]; then
is_correct=true
input_valid=true
printf "\n"
elif [ "$is_correct" == "n" ]; then
is_correct=true
input_valid=false
else
is_correct=false
printf "${COLOR_ERROR}Invalid option: ${is_correct}\n$NC"
fi
done
done
fi
#-------------------------------------------------------------------------------------
# PATCH VERSION
#-------------------------------------------------------------------------------------
printf "\n"
printf "${COLOR_HEADING}Patching the app version...\n\n$NC"
# tmp files
cp -R "${ANDROID_GRADLE_PROPERTIES}" "${ANDROID_TEMP_RELEASE_KEYSTORY}"
cp -R "${IOS_PBXPROJ}" "${IOS_TEMP_PBXPROJ}"
# patch version
sed -i -e "s/.*VERSION_CODE.*/VERSION_CODE=${NEXT_VERSION_CODE}/" "${ANDROID_TEMP_RELEASE_KEYSTORY}"
sed -i -e "s/.*VERSION_NAME.*/VERSION_NAME=${NEXT_VERSION_NAME}/" "${ANDROID_TEMP_RELEASE_KEYSTORY}"
sed -i -e "s/.*MARKETING_VERSION.*/ MARKETING_VERSION = ${NEXT_VERSION_NAME};/" "${IOS_TEMP_PBXPROJ}"
sed -i -e "s/.*CURRENT_PROJECT_VERSION.*/ CURRENT_PROJECT_VERSION = ${NEXT_VERSION_CODE};/" "${IOS_TEMP_PBXPROJ}"
# update files
cp -R "${ANDROID_TEMP_RELEASE_KEYSTORY}" "${ANDROID_GRADLE_PROPERTIES}"
cp -R "${IOS_TEMP_PBXPROJ}" "${IOS_PBXPROJ}"
rm "${ANDROID_TEMP_RELEASE_KEYSTORY}"
rm "${IOS_TEMP_PBXPROJ}"
printf "${COLOR_OK}Set the next version ${NEXT_VERSION_NAME}_${NEXT_VERSION_CODE}. OK\n$NC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment