Created
July 27, 2024 05:08
-
-
Save fastmover/cb1d45285bef675f34f1de5106980600 to your computer and use it in GitHub Desktop.
Setup a fresh WSL 2 environment with PHP, Composer & Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/sudo bash | |
current_user=$(logname) | |
RED="\e[31m" | |
GREEN="\e[32m" | |
NC="\e[0m" | |
# blue=$(tput setaf 4) | |
green=$(tput setaf 2) | |
red=$(tput setaf 1) | |
normal=$(tput sgr0) | |
if ! command -v printline -v &> /dev/null | |
then | |
printline () { | |
printf "$1$2$3$4\n" | |
} | |
fi | |
printline "\n\n\n" | |
printline "${green}Dev Setup for WSL 2.0 with PHP & Laravel${normal}" | |
printline "\n\n\n" | |
last_update=$(ls -l /var/cache/apt/pkgcache.bin | cut -d' ' -f6,7,8) | |
last_update=$(date +%F -d"$last_update") | |
update_threshold=$(date +%F -d '7 days ago') | |
# i put this here because i don't like waiting on apt update | |
if [[ $last_update < $update_threshold ]] | |
then | |
printline "Running ${red}apt update${normal}" | |
apt update | |
else | |
printline "${green}apt update${normal} ran recently." | |
fi | |
if ! command -v php8.3 -v &> /dev/null | |
then | |
printline "${red}php8.3${normal} not found, installing." | |
apt install php8.3-cli | |
printline "${green}php8.3${normal} installed, you can now run 'php8.3'." | |
else | |
printline "${green}php8.3${normal} installed." | |
fi | |
if ! command -v composer &> /dev/null | |
then | |
printline "${red}Composer${normal} not be found, installing." | |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" | |
php composer-setup.php | |
php -r "unlink('composer-setup.php');" | |
mv composer.phar /usr/local/bin/composer | |
printline "${green}Composer${normal} installed, you can now run 'composer'." | |
else | |
printline "${green}composer${normal} installed." | |
fi | |
if ! command -v 7z &> /dev/null | |
then | |
printline "${red}7zip${normal} not be found, installing." | |
apt install 7zip | |
printline "${green}7zip${normal} installed, you can now run '${green}7z${normal}'." | |
else | |
printline "${green}7zip${normal} installed." | |
fi | |
composer_bin="/home/$current_user/.config/composer/vendor/bin" | |
# profile_test="/home/$current_user/test.test" | |
profile_user="/home/$current_user/.profile" | |
current_path=$profile_user | |
# create profile if it doesn't exist | |
if [ ! -f $current_path ] | |
then | |
touch $current_path | |
printline "Creating file ${green}$current_path${normal}" | |
fi | |
# count if there's already references to this path in the profile | |
path_count=$(grep -c 'composer/vendor/bin' $current_path) | |
if [ $path_count -eq 0 ] | |
then | |
printline "Adding composer ${red}bin${normal} to PATH" | |
cat >> $current_path <<EOF | |
# set PATH so it includes composers's global bin if it exists | |
if [ -d "/home/$current_user/.config/composer/vendor/bin" ] ; then | |
PATH="/home/$current_user/.config/composer/vendor/bin:$PATH" | |
fi | |
EOF | |
printline "Added ${green}composer/vendor/bin${normal} to ${green}PATH${normal} in ${green}$current_path${normal}" | |
printline "!!!${red}Be sure to run ${green}source $current_path${red} before running ${green}laravel${normal}!!!" | |
else | |
printline "${green}composer/vendor/bin${normal} path already found in ${green}$current_path${normal}" | |
fi | |
source $current_path | |
if ! command -v laravel &> /dev/null | |
then | |
printline "${red}laravel${normal} not be found, installing." | |
su -c "composer global require laravel/installer" -m $current_user | |
printline "${green}laravel${normal} installed, you can now run '${green}laravel${normal}'." | |
else | |
printline "${green}laravel${normal} installed." | |
fi | |
printline "\n\n\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment