Quickly set up existing Laravel projects

Lately, a lot of cool shell aliases and functions have been posted on Twitter. Some of my favourite ones include nah (short for git reset --hard), an alias to tail the Laravel log without including exceptions and single letter abbreviations for common tools like php artisan and yarn.

I've decided to join in on the fun and create a shell function for something I do about 5 times per day: setting up an existing Laravel project. This usually take about 10 minutes per project (and even longer if you forget to execute the next command halfway through). The whole process includes cloning the repository, installing its dependencies, creating and migrating the database, generating an APP_KEY and finally building front-end assets.

The linstall function takes care of the entire process:

#!/bin/bash

linstall ()
{
    REPO=$1
    BASENAME=$(basename $REPO)
    DIR_NAME=${BASENAME%.*}
    NAME=${2:-$DIR_NAME}
    DB_NAME=${3:-${NAME/./_}}

    git clone $REPO $NAME;
    cd $NAME;
    mysql -u root -e "CREATE DATABASE IF NOT EXISTS $DB_NAME";
    cp .env.example .env;
    sed -i '' -e "/DB_DATABASE=/ s/=.*/=$DB_NAME/" .env;
    composer install;
    php artisan key:generate;
        php artisan migrate --seed;

    if [ -f package.json ]
    then
        yarn install;
        yarn run dev;
    fi
}

To install, simply add the function to the end of your ~/.bashrc file and start a new shell session.

Installing an existing Laravel project is now super easy using the following command:

linstall git@github.com:spatie/spatie.be.git

By default linstall will use the repository name as the project's directory and database name. You can specify a different directory and database name by passing a second argument to the linstall command:

# clones into spatie_site directory and creates spatie_site database
linstall git@github.com:spatie/spatie.be.git spatie_site

# clones into spatie_site and creates spatie_db database
linstall git@github.com:spatie/spatie.be.git spatie_site spatie_db