R/find-package-root.R

Defines functions find_package_root

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 <- normalizePath(path, winslash = "/")
  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
}
r-lib/pkgdepends documentation built on April 28, 2024, 3:23 a.m.