Skip to content

Commit

Permalink
Drop rprojroot dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborcsardi committed Nov 20, 2023
1 parent ce0fc34 commit fd19ffb
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Imports:
cli (>= 3.4.0),
desc,
processx,
R6,
rprojroot
R6
Suggests:
covr,
cpp11,
Expand Down
29 changes: 29 additions & 0 deletions R/find-package-root.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
find_package_root <- function(path = ".") {
is_root <- function(path) {
identical(
normalizePath(path, winslash = "/"),
normalizePath(dirname(path), winslash = "/")
)
}

if (!file.exists(path)) {
stop("Path does not exist: ", path)
}
cur_path <- path
errmsg <- paste0(
"Could not find R package in `",
path,
"` or its parent directories."
)
max_depth <- 100
for (i in 1:max_depth) {
if (file.exists(file.path(cur_path, "DESCRIPTION"))) {
return(cur_path)
} else if (is_root(cur_path)) {
stop(errmsg)
} else {
cur_path <- dirname(cur_path)
}
}
stop(errmsg, " Checked ", max_depth, " parent directories.") # nocov
}
60 changes: 44 additions & 16 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dir.exists <- function(x) {
}

pkg_path <- function(path = ".") {
rprojroot::find_root("DESCRIPTION", path)
find_package_root(path)
}

pkg_name <- function(path = ".") {
Expand Down Expand Up @@ -85,9 +85,15 @@ flag_false_values <- c("false", "no", "off", "0")

interpret_envvar_flag <- function(name, default = "false") {
env <- tolower(Sys.getenv(name, default))
if (env %in% flag_true_values) return(TRUE)
if (env %in% flag_false_values) return(FALSE)
if (is.na(env)) return(NA)
if (env %in% flag_true_values) {
return(TRUE)
}
if (env %in% flag_false_values) {
return(FALSE)
}
if (is.na(env)) {
return(NA)
}

stop(cli::format_error(
"The {.envvar {name}} environment variable must be {.code true} or
Expand Down Expand Up @@ -116,15 +122,21 @@ should_stop_for_warnings <- function() {
get_config_flag_value("stop_for_warnings")
}

isFALSE <- function (x) {
isFALSE <- function(x) {
is.logical(x) && length(x) == 1L && !is.na(x) && !x
}

should_add_compiler_flags <- function() {
val <- getOption("pkg.build_extra_flags", NULL)
if (isTRUE(val)) return(TRUE)
if (isFALSE(val)) return(FALSE)
if (identical(val, "missing")) return(length(makevars_user()) == 0)
if (isTRUE(val)) {
return(TRUE)
}
if (isFALSE(val)) {
return(FALSE)
}
if (identical(val, "missing")) {
return(length(makevars_user()) == 0)
}
if (!is.null(val)) {
if (!is_string(val)) {
stop(cli::format_error(c(
Expand All @@ -142,9 +154,15 @@ should_add_compiler_flags <- function() {
}

val <- Sys.getenv("PKG_BUILD_EXTRA_FLAGS", "true")
if (val %in% flag_true_values) return(TRUE)
if (val %in% flag_false_values) return(FALSE)
if (val %in% "missing") return(length(makevars_user()) == 0)
if (val %in% flag_true_values) {
return(TRUE)
}
if (val %in% flag_false_values) {
return(FALSE)
}
if (val %in% "missing") {
return(length(makevars_user()) == 0)
}

stop(cli::format_error(c(
"Invalid {.envvar PKG_BUILD_EXTRA_FLAGS} environment variable.",
Expand All @@ -155,10 +173,16 @@ should_add_compiler_flags <- function() {
get_desc_config_flag <- function(path, name) {
name <- paste0("Config/build/", name)
val <- desc::desc_get(name, file = path)
if (is.na(val)) return(NULL)
if (is.na(val)) {
return(NULL)
}
lval <- tolower(val)
if (lval %in% flag_true_values) return(TRUE)
if (lval %in% flag_false_values) return(FALSE)
if (lval %in% flag_true_values) {
return(TRUE)
}
if (lval %in% flag_false_values) {
return(FALSE)
}

stop(cli::format_error(
"The {.code {name}} entry in {.path DESCRIPTION} must be {.code TRUE}
Expand All @@ -170,10 +194,14 @@ get_desc_config_flag <- function(path, name) {
mkdirp <- function(path, mode = NULL) {
if (file.exists(path)) {
if (file.info(path)$isdir) {
if (is.null(mode)) return()
if (is.null(mode)) {
return()
}
mode <- as.octmode(mode)
emode <- as.octmode(file.info(path)$mode)
if (emode == mode) return()
if (emode == mode) {
return()
}
ret <- Sys.chmod(path, mode, use_umask = FALSE)
if (!ret) {
stop(cli::format_error(c(
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-find-package-root.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
test_that("find_package_root", {
tmp <- tempfile()
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
mkdirp(file.path(tmp, "a", "b", "c", "d"))
lns <- "Package: this"
writeLines(lns, file.path(tmp, "DESCRIPTION"))

expect_equal(
readLines(file.path(find_package_root(tmp), "DESCRIPTION")),
lns
)

expect_equal(
readLines(file.path(
find_package_root(file.path(tmp, "a")), "DESCRIPTION"
)),
lns
)

expect_equal(
readLines(file.path(
find_package_root(file.path(tmp, "a", "b", "c", "d")), "DESCRIPTION"
)),
lns
)
})

test_that("find_package_root errors", {
expect_error(
find_package_root(basename(tempfile())),
"Path does not exist"
)

if (!file.exists("/DESCRIPTION")) {
expect_error(
find_package_root("/"),
"Could not find R package"
)
}
})

0 comments on commit fd19ffb

Please sign in to comment.