Skip to content

Instantly share code, notes, and snippets.

@relliv
Last active July 14, 2022 19:11
Show Gist options
  • Save relliv/ab40c5857b61e51ec528de9ca0b07c3a to your computer and use it in GitHub Desktop.
Save relliv/ab40c5857b61e51ec528de9ca0b07c3a to your computer and use it in GitHub Desktop.
Laravel Installer Shell Script
#!/bin/sh
# false condititon on if statement
# sudo apt-get purge --auto-remove php
# Colorize given text
colorize() {
local defaultcolor='\033[0m'
local textColor='\033[0m'
case $1 in
'red')
textColor='\033[0;31m'
;;
'green')
textColor='\033[0;32m'
;;
'orange')
textColor='\033[0;33m'
;;
'blue')
textColor='\033[0;34m'
;;
'purple')
textColor='\033[0;35m'
;;
'cyan')
textColor='\033[0;36m'
;;
'white')
textColor='\033[0;37m'
;;
*)
textColor=$defaultcolor
;;
esac
echo "$textColor$2${defaultcolor}\n"
}
# Get current PHP version
getPhpVersion() {
version=$(php -v | grep --only-matching --perl-regexp "PHP (\d+.\d+.\d+)")
echo $version | cut -d" " -f2
}
# Get current Composer version
getComoposerVersion() {
version=$(composer -V | grep --only-matching --perl-regexp "Composer version (\d+.\d+.\d+)")
echo $version | cut -d" " -f3
}
# Check app is exists
appExists() {
if ! type $1 >/dev/null; then
return 1 # installed
else
return 0 # not found
fi
}
# Check plugin is exists
hashExists() {
if ! hash $1 2>/dev/null; then
return 1 # installed
else
return 0 # not found
fi
}
echo
echo
echo $(colorize green '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
echo $(colorize green '<<<<< 🧑 Welcome to Laravel Installer 🧑 >>>>>')
echo $(colorize green '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
if $(appExists 'curl'); then
echo
else
echo $(colorize cyan 'Curl not found. Dou you want to install (y/n)?')
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then
sudo apt install curl -y
echo
echo 'βœ… Curl is installed!'
echo
fi
fi
echo $(colorize purple 'Step 1: PHP')
echo
if $(appExists 'php'); then
echo 'πŸ‘‰ Your PHP version is '$(getPhpVersion)
echo
else
echo $(colorize orange 'πŸ”₯ PHP is not installed. Please install PHP first!')
echo
echo $(colorize cyan 'Dou you want to install latest verson of PHP (y/n)?')
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then
sudo apt install php -y
echo
echo 'βœ… PHP is installed!'
echo 'πŸ‘‰ Your php version is ' $(getPhpVersion)
echo
else
echo No
fi
fi
echo $(colorize purple 'Step 2: Composer')
echo
if $(appExists 'composer'); then
echo 'πŸ‘‰ Your composer version is '$(getComoposerVersion)
echo
else
echo $(colorize orange 'πŸ”₯ Composer is not installed.')
echo
echo $(colorize cyan 'Dou you want to install latest verson of Composer (y/n)?')
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then
# https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
echo >&2 'ERROR: Invalid installer checksum'
rm composer-setup.php
exit 1
fi
php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
if [ $RESULT -eq 0 ]; then
echo
echo 'βœ… Composer is installed!'
echo 'πŸ‘‰ Your composer version is '$(getComoposerVersion)
echo
else
echo
echo '❌ Composer could not installed!'
echo
fi
fi
fi
echo $(colorize purple 'Step 3: Laravel Installer')
echo
# exists() {
# # command -v "$1" >/dev/null 1>&1
# # hash $1 1>/dev/null
# # hash laravel 0>/dev/null
# }
# if exists laravel; then
# echo 'Bash exists!'
# else
# echo 'Your system does not have Bash'
# fi
if $(hashExists 'laravel'); then
echo 'πŸ‘‰ Your Laravel Installer ready!'
echo
else
echo $(colorize orange 'πŸ”₯ Laravel Installer is not installed.')
echo
echo $(colorize cyan 'Dou you want to install latest verson of Laravel Installer (y/n)?')
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then
composer global require laravel/installer
export PATH="~/.config/composer/vendor/bin:$PATH"
echo
echo 'βœ… Laravel Installer is installed!'
echo
else
echo No
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment