Skip to content

Commit

Permalink
Merge pull request #96 from schochastics/dir_checking
Browse files Browse the repository at this point in the history
package guesstimating wit renv::dependencies (#53, #55)
  • Loading branch information
chainsawriot authored Feb 26, 2023
2 parents 009a4dc + f7070a2 commit 17edbfc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Imports:
remotes,
utils,
httr,
vctrs
vctrs,
renv
Depends:
R (>= 3.5.0)
VignetteBuilder: knitr
77 changes: 48 additions & 29 deletions R/as_pkgrefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ as_pkgrefs.default <- function(x, ...) {
#' @export
as_pkgrefs.character <- function(x, bioc_version = NULL, ...) {
if(.detect_renv_lockfile(x)){
return(.extract_pkgrefs_renv_lockfile(path = x))
return(.extract_pkgrefs_renv_lockfile(path = x))
}
if(.is_directory(x)){
return(.extract_pkgrefs_dir(x,bioc_version))
}
return(.normalize_pkgs(pkgs = x, bioc_version = bioc_version))
}
Expand All @@ -48,23 +51,23 @@ as_pkgrefs.sessionInfo <- function(x, ...) {
}

.extract_pkgrefs_renv_lockfile <- function(path){
lockfile <- .parse_renv_lockfile(path)
sources <- vapply(lockfile[["Packages"]],`[[`,character(1),"Source",USE.NAMES = FALSE)
pkgs <- c()
if("Repository"%in%sources){
pkgs <- c(pkgs, paste0("cran::",vapply(lockfile[["Packages"]][sources=="Repository"],`[[`,character(1),"Package",USE.NAMES = FALSE)))
}
if("Bioconductor"%in%sources){
pkgs <- c(pkgs,paste0("bioc::",vapply(lockfile[["Packages"]][sources=="Bioconductor"],`[[`,character(1),"Package",USE.NAMES = FALSE)))
}
if("GitHub"%in%sources){
pkgs <- c(pkgs,
paste0("github::",
vapply(lockfile[["Packages"]][sources=="GitHub"],`[[`,character(1), "RemoteUsername", USE.NAMES = FALSE),"/",
vapply(lockfile[["Packages"]][sources=="GitHub"],`[[`,character(1), "Package", USE.NAMES = FALSE))
)
}
return(pkgs)
lockfile <- .parse_renv_lockfile(path)
sources <- vapply(lockfile[["Packages"]],`[[`,character(1),"Source",USE.NAMES = FALSE)
pkgs <- c()
if("Repository"%in%sources){
pkgs <- c(pkgs, paste0("cran::",vapply(lockfile[["Packages"]][sources=="Repository"],`[[`,character(1),"Package",USE.NAMES = FALSE)))
}
if("Bioconductor"%in%sources){
pkgs <- c(pkgs,paste0("bioc::",vapply(lockfile[["Packages"]][sources=="Bioconductor"],`[[`,character(1),"Package",USE.NAMES = FALSE)))
}
if("GitHub"%in%sources){
pkgs <- c(pkgs,
paste0("github::",
vapply(lockfile[["Packages"]][sources=="GitHub"],`[[`,character(1), "RemoteUsername", USE.NAMES = FALSE),"/",
vapply(lockfile[["Packages"]][sources=="GitHub"],`[[`,character(1), "Package", USE.NAMES = FALSE))
)
}
return(pkgs)
}

.extract_pkgref_packageDescription <- function(packageDescription) {
Expand All @@ -87,21 +90,37 @@ as_pkgrefs.sessionInfo <- function(x, ...) {
}

.detect_renv_lockfile <- function(path){
# assuming all renv lockfiles are called renv.lock and path is only length 1
if(length(path)!=1){
return(FALSE)
}
if(isFALSE(file.exists(path))){
return(FALSE)
}
if (isFALSE(basename(path) == "renv.lock")) {
return(FALSE)
}
TRUE
# assuming all renv lockfiles are called renv.lock and path is only length 1
if(length(path)!=1){
return(FALSE)
}
if(isFALSE(file.exists(path))){
return(FALSE)
}
if (isFALSE(basename(path) == "renv.lock")) {
return(FALSE)
}
TRUE
}

.parse_renv_lockfile <- function(path){
lockfile <- jsonlite::fromJSON(path, simplifyVector = FALSE)
# class(lockfile) <- "renv_lockfile"
lockfile
}

.is_directory <- function(path){
if(length(path)!=1){
return(FALSE)
}
if(isFALSE(dir.exists(path))){
return(FALSE)
}
TRUE
}

.extract_pkgrefs_dir <- function(path, bioc_version = NULL){
pkgs <- suppressMessages(unique(renv::dependencies(path,progress = FALSE)$Package))
warning("scanning directories for R packages cannot detect github packages.",call. = FALSE)
return(.normalize_pkgs(pkgs = pkgs, bioc_version = bioc_version))
}
2 changes: 2 additions & 0 deletions tests/testdata/test_dir/script.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
library(BiocGenerics)
library(rtoot)
12 changes: 12 additions & 0 deletions tests/testthat/test_pkgref.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,15 @@ test_that(".detect_renv_lockfile false",{
expect_false(.detect_renv_lockfile(c("../testdata/graph.RDS", "../testdata/renv.lock")))
expect_false(.detect_renv_lockfile("../testdata/fake_renv.lock"))
})

test_that(".is_directory flse",{
expect_false(.is_directory(c("a/","b/")))
expect_false(.is_directory("a/"))
})

test_that("as_pkgrefs directory", {
skip_if_offline()
skip_on_cran()
res <- suppressWarnings(as_pkgrefs("../testdata/test_dir",bioc_version = "3.16"))
expect_equal(res, c("bioc::BiocGenerics", "cran::rtoot"))
})

0 comments on commit 17edbfc

Please sign in to comment.