Skip to content

Instantly share code, notes, and snippets.

@richhh7g
Created May 28, 2024 11:00
Show Gist options
  • Save richhh7g/9a34861c591b548020b6c83a640f2cc8 to your computer and use it in GitHub Desktop.
Save richhh7g/9a34861c591b548020b6c83a640f2cc8 to your computer and use it in GitHub Desktop.
Shell - Install programming language
#!/bin/bash
node_version=""
php_version=""
global_flag=false
while [[ "$#" -gt 0 ]]; do
case $1 in
--node=*) node_version="${1#*=}"; shift ;;
--php=*) php_version="${1#*=}"; shift ;;
--node) node_version="$2"; shift 2 ;;
--php) php_version="$2"; shift 2 ;;
--global) global_flag=true; shift ;;
*) echo "Invalid parameter: $1" >&2; exit 1 ;;
esac
done
main() {
if [ "$(_get_debian_based_distros)" == "The system is not based on Debian" ]; then
echo $(_get_debian_based_distros)
exit 1
fi
_install_asdf
_asdf_install_plugins
_install_php_deps
_asdf_install_languages
_add_asdf_global
exit 0
}
_get_debian_based_distros() {
local str_is_debian="The system is based on Debian."
if command -v apt-get >/dev/null; then
echo $str_is_debian
else
if [ -f /etc/debian_version ]; then
echo $str_is_debian
else
echo "The system is not based on Debian."
fi
fi
}
_install_asdf() {
if !command -v git >/dev/null; then
echo "Installing git..."
sudo apt install git
echo "Git installed."
fi
if !command -v curl >/dev/null; then
echo "Installing curl..."
sudo apt install curl
echo "Curl installed."
fi
echo "Cloning asdf..."
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0
echo "Adding asdf to .bashrc..."
echo "Adding in other shells: https://asdf-vm.com/guide/getting-started.html#_3-install-asdf"
echo "" >> ~/.bashrc
echo ". $HOME/.asdf/asdf.sh" >> ~/.bashrc
}
_asdf_install_plugins() {
if !command -v asdf >/dev/null; then
echo "asdf is not installed."
exit 1
fi
echo "Installing asdf plugins..."
echo "Adding PHP plugin: https://github.com/asdf-community/asdf-php"
asdf plugin add php https://github.com/asdf-community/asdf-php.git
echo "Adding NodeJS plugin: https://github.com/asdf-vm/asdf-nodejs"
asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git
}
_install_php_deps() {
sudo apt update
echo "Installing PHP dependencies..."
sudo apt install $(echo autoconf bison build-essential gettext libgd-dev libcurl4-openssl-dev libedit-dev libicu-dev libjpeg-dev libmysqlclient-dev libonig-dev libpng-dev libpq-dev libreadline-dev libsqlite3-dev libssl-dev libxml2-dev libzip-dev openssl pkg-config re2c zlib1g-dev | xargs)
echo "PHP dependencies installed."
}
_asdf_install_languages() {
if [[ -z "$php_version" || -z "$node_version" ]]; then
echo "PHP version and NodeJs version are required."
echo "Usage: ./install.sh --php=x.x --node=x.x.x"
exit 1
fi
echo "ASDF install php: $php_version"
asdf install php $php_version
echo "ASDF install nodejs: $node_version"
asdf install nodejs $node_version
}
_add_asdf_global() {
if [ "$global_flag" == false ]; then
return
fi
asdf global nodejs $node_version
asdf global php $php_version
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment