From e817b86134e7d35d423c223577fae574c7f59d0e Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Wed, 8 Nov 2023 21:46:59 -0500 Subject: [PATCH] vendor in src/ --- R/vendor.R | 72 ++++++++++++++++++++++++------------ man/cpp_vendor.Rd | 14 +++---- tests/testthat/test-vendor.R | 6 +-- 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/R/vendor.R b/R/vendor.R index f7c4b150..09148b63 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -4,18 +4,18 @@ #' project is using. It is often used in the go language community. #' #' This function vendors cpp11 into your package by copying the cpp11 -#' headers into the `inst/include` folder of your package and adding -#' 'cpp11 version: XYZ' to the top of the files, where XYZ is the version of -#' cpp11 currently installed on your machine. +#' headers into the `src/vendor` folder and adding 'cpp11 version: XYZ' to the +#' top of the files, where XYZ is the version of cpp11 currently installed on +#' your machine. #' #' If you choose to vendor the headers you should _remove_ `LinkingTo: -#' cpp11` from your DESCRIPTION. +#' cpp11` from your DESCRIPTION. This is done automatically by this function. #' #' **Note**: vendoring places the responsibility of updating the code on #' **you**. Bugfixes and new features in cpp11 will not be available for your #' code until you run `cpp_vendor()` again. #' -#' @inheritParams cpp_register +#' @param path The path to vendor the code into. #' @return The file path to the vendored code (invisibly). #' @export #' @examples @@ -26,18 +26,18 @@ #' # vendor the cpp11 headers into the directory #' cpp_vendor(dir) #' -#' list.files(file.path(dir, "inst", "include", "cpp11")) +#' list.files(file.path(dir, "src", "vendor")) #' #' # cleanup #' unlink(dir, recursive = TRUE) -cpp_vendor <- function(path = ".") { - new <- file.path(path, "inst", "include", "cpp11") +cpp_vendor <- function(path = "./src/vendor") { + new <- file.path(path, "cpp11") if (dir.exists(new)) { stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) } - dir.create(new , recursive = TRUE, showWarnings = FALSE) + dir.create(new, recursive = TRUE, showWarnings = FALSE) current <- system.file("include", "cpp11", package = "cpp11") if (!nzchar(current)) { @@ -64,38 +64,62 @@ cpp_vendor <- function(path = ".") { # 1. Check if `src/Makevars` exists makevars_exists <- file.exists("src/Makevars") - # 2. If makevars exists, it should have a line that reads `PKG_CPPFLAGS = -I../inst/include` + # 2. If makevars exists, it should have a line that reads + # `PKG_CPPFLAGS = -I../inst/include` or similar + + vendor_line <- paste0(" -I", new) + if (isTRUE(makevars_exists)) { makevars <- readLines("src/Makevars") - if (!any(grepl("PKG_CPPFLAGS = -I../inst/include", makevars))) { - # add the line - makevars <- c(makevars, "PKG_CPPFLAGS = -I../inst/include") + + if (any(grepl("^PKG_CPPFLAGS", makevars))) { + cat("There is a `PKG_CPPFLAGS` line in src/Makevars. It will be modified.\n") + + # which line contains `PKG_CPPFLAGS`? + cppflags_line <- grep("^PKG_CPPFLAGS", makevars) + + # append the vendoring line + if (!grepl(vendor_line, makevars[cppflags_line])) { + makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) + } writeLines(makevars, "src/Makevars") + } else { + # add the line + makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) - # warn about the change - cat("`PKG_CPPFLAGS = -I../inst/include` was added to src/Makevars\n") + writeLines(makevars, "src/Makevars") } + + cat("The existing src/Makevars was modified. Please check it.\n") } else { # create the file - writeLines("PKG_CPPFLAGS = -I../inst/include", "src/Makevars") + writeLines(paste0("PKG_CPPFLAGS = ", vendor_line), "src/Makevars") # warn about the change - cat("A new src/Makevars file was created\n") + cat("A new src/Makevars file was created.\n") } # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11` or `LinkingTo: \n\tcpp11` description <- readLines("DESCRIPTION") - # remove the lines - description <- description[!grepl("LinkingTo: cpp11", description)] - description <- description[!grepl("LinkingTo: ", description)] - description <- description[!grepl(" cpp11", description)] + cpp11_in_desc <- any( + grepl("LinkingTo: cpp11", description), + grepl("LinkingTo: ", description), + grepl(" cpp11", description) + ) + + if (isTRUE(cpp11_in_desc)) { + # remove the lines + description <- description[!grepl("LinkingTo: cpp11", description)] + description <- description[!grepl("LinkingTo: ", description)] + description <- description[!grepl(" cpp11", description)] - writeLines(description, "DESCRIPTION") + writeLines(description, "DESCRIPTION") - # warn about the change - cat("`LinkingTo: cpp11` was removed from DESCRIPTION\n") + # warn about the change + cat("`LinkingTo: cpp11` was removed from DESCRIPTION.\n") + } invisible(new) } diff --git a/man/cpp_vendor.Rd b/man/cpp_vendor.Rd index 857e49cf..a02f36de 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 = ".") +cpp_vendor(path = "./src/vendor") } \arguments{ -\item{path}{The path to the package root directory} +\item{path}{The path to vendor the code into.} } \value{ The file path to the vendored code (invisibly). @@ -18,11 +18,11 @@ project is using. It is often used in the go language community. } \details{ This function vendors cpp11 into your package by copying the cpp11 -headers into the \code{inst/include} folder of your package and adding -'cpp11 version: XYZ' to the top of the files, where XYZ is the version of -cpp11 currently installed on your machine. +headers into the \code{src/vendor} folder and adding 'cpp11 version: XYZ' to the +top of the files, where XYZ is the version of cpp11 currently installed on +your machine. -If you choose to vendor the headers you should \emph{remove} \code{LinkingTo: cpp11} from your DESCRIPTION. +If you choose to vendor the headers you should \emph{remove} \code{LinkingTo: cpp11} from your DESCRIPTION. This is done automatically by this function. \strong{Note}: vendoring places the responsibility of updating the code on \strong{you}. Bugfixes and new features in cpp11 will not be available for your @@ -36,7 +36,7 @@ dir.create(dir) # vendor the cpp11 headers into the directory cpp_vendor(dir) -list.files(file.path(dir, "inst", "include", "cpp11")) +list.files(file.path(dir, "src", "vendor")) # cleanup unlink(dir, recursive = TRUE) diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index 361c9ad9..a667ce6b 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, "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_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"))) }) })