1#' Find file in a package.
2#'
3#' It always starts by finding by walking up the path until it finds the
4#' root directory, i.e. a directory containing `DESCRIPTION`. If it
5#' cannot find the root directory, or it can't find the specified path, it
6#' will throw an error.
7#'
8#' @param ... Components of the path.
9#' @param path Place to start search for package directory.
10#' @export
11#' @examples
12#' \dontrun{
13#' package_file("figures", "figure_1")
14#' }
15package_file <- function(..., path = ".") {
16  file.path(pkg_path(path), ...)
17}
18
19# Mockable variant of interactive
20interactive <- function() .Primitive("interactive")()
21
22#' Is the package currently under development?
23#'
24#' Returns `TRUE` or `FALSE` depending on if the package has been loaded by
25#' **pkgload**.
26#'
27#' @param name the name of a package.
28#' @export
29is_dev_package <- function(name) name %in% dev_packages()
30
31#' Helper functions for working with development packages.
32#'
33#' All functions search recursively up the directory tree from the input path
34#' until they find a DESCRIPTION file.
35#' @inheritParams load_all
36#' @name packages
37NULL
38
39#' @describeIn packages Return the normalized package path.
40#' @export
41pkg_path <- function(path = ".") {
42  path <- tryCatch({
43    rprojroot_find_package_root_file(path = path)
44  },
45  error = function(e) {
46    abort(paste(
47      "Could not find a root 'DESCRIPTION' file that starts with '^Package' in",
48      paste0("'", normalizePath(path), "'."),
49      "Are you in your project directory,",
50      "and does your project have a 'DESCRIPTION' file?"
51    ), class = "pkgload_no_desc")
52  })
53
54  # Strip trailing slashes, which can cause errors on Windows (#73)
55  sub("[/\\]$", "", path)
56}
57
58#' @describeIn packages Return the package name.
59#' @export
60pkg_name <- function(path = ".") {
61  desc_desc_get("Package", pkg_path(path))[[1]]
62}
63
64#' @describeIn packages Return the package DESCRIPTION as a [desc::desc()] object.
65#' @export
66pkg_desc <- function(path = ".") {
67  desc_desc(pkg_path(path))
68}
69
70#' @describeIn packages Return the package version.
71#' @export
72pkg_version <- function(path = ".") {
73  desc_desc_get_version(pkg_path(path))
74}
75
76#' @describeIn packages Return the package namespace.
77#' @export
78pkg_ns <- function(path = ".") {
79  ns_env(pkg_name(path))
80}
81