From 2729233dc47a76f9397401a0929866b7aac402e0 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Thu, 5 Dec 2024 23:37:37 -0500 Subject: [PATCH 1/3] add and test unvendor function --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/unvendor.R | 39 ++++++++++++++++++++++++++++++++++++ R/vendor.R | 3 ++- man/cpp_unvendor.Rd | 34 +++++++++++++++++++++++++++++++ tests/testthat/test-vendor.R | 12 ++--------- 6 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 R/unvendor.R create mode 100644 man/cpp_unvendor.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 7a418560..46e2c18a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: cpp11 Title: A C++11 Interface for R's C Interface -Version: 0.5.1.9000 +Version: 0.5.1.9001 Authors@R: c( person("Davis", "Vaughan", email = "davis@posit.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4777-038X")), diff --git a/NAMESPACE b/NAMESPACE index 876655dc..d9bcf668 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,4 +4,5 @@ export(cpp_eval) export(cpp_function) export(cpp_register) export(cpp_source) +export(cpp_unvendor) export(cpp_vendor) diff --git a/R/unvendor.R b/R/unvendor.R new file mode 100644 index 00000000..b4503fc4 --- /dev/null +++ b/R/unvendor.R @@ -0,0 +1,39 @@ +#' Unvendor the cpp11 dependency +#' +#' This function removes the vendored cpp11 headers from your package and +#' restores the `LinkingTo: cpp11` field in the DESCRIPTION file if it was removed. +#' +#' @inheritParams cpp_register +#' @return The file path to the unvendored code (invisibly). +#' @export +#' @examples +#' # create a new directory +#' dir <- tempfile() +#' dir.create(dir) +#' +#' # vendor the cpp11 headers into the directory +#' cpp_vendor(dir) +#' +#' # unvendor the cpp11 headers from the directory +#' unvendor_cpp11(dir) +#' +#' list.files(file.path(dir, "inst", "include", "cpp11")) +#' +#' # cleanup +#' unlink(dir, recursive = TRUE) +cpp_unvendor <- function(path = ".") { + new <- file.path(path, "inst", "include", "cpp11") + + if (!dir.exists(new)) { + stop("'", new, "' does not exist", call. = FALSE) + } + + unlink(new, recursive = TRUE) + + cpp11_hpp <- file.path(dirname(new), "cpp11.hpp") + if (file.exists(cpp11_hpp)) { + unlink(cpp11_hpp) + } + + invisible(new) +} diff --git a/R/vendor.R b/R/vendor.R index 5fe10fe2..494e8726 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -34,7 +34,8 @@ cpp_vendor <- function(path = ".") { new <- file.path(path, "inst", "include", "cpp11") if (dir.exists(new)) { - stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) + message("'", new, "' already exists, removing it") + cpp_unvendor(path) } dir.create(new , recursive = TRUE, showWarnings = FALSE) diff --git a/man/cpp_unvendor.Rd b/man/cpp_unvendor.Rd new file mode 100644 index 00000000..d3f034f1 --- /dev/null +++ b/man/cpp_unvendor.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/unvendor.R +\name{cpp_unvendor} +\alias{cpp_unvendor} +\title{Unvendor the cpp11 dependency} +\usage{ +cpp_unvendor(path = ".") +} +\arguments{ +\item{path}{The path to the package root directory} +} +\value{ +The file path to the unvendored code (invisibly). +} +\description{ +This function removes the vendored cpp11 headers from your package and +restores the \code{LinkingTo: cpp11} field in the DESCRIPTION file if it was removed. +} +\examples{ +# create a new directory +dir <- tempfile() +dir.create(dir) + +# vendor the cpp11 headers into the directory +cpp_vendor(dir) + +# unvendor the cpp11 headers from the directory +unvendor_cpp11(dir) + +list.files(file.path(dir, "inst", "include", "cpp11")) + +# cleanup +unlink(dir, recursive = TRUE) +} diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index 361c9ad9..c8ea2b05 100644 --- a/tests/testthat/test-vendor.R +++ b/tests/testthat/test-vendor.R @@ -8,16 +8,6 @@ describe("cpp_vendor", { ) }) - it("errors if cpp11 is already vendored", { - pkg <- local_package() - cpp_vendor(pkg_path(pkg)) - - expect_error( - cpp_vendor(pkg_path(pkg)), - "already exists" - ) - }) - it("vendors cpp11", { pkg <- local_package() p <- pkg_path(pkg) @@ -27,5 +17,7 @@ describe("cpp_vendor", { expect_true(dir.exists(file.path(p, "inst", "include", "cpp11"))) expect_true(file.exists(file.path(p, "inst", "include", "cpp11.hpp"))) expect_true(file.exists(file.path(p, "inst", "include", "cpp11", "declarations.hpp"))) + + expect_silent(cpp_unvendor(pkg_path(pkg))) }) }) From f717e3d16892da0b12b0c304be95fd7273cf8c8e Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Wed, 1 Jan 2025 06:10:13 -0500 Subject: [PATCH 2/3] fix unvendor example --- R/unvendor.R | 2 +- man/cpp_unvendor.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/unvendor.R b/R/unvendor.R index b4503fc4..186bf696 100644 --- a/R/unvendor.R +++ b/R/unvendor.R @@ -15,7 +15,7 @@ #' cpp_vendor(dir) #' #' # unvendor the cpp11 headers from the directory -#' unvendor_cpp11(dir) +#' cpp_unvendor(dir) #' #' list.files(file.path(dir, "inst", "include", "cpp11")) #' diff --git a/man/cpp_unvendor.Rd b/man/cpp_unvendor.Rd index d3f034f1..bc104402 100644 --- a/man/cpp_unvendor.Rd +++ b/man/cpp_unvendor.Rd @@ -25,7 +25,7 @@ dir.create(dir) cpp_vendor(dir) # unvendor the cpp11 headers from the directory -unvendor_cpp11(dir) +cpp_unvendor(dir) list.files(file.path(dir, "inst", "include", "cpp11")) From 6dbf339df5ca22f992742757738314cf5d8f7d5a Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Wed, 1 Jan 2025 07:28:36 -0500 Subject: [PATCH 3/3] fix pkgdown error --- _pkgdown.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/_pkgdown.yml b/_pkgdown.yml index 06dec1ab..f38f387b 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -25,6 +25,7 @@ reference: - title: Vendoring cpp11 contents: - cpp_vendor + - cpp_unvendor navbar: type: default