Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created July 28, 2021 22:19
Show Gist options
  • Save vjcitn/d9ff5ca5919b5a5cd47c7a9a963b8a62 to your computer and use it in GitHub Desktop.
Save vjcitn/d9ff5ca5919b5a5cd47c7a9a963b8a62 to your computer and use it in GitHub Desktop.
code that can revise DESCRIPTION to add a package that is needed, bump version, and take care of git operations
#' @param dry_run logical(1) if TRUE, just return revised DESCRIPTION
#' @return `desc::description` instance; will write revised DESCRIPTION to `path` if `dry_run` FALSE.
#' @note Bumps third component of version tag
#' @export
revise_desc = function(path, type="Suggests", to_add="rmarkdown", dry_run=TRUE) {
stopifnot(is.atomic(to_add) && length(to_add)==1)
init = desc::description$new(path)
deps = init$get_deps()
if (to_add %in% deps$package) stop("already depended upon")
init = init$set_dep(type=type, package=to_add)
init$bump_version(3)
if (!dry_run) init$write(path)
init
}
#' use gert and desc to revise DESCRIPTION to add a single package to a DESCRIPTION dependencies field
#' @param pkgname package for which DESCRIPTION is to be revised
#' @param to_add character(1) package name to add to DESCRIPTION field
#' @param field character(1) a DESCRIPTION field
#' @param msg character(1) commit message, edit
#' @param branch character(1) defaults to "master"
#' @param dry_run logical(1) if TRUE, just return revised DESCRIPTION
#' @param where character(1) where to carry out revision, defaults to `tempdir()`
#' @export
revise_source = function(pkgname, to_add="rmarkdown", field="Suggests", msg="add rmarkdown to Suggests",
branch="master", dry_run=TRUE, where=tempdir()) {
curd = getwd()
td = where
on.exit(setwd(curd))
setwd(td)
if (!dir.exists(pkgname)) gert::git_clone(
sprintf("[email protected]:packages/%s", pkgname))
setwd(pkgname)
gert::git_branch_checkout(branch)
rd = revise_desc("DESCRIPTION", to_add=to_add, type=field, dry_run=dry_run)
if (dry_run) return(rd)
gert::git_add("DESCRIPTION")
gert::git_commit(msg)
gert::git_push("origin")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment