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..186bf696 --- /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 +#' cpp_unvendor(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/_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 diff --git a/man/cpp_unvendor.Rd b/man/cpp_unvendor.Rd new file mode 100644 index 00000000..bc104402 --- /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 +cpp_unvendor(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))) }) })