Skip to content

Commit

Permalink
[WIP] new function 'addPackageGitHub()' (#50)
Browse files Browse the repository at this point in the history
implemented a version of @msteijaert's function, though note the following TODOs need to make it fully functional:

* needs to handle `HEAD` instead of `master` (to allow for `main` branch)
* allow arbitrary git reference, à la `devtools::install_github()`
* allow installation from github repo that keeps pkg in a subdir
* use `GITHUB_PAT` to allow downloading from private repos
* `addLocalPackage()` does not add the dependencies of the locally built package!
  • Loading branch information
achubaty committed Jul 16, 2021
1 parent 482b84d commit acdf645
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ importFrom(utils,download.packages)
importFrom(utils,flush.console)
importFrom(utils,installed.packages)
importFrom(utils,select.list)
importFrom(utils,unzip)
37 changes: 37 additions & 0 deletions R/addPackages.R
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,40 @@ addLocalPackage <- function(pkgs = NULL, pkgPath = NULL, path = NULL,
}
invisible(ret)
}

#' Add package from GitHub
#'
#' @param repo Github repository name in the format `username/repo`.
#' @param ... Additional arguments passed to `addLocalPackage()`.
#'
#' @export.
#' @docType methods
#'
#' @author Marvin Steijaert
#' @author Alex Chubaty
#'
#' @note Currently, only installing from master branch is supported.
#'
#' @importFrom utils unzip
#'
#' @examples
#' \dontrun{
#' addPackageGitHub("andrie/miniCRAN", path = reposPath)
#' }
addPackageGitHub <- function(repo, ...) {
packageName <- basename(repo)
exDir <- file.path(tempdir(), packageName)
if (dir.exists(exDir)) unlink(exDir, recursive = TRUE)
zipFile <- file.path(tempdir(), paste0(packageName, ".zip"))

## TODO: needs to handle HEAD instead of 'master' (to allow for 'main' branch)
## TODO: allow arbitrary git reference, à la devtools::install_github()
## TODO: allow installation from github repo that keeps pkg in a subdir
## TODO: use GITHUB_PAT to allow downloading from private repos
download.file(paste0("https://github.com/", repo, "/zipball/master"), zipFile, mode = "wb")
unzip(zipFile, exdir = exDir)
file.rename(list.files(exDir, full.names = TRUE)[1], file.path(exDir, packageName))

## TODO: addLocalPackage() does not add the dependencies of the locally built package!
addLocalPackage(packageName, exDir, ..., build = TRUE)
}
30 changes: 30 additions & 0 deletions man/addPackageGitHub.Rd

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

23 changes: 22 additions & 1 deletion tests/testthat/test-7-addGithubPackage.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

test_that("can add package from github",{
test_that("can add package listing from github",{

skip_on_cran()

Expand All @@ -15,3 +15,24 @@ test_that("can add package from github",{
expect_equal(nrow(pdb), nrow(newpdb))
expect_equal(ncol(pdb), ncol(newpdb))
})

repo_path <- file.path(tempdir(), "cran")

test_that("can add github package", {

skip_on_cran()

# Create a miniCRAN repo
unlink(repo_path, recursive = TRUE)
dir.create(repo_path, showWarnings = FALSE)
repo <- "https://cran.r-project.org"
miniCRAN::makeRepo(pkgs = c(), path = repo_path, type = "win.binary", repos = repo)

addPackageGitHub("andrie/miniCRAN", path = repo_path)

expect_message(
addPackageGitHub("andrie/miniCRAN", path = repo_path),
"All packages up to date. Nothing to add."
)

})

0 comments on commit acdf645

Please sign in to comment.