Problem
I am developing an R package and I want to increase the version automatically each time I build it. I want that to be able to associate my results to package versions. For now I was using my own ugly function to do that.
My question is: is there a way to do it better? Or, should I avoid doing that in general?
Another option
Another option I was thinking of is to install my package (hosted in github) using ´devtools::install_github´ and then save with my results (or adding to plots) the GithubSHA1 that is saved in the installed DESCRIPTION file.
For example I can get the version and GithubSHA1 like that for the ´devtools´ package:
read.dcf(file=system.file("DESCRIPTION", package="devtools"), fields=c("Version", "GithubSHA1")) ## Version GithubSHA1 ## [1,] "1.5.0.99" "3ae58a2a2232240e67b898f875b8da5e57d1b3a8"
My tries so far
I wrote the following function to produce a new DESCRIPTION file, with updated version and date. (Increasing the major version is something I don't mind increasing per hand)
incVer <- function(pkg, folder=".", increase="patch"){ ## Read DESCRIPTION from installed package ´pkg´ and make new one on the specified ## ´folder´. Two options for ´increase´ are "patch" and "minor" f <- read.dcf(file=system.file("DESCRIPTION", package=pkg), fields=c("Package", "Type", "Title", "Version", "Date", "Author", "Maintainer", "Description", "License", "Depends", "Imports", "Suggests")) curVer <- package_version(f[4]) if(increase == "patch") { curVer[[1,3]] <- ifelse(is.na(curVer$patchlevel), 1, curVer$patchlevel + 1) } else if (increase == "minor") { curVer[[1,2]] <- ifelse(is.na(curVer$minor), 1, curVer$minor + 1) curVer[[1,3]] <- 0 } else { stop(paste("Can not identify the increase argument: " , increase)) } f[4] <- toString(curVer) ## Update also the date f[5] <- format (Sys.time(), "%Y-%m-%d") write.dcf(f, file=paste(folder, "DESCRIPTION", sep="/")) }
If you're updating R itself, you should update your packages. Depending on your installation, R will make a new directory for packages anyway, so you either have to relink your old set or reinstall them. If you're going to relink, update and rebuild, or you'll likely get a lot of annoying warnings later to do so.
If you only want to update a single package, the best way to do it is using install. packages() again. In RStudio, you can also manage packages using Tools -> Install Packages.
To install a specific version of a package, we need to install a package called “remotes” and then load it from the library. Afterwards we can use install_version() by specifying the package name and version needed as shown below.
If you are using git, then you can use git tags to create a version string. This is how we generate the version string of our igraph
library:
git describe HEAD --tags | rev | sed 's/g-/./' | sed 's/-/+/' | rev
It gives you a format like this:
0.8.0-pre+131.ca78343
0.8.0-pre
is the last tag on the current branch. (The last released version was 0.7.1, and we create a -pre
tag immediately after the release tag.) 131
is the number of commits since the last tag. ca78343
is the first seven character of the hex id of the last commit.
This would be great, except that you cannot have version strings like this in R packages, R does not allow it. So for R we transform this version string using the following script: https://github.com/igraph/igraph/blob/develop/interfaces/R/tools/convertversion.sh
Essentially it creates a version number that is larger than the last released version and smaller than the next versions (the one in the -pre
tag). From 0.8.0-pre+131.ca78343
it creates
0.7.999-131
where 131
is the number of commits since the last release.
I put the generation of the DESCRIPTION
file in a Makefile. This replaces the date, and the version number:
VERSION=$(shell ./tools/convertversion.sh) igraph/DESCRIPTION: src/DESCRIPTION version_number sed 's/^Version: .*$$/Version: '$(VERSION)'/' $< | \ sed 's/^Date: .*$$/Date: '`date "+%Y-%m-%d"`'/' > $@
This is quite convenient, you don't need to do anything, except for adding the release tags and the -pre
tags.
Btw. this was mostly worked out by my friend and igraph co-developer, Tamás Nepusz, so the credit is his.
For a simpler approach, consider using the crant
tool with the -u
switch. For instance,
crant -u 3
will increment the third component of the version by one. There is also Git and SVN integration, and a bunch of other useful switches for roxygenizing, building, checking etc..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With