File: package.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 (87 lines) | stat: -rw-r--r-- 2,496 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#' Find file in a package.
#'
#' It always starts by finding by walking up the path until it finds the
#' root directory, i.e. a directory containing `DESCRIPTION`. If it
#' cannot find the root directory, or it can't find the specified path, it
#' will throw an error.
#'
#' @param ... Components of the path.
#' @param path Place to start search for package directory.
#' @export
#' @examples
#' \dontrun{
#' package_file("figures", "figure_1")
#' }
package_file <- function(..., path = ".") {
  file.path(pkg_path(path), ...)
}

# Mockable variant of interactive
interactive <- function() .Primitive("interactive")()

#' Is the package currently under development?
#'
#' Returns `TRUE` or `FALSE` depending on if the package has been loaded by
#' **pkgload**.
#'
#' @param name the name of a package.
#' @export
is_dev_package <- function(name) name %in% dev_packages()

#' Helper functions for working with development packages.
#'
#' All functions search recursively up the directory tree from the input path
#' until they find a DESCRIPTION file.
#' @inheritParams load_all
#' @name packages
NULL

#' @describeIn packages Return the normalized package path.
#' @export
pkg_path <- function(path = ".") {
  path <- tryCatch(
    {
      rprojroot_find_package_root_file(path = path)
    },
    error = function(e) {
      msg <- c(
        "Could not find a root 'DESCRIPTION' file that starts with '^Package' in {.path {normalizePath(path)}}.",
        "i" = "Are you in your project directory and does your project have a 'DESCRIPTION' file?"
      )
      cli::cli_abort(msg, class = "pkgload_no_desc")
    }
  )

  # Strip trailing slashes, which can cause errors on Windows (#73)
  sub("[/\\]$", "", path)
}

#' @describeIn packages Return the package name.
#' @export
pkg_name <- function(path = ".") {
  desc_desc_get_field("Package", file = pkg_path(path))
}

#' @describeIn packages Return the package DESCRIPTION as a [desc::desc()] object.
#' @export
pkg_desc <- function(path = ".") {
  desc_desc(pkg_path(path))
}

#' @describeIn packages Return the parsed package version.
#' @export
pkg_version <- function(path = ".") {
  desc_desc_get_version(pkg_path(path))
}

#' @describeIn packages Return the raw package version (as a string).
#' @export
pkg_version_raw <- function(path = ".") {
  desc_desc_get_field("Version", file = pkg_path(path))[[1]]
}

#' @describeIn packages Return the package namespace.
#' @export
pkg_ns <- function(path = ".") {
  ns_env(pkg_name(path))
}