Shell Script Suggestions for Speedy Setups

We aim to rotate onto new projects every 2-4 months. This gives us the benefit
of fresh eyes on each project every so often. Every time we rotate someone new
onto a project, they bring new energy and make a positive impact on aspects that
the current team have become complacent to.

Getting a new developer setup quickly is important for making sure they feel
equipped to succeed. If getting a laptop setup with your codebase takes more
then 30 minutes on average then there is room to improve!

Ideally, if I already have all the application’s dependencies this should take
closer to 5 minutes. Thirty minutes is attainable for most products and any
longer is going to directly affect my first impressions of the codebase. I
always want a new team member’s introduction to the product to be a joyful one.

We’ve written before about having a bin/setup script to automate
this process. There’s even a default bin/setup script shipped as part of all
new rails applications. Here are some tips I’ve recently learnt to improve first
time setups.

Be respectful of everyone’s time and attention. The rule of silence
is simply that “when a program has nothing surprising, interesting or useful to
say, it should say nothing”. I want as little information as possible when
everything is working as expected. I aim for the following examples to fulfil on
this ideal.

Every app has dependencies, be it Elasticsearch, Redis or even a certain
version of Node.js. Let us start by making sure these are installed or prompt
our script user to get them:

#!/bin/sh# Exit if any subcommand failsset-eRED='\033[0;31m'GREEN='\033[0;32m'YELLOW='\033[0;33m'NO_COLOR='\033[0m'CLEAR_LINE='\r\033[K'printf"[1/6]🔎 checking dependencies"if!command-v node > /dev/null;then
printf"${CLEAR_LINE}💀${RED} You must install node on your system before setup can continue${NO_COLOR}\n"printf"ℹ️ On macOS🍎 you should 'brew install node'\n"exit-1fi
if[[$(node --version)!="v4.6.0"]];then
printf"${CLEAR_LINE}⚠️${YELLOW} You are not using a known working version of node.${NO_COLOR}\n"printf"ℹ️ This might not be a problem but if you're having issues, try installing 4.6.0\n"printf"[1/6]🔎 checking dependencies"fi

Checkout this excellent description of why we use command -v
instead of which. This script checks if you have node installed. If not,
you’re prompted to get it. If you have it installed it will then check the
version and leave a helpful warning if your local version doesn’t match the
known working version.

The first is colors! Colors are a great way to indicate which output I need to
care about. Green generally means all is well. Yellow is a potential issue and
should be avoided at your own peril. And finally, Red states that there’s a
problem.

The second is the careful and thoughtful application of emoji. These powerful
characters have the ability to punctuate the message your trying to convey.
Notice the lack of color on lines that are purely informational. By tactfully
prefixing these lines with ℹ️ we allow users to quickly understand the essence of
the message.

A word of warning: despite popular opinion, emoji can be overused. I can give no
advice when that line is crossed but I suggest you follow your ❤️.

There are many tools that do not follow the rule of silence. To fix this we can
redirect the output to /dev/null. This is to keep our setup as noiseless as
possible. This pattern should be applied on a case by case basis. It is
sometimes desirable to allow the tool you’re using to give feedback but often
there is a lot more being output then I’m interested in. > /dev/null will only
redirect output to stdout, program errors are written to stderr and so will
still be output.

Let’s say you have something very specific to your application such as cloning
internal dependencies. You might write a script to pull down all the required
repos:

#!/bin/sh# Exit if any subcommand failsset-eDIR=$PWD. ./bin/colors
CLEAR_LINE='\r\033[K'function clone {printf"⏳ $1 is being cloned"cd$DIR/..
if[!-d$1];then# don't stop the script if there is an error cloning from githubset +e
git clone $2> /dev/null
# if the last command gives a non-zero exit code we should warn the userif[[$?!= 0 ]];then
printf"${CLEAR_LINE}❌${RED}$1 failed to clone! You might not have permissions.${NO_COLOR}\n"fi
set-efi
cd$DIR}
clone dependency_one git@github.com:my_org/dependency_one.git
clone dependency_two git@github.com:my_org/dependency_two.git

In this case I don’t want to stop the script from completing, but I do want the
user to know they do not have a complete setup. We’re using set +e to make
sure the script doesn’t stop if it hits and error and then manually checking for
a non-zero exit code.