Appalling Farrago

A blog about stuff

Shell Script Suggestions for Speedy Setups
12 Jan 2017

This article was originally posted on the thoughtbot blog here

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.

The Rule of Silence

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:


# Exit if any subcommand fails
set -e


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 -1

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"

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.

A Side Note on Joy

You’ll notice two elements in this script to improve readability.

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 ❤️.

Installing Packages/Gems/Addons

Once we’ve determined that all dependencies are installed we can automate installing packages:


# Exit if any subcommand fails
set -e


printf "${CLEAR_LINE}[2/6]⏳   Installing yarn packages"

yarn > /dev/null

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.

Your Special Snowflake Specific Steps

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:


# Exit if any subcommand fails
set -e


. ./bin/colors

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 github
    set +e
    git clone $2 > /dev/null

    # if the last command gives a non-zero exit code we should warn the user
    if [[ $? != 0 ]]; then
      printf "${CLEAR_LINE}${RED}   $1 failed to clone! You might not have permissions.${NO_COLOR}\n"
    set -e

  cd $DIR

clone dependency_one [email protected]:my_org/dependency_one.git
clone dependency_two [email protected]: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.

Pulling it all together

I like to continue this pattern of having scripts with a single responsibility and calling them all together in bin/setup:


# Exit if any subcommand fails
set -e


. ./bin/colors


printf "${CLEAR_LINE}[6/6]🎉${GREEN}   Finished!${NO_COLOR}\n"