R/increment_version.R

#' increment_dev_version
#'
#' Increment development version number in DESCRIPTION and 'codemeta.json' if it
#' exists. Current directory must be within an R package.
#'
#' The 'usethis::use_dev_version()' function only adds a fixed '.9000' as the
#' fourth component. This function is intended to be used to increment this
#' fourth component on every commit. It will also update the version number in
#' 'codemeta.json' as generated by the 'codemetar' package, if that file exists.
#'
#' This function can easily be alised for ease of use, for example in
#' '~/.bash_aliases' as 'incr="Rscript -e 'mpmisc::increment_dev_version()'"'.
#' You then only need to type 'incr' in your shell to automatically increment
#' the version.
#'
#' @export
increment_dev_version <- function () {
    pkg_dir <- here::here ()

    desc <- readLines (file.path (pkg_dir, "DESCRIPTION"))
    vers_line_num <- grep ("Version:", desc)
    vers <- strsplit (gsub ("Version: ", "", desc [vers_line_num]), "\\.") [[1]]
    old_vers <- vers
    message ("Incrementing version from ",
        paste0 (old_vers, collapse = "."),
        appendLF = FALSE
    )
    if (length (vers) == 4) {
        old_vers <- vers [-length (vers)]
        new_vers <- sprintf ("%03d", as.integer (utils::tail (vers, 1)) + 1)
    } else {
        new_vers <- "001"
    }
    new_vers <- paste0 (c (old_vers, new_vers), collapse = ".")
    message (" to ", new_vers)
    desc [vers_line_num] <- paste0 ("Version: ", new_vers)
    writeLines (desc, file.path (pkg_dir, "DESCRIPTION"))

    # codemeta
    f <- file.path (pkg_dir, "codemeta.json")
    if (file.exists (f)) {
        x <- jsonlite::fromJSON (f, simplifyVector = FALSE)
        x$version <- new_vers
        jsonlite::write_json (x, f, pretty = TRUE, auto_unbox = TRUE)
    }
}
mpadge/mpmisc documentation built on Oct. 29, 2024, 5:30 p.m.