From 11051ed729bfcfbb35fd944af84c8fe7e75aac7b Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Wed, 31 Jan 2024 09:58:23 -0500 Subject: [PATCH] pass tests --- DESCRIPTION | 2 +- R/vendor.R | 109 +++----------------------- man/cpp_vendor.Rd | 8 +- tests/testthat/_snaps/register.md | 2 +- tests/testthat/_snaps/register.new.md | 9 --- tests/testthat/test-vendor.R | 6 +- 6 files changed, 21 insertions(+), 115 deletions(-) delete mode 100644 tests/testthat/_snaps/register.new.md diff --git a/DESCRIPTION b/DESCRIPTION index d668e342..bea26372 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -55,4 +55,4 @@ Config/Needs/cpp11/cpp_register: vctrs Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 diff --git a/R/vendor.R b/R/vendor.R index 896cc037..07cda1e3 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -15,32 +15,30 @@ #' **you**. Bugfixes and new features in cpp11 will not be available for your #' code until you run `cpp_vendor()` again. #' -#' @param path The path to vendor the code into. The default is `src/vendor/`. +#' @param path The path to vendor the code into. The default is `./inst/include/`. #' @return The file path to the vendored code (invisibly). #' @export #' @examples #' # create a new directory -#' dir <- tempfile() +#' dir <- tempdir() #' dir.create(dir) #' #' # vendor the cpp11 headers into the directory #' cpp_vendor(dir) #' -#' list.files(file.path(dir, "src", "vendor")) -#' #' # cleanup #' unlink(dir, recursive = TRUE) -cpp_vendor <- function(path = "./src/vendor") { - if (dir.exists(path)) { - stop("'", path, "' already exists\n * run unlink('", path, "', recursive = TRUE)", call. = FALSE) - } +cpp_vendor <- function(path = "./inst/include") { + new <- file.path(path, "cpp11") - dir.create(path, recursive = TRUE, showWarnings = FALSE) + if (dir.exists(new)) { + stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) + } # Vendor cpp11 ---- dir.create( - file.path(path, "cpp11"), + new, recursive = TRUE, showWarnings = FALSE ) @@ -75,97 +73,16 @@ cpp_vendor <- function(path = "./src/vendor") { # Additional steps to make vendoring work ---- - # 1. Check if `src/Makevars` exists - makevars_exists <- file.exists("src/Makevars") - makevars.win_exists <- file.exists("src/Makevars.win") - - # 2. If makevars exists, it should have a line that reads - # `PKG_CPPFLAGS = -I../inst/include` or similar - - vendor_line <- " -I vendor/" - - makevars_file <- "src/Makevars" - if (isTRUE(makevars_exists)) { - makevars <- readLines(makevars_file) - alter_makevars(makevars, makevars_file, vendor_line) - } else { - create_makevars(makevars_file, vendor_line) - } - - makevars.win_file <- "src/Makevars.win" - if (isTRUE(makevars.win_exists)) { - makevars.win <- readLines(makevars.win_file) - alter_makevars(makevars.win, makevars.win_file, vendor_line) - } else { - create_makevars(makevars.win_file, vendor_line) - } - - # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11armadillo` or - # `LinkingTo: \n\tcpp11armadillo` - description_file <- "DESCRIPTION" - description <- readLines(description_file) - - cpp11armadillo_in_desc <- any( - grepl("LinkingTo: cpp11, cpp11armadillo", description), - grepl("LinkingTo: ", description), - grepl(" cpp11,", description), - grepl(" cpp11armadillo", description) - ) - - if (isTRUE(cpp11armadillo_in_desc)) { - # remove the lines - description <- description[!grepl( - "LinkingTo: cpp11, cpp11armadillo", - description - )] - description <- description[!grepl("LinkingTo: ", description)] - description <- description[!grepl(" cpp11,", description)] - description <- description[!grepl(" cpp11armadillo", description)] + message(paste( + "Makevars and/or Makevars.win should have a line such as", + "'PKG_CPPFLAGS = -I../inst/include'" + )) - writeLines(description, description_file) - - # warn about the change - cat("`LinkingTo: cpp11, cpp11armadillo` was removed from DESCRIPTION.\n") - } + message("DESCRIPTION should not have lines such as 'LinkingTo: cpp11'") invisible(path) } -alter_makevars <- function(makevars, makevars_file, vendor_line) { - if (any(grepl("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars))) { - # which line contains `PKG_CPPFLAGS`? - cppflags_line <- grep("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars) - - if (length(cppflags_line) > 1) { - if (any(grepl(vendor_line, makevars[cppflags_line]))) { - return(TRUE) - } - } - - # append the vendoring line - if (!grepl(vendor_line, makevars[cppflags_line])) { - makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) - } - - writeLines(makevars, makevars_file) - - cat(paste0(makevars_file, "was modified.\n")) - } else { - # add the line - makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) - - writeLines(makevars, makevars_file) - } -} - -create_makevars <- function(filename, vendor_line) { - # create the file - writeLines(paste0("PKG_CPPFLAGS = ", vendor_line), filename) - - # warn about the change - cat("A new src/Makevars file was created.\n") -} - write_header <- function(path, header, pkg, cpp11_header) { writeLines( c( diff --git a/man/cpp_vendor.Rd b/man/cpp_vendor.Rd index a02f36de..1e9253fc 100644 --- a/man/cpp_vendor.Rd +++ b/man/cpp_vendor.Rd @@ -4,10 +4,10 @@ \alias{cpp_vendor} \title{Vendor the cpp11 dependency} \usage{ -cpp_vendor(path = "./src/vendor") +cpp_vendor(path = "./inst/include") } \arguments{ -\item{path}{The path to vendor the code into.} +\item{path}{The path to vendor the code into. The default is \verb{./inst/include/}.} } \value{ The file path to the vendored code (invisibly). @@ -30,14 +30,12 @@ code until you run \code{cpp_vendor()} again. } \examples{ # create a new directory -dir <- tempfile() +dir <- tempdir() dir.create(dir) # vendor the cpp11 headers into the directory cpp_vendor(dir) -list.files(file.path(dir, "src", "vendor")) - # cleanup unlink(dir, recursive = TRUE) } diff --git a/tests/testthat/_snaps/register.md b/tests/testthat/_snaps/register.md index 0e4b143a..75125c45 100644 --- a/tests/testthat/_snaps/register.md +++ b/tests/testthat/_snaps/register.md @@ -2,7 +2,7 @@ Code cpp_register(p, quiet = FALSE) - Message + Message i 1 functions decorated with [[cpp11::register]] v generated file 'cpp11.R' v generated file 'cpp11.cpp' diff --git a/tests/testthat/_snaps/register.new.md b/tests/testthat/_snaps/register.new.md deleted file mode 100644 index 75125c45..00000000 --- a/tests/testthat/_snaps/register.new.md +++ /dev/null @@ -1,9 +0,0 @@ -# cpp_register: can be run with messages - - Code - cpp_register(p, quiet = FALSE) - Message - i 1 functions decorated with [[cpp11::register]] - v generated file 'cpp11.R' - v generated file 'cpp11.cpp' - diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index a667ce6b..3cf21f8f 100644 --- a/tests/testthat/test-vendor.R +++ b/tests/testthat/test-vendor.R @@ -24,8 +24,8 @@ describe("cpp_vendor", { cpp_vendor(pkg_path(pkg)) - expect_true(dir.exists(file.path(p, "src", "vendor", "cpp11"))) - expect_true(file.exists(file.path(p, "src", "vendor", "cpp11.hpp"))) - expect_true(file.exists(file.path(p, "src", "vendor", "cpp11", "declarations.hpp"))) + expect_true(dir.exists(file.path(p, "cpp11"))) + expect_true(file.exists(file.path(p, "cpp11.hpp"))) + expect_true(file.exists(file.path(p, "cpp11", "declarations.hpp"))) }) })