File: dev-meta.R

package info (click to toggle)
r-cran-pkgload 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,164 kB
  • sloc: sh: 13; cpp: 9; ansic: 8; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,135 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' Return devtools metadata environment
#'
#' If the package was not loaded with devtools, returns `NULL`.
#'
#' @param name The name of a loaded package
#' @keywords internal
#' @examples
#' dev_meta("stats") # NULL
#'
#' if (has_tests()) {
#' # Load the test package in directory "testLoadHooks"
#' load_all(pkgtest("testLoadHooks"))
#'
#' # Get metadata for the package
#' x <- dev_meta("testLoadHooks")
#' as.list(x)
#'
#' # Clean up.
#' unload("testLoadHooks")
#' }
#' @export
dev_meta <- function(name) {
  ns <- .getNamespace(name)
  if (is.null(ns)) {
    cli::cli_abort(c(
      "Namespace not found for {.arg {name}}",
      "i" = "Is it loaded?"
    ))
  }

  if (is.null(ns$.__DEVTOOLS__)) {
    return(NULL)
  }

  ns$.__DEVTOOLS__
}


# Create the devtools metadata environment for a package.
# This should be run when packages are loaded by devtools.
create_dev_meta <- function(name) {
  ns <- .getNamespace(name)

  if (!is.null(ns$.__DEVTOOLS__)) {
    cli::cli_abort(
      "Devtools metadata for package {.pkg {name}} can't already exist."
    )
  }

  ns$.__DEVTOOLS__ <- new.env(parent = ns)
  ns$.__DEVTOOLS__
}