Skip to content

Commit

Permalink
Merge pull request #53 from insightsengineering/fix_max_deps
Browse files Browse the repository at this point in the history
include CRAN refs in get_ref_max()
  • Loading branch information
pawelru authored Aug 13, 2024
2 parents 5801a1e + 0743c1d commit da8f535
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 21 deletions.
75 changes: 54 additions & 21 deletions R/get_ref.R
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,12 @@ get_desc_from_gh <- function(org, repo, ref = "") {
#' @inherit get_ref_min return
#'
#' @export
#'
#' @examplesIf Sys.getenv("R_USER_CACHE_DIR", "") != ""
#' get_ref_max(pkgdepends::parse_pkg_ref("dplyr"))
#' get_ref_max(pkgdepends::parse_pkg_ref("tidyverse/dplyr"))
get_ref_max <- function(remote_ref) {
remote_ref
get_ref_internal(remote_ref, include_release = FALSE)
}

#' Get reference to the release version of the package.
Expand All @@ -289,16 +293,42 @@ get_ref_max <- function(remote_ref) {
#' get_ref_release(pkgdepends::parse_pkg_ref("dplyr"))
#' get_ref_release(pkgdepends::parse_pkg_ref("tidyverse/dplyr"))
get_ref_release <- function(remote_ref) {
get_ref_internal(remote_ref)
}

#' Get reference of the maximal version of the package.
#'
#' @inheritParams get_ref_min
#' @param include_input (`logical(1)`) if `TRUE` then include input reference in search
#' @param include_cran (`logical(1)`) if `TRUE` then include CRAN reference in search
#' @param include_release (`logical(1)`) if `TRUE` then also check for released version.
#' Supported only for GitHub type of reference.
#'
#' @inherit get_ref_min return
#' @keywords internal
get_ref_internal <- function(
remote_ref,
include_input = TRUE,
include_cran = TRUE,
include_release = TRUE) {
# create list of ref candidates to check
# return the one of the highest version
# this is a named list of character with version values and refs names
ref_candidates <- list()
if (check_if_on_cran(remote_ref)) {

if (include_input) {
input_ref <- remote_ref$ref
input_ver <- get_version(remote_ref)
ref_candidates <- c(ref_candidates, setNames(list(input_ver), input_ref))
}

if (include_cran && check_if_on_cran(remote_ref)) {
cran_ref <- remote_ref$package
cran_ver <- get_version(pkgdepends::parse_pkg_ref(cran_ref))
ref_candidates <- c(ref_candidates, setNames(list(cran_ver), cran_ref))
}
if (inherits(remote_ref, "remote_ref_github")) {

if (include_release && inherits(remote_ref, "remote_ref_github")) {
gh_release_ref <- cond_parse_pkg_ref_release(remote_ref)
if (!is.null(gh_release_ref)) {
gh_release_ver <- get_version(gh_release_ref)
Expand All @@ -313,27 +343,26 @@ get_ref_release <- function(remote_ref) {
gh_ver <- get_version(gh_ref)
ref_candidates <- c(ref_candidates, setNames(list(gh_ver), gh_ref$ref))
}
} else {
input_ref <- remote_ref$ref
input_ver <- get_version(remote_ref)
ref_candidates <- c(ref_candidates, setNames(list(input_ver), input_ref))
}

if (length(ref_candidates) == 0 || all(is.na(ref_candidates))) {
return(remote_ref)
} else {
max_ver <- ref_candidates[[1]]
max_ref <- names(ref_candidates[1])
for (i in seq_along(ref_candidates)) {
i_ver <- ref_candidates[[i]]
i_ref <- names(ref_candidates[i])
if (!is.na(i_ver) && i_ver > max_ver) {
max_ref <- i_ref
max_ver <- i_ver
}
ref_candidates <- Filter(Negate(is.na), ref_candidates)

if (length(ref_candidates) == 0) {
return(NA)
}

max_ver <- ref_candidates[[1]]
max_ref <- names(ref_candidates[1])
for (i in seq_along(ref_candidates)) {
i_ver <- ref_candidates[[i]]
i_ref <- names(ref_candidates[i])
if (!is.na(i_ver) && i_ver > max_ver) {
max_ref <- i_ref
max_ver <- i_ver
}
return(pkgdepends::parse_pkg_ref(max_ref))
}

return(pkgdepends::parse_pkg_ref(max_ref))
}

#' @importFrom pkgdepends parse_pkg_ref
Expand Down Expand Up @@ -443,6 +472,10 @@ get_release_date.remote_ref_github <- function(remote_ref) {
get_release_date.remote_ref_cran <- function(remote_ref) {
rel_data <- get_release_data(remote_ref$package)

if (nrow(rel_data) == 0) {
return(as.Date(NA))
}

if (remote_ref$atleast != "") {
idx <- do.call(
remote_ref$atleast,
Expand Down Expand Up @@ -496,7 +529,7 @@ get_release_data <- function(package) {
)

# handle missing dates
if (nrow(cran_current > 0)) {
if (nrow(cran_current) > 0) {
if (is.na(cran_current$published)) {
if (cran_current$type == "cran") {
# in general, this should not happen for cran - https://github.com/r-lib/pkgcache/issues/109
Expand Down
30 changes: 30 additions & 0 deletions man/get_ref_internal.Rd

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

6 changes: 6 additions & 0 deletions man/get_ref_max.Rd

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

0 comments on commit da8f535

Please sign in to comment.