Skip to content

Instantly share code, notes, and snippets.

@paul1522
Last active October 17, 2017 05:27
Show Gist options
  • Save paul1522/bfa6c8b10e5244912cfb179d0989050e to your computer and use it in GitHub Desktop.
Save paul1522/bfa6c8b10e5244912cfb179d0989050e to your computer and use it in GitHub Desktop.
Yaris
# Add this to your .bash_aliases
function _yaris_showhelp()
{
echo " "
echo "Yaris: Affordable 'laravel new' for Laravel Homestead"
echo "________________________________________________________"
echo "This command line tool will: "
echo " - Create a new Laravel project."
echo " - Initialize a git repo, make the initial commit, and optionally checkout a new branch."
echo " - Update the '.env' file."
echo " - Create the mysql database."
echo " - Create the nginx config files and reload nginx."
echo " - Update the 'Homestead.json' file."
echo " "
echo "Usage:"
echo " yaris project_name [arguments]"
echo " "
echo "Options:"
echo " -h, --help Show brief help"
echo " -p, --path Install application in this path"
echo " -d, --dev Install the latest development release of the Laravel framework"
echo " -a, --auth Use Artisan to scaffold all of the routes and views you need for authentication"
echo " -n, --node Runs 'yarn' after creating the project"
echo " -b, --branch Create a new branch named 'dev' make it current"
echo " -j, --jetbrains Prepare for JetBrains IDEs"
echo " -v, --voyager Install Voyager"
echo " "
}
## Spooned from https://github.com/tightenco/lambo
function yaris() {
PROJECTPATH="."
MESSAGE="Initial commit."
DEVELOP=false
AUTH=false
NODE=false
BRANCH=false
JETBRAINS=false
VOYAGER=false
SHORTOPTS="hdanjvp:b::"
LONGOPTS="help,dev,develop,auth,node,jetbrains,voyager,path:,branch::"
PARSED=$(getopt --options "$SHORTOPTS" --longoptions "$LONGOPTS" --name yaris -- "$@")
if [[ $? -ne 0 ]]; then
return 2
fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
_yaris_showhelp
return 0
;;
-d|--develop|--dev)
DEVELOP=true
shift
;;
-a|--auth)
AUTH=true
shift
;;
-n|--node)
NODE=true
shift
;;
-p|--path)
PROJECTPATH="$2"
shift 2
;;
-b|--branch)
case "$2" in
"") BRANCH="dev" ; ;;
*) BRANCH="$2" ; ;;
esac
shift 2
;;
-j|--jetbrains)
JETBRAINS=true
shift
;;
-v|--voyager)
VOYAGER=true
shift
;;
--)
shift
break
;;
*)
echo "Invalid option $1"
return 3
;;
esac
done
if [[ $# -ne 1 ]]; then
echo "yaris: project_name is required."
_yaris_showhelp
return 0
fi
PROJECTNAME=$1
PROJECTDOMAIN=`echo "$PROJECTNAME.dev" | awk '{print tolower($0)}'`
DATABASENAME=`echo "$PROJECTNAME" | awk '{print tolower($0)}'`
echo " "
echo " ***********************************************"
echo " Creating new Laravel app $PROJECTNAME"
echo " ***********************************************"
cd "$PROJECTPATH" || return 0
[[ -d "$PROJECTNAME" ]] && echo "yaris: project_name $PROJECTNAME already in use." && return 0
if [[ "$DEVELOP" = true ]]; then
laravel new "$PROJECTNAME" --dev
else
laravel new "$PROJECTNAME"
fi
cd "$PROJECTNAME" || return 0
## Update .env to point to this database and this URL.
PROJECTURL="http://$PROJECTDOMAIN"
sedCommands=(
"/DB_DATABASE/s/homestead/$DATABASENAME/"
"/APP_URL/s/localhost/$PROJECTDOMAIN/"
"/APP_NAME/s/Laravel/$PROJECTNAME/"
)
for sedCommand in "${sedCommands[@]}"; do
sed -i "$sedCommand" .env
done
## Create database.
sudo bash /vagrant/scripts/create-mysql.sh "$DATABASENAME"
## Create nginx site.
PROJECTROOT=$(readlink -f .)
PROJECTHOME="$PROJECTROOT/public"
sudo bash /vagrant/scripts/serve-laravel.sh "$PROJECTDOMAIN" "$PROJECTHOME"
sudo bash /vagrant/scripts/create-certificate.sh "$PROJECTDOMAIN"
## Restart nginx.
sudo /etc/init.d/nginx reload
## Update Homestead.json with the new database name and site mapping.
if [[ -f "/vagrant/Homestead.json" ]]; then
cp "/vagrant/Homestead.json" "/vagrant/Homestead.json.bak"
jq ".databases = ( .databases + [\"$DATABASENAME\"] | unique )" \
< "/vagrant/Homestead.json.bak" \
| jq ".sites = ( .sites + [ { \"map\":\"$PROJECTDOMAIN\",\"to\":\"$PROJECTHOME\" } ] | unique_by(.map) )" \
> "/vagrant/Homestead.json"
fi
if [[ "$NODE" = true ]]; then
yarn
fi
if [[ "$AUTH" = true ]]; then
php artisan make:auth
fi
if [[ "$VOYAGER" = true ]]; then
composer require tcg/voyager
sed -i -e '/Package Service Providers/,/\*\// s/\*\//\*\/\
TCG\\Voyager\\VoyagerServiceProvider::class,/' config/app.php
php artisan voyager:install
#php artisan voyager:admin homestead@$PROJECTDOMAIN --create
fi
if [[ "$JETBRAINS" = true ]]; then
composer require --dev barryvdh/laravel-ide-helper
sed -i -e '/public function register/,/{/ s/{/{\
if ($this->app->environment() !== '\''production'\'') {\
$this->app->register(\\Barryvdh\\LaravelIdeHelper\\IdeHelperServiceProvider::class);\
}/' app/Providers/AppServiceProvider.php
fi
git init
git add --all
git commit -m "$MESSAGE"
if [[ "$BRANCH" != false ]]; then
git checkout -B "$BRANCH"
fi
echo "***********************************************"
echo "Project Ready! Now go build something practical."
echo " "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment