Minor optimizing of a Laravel workflow using UNIX, Bash, and Python.

Kasper Kloster
3 min readDec 4, 2020

And how I’m saving 1 sec in 19 years

On a typical day, I start up my Mac and begin to work.
Usually, I’ll begin with:

  • Open the terminal
  • Start Mysql.
  • Cd into my project folder
  • Open the project in Atom
  • Run php artisan serve
  • Open the localhost url in my browser

It doesn’t take long, but it’s just that quicker to write a simple command, so I think it’s worth the time to create.
We will create a bash script + an alias, which opens our python script, that will do the work for us.

Creating bash script

cd /usr/local/bin && touch work.sh

Open work.sh in vim or nano, or whatever text editor you prefer.

sudo nano work.sh

I’ll add a small line of code, just so we can check if everything is working.

#! /bin/bashecho “hey”

Save and exit.
To make sure it’s executable, we’ll change the permissions.

sudo chmod +x work.sh

Run this file by typing

work.sh

You should see “hey” on the screen.

We are to save time, let’s add an alias, so we don’t have to write ‘work.sh’, but only ‘work’.

Navigate to your home directory, by typing cd

cd

and type ls -la To show all files and folder (including hidden ones)

ls -la

If you don’t see .zshrc here, then create it.
If you already have it, then edit the file

sudo vim .zshrc

create the alias by adding this line:

alias work=’work.sh’

Save and exit

Refresh the shell environment by:

source ~/.zshrc

If you type

work

we should see “hey” on our screen again,

Python Script

We will stay in our home folder.
We will create a hidden directory called .work and cd into this.

mkdir .work && cd .work

Here will our little app live. Let’s create the python file

touch main.py

And make sure it’s executable

sudo chmod +x main.py

Open this file, and let’s begin writing in python.

The script is really simple.
First, we import os and webbrowser:

import os
import webbrowser

We save a variable, which has the path to our Laravel projects (I keep all my Laravel projects in the same folder).
Then we save a list, which contains all our Laravel projects/folder names.

# Path to the folder that contains your apps
path = str(“/Users/kasper/Sites/”)
projectFolders = list(os.listdir(path)

Next we print out the folder names and it’s index.

# Loop through folders
i = 0
while i < len(projectFolders):
# Prints folder name and index
print projectFolders[i] + “[“,i , “]”
i += 1

The user gets a choice / Which index will you open?

# User choice
choice = raw_input(‘Which project will you work on?’)

From here we are:

  • Starting MySQL
  • Opens the site in the browser.
  • Navigate to our selected folder
  • Run php artisan serve
# Start mysql
os.system(“mysql.server start”)
# Open Browser
webbrowser.open(‘http://127.0.0.1:8000')
# Opens Atom
os.system(“atom “ + path + projectFolders[int(choice)])
# Navigate to folder, run php artisan serve
os.system( “cd “ + path + projectFolders[int(choice)] + “ && php artisan serve” )

Update the Bash script to run the Python script

Navigate back to /usr/local/bin and edit work.sh

vim /usr/local/bin/work.sh

Update work.sh to this:

#! /bin/bashpython /Users/username/.work/main.py

Save and exit.

And that’s it!

If you open your terminal and type work, our scripts will run.

From now on, I’ll save 1 sec. everyday!

It took me two hours, to write the scripts and write this article. Which means that in 19 years, my script will finally pay off….
Damn.

You, however, can start saving your 1 second now!

Download find the Python script here:
https://github.com/KasperKloster/saving-a-second

--

--