From 9a4d8af900addb48bbafa6a0764d6feedde0cfad Mon Sep 17 00:00:00 2001 From: George Stagg Date: Wed, 29 Nov 2023 11:07:04 +0000 Subject: [PATCH] Fix building packages from local source dir --- R/build.R | 63 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/R/build.R b/R/build.R index aaed8ef..4709cb7 100644 --- a/R/build.R +++ b/R/build.R @@ -54,38 +54,49 @@ build <- function(packages, } # Download a remote source to a source tarball on disk -make_remote_tarball <- function(pkg, url, target) { - tmp_dir <- tempfile() - on.exit(unlink(tmp_dir, recursive = TRUE)) - dir.create(tmp_dir) +make_remote_tarball <- function(pkg, src, target) { + is_local <- grepl("^file://", src) + target <- fs::path_abs(target) - source_tarball <- file.path(tmp_dir, "dest.tar.gz") - download.file(url, source_tarball) + parent_dir <- if (is_local && fs::is_dir(gsub("^file://", "", src))) { + # Use local source directory directly + fs::path(gsub("^file://", "", src), "..") + } else { + # Obtain a copy of the R source and extract to a temporary directory + tmp_dir <- tempfile() + on.exit(unlink(tmp_dir, recursive = TRUE)) + dir.create(tmp_dir) - # Recreate a new .tar.gz with the directory structure expected from - # a source package - result_code <- attr( - suppressWarnings(untar(source_tarball, list = TRUE)), - "status" - ) - if (is.null(result_code) || result_code == 0L) { - untar( - source_tarball, - exdir = file.path(tmp_dir, pkg), - extras = "--strip-components=1" + source_tarball <- file.path(tmp_dir, "dest.tar.gz") + download.file(src, source_tarball) + + result_code <- attr( + suppressWarnings(untar(source_tarball, list = TRUE)), + "status" ) - } else { - # Try to unzip if untar fails - # Get root folder name, necessary as it won't unzip as `pkg` - folder_name <- unzip(source_tarball, list = TRUE)$Name[[1]] - zip::unzip(source_tarball, exdir = file.path(tmp_dir)) - # rename folder_name to `pkg` - file.rename(file.path(tmp_dir, folder_name), file.path(tmp_dir, pkg)) + if (is.null(result_code) || result_code == 0L) { + untar( + source_tarball, + exdir = file.path(tmp_dir, pkg), + extras = "--strip-components=1" + ) + } else { + # Try to unzip if untar fails + # Get root folder name, necessary as it won't unzip as `pkg` + folder_name <- unzip(source_tarball, list = TRUE)$Name[[1]] + zip::unzip(source_tarball, exdir = file.path(tmp_dir)) + # rename folder_name to `pkg` + file.rename(file.path(tmp_dir, folder_name), file.path(tmp_dir, pkg)) + } + unlink(source_tarball) + + tmp_dir } - unlink(source_tarball) + # Recreate a new .tar.gz with the directory structure expected from + # a source package withr::with_dir( - tmp_dir, + parent_dir, tar(target, files = pkg, compression = "gzip") ) }