Skip to content

Commit

Permalink
Sketch record_versions()
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau-lilly committed Mar 5, 2024
1 parent 821f03b commit 31551bf
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: r.releases.utils
Title: Utilities for An R Universe of Package Releases
Description: Utilities for an R universe of package releases.
Version: 0.0.7.9000
Version: 0.0.8
License: MIT + file LICENSE
URL:
https://r-releases.github.io/r.releases.utils/,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export(assert_cran_url)
export(assert_package)
export(assert_release_exists)
export(build_universe)
export(record_versions)
export(review_pull_request)
export(review_pull_requests)
export(try_message)
importFrom(gh,gh)
importFrom(jsonlite,parse_json)
importFrom(jsonlite,read_json)
importFrom(jsonlite,write_json)
importFrom(nanonext,ncurl)
importFrom(nanonext,parse_url)
importFrom(nanonext,status_code)
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# r.releases.utils 0.0.7.9000 (development)
# r.releases.utils 0.0.8

* Use R-releases and not `r-releases` to refer to the project.
* Edit bot messages.
* Add `record_versions()`.

# r.releases.utils 0.0.7

Expand Down
2 changes: 1 addition & 1 deletion R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @name r.releases.utils-package
#' @family help
#' @importFrom gh gh
#' @importFrom jsonlite parse_json read_json
#' @importFrom jsonlite parse_json read_json write_json
#' @importFrom nanonext ncurl parse_url status_code
#' @importFrom pkgsearch cran_package
#' @importFrom vctrs vec_rbind
Expand Down
71 changes: 71 additions & 0 deletions R/record_versions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' @title Record the manifest of package versions.
#' @export
#' @keywords internal
#' @description Record the manifest of versions of packages
#' and their MD5 hashes.
#' @details As well as their versions and MD5 hashes, this function
#' records the highest version ever recorded and the MD5 of that
#' version. This information helps check if a package complies with
#' best practices for version numbers: the version number should increment
#' on each new release.
#' @return `NULL` (invisibly). Writes a package manifest as a JSON file.
#' @param manifest Character of length 1, file path to the JSON manifest.
#' @param repos Character string of package repositories to track.
record_versions <- function(
manifest = "versions.json",
repos = "https://r-releases.r-universe.dev"
) {
current <- get_versions(repos = repos)
if (!file.exists(manifest)) {
jsonlite::write_json(x = current, path = manifest, pretty = TRUE)
return(invisible())
}
previous <- read_previous(manifest = manifest)
new <- update_manifest(current = current, previous = previous)
jsonlite::write_json(x = new, path = manifest, pretty = TRUE)
}

get_versions <- function(repos) {
out <- available.packages(repos = "https://r-releases.r-universe.dev")
out <- as.data.frame(out)
out <- out[, c("Package", "Version", "MD5sum")]
colnames(out) <- c("package", "version_current", "md5_current")
rownames(out) <- NULL
out
}

read_previous <- function(manifest) {
out <- jsonlite::read_json(path = manifest)
out <- do.call(what = vctrs::vec_rbind, args = out)
for (field in colnames(out)) {
out[[field]] <- as.character(out[[field]])
}
if (is.null(out$version_highest)) {
out$version_highest <- out$version_current
}
if (is.null(out$md5_highest)) {
out$md5_highest <- out$md5_current
}
out$version_current <- NULL
out$md5_current <- NULL
out
}

update_manifest <- function(current, previous) {
new <- merge(x = current, y = previous, all = TRUE)
incremented <- apply(
X = new,
MARGIN = 1L,
FUN = function(row) {
print(.subset2(row, "version_current"))
print(.subset2(row, "version_highest"))
utils::compareVersion(
a = .subset2(row, "version_current"),
b = .subset2(row, "version_highest")
) > 0.5
}
)
new$version_highest[incremented] <- new$version_current[incremented]
new$md5_highest[incremented] <- new$md5_current[incremented]
new
}
31 changes: 31 additions & 0 deletions man/record_versions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31551bf

Please sign in to comment.