Skip to content

Instantly share code, notes, and snippets.

@beiker
Last active October 2, 2019 20:39
Show Gist options
  • Select an option

  • Save beiker/899967a1cd3dbfb8ff46 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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
@beiker
Copy link
Author

beiker commented Mar 28, 2015

This is a shell script that creates a new Laravel / Homestead project for a given name without having to go through the entire process.

Some tasks that this script does for you are:

  • Add the new site to the /etc/hosts file.
  • Add the propertys (folders, sites, databases) to the Homestead.yaml file.
  • Creates the database in mysql or postgres.
  • Serve the new site in Homestead with the serve script that is available on your Homestead environment.
  • Can remove sites from /etc/hosts and Homestead.yaml file.
  • You can provision Homestead with the argument --provision (Your existing databases will be destroyed and recreated).

This script is designed to work with Homestead

Installation

Download the script to /usr/local/bin and chmod to make it executable

cd /usr/local/bin && sudo wget https://gist.githubusercontent.com/beiker/899967a1cd3dbfb8ff46/raw/3b92dfd2867e65dc81db589abf0db387088c298a/laravel-project.sh && sudo mv /usr/local/bin/laravel-project.sh /usr/local/bin/laravel-project && sudo chmod +x /usr/local/bin/laravel-project

Usage

Run the laravel-project --help command to see instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment