Contact Me
Blog

A Bash Script for New projects

Whenever I start a new web project, the initial steps are pretty much the same:

  • Create a new folder, in my case in /var/www
  • Change the ownership of the new folder
  • Come up with a new hostname, e.g. myproject.local
  • Create an entry in etc/hosts
  • Create an Apache virtual host for it
  • Create a new database
  • Create a new Git repository
  • Intialise the Git repository
  • Create a new project in Sublime

Not particularly hard, but pretty repetitive. And if something's repetitive, chances are it can be scripted!

So here's what I came up with:

# CONFIGURATION
#MYSQL credentials
MYSQL_USER="root"
MYSQL_PASS=""
# The user Apache runs as - e.g. _www, www-data
APACHE_RUNAS="_www"
#Bitbucket credentials
BB_USER="USERNAME"
BB_PASS="PASSWORD"
#Hostname TLD - e.g. .local, .dev
HOSTNAME_TLD=".local"
#Slugs are generated by replacing spaces with... (e.g. a dash, or underscore)
space_replace_char='-'
# CONFIGURATION ENDS
# Ask the user for a project name
echo "Please enter the project name:"
read name
# Generate a default slug, e.g. a-project-named-something
default_slug=$(echo "$name" | tr A-Z a-z )
default_slug=$(echo "$default_slug" | tr ' ' "$space_replace_char")
# Allow the user to override the generated slug
echo "Enter the slug, or enter to accept the default: "$default_slug
read slug
if [ "$slug" = "" ]; then
slug=$default_slug
fi
echo "Using "$slug" as the slug"
# Assign the variables
host=$slug".local"
# Create a project directory
directory="/var/www/"$slug
# Generate a database name
database=$(echo "$name" | tr A-Z a-z )
database=$(echo $database | tr -d ' ')
echo "Creating directory "$directory
# Create the directory
sudo mkdir $directory
# Change ownership
sudo chown -R $USER:$APACHE_RUNAS $directory
echo "Creating virtual host "$host
# Create the entry in hosts
echo "127.0.0.1\t\t$host\n" >> /etc/hosts
# Append the virtual host entry
echo "<VirtualHost *:80>\nServerName $host\nDocumentRoot $directory\n\n\t<Directory />\n\t\tAllowOverride All\n\t\tRewriteEngine On\n\t\tRewriteBase /\n\t</Directory>\n\n</VirtualHost>" >> /etc/apache2/extra/httpd-vhosts.conf
echo "Creating database "$database
# Create a temporary file to create a database schema
echo "create database $database;" > temp.sql
# Create the schema; of course we'll have to stop and ask for the DB password
if [ "$MYSQL_PASS" = "" ]; then
mysql -u$MYSQL_USER < temp.sql
else
mysql -u$MYSQL_USER -p$MYSQL_PASS < temp.sql
fi
# Remove the temporary SQL file
rm temp.sql
# Create the git repo
curl -u$BB_USER:$BB_PASS -X POST https://api.bitbucket.org/1.0/repositories -d "name=$slug" -d 'is_private=1' -d 'scm=git'
# Change into the new project directory
cd $directory
# Run Git init...
git init
# ...and point it to Bitbucket
git remote add origin ssh://git@bitbucket.org/$BB_USER/$slug.git
# Create the Sublime Project
echo '{\n\t"folders":\n\t[\n\t\t{\n\t\t\t"path": "'$directory'"\n\t\t}\n\t]\n}' > ~/$name.sublime-project
echo "Finished."

A few notes:

  • This script is designed for a Mac, but it should work on any OS provided you check / modify things like the paths
  • Don't forget to set your MySQL / Bitbucket credentials at the top of the file
  • You'll probably need to use sudo to run this
  • The script asks for a "friendly" name, e.g. "My Awesome Project". It then generates a slug, e.g. my-awesome-project. The hostname becomes my-awesome-project.local, the directory gets named my-awesome-project, as does the Git repo, and the database ends up being called myawesomeproject. You can always modify the script to alter this behaviour
  • You might want to expand the script to create a database user too, perhaps with a random password
  • It creates a Git repository using Bitbucket's REST API. You may wish to alter the behaviour - in which case consult the documentation - or you might be able to add Github support, which isn't something I tried.