Skip to content

Instantly share code, notes, and snippets.

@Michael-Stokoe
Last active April 17, 2019 13:23
Show Gist options
  • Save Michael-Stokoe/ae660e6047767c42e8f10e75cbd919b5 to your computer and use it in GitHub Desktop.
Save Michael-Stokoe/ae660e6047767c42e8f10e75cbd919b5 to your computer and use it in GitHub Desktop.
Laravel/Homestead New Project Script
# Usage:
# Copy this into your .zshrc or .bashrc, then run `source .zshrc`/`source .bashrc`
# the command 'nps' will be available to you afterwards.
# There are some variables you might need to change starting at line 22, ending at line 28.
function nps () {
# New Project Setup. (MUST be ran from inside project DIR)
# Uses the directory name of the project you're in as the URL for the site
# Copies .env, replaces APP_NAME, APP_URL, DB_HOST, DB_DATABASE and REDIS_HOST
# Might need to change the new values for something a bit more specific to your own setup.
echo "Checking current working directory:"
pwd
if ! [ -f .env.example ]; then
echo "Current working directory does not appear to be a Laravel Project."
return 0
fi
# Change these variables to match your setup!
homesteadip=192.168.10.10
# Top-level Domain: I use .test for my development, personally. But others may use .dev etc.
tld=test
# Homestead directory
hsdir=~/Homestead
# Homestead.yaml location
hsyaml=~/Homestead/Homestead.yaml
# Current directory name. (To use as URL and DB name).
dirname=${PWD##*/}
if ! [ -f .env ]; then
cp .env.example .env
echo ".env.example has been cloned into .env."
fi
sed -i "s/APP_NAME=Laravel/APP_NAME="$dirname"/g" .env
sed -i "s/APP_URL=http:\/\/localhost/APP_URL=http:\/\/"$dirname".$tld/g" .env
sed -i "s/DB_HOST=127\.0\.0\.1/DB_HOST=$homesteadip/g" .env
sed -i "s/DB_DATABASE=homestead/DB_DATABASE="$dirname"/g" .env
sed -i "s/REDIS_HOST=127\.0\.0\.1/REDIS_HOST=$homesteadip/g" .env
# Comment out the following few lines if you DO NOT also wish to automatically add this new project to your hosts & homestead.yaml
# For this echo command to work you'll need to have passwordless sudo enabled on your profile. (Line 45.)
# You can enable this with the following command on most Linux Distros
# echo "yourusername ALL=(ALL)NOPASSWD:ALL" | sudo tee -a /etc/sudoers
if grep -q "$homesteadip $dirname.$tld" /etc/hosts; then
echo "Hosts entry didn't exist for application."
echo "Adding hosts entry for this application."
echo "\n$homesteadip $dirname.$tld" | sudo tee -a /etc/hosts
echo "Hosts entry added. Your app will be live at http://$dirname.$tld"
fi
if grep -q "map: $dirname.$tld" $hsyaml; then
echo "No entries in $hsyaml for this application, adding them now."
sed -i "/^sites:/a \ \ \ \ - map: $dirname.$tld" $hsyaml
sed -i "/^\ \ \ \ - map: $dirname.$tld/a \ \ \ \ \ \ to: /home/vagrant/projects/$dirname/public \n" $hsyaml
echo "Adding database with name $dirname to $hsyaml"
sed -i "/^databases:/a \ \ \ \ - $dirname" $hsyaml
fi
echo "Running yarn & composer"
yarn
composer install
echo "Generating App Key"
php artisan key:generate
# Comment out these 3 lines if you DO NOT wish to reprovision your vagrant box after doing this setup.
echo "Reprovisioning Vagrant Box"
cd $hsdir
vagrant up --provision
cd -
# Perhaps not entirely useful on a new project, but at least it makes sure you've set up the scripts correctly.
echo "Running database migrations"
php artisan migrate:fresh --force --seed
echo "ALL DONE!"
echo "Find your app online at https://$dirname.$tld"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment