[ JagoPG Site ]
This publication is more than a year old. The information can be out-dated.

The Art of Automation

This is one of area that the University teachers did not delve enough. At least in my University I were only been taught to automatize some compiling and dependency downloading tasks with Maven. But when the lessons arrived to Web Development, all the development process was very slow and repetitive, but just the manual way was taught.

When I arrived to my first Web Development company I found a full new world. Despite being a small company they had all tasks automatized: from the installation of a new computer to the deploy of an application in the server. Veteran developers will be astonished to my lack of knowledge, but everyone have been beginners sometime. In my case, I never found a good practice manual and you can miss lots of handy tools if you are sharp enough.

I will focus on a Django environment, but I know for a fact that other frameworks have similar ways of implementing the following tips.

Frontend automation

As a good newbie, I have developed for years coding pure CSS and vanilla JS. But this has changed since I meet the transpilers. The idea of transpilers is to convert one simpler code syntax to the original language syntax. For instance, SASS is a much simpler way to code CSS: class inheritance, support of variables, operators, automatic management of different browser attributes... you can find all its advantages at SASS.

  • Compiling SASS and JS: I use tools like Gulp for managing all transpiling tasks. On the one hand I can transpile SASS to CSS code, and on the other hand I transpile ES2015 code to JS5 code.

  • Dependency management: at the beginning I used node for downloading different libraries and then I merged with my code using some Gulp tasks. Nowadays I use Yarn that it is similar to npm, but it has a better management of old libraries than npm.

Framework automation

In my own projects, I tend to use Python as my main language due to its simplicity. But web frameworks as Django, despite being powerful tools, do not have a standardized way of doing things - at least I have never found one. So I follow this steps to automatize a bit my daily tasks:

  • Switch from development to production environment: the manage.py runserver command accepts as argument a configuration file. By default, your_project/settings.py file is employed, but you can duplicate this file and write a shell script for executing the application.

  • Install dependencies: one thing that I envy from PHP is the Composer tool. It is a clean and powerful solution for managing a project. This in Python is quite fragmented in different tools, and they depend on the platform you are going to deploy. In Django I store the dependencies in a requirements.txt file that I read and write from pip.

## Write dependencies in a file
$ pip freeze > requirements.txt

## Read dependencies from a file
$ pip install -r requirements.txt
  • Deploying the application: if you hired an IaaS you can use tools like Capistrano or Ansiable to automatize all deploy phases: JS and SASS compiling and minifying, image compression, translation building, file uploading... But if you do not have a very heavy project, or you have a very special provider, a shell script may be enough for deploying your project.
#!/bin/sh -e
## Prepare environment
source venv/bin/activate
source bin/app_consts.sh

## Transpile JS, SCSS, minify...
gulp prod

## Deploy to S3 and Heroku
python manage.py collectstatic <<< yes
git push heroku master

All these steps will speed up your development process, try applying them to your projects!