Skip to content

Commit

Permalink
fix eventual CRAN warning
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed May 13, 2024
1 parent 8845015 commit 6778095
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ Config/Needs/cpp11/cpp_register:
vctrs
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
68 changes: 41 additions & 27 deletions R/vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#' **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.
#' @param dir The directoyy to vendor the code into.
#' @param subdir The subdirectory to vendor the code into.
#' @return The file path to the vendored code (invisibly).
#' @export
#' @examples
Expand All @@ -27,42 +28,53 @@
#'
#' # cleanup
#' unlink(dir, recursive = TRUE)
cpp_vendor <- function(path = NULL) {
if (is.null(path)) {
cpp_vendor <- function(dir = NULL, subdir = "/inst/include") {
if (is.null(dir)) {
stop("You must provide a path to vendor the code into", call. = FALSE)
} else {
path <- paste0(path, "/inst/include")
}

new <- file.path(path)

if (dir.exists(new)) {
stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE)
path <- paste0(dir, subdir)

path2 <- file.path(path, "cpp11")
if (dir.exists(path2)) {
stop("'", path2, "' already exists\n * run unlink('", path2, "', recursive = TRUE)", call. = FALSE)
}

# Vendor cpp11 ----

dir.create(new, recursive = TRUE, showWarnings = FALSE)
dir.create(file.path(new, "cpp11"), recursive = TRUE, showWarnings = FALSE)
dir.create(
path2,
recursive = TRUE,
showWarnings = FALSE
)

current <- system.file("include", "cpp11", package = "cpp11")
current_cpp11 <- system.file(
"include",
"cpp11",
package = "cpp11"
)

if (!nzchar(current)) {
if (!nzchar(current_cpp11)) {
stop("cpp11 is not installed", call. = FALSE)
}

cpp11_version <- utils::packageVersion("cpp11")

cpp11_header <- sprintf("// cpp11 version: %s\n// vendored on: %s", cpp11_version, Sys.Date())

main_header <- list.files(current, pattern = "\\.hpp$", full.names = TRUE)
headers <- list.files(file.path(current, "cpp11"), pattern = "\\.hpp$", full.names = TRUE)
cpp11_header <- sprintf(
"// cpp11 version: %s\n// vendored on: %s",
cpp11_version,
Sys.Date()
)

writeLines(c(cpp11_header, readLines(main_header)), file.path(new, basename(main_header)))
write_header(
path, "cpp11.hpp", "cpp11",
cpp11_header
)

for (h in headers) {
writeLines(c(cpp11_header, readLines(h)), file.path(new, "cpp11", basename(h)))
}
copy_files(
list.files(current_cpp11, full.names = TRUE),
path, "cpp11", cpp11_header
)

# Additional steps to make vendoring work ----

Expand All @@ -73,23 +85,25 @@ cpp_vendor <- function(path = NULL) {

message("DESCRIPTION should not have lines such as 'LinkingTo: cpp11'")

invisible(new)
invisible(path)
}

write_header <- function(path, header, pkg, cpp11_header) {
write_header <- function(path, header, pkg, cpp11armadillo_header) {
writeLines(
c(
cpp11_header,
readLines(system.file("include", header, package = pkg))
cpp11armadillo_header,
readLines(
system.file("include", header, package = pkg)
)
),
file.path(path, header)
)
}

copy_files <- function(files, path, out, cpp11_header) {
copy_files <- function(files, path, out, cpp11armadillo_header) {
for (f in files) {
writeLines(
c(cpp11_header, readLines(f)),
c(cpp11armadillo_header, readLines(f)),
file.path(path, out, basename(f))
)
}
Expand Down
9 changes: 4 additions & 5 deletions man/cpp_vendor.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/test-vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ describe("cpp_vendor", {
pkg <- local_package()
p <- pkg_path(pkg)

cpp_vendor(file.path(pkg_path(pkg), "inst/include"))
cpp_vendor(pkg_path(pkg))

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")))
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")))
})
})

0 comments on commit 6778095

Please sign in to comment.