自动增加 R 包的版本 [英] Automating version increase of R packages

查看:14
本文介绍了自动增加 R 包的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我正在开发一个 R 包,我想在每次构建它时自动增加版本.我希望它能够将我的结果与包版本相关联.现在我正在使用我自己的 ugly 函数来做到这一点.

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?

另一种选择

我正在考虑的另一个选择是使用devtools::install_github"安装我的包(托管在 github),然后使用我的结果(或添加到绘图)保存在安装的说明文件中的 GithubSHA1.

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.

例如,我可以获取 'devtools' 包的版本和 GithubSHA1:

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"

我目前的尝试

我编写了以下函数来生成一个新的说明文件,其中包含更新的版本和日期.(我不介意增加主版本)

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="/"))
}

推荐答案

如果你使用的是git,那么你可以使用git标签来创建一个版本字符串.这就是我们生成 igraph 库的版本字符串的方式:

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

它为您提供如下格式:

0.8.0-pre+131.ca78343

0.8.0-pre 是当前分支的最后一个标签.(最后发布的版本是 0.7.1,我们在发布标签之后立即创建一个 -pre 标签.)131 是自上一个标签以来的提交次数.ca78343 是最后一次提交的十六进制 id 的前七个字符.

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.

这很好,除了你不能在 R 包中包含这样的版本字符串,R 不允许它.因此,对于 R,我们使用以下脚本转换此版本字符串:https://github.com/igraph/igraph/blob/develop/interfaces/R/tools/convertversion.sh

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

本质上,它创建的版本号大于上一个发布的版本,小于下一个版本(-pre 标记中的那个).它从 0.8.0-pre+131.ca78343 创建

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

其中 131 是自上次发布以来的提交次数.

where 131 is the number of commits since the last release.

我把 DESCRIPTION 文件的生成放在一个 Makefile 中.这将替换日期和版本号:

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"`'/' > $@

这很方便,你不需要做任何事情,除了添加发布标签和-pre 标签.

This is quite convenient, you don't need to do anything, except for adding the release tags and the -pre tags.

顺便说一句.这主要是由我的朋友和 igraph 的共同开发者 Tamás Nepusz 完成的,所以功劳是他的.

Btw. this was mostly worked out by my friend and igraph co-developer, Tamás Nepusz, so the credit is his.

这篇关于自动增加 R 包的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆