Skip to content

Commit

Permalink
vendor in src/
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Nov 9, 2023
1 parent 8e83408 commit e817b86
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 34 deletions.
72 changes: 48 additions & 24 deletions R/vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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)
}
14 changes: 7 additions & 7 deletions man/cpp_vendor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test-vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
})
})

0 comments on commit e817b86

Please sign in to comment.