Skip to content

Commit

Permalink
feat: Enable package installation from GitHub, Bioconductor and local…
Browse files Browse the repository at this point in the history
… directory.
  • Loading branch information
jakubnowicki committed Nov 8, 2023
1 parent b032a3c commit d0f43ef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
22 changes: 20 additions & 2 deletions R/dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ write_dependencies <- function(deps) {
writeLines(deps, "dependencies.R")
}

extract_package_name <- function(package) {
if (grepl("@", package)) package <- strsplit(package, "@")[[1]][1]
if (grepl("bioc::", package)) return(strsplit(package, "::")[[1]][2])

if (grepl("/", package)) {
package_splited <- strsplit(package, "/")[[1]]
return(package_splited[length(package_splited)])
}

package
}

extract_packages_names <- function(packages) {
purrr::map_chr(packages, extract_package_name)
}

# nolint start: line_length_linter
#' Manage dependencies
#'
Expand Down Expand Up @@ -58,7 +74,8 @@ NULL
pkg_install <- function(packages) {
stopifnot(is.character(packages))
renv::install(packages)
write_dependencies(c(packages, read_dependencies()))
packages_names <- extract_packages_names(packages)
write_dependencies(c(packages_names, read_dependencies()))
renv::snapshot()
invisible()
}
Expand All @@ -68,7 +85,8 @@ pkg_install <- function(packages) {
pkg_remove <- function(packages) {
stopifnot(is.character(packages))
renv::remove(packages)
write_dependencies(setdiff(read_dependencies(), packages))
packages_names <- extract_packages_names(packages)
write_dependencies(setdiff(read_dependencies(), packages_names))
renv::snapshot()
invisible()
}
28 changes: 28 additions & 0 deletions tests/testthat/test-dependencies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
describe("extract_package_name", {
it("returns the package name intact when using only the package name", {
expect_equal(extract_package_name("shiny"), "shiny")
})

it("returns the package name intact when using the package name and version", {
expect_equal(extract_package_name("shiny@1.6.0"), "shiny")
})

it("returns the package name when installing a package from GitHub", {
expect_equal(extract_package_name("r-lib/httr"), "httr")
expect_equal(extract_package_name("r-lib/testthat@c67018fa4970"), "testthat")
})

it("returns the package name when installing a package from a local path", {
expect_equal(extract_package_name("~/path/to/package"), "package")
})

it("returns the package name when installing a package from Bioconductor", {
expect_equal(extract_package_name("bioc::Biobase"), "Biobase")
})
})

describe("extract_packages_names", {
it("returns a vector of package names when installing multiple packages", {
expect_equal(extract_packages_names(c("shiny", "dplyr")), c("shiny", "dplyr"))
})
})

0 comments on commit d0f43ef

Please sign in to comment.