At some point during the development of version 2.0 of my software package XMI-MSIM, I decided to implement a routine that would allow for the program to check if newer versions (updates) were available. This would be extremely useful for the users of the Windows and Mac OS X builds of my package, since these operating systems do not come with package-management tools as most Linux distributions do (I am staying far away from Mac App Store and Windows store as I am not willing to pay their developer fees).
Initially I was looking at Sparkle, a fantastic tool for OS X apps. However, this would mean a different solution for my Windows build…
Since my goal is to keep the platform specific code as low as possible (#ifdef’s really are quite ugly things), obviously I had to come up with a different solution.
Getting the tags with curl
In a rare moment of clarity, I came up with the idea of using git tags for this. Like most people, I am using the tags to indicate releases, and as a rule I include the version number in the tagname (e.g. XMI-MSIM-1.0). My method consists of having a routine called
check_for_updates (what’s in a name?), download the list of tags from github.com (using the github v3 API for tags). As I was writing (this part of) my application XMI-MSIM in C, I was looking for a library that could easily accomplish this. The quest yielded libcurl, an extremely versatile tool for transferring data using many, many protocols. The code I used for this was something like (full code at the end of this post):
Parse the JSON code with Json-Glib and compare versions
The buffer that is returned in
chunk, contains JSON code. To parse this, I used the Json-Glib library, a logical choice since my project is written in Gtk+ anyway… Code extract:
Important here is the
json_array_foreach_element function, which will call
check_version_of_tag for each tag, and update the highest found tag version number with each iteration.
After this, all that needs to be done is to compare this highest tag version with the internal version number (
PACKAGE_VERSION, typically provided by a configure script), and the result is returned.
Now the method that I just described assumes that the version numbering is done with one major number and one minor number, allowing me to easily convert into a float for version comparison. Although this is sufficient for my personal needs, others may have to come up with a slightly more complex algorithm allowing to compare version numbers consisting of a major, minor and macro version number.
This is the full code: feel free to hack away at it. It is taken from xmimsim-gui-updater.c, which also contains code to download the new packages from a webserver (also using curl).