Preetesh Jain

Automating Laravel Homestead workflow with Bash

If starting Homestead, code editor, browser, running PM2 tasks and others has become a part of your daily life, then this article is for you.

Recently I automated some of these daily tasks by making bash aliases (or shortcuts) to quickly get started with my day. I call this command, smd short for start my day. So from now on, whenever I have to start my work, I just run this command and sit back enjoying a sip of tea while this bash script does everything for me. ☕️

Before we begin…

First, let’s get on the same page. I use Homestead to run my projects built on Laravel. Other things I use are Git Bash, Sublime Text 3, phpMyAdmin (to administer databases), and PM2. If you are using any of these, you will find this article helpful. Although I am assuming you know the basics of the Bash shell.

Step 1 — Create a bash profile

Open up your .bash_profile in any code editor of your choice. You can create this file if it doesn’t exist. Here’s how:

cd ~; touch .bash_profile

Now you can open this file in a code editor. This is the file where we will add all our aliases and bash functions to make our lives easier.

Step 2— Setup Homestead shortcut

Setup a Homestead command so that we don’t have to go Homestead installation directory to run vagrant up again and again.

# Homestead shortcut
function homestead() {
    ( cd ~/Homestead && vagrant $* )

Save and source the bash profile:

source ~/.bash_profile

Now you can run homestead up from anywhere in your system (not just the ~/Homestead folder). This will also help us with the aliases we will be setting up next.

Step 3 — Create shortcut for Sublime Text (for Windows users)

If you are using Windows, doing subl <filename> won’t do anything. Because Sublime Text installer doesn’t register its PATH in the system environment variables, unlike VSCode. But we can do it easily through a simple bash alias like so:

# Sublime Text shortcut
alias 'subl="/c/Program Files/Sublime Text 3/subl.exe"'

Save, exit and source again.

Now you can just do things like subl <filename> and it will open that file for you in Sublime Text! Pretty neat. ⚡️

Step 4 — Create the SMD script

Finally the fun part, creating your own smd bash function which will help you ease into your day at work. Here’s the function I use:

smd() {
    if [ $1 = "a" ]
        homestead up
        cd /e/homestead_projects/project
        subl .
        explorer .
        python -mwebbrowser "http://project.test"
        python -mwebbrowser "http://phpmyadmin.test/db_structure.php?server=1&db=project_db"
        npm run watch
    elif [ $1 = "b" ]
        homestead up
        cd /e/homestead_projects/project2
        subl .
        explorer .
        python -mwebbrowser "http://project2.test"
        python -mwebbrowser "http://phpmyadmin.test/db_structure.php?server=1&db=project2_db"
        npm run watch
    elif [ $1 = "c" ]
        cd /e/other_projects/some-website
        subl .
        explorer .
        python -mwebbrowser "http://localhost:8000"
        npm run serve

Save and source the bash profile like I mentioned above. Now you just need to run smd <option> and it will begin running your tasks. But let’s break it down to what this script is doing.

This is basically a if else function which does things based on the option you provide in the argument. So if you do smd a, it will fire up the set of tasks in the if statement. Here’s how this function will perform the tasks for option a:

  • Start Homestead, wait for it to boot and finish.
  • cd to your project directory (Assuming its in E: drive inside a folder named homestead_projects).
  • Open Sublime Text (with the project loaded, so you don’t have to manually navigate and open)
  • A file manager window (in Windows its explorer, you can change it for your OS) in your project directory.
  • Your browser with your Homestead configured sites open (this step utilizes Python, so make sure you have it installed).
  • Then it will start the Laravel Mix script which will listen to any changes you make in your front-end.

You need to customize this script to make it work for you and your projects. Extend it however you like, the basic idea is to do it in blocks of if else so that you can just do smd <option> and that in minutes, the project is ready to work on. You can read news, have coffee or whatever in the meantime. This profile will bring great Zen of mind to you because slowly these small tasks can become a hassle. I hope this inspires you to automate your daily tasks as a kick-ass Engineer.

🍻 Cheers!