Automated Project Versioning

For certain projects, you might find yourself in need of a way to store and manipulate version numbers for a project (i.e., managing website revisions.) Using JSON, C, and Bash, you can easily create a system for automatically managing version numbers.

JSON is a great format for storing data, especially simple datasets, such as project version numbers. Classically, version number sets contain MAJOR, MINOR, RELEASE, and BUILD.
If you’re working on a project that has hundreds of revisions, it’s a good idea to automate your version numbering (or use something like Subversion that stores revision numbers).

Assuming you’ve decided that your best option is an automated versioning system, you can easily write one using C and Bash scripts, and using JSON as a storage format. JSON has the advantage of being widely supported, allowing you to have the option of writing a script in a variety of languages that can also process the JSON file.

First, you’ll need the json-c project from Github (git clone https://github.com/json-c/json-c.git). Follow the instructions for building and installing json-c (wiki).

Next, we’ll need a sample JSON file to work with:


{ "MAJOR": "6", "MINOR": "2", "RELEASE": "3", "BUILD": "12" }

Next, we need some utilities to read/write a simple JSON file. Let’s call it the json-utils project.

jsonread.c – Read simple JSON data
jsonwrite.c – Write simple JSON data
utils.c – Utility code
utils.h – Utility code
Makefile – Makefile for the json-utils project.

Note: These utilities are for basic JSON manipulation, and don’t support arrays.

Build the json-utils project with $ make all, $ make strip, $ make install. (By default, this installs the utilities to “$HOME/scripts”.)

Why use utilities written in C when the same thing can be accomplished in, for example, PHP? Using C programs versus a scripting language is significantly faster. If speed is not a priority, you can easily port jsonread and jsonwrite to PHP, Python, etc.

Now that we have some basic JSON utilities, we need the core script: A bash script to increment the various version numbers.

Note: You may need to install realpath: $ sudo apt-get install realpath

The idea behind this script is that if called without any arguments, it will, by default, increment the BUILD field. If the BUILD field reaches its maximum value, then set it to zero and increase the RELEASE field. If that reaches its maximum value, set to zero and increment the MINOR field, and so on. This allows the script to be called thousands of times and still correctly manage the version numbers.

In addition, the script can be called with various flags to specify a specific field to modify (i.e., calling with -j will increment the MAJOR field.) It can also be called with –increase=NN or –decrease=NN to specify the amount to increase or decrease the field by.

Let’s test the script.

$ version-build.sh
$ jsonread VERSION.json BUILD
$ version-build.sh
$ jsonread VERSION.json BUILD

And that’s it! There is always room for improvement, but this basic set of tools has proven extremely useful.

Permanent link to this article: http://permafrostcodingstudio.com/articles/coding/automated-project-versioning/

Leave a Reply

Your email address will not be published. Required fields are marked *