Skip to content

Commit

Permalink
- renv2nix(): switch from RemoteType to RemoteHost in renv.lock f…
Browse files Browse the repository at this point in the history
…iles to detect packages to be added from Github or Gitlab
  • Loading branch information
b-rodrigues authored Nov 20, 2024
2 parents b3b1c42 + 78c6be8 commit 849abcd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 47 deletions.
52 changes: 26 additions & 26 deletions R/renv_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ read_renv_lock <- function(renv_lock_path = "renv.lock") {
#' build the packages from their external repositories.
#'
#' @param renv_lock_remote_pkgs the list of package information from an renv.lock file.
#' @param type the type of remote package, defaults to NULL meaning the RemoteType of the
#' @param host the host of remote package, defaults to NULL meaning the RemoteHost of the
#' renv entry will be used.
#' currently supported types: 'github' 'gitlab'
#' currently supported hosts: 'api.github.com' 'gitlab.com'
#' see [remotes](https://remotes.r-lib.org/) for more.
#'
#' @return a list of lists with three elements named:
Expand All @@ -40,45 +40,45 @@ read_renv_lock <- function(renv_lock_path = "renv.lock") {
#' renv_remote_pkgs(read_renv_lock()$Packages)
#' }
renv_remote_pkgs <- function(
renv_lock_remote_pkgs, type = NULL) {
renv_lock_remote_pkgs, host = NULL) {
# , "bitbucket", "git", "local", "svn", "url", "version", "cran", "bioc"
supported_pkg_types <- c("github", "gitlab")
if (!(is.null(type) || (type %in% supported_pkg_types))) {
stop("Unsupported remote type: ", type)
supported_pkg_hosts <- c("api.github.com", "gitlab.com")
if (!(is.null(host) || (host %in% supported_pkg_hosts))) {
stop("Unsupported remote host: ", host)
}
initial_type_state <- type
initial_host_state <- host
git_pkgs <- vector(mode = "list", length = length(renv_lock_remote_pkgs))
names(git_pkgs) <- names(renv_lock_remote_pkgs)
for (i in seq_along(renv_lock_remote_pkgs)) {
renv_lock_pkg_info <- renv_lock_remote_pkgs[[i]]
if (is.null(type)) {
if (is.null(renv_lock_pkg_info$RemoteType)) {
if (is.null(host)) {
if (is.null(renv_lock_pkg_info$RemoteHost)) {
stop(
"Not a package installed from a remote outside of the main package repositories\n",
"renv_remote_pkgs() only handles pkgs where remote type is specified"
"renv_remote_pkgs() only handles pkgs where RemoteHost is specified"
)
} else if (renv_lock_pkg_info$RemoteType %in% supported_pkg_types) {
type <- renv_lock_pkg_info$RemoteType
} else if (renv_lock_pkg_info$RemoteHost %in% supported_pkg_hosts) {
host <- renv_lock_pkg_info$RemoteHost
} else {
stop(
renv_lock_pkg_info$Package, " has unsupported remote type: ",
renv_lock_pkg_info$RemoteType, "\nSupported types are: ",
paste0(supported_pkg_types, collapse = ", ")
renv_lock_pkg_info$Package, " has unsupported remote host: ",
renv_lock_pkg_info$RemoteHost, "\nSupported hosts are: ",
paste0(supported_pkg_hosts, collapse = ", ")
)
}
} else {
if (type != renv_lock_pkg_info$RemoteType) {
if (host != renv_lock_pkg_info$RemoteHost) {
stop(
"Remote type (", renv_lock_pkg_info$RemoteType, ") of ", renv_lock_pkg_info$Package,
" does not match the provided type (", type, ")"
"Remote host (", renv_lock_pkg_info$RemoteHost, ") of ", renv_lock_pkg_info$Package,
" does not match the provided host (", host, ")"
)
}
}

pkg_info <- vector(mode = "list", length = 3)
names(pkg_info) <- c("package_name", "repo_url", "commit")
switch(type,
"github" = {
switch(host,
"api.github.com" = {
pkg_info[[1]] <- renv_lock_pkg_info$Package
pkg_info[[2]] <- paste0(
# RemoteHost is listed as api.github.com for some reason
Expand All @@ -87,7 +87,7 @@ renv_remote_pkgs <- function(
)
pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha
},
"gitlab" = {
"gitlab.com" = {
pkg_info[[1]] <- renv_lock_pkg_info$Package
pkg_info[[2]] <- paste0(
"https://", renv_lock_pkg_info$RemoteHost, "/",
Expand All @@ -97,7 +97,7 @@ renv_remote_pkgs <- function(
pkg_info[[3]] <- renv_lock_pkg_info$RemoteSha
}
)
type <- initial_type_state
host <- initial_host_state
git_pkgs[[i]] <- pkg_info
}
git_pkgs
Expand Down Expand Up @@ -146,19 +146,19 @@ renv2nix <- function(
for (i in seq_along(renv_lock$Packages)) {
if (renv_lock$Packages[[i]]$Source %in% c("Repository", "Bioconductor")) {
repo_pkgs[[renv_lock_pkg_names[i]]] <- renv_lock$Packages[[i]]
} else if (renv_lock$Packages[[i]]$RemoteType %in% c("github", "gitlab")) {
} else if (renv_lock$Packages[[i]]$RemoteHost %in% c("api.github.com", "gitlab.com")) {
remote_pkgs[[renv_lock_pkg_names[i]]] <- renv_lock$Packages[[i]]
} else {
# unsupported_pkgs[[renv_lock_pkg_names[i]]] <- renv_lock$Packages[[i]]
warning(
renv_lock$Packages[[i]]$Package, " has the unsupported remote type ",
renv_lock$Packages[[i]]$RemoteType, " and will not be included in the Nix expression.",
renv_lock$Packages[[i]]$Package, " has the unsupported remote host ",
renv_lock$Packages[[i]]$RemoteHost, " and will not be included in the Nix expression.",
"\n Consider manually specifying the git remote or a local package install."
)
}
}
git_pkgs <- NULL
# as local_r_pkgs expects an archive not sure how to set type here..
# as local_r_pkgs expects an archive not sure how to set host here..
# local_r_pkgs <- NULL
if (length(remote_pkgs) > 0) {
git_pkgs <- renv_remote_pkgs(remote_pkgs)
Expand Down
6 changes: 3 additions & 3 deletions man/renv_remote_pkgs.Rd

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

26 changes: 24 additions & 2 deletions tests/testthat/_snaps/renv_helpers/default_v0-17-3.nix
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,29 @@ let
yaml
zip;
};


git_archive_pkgs = [
(pkgs.rPackages.buildRPackage {
name = "emo";
src = pkgs.fetchgit {
url = "https://github.com/hadley/emo";
rev = "3f03b11491ce3d6fc5601e210927eff73bf8e350";
sha256 = "sha256-b9IlaJ6c0FlXKizvJU8SEv49mlp2de7Y0at5DK5yBVA=";
};
propagatedBuildInputs = builtins.attrValues {
inherit (pkgs.rPackages)
stringr
glue
crayon
magrittr
assertthat
lubridate
rlang
purrr;
};
})
];

system_packages = builtins.attrValues {
inherit (pkgs)
R
Expand All @@ -150,6 +172,6 @@ pkgs.mkShell {
LC_PAPER = "en_US.UTF-8";
LC_MEASUREMENT = "en_US.UTF-8";

buildInputs = [ rpkgs system_packages ];
buildInputs = [ git_archive_pkgs rpkgs system_packages ];

}
29 changes: 13 additions & 16 deletions tests/testthat/test-renv_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ testthat::test_that("testing renv_helpers", {
testthat::expect_error(
renv_remote_pkgs(synthetic_renv_lock_example$Packages[
c("githubpkg", "gitlabpkg", "unsupported")
], type = "unsupported"),
"Unsupported remote type:"
], host = "unsupported"),
"Unsupported remote host:"
)
testthat::expect_error(
renv_remote_pkgs(synthetic_renv_lock_example$Packages[
c("githubpkg", "gitlabpkg", "unsupported")
]),
"has unsupported remote type"
"has unsupported remote host"
)
testthat::expect_error(
renv_remote_pkgs(synthetic_renv_lock_example$Packages[
c("githubpkg", "gitlabpkg", "unsupported")
], type = "github"),
"does not match the provided type"
], host = "api.github.com"),
"does not match the provided host"
)
})

Expand All @@ -123,15 +123,15 @@ testthat::test_that("testing renv_helpers", {
{
call <- renv2nix(tmpf, return_rix_call = TRUE, message_type = "quiet")
},
"has the unsupported remote type"
"has the unsupported remote host"
)
testthat::expect_equal(call, test_call)

warns <- testthat::expect_warning(
{
call <- renv2nix(tmpf, return_rix_call = TRUE, message_type = "quiet", ide = "rstudio")
},
"has the unsupported remote type"
"has the unsupported remote host"
)
test_call$ide <- "rstudio"
testthat::expect_equal(call, test_call)
Expand All @@ -151,15 +151,12 @@ testthat::test_that("testing renv_helpers", {
path_env_nix <- tempdir()

save_renv2nix_test <- function(renv_lock_path, path_env_nix, output_nix_file, ...) {
# we will need to remove this suppressWarnings and deal with the emo package
suppressWarnings(
renv2nix(
renv_lock_path = renv_lock_path,
project_path = path_env_nix,
message_type = "quiet",
overwrite = TRUE,
...
)
renv2nix(
renv_lock_path = renv_lock_path,
project_path = path_env_nix,
message_type = "quiet",
overwrite = TRUE,
...
)

file.copy(
Expand Down

0 comments on commit 849abcd

Please sign in to comment.