Last active
October 2, 2019 20:39
-
-
Save beiker/899967a1cd3dbfb8ff46 to your computer and use it in GitHub Desktop.
Bash script that creates a new laravel project and performs all Homestead configurations for you
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
| #!/bin/bash | |
| # This script creates a new laravel project and performs all Homestead configuration for you. | |
| # | |
| # This are some other things that the script do for you: | |
| # | |
| # - Add the new site to the /etc/hosts file | |
| # - Add the configurations (folders, sites, databases) to the Homestead.yaml file | |
| # - Creates the database in mysql or postgres | |
| # - Serve the new site in homestead | |
| # - Can remove a site from /etc/hosts and Homestead.yaml file | |
| # | |
| # This script is based on the following script by @jasonclewis | |
| # https://gist.github.com/jasonlewis/6291983 | |
| # | |
| # Default Variables | |
| application=0 | |
| domain="app" # Default domain app. i.e. foobar.app | |
| database="mysql" # Default database engine | |
| provision=false # true = provision homestead | |
| args=("$@") # Arguments from terminal | |
| index=0 | |
| homestead_ip="192.168.10.10" | |
| composer_path="$HOME/.composer/vendor" | |
| laravel_installer_path="$composer_path/bin/laravel" | |
| homestead_composer_path="$composer_path/laravel/homestead/homestead" | |
| homestead_yaml_file="$HOME/.homestead/Homestead.yaml" | |
| # Colors | |
| red='\033[0;31m' | |
| green='\033[0;32m' | |
| yellow='\033[1;33m' | |
| blue='\033[1;34m' | |
| NC='\033[0m' # No Color | |
| # Display the usage information of the command. | |
| create-project-usage() { | |
| cat <<"USAGE" | |
| Usage: laravel-project <name> [OPTIONS] | |
| -h, --help Show this help screen | |
| -d, --domain Specify a domain, default is "app". i.e. http://name.app | |
| -db, --database Specify the database engine to use in the site, values: mysql and postgres. | |
| -r, --remove Remove a site | |
| --provision Provision Homestead (Your existing databases will be destroyed and recreated) | |
| Examples: | |
| laravel-project foo | |
| laravel-project foo --domain dev | |
| laravel-project foo --database postgres | |
| laravel-project foo -d dev -db postgres | |
| laravel-project --remove foo | |
| USAGE | |
| exit 0 | |
| } | |
| # Create the new laravel proyect using the Laravel Installer or Composer | |
| create-project() { | |
| if [ -f $laravel_installer_path ]; | |
| then | |
| echo -e "${yellow}Creating the Application '$application' with Laravel Installer...${NC}" | |
| laravel new $application | |
| else | |
| echo -e "${yellow}Creating the Application '$application; with Composer...${NC}" | |
| composer.phar create-project --prefer-dist laravel/laravel $application | |
| fi | |
| } | |
| # Add new site to Hosts file | |
| add-to-hosts-file() { | |
| echo -e "${yellow}Adding $application.$domain to the /etc/hosts file...${NC}" | |
| sudo sed -i '1s/^/'$homestead_ip' '$application.$domain'\n/' /etc/hosts | |
| } | |
| # Add new site configuration to Homestead.yaml file located in ~/.homestad/Homestead.yaml | |
| add-to-homestead-file() { | |
| if [ -f $homestead_yaml_file ]; | |
| then | |
| echo -e "${yellow}Adding '$application' folder to the $homestead_yaml_file file...${NC}" | |
| path="${PWD/$HOME/\\~}/$application" | |
| map_folder="${path//\//\\/}" | |
| to_folder="\/home\/vagrant\/$application" | |
| # Add the folder map | |
| sed -i 's/.*folders:.*/&\n - map: '$map_folder'\n to: '$to_folder' /' $homestead_yaml_file | |
| # Add the site map | |
| sed -i 's/.*sites:.*/&\n - map: '$application.$domain'\n to: '$to_folder'\/public /' $homestead_yaml_file | |
| # Add the database | |
| sed -i 's/.*databases:.*/&\n - '$application'/' $homestead_yaml_file | |
| fi | |
| } | |
| # Add the new site to nginx. | |
| serve-domain() { | |
| echo -e "${yellow}Adding site to nginx...${NC}" | |
| ssh -t -p 2222 [email protected] "sudo bash /vagrant/scripts/serve.sh $application.$domain /home/vagrant/$application/public 80" | |
| } | |
| # Create the database in mysql or postgres. | |
| create-database() { | |
| echo -e "${yellow}Creating database in $database...${NC}" | |
| if [ $database == "mysql" ]; then | |
| ssh -t -p 2222 [email protected] "mysql -uhomestead -psecret -e \"CREATE DATABASE $application DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci\";" | |
| else | |
| ssh -t -p 2222 [email protected] "PGPASSWORD=secret psql -U homestead -h localhost -c \"CREATE DATABASE $application OWNER homestead\"" | |
| fi | |
| } | |
| # Reload Homestead to take new changes. | |
| reload-homestead() { | |
| echo -e "${yellow}Reloading Homestead${NC}" | |
| if [ -f $homestead_composer_path ]; | |
| then | |
| homestead reload | |
| else | |
| cd $HOME/Homestead | |
| vagrant reload | |
| fi | |
| } | |
| # Provision Homestead to take new changes. | |
| provision-homestead() { | |
| echo -e "${yellow}Provisioning Homestead...${NC}" | |
| if [ -f $homestead_composer_path ]; | |
| then | |
| homestead halt | |
| homestead up --provision | |
| else | |
| cd $HOME/Homestead | |
| vagrant halt | |
| vagrant up --provision | |
| fi | |
| } | |
| # Remove a project. | |
| remove-project() { | |
| echo -e "${yellow}Removing $application from /etc/hosts.${NC}" | |
| sudo sed -i '/'$application'/d' /etc/hosts | |
| echo -e "${yellow}Removing $application from $homestead_yaml_file ${NC}" | |
| sed -i '/- '$application'/d' $homestead_yaml_file | |
| sed -i '/'$application'/d' $homestead_yaml_file | |
| reload-homestead; | |
| echo -e "${green}Project has been removed. The Laravel folder application still exists.${NC}" | |
| exit 0 | |
| } | |
| if [ $1 ]; then | |
| application="$1"; | |
| database_name=application | |
| # Loop to read options and arguments. | |
| for key in $@ | |
| do | |
| case "$key" in | |
| '--domain'|'-d') | |
| domain=${args[index+1]};; | |
| '--database'|'-db') | |
| database=${args[index+1]};; | |
| '--remove'|'-r') | |
| application=$2 | |
| remove-project;; | |
| '--provision'|'-p') | |
| provision=true;; | |
| '--help'|'-h') | |
| create-project-usage;; | |
| esac | |
| index=$((index+1)) | |
| done | |
| create-project; | |
| add-to-hosts-file; | |
| add-to-homestead-file; | |
| if [ $provision == true ]; | |
| then | |
| provision-homestead; | |
| else | |
| serve-domain; | |
| create-database; | |
| reload-homestead; | |
| fi | |
| echo -e "${green}You can now browse to your Virtual Host at http://$application.$domain :)${NC}" | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a shell script that creates a new
Laravel/Homesteadproject for a given name without having to go through the entire process.Some tasks that this script does for you are:
/etc/hostsfile.Homestead.yamlfile.mysqlorpostgres.Homesteadwith theservescript that is available on your Homestead environment./etc/hostsandHomestead.yamlfile.--provision(Your existing databases will be destroyed and recreated).This script is designed to work with Homestead
Installation
Download the script to
/usr/local/binandchmodto make it executableUsage
Run the
laravel-project --helpcommand to see instructions.