dotfiles/.bash/.bash_jaaENV

154 lines
4.9 KiB
Bash
Raw Permalink Normal View History

2024-02-19 16:07:53 +01:00
function jaaENV {
if [ "$1" = "--help" ]; then
echo "Utility to combine nvm/sdk/… in one script/config file '.jaaENV'"
echo " ↘ instead of 'nvm use 16 & sdk use …'"
echo ""
echo "Usage:"
echo " jaaENV"
echo " jaaENV [--help|--ls]"
echo " jaaENV --save …to_save (example: jaaENV --save nodejs php)"
echo ""
echo "Description:"
echo " - Without arguments, loads info from '.jaaENV'"
echo " - 'ls' shows supported options to auto-load"
echo " - 'save' creates a config file '.jaaENV'"
2024-02-19 16:07:53 +01:00
echo ""
echo "Usage '.jaaENV' files: "
echo " typical: '\nphp=5.6\nnodejs=16.13.0'"
echo " link: '. ../.jaaENV' … use settings from parent folder"
echo " Options:"
echo " 'android_home': Exports 'ANDROID_HOME' (uses 'ANDROID_HOME_BAK') instead of 'ANDROID_SDK_ROOT'. Value can be anything (use '1')."
echo " 'jdk': Exports 'JAVA_HOME' based on the wanted version (currently 11/1.8)"
2024-02-19 16:07:53 +01:00
echo " 'nodejs'|'php': NodeJS/PHP versions"
echo " 'gradle': Cordova uses folder-scoped → so unnecesarly"
2024-02-19 16:07:53 +01:00
echo ""
echo "Install: "
echo " gradle ⇒ https://sdkman.io/"
echo " node ⇒ https://github.com/nvm-sh/nvm"
echo " php ⇒ \`update-alternatives\` + https://deb.sury.org/"
echo " java ⇒ \`update-alternatives\`"
2024-02-19 16:07:53 +01:00
echo ""
echo "Tips:"
echo " sudo update-alternatives --set php /usr/bin/php\$php"
return 0
fi
if [ "$1" == "--save" ]; then
2024-02-19 16:07:53 +01:00
shift
rm -vi .jaaENV
while [ $# -gt 0 ]; do
2024-02-19 16:07:53 +01:00
case "$1" in
nodejs|node)
echo "nodejs=$(node --version | cut -c2-)" >> .jaaENV
2024-02-19 16:07:53 +01:00
;;
gradle)
echo "gradle=$(gradle --version | awk '/Gradle/ {print $2}')" >> .jaaENV
2024-02-19 16:07:53 +01:00
;;
php)
echo "php=$(php --version | awk '/^PHP/ {print $2}')" >> .jaaENV
2024-02-19 16:07:53 +01:00
;;
java)
local java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ "$java_version" == 1.8* ]]; then
java_version=8
else
java_version=$(echo "$java_version" | cut -d'.' -f1)
fi
echo "java=$java_version" >> .jaaENV
;;
2024-02-19 16:07:53 +01:00
*)
echo "Unknown '$1' skipped"
2024-02-19 16:07:53 +01:00
;;
esac
shift
done
return 0
fi
if [ "$1" == "--ls" ]; then
echo ":: nvm ls --no-alias ::"
nvm ls --no-alias
echo ":: update-alternatives --list php ::"
update-alternatives --list php
echo ":: update-alternatives --list java ::"
update-alternatives --list java
echo ":: sdk ls gradle | grep -E '\*|>' :: "
sdk ls gradle | grep -E '\*|>'
2024-02-19 16:07:53 +01:00
return 0
fi
if [ -f "compose.yaml" ]; then
jdk=$(grep -Po 'VERSION_JAVA=\K.*' compose.yaml)
nodejs=$(grep -Po 'VERSION_NODEJS=\K.*' compose.yaml)
elif [ -f ".jaaENV" ]; then
2024-02-19 16:07:53 +01:00
. ./.jaaENV
else
echo 'No env file `compose.yaml` or `.jaaENV`'
return 1
2024-02-19 16:07:53 +01:00
fi
2024-10-11 09:25:12 +02:00
local chP="\[\033[0;35m\]" #purple
local chW="\[\033[00m\]" #white
export PS1_jaaENV="${chP}¦${chW}"
2024-10-11 09:25:12 +02:00
if [ ! -z ${ANDROID_SDK_ROOT_BAK+x} ]; then
export ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT_BAK
echo \$ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT_BAK
unset ANDROID_SDK_ROOT_BAK
fi
if [ ! -z ${unset_android_sdk+x} ]; then
export ANDROID_SDK_ROOT_BAK=$ANDROID_SDK_ROOT
unset ANDROID_SDK_ROOT
fi
if [ ! -z ${java+x} ]; then
[[ "$java" == "8" ]] && java=1.8
local java_local=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ "$java_local" != "$java"* ]]; then
[[ "$java" == "1.8" ]] && java=8
echo "Switching Java version: $java$java_local"
local java_path="/usr/lib/jvm/java-$java-openjdk-amd64/"
[[ "$java" == "8" ]] && java_path+="jre/"
sudo update-alternatives --set java "${java_path}bin/java"
local javac=$(update-alternatives --list javac | grep java-$java)
[ -n "$javac" ] && sudo update-alternatives --set javac "$javac"
sudo -k
fi
export JAVA_HOME=$(readlink -f $(which java) | cut -d '/' -f1-5)/
echo "\$JAVA_HOME=$JAVA_HOME"
fi
if [ ! -z ${android_home+x} ]; then
export ANDROID_HOME=$ANDROID_HOME_BAK
echo "\$ANDROID_HOME=$ANDROID_HOME"
else
unset ANDROID_HOME
echo "\$ANDROID_SDK_ROOT=$ANDROID_SDK_ROOT"
fi
if [ ! -z ${android_home_is_sdk+x} ]; then
export ANDROID_HOME=$ANDROID_SDK_ROOT
echo "\$ANDROID_HOME=$ANDROID_HOME"
fi
if [ ! -z ${JAVA_HOME_BAK+x} ]; then export JAVA_HOME=$JAVA_HOME_BAK; unset JAVA_HOME_BAK; fi
2024-02-19 16:07:53 +01:00
if [ ! -z ${jdk+x} ]; then
if (( $(echo "$jdk > 9" | bc -l) )); then
export JAVA_HOME_BAK=$JAVA_HOME
export JAVA_HOME="/snap/android-studio/current/android-studio/jre"
2024-02-19 16:07:53 +01:00
fi
echo "\$JAVA_HOME=$JAVA_HOME"
fi
if [ ! -z ${nodejs+x} ]; then nvm use $nodejs; fi
if [ ! -z ${gradle+x} ]; then sdk use gradle $gradle | tr -d '\n'; echo ; fi
if [ ! -z ${php+x} ]; then
local php_local=$(php --version | awk '/^PHP/ {print $2}')
2024-02-19 16:07:53 +01:00
if [[ "$php_local" == "$php"* ]]; then
echo "PHP version: $php"
2024-02-19 16:07:53 +01:00
else
echo "Switching PHP version: $php$php_local"
sudo update-alternatives --set php "/usr/bin/php$php"
2024-02-19 16:07:53 +01:00
sudo -k
fi
fi
# Cleanup variables
2024-10-11 09:25:12 +02:00
unset android_home unset_android_sdk nodejs gradle php java
2024-02-19 16:07:53 +01:00
}