Last active
September 23, 2021 19:27
-
-
Save joelbutcher/9fb4b36c45b92ac0516f2861589e96c2 to your computer and use it in GitHub Desktop.
A nice shell script for setting up a Laravel project using Homebrew, MySQL and Laravel Valet
This file contains 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
#!/bin/sh | |
# ---------------------------------- | |
# Colors | |
# ---------------------------------- | |
NOCOLOR='\033[0m' | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
ORANGE='\033[0;33m' | |
BLUE='\033[0;34m' | |
PURPLE='\033[0;35m' | |
CYAN='\033[0;36m' | |
LIGHTGRAY='\033[0;37m' | |
DARKGRAY='\033[1;30m' | |
LIGHTRED='\033[1;31m' | |
LIGHTGREEN='\033[1;32m' | |
YELLOW='\033[1;33m' | |
LIGHTBLUE='\033[1;34m' | |
LIGHTPURPLE='\033[1;35m' | |
LIGHTCYAN='\033[1;36m' | |
WHITE='\033[1;37m' | |
# Setup variables and input arguments | |
dbUser=${dbUser:-root} | |
dbPass=${dbPass:-secret} | |
dbName=${dbName:-laravel} | |
phpVersion=${phpVersion:-8.0} | |
while [ $# -gt 0 ] | |
do | |
if [[ $1 == *"--"* ]] | |
then | |
param="${1/--/}" | |
declare $param="$2" | |
fi | |
shift | |
done | |
# Let's start here by caffinating the mac so it stays awake or bad things happen. | |
caffeinate -d -i -m -u & | |
caffeinatepid=$! | |
echo "${GREEN}Caffinating the mac under process id: $caffeinatepid ${NOCOLOR}" | |
# ============================================================== | |
# Rosetta 2.0 | |
# ============================================================== | |
# Determine OS version | |
# Save current IFS state | |
OLDIFS=$IFS | |
IFS='.' read osvers_major osvers_minor osvers_dot_version <<< "$(/usr/bin/sw_vers -productVersion)" | |
# restore IFS to previous state | |
IFS=$OLDIFS | |
# Check to see if the Mac is reporting itself as running macOS 11 | |
if [[ ${osvers_major} -ge 11 ]] | |
then | |
# Check to see if the Mac needs Rosetta installed by testing the processor | |
processor=$(/usr/sbin/sysctl -n machdep.cpu.brand_string) | |
if [[ "$processor" == *"Intel"* ]] | |
then | |
echo "${CYAN}$processor processor installed. No need to install Rosetta.${NOCOLOR}" | |
else | |
echo "${CYAN}Running on Apple Silicon (${processor}), checking if we need to install Rosetta...${NOCOLOR}" | |
# Check for Rosetta "oahd" process. If not found, | |
# perform a non-interactive install of Rosetta. | |
if /usr/bin/pgrep oahd >/dev/null 2>&1 | |
then | |
echo "${CYAN}Rosetta is already installed and running. Nothing to do.${NOCOLOR}" | |
else | |
echo "${CYAN}Rosetta is not installed, installing now...${NOCOLOR}" | |
/usr/sbin/softwareupdate –install-rosetta –agree-to-license | |
if [[ $? -eq 0 ]] | |
then | |
echo "${CYAN}Rosetta has been successfully installed.${NOCOLOR}" | |
else | |
echo "${CYAN}Rosetta installation failed!${NOCOLOR}" | |
exitcode=1 | |
fi | |
fi | |
fi | |
else | |
echo "${CYAN}Mac is running macOS $osvers_major.$osvers_minor.$osvers_dot_version.${NOCOLOR}" | |
echo "${CYAN}No need to install Rosetta on this version of macOS.${NOCOLOR}" | |
fi | |
# ============================================================== | |
# Xcode CLI Tools | |
# ============================================================== | |
# Have the xcode command line tools been installed? | |
check=$( pkgutil --pkgs | grep com.apple.pkg.CLTools_Executables | wc -l | awk '{ print $1 }' ) | |
if [[ "$check" != 1 ]] | |
then | |
echo "${GREEN}[Xcode CLI tools] is not installed, installing it now...${NOCOLOR}" | |
# This temporary file prompts the 'softwareupdate' utility to list the Command Line Tools | |
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress > /dev/null | |
clt=$(softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | tail -n1) | |
softwareupdate -i "$clt" | |
rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress > /dev/null | |
/usr/bin/xcode-select --switch /Library/Developer/CommandLineTools > /dev/null | |
fi | |
# ============================================================== | |
# Homebrew | |
# ============================================================== | |
# Is homebrew already installed? | |
which -s brew | |
if [[ $? = 1 ]] | |
then | |
# Install Homebrew. This doesn't like being run as root so we must do this manually. | |
echo "${GREEN}[homebrew] is not installed, installing it now... 🍻${NOCOLOR}" | |
echo "${YELLOW} This could take a while, please do not close this window${NOCOLOR}" | |
# Curl down the latest tarball and install to /usr/local | |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" | |
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/joel/.bash_profile > /dev/null | |
eval "$(/opt/homebrew/bin/brew shellenv)" > /dev/null | |
source ~/.bash_profile > /dev/null | |
else | |
# Make sure everything is up to date | |
echo "${GREEN}Updating homebrew...${NOCOLOR}" | |
/opt/homebrew/bin/brew update | |
echo "${GREEN}Cleaning up...${NOCOLOR}" | |
/opt/homebrew/bin/brew cleanup | |
fi | |
# ============================================================== | |
# PHP | |
# ============================================================== | |
which -s php | |
if [[ $? = 1 ]] | |
then | |
# Insall PHP | |
echo "${GREEN}[php@${phpVersion}] is not installed, installing it now via Brew... 🍻${NOCOLOR}" | |
# Install PHP | |
/opt/homebrew/bin/brew install "php@${phpVersion}" > /dev/null | |
fi | |
# Forcibly overwrite the currently linked PHP version to the one specified in the script | |
/opt/homebrew/bin/brew link --overwrite php@${phpVersion} > /dev/null | |
echo "${GREEN}Starting php@${phpVersion}...${NOCOLOR}" | |
/opt/homebrew/bin/brew services restart "php@${phpVersion}" > /dev/null | |
# ============================================================== | |
# Composer | |
# ============================================================== | |
which -s composer | |
if [[ $? = 1 ]] | |
then | |
# Install Composer | |
echo "${GREEN}[composer] is not installed, installing it now...${NOCOLOR}" | |
curl -sS https://getcomposer.org/installer | php > /dev/null | |
sudo mv composer.phar /usr/local/bin/composer > /dev/null | |
fi | |
# Install PHP dependencies. | |
if [ -f ./vendor ] | |
then | |
/usr/local/bin/composer install > /dev/null | |
fi | |
# ============================================================== | |
# Valet | |
# ============================================================== | |
which -s valet | |
if [[ $? = 1 ]] | |
then | |
# Install Valet | |
echo "${GREEN}Valet is not installed, installing it now${NOCOLOR}" | |
/usr/local/bin/composer global require laravel/valet | |
/Users/joel/.composer/vendor/bin/valet install | |
echo "${GREEN}Laravel Valet installed successfully${NOCOLOR}" | |
fi | |
echo "${GREEN}Configuring Laravel Valet...${NOCOLOR}" | |
/Users/joel/.composer/vendor/bin/valet use "php${phpVersion}" > /dev/null | |
/Users/joel/.composer/vendor/bin/valet link adzooma && /Users/joel/.composer/vendor/bin/valet secure > /dev/null | |
echo "${GREEN}Laravel Valet is now configured${NOCOLOR}" | |
# ============================================================== | |
# Node / NPM | |
# ============================================================== | |
which -s node | |
if [[ $? = 1 ]] | |
then | |
echo "${RED}[node] not installed, please install this from https://nodejs.org" | |
sleep 3 # sleep to give the user time to read the error | |
else | |
# Install npm dependencies | |
if [ ! -d ./node_modules ] | |
then | |
echo "${GREEN} NODE dependencies not installed, installing now..." | |
npm install | |
fi | |
fi | |
which -s npm | |
if [[ $? = 1 ]] | |
then | |
echo "${RED}[npm] not installed, please install this from https://nodejs.org${NOCOLOR}" | |
sleep 3 # sleep to give the user time to read the error | |
else | |
echo "${GREEN}Compiling frontend assets...${NOCOLOR}" | |
if npm run dev | |
then | |
echo "${GREEN}Frontend assets compiled successfully...${NOCOLOR}" | |
else | |
echo "${RED} NPM failed to compile, this ios probably a problem with node-sass...${NOCOLOR}" | |
echo "${GREEN} Rebuilding node-sass...${NOCOLOR}" | |
npm rebuild node-sass | |
echo "${GREEN}Compiling frontend assets...${NOCOLOR}" | |
npm run dev | |
echo "${GREEN}Frontend assets compiled successfully...${NOCOLOR}" | |
fi | |
fi | |
# ============================================================== | |
# MySQL | |
# ============================================================== | |
which -s mysql | |
if [[ $? = 1 ]] | |
then | |
# Install MySQL | |
echo "${GREEN}[mysql] is not installed, installing it now... 🍻${NOCOLOR}" | |
/opt/homebrew/bin/brew install mysql > /dev/null | |
fi | |
echo "${GREEN}(Re)Starting MySQL...${NOCOLOR}" | |
/opt/homebrew/bin/brew services restart mysql | |
# Sleep for 5 seconds to give MySQL a chance to start up | |
sleep 2 | |
echo "${GREEN}Creating database ${dbName}...${NOCOLOR}" | |
mysql -u${dbUser} -e "CREATE DATABASE IF NOT EXISTS ${dbName}" | |
mysql -u${dbUser} ${dbName} < ./docker/templates/init-db.sql | |
echo "${GREEN}Setting privileges for database ${dbName}${NOCOLOR}" | |
mysql -u${dbUser} -e "ALTER USER '${dbUser}'@'localhost' IDENTIFIED WITH mysql_native_password BY '${dbPass}';" | |
echo "${GREEN}Updating your migrations...${NOCOLOR}" | |
/opt/homebrew/bin/php artisan migrate > /dev/null | |
echo "${GREEN}Generating application key...${NOCOLOR}" | |
/opt/homebrew/bin/php artisan key:generate | |
# No more caffeine please. I've a headache. | |
kill "$caffeinatepid" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment