Automating version increase of R packages

前端 未结 3 1361
遇见更好的自我
遇见更好的自我 2020-12-13 18:47

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 r

相关标签:
3条回答
  • 2020-12-13 19:06

    As auto-incrementing version numbering is not going to be built into the devtools package, I figured out a way based on Gabor's answer (the link to igraph in his answer is dead btw).

    When I am about to commit to our repository, I run this bash script to set the date to today and to set the version number based on the latest tag, the .9000 suffix (as suggested here in the book R Packages by Hadley Wickham) and the number of commits within that tag:

    echo "••••••••••••••••••••••••••••••••••••••••••••"
    echo "• Updating package date and version number •"
    echo "••••••••••••••••••••••••••••••••••••••••••••"
    sed -i -- "s/^Date: .*/Date: $(date '+%Y-%m-%d')/" DESCRIPTION
    # get latest tags
    git pull --tags --quiet
    current_tag=`git describe --tags --abbrev=0 | sed 's/v//'`
    current_commit=`git describe --tags | sed 's/.*-\(.*\)-.*/\1/'`
    # combine tag (e.g. 0.1.0) and commit number (like 40) increased by 9000 to indicate beta version
    new_version="$current_tag.$((current_commit + 9000))" # results in 0.1.0.9040
    sed -i -- "s/^Version: .*/Version: ${new_version}/" DESCRIPTION
    echo "First 3 lines of DESCRIPTION:"
    head -3 DESCRIPTION
    echo
    # ... after here more commands like devtools::document() and git commit
    

    To be clear - this script actually makes these changes to the DESCRIPTION file.

    EDIT: support for hundreds - now just increases the commit sequence number by 9000. So commit #120 in tag v0.6.1 leads to 0.6.1.9120.

    0 讨论(0)
  • 2020-12-13 19:09

    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.

    0 讨论(0)
  • 2020-12-13 19:18

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

    0 讨论(0)
提交回复
热议问题