88}
99
1010# Borrowed from pkgload:::dev_meta, with some modifications.
11+ # Returns TRUE if `pkg` was loaded with `devtools::load_all()`.
1112devtools_loaded <- function (pkg ) {
1213 ns <- .getNamespace(pkg )
1314 if (is.null(ns ) || is.null(ns $ .__DEVTOOLS__ )) {
@@ -16,8 +17,9 @@ devtools_loaded <- function(pkg) {
1617 TRUE
1718}
1819
19- # Since I/O can be expensive, only utils::packageVersion() if the package isn't already loaded
2020get_package_version <- function (pkg ) {
21+ # `utils::packageVersion()` can be slow, so first try the fast path of
22+ # checking if the package is already loaded.
2123 ns <- .getNamespace(pkg )
2224 if (is.null(ns )) {
2325 utils :: packageVersion(pkg )
@@ -127,10 +129,12 @@ s3_register <- function(generic, class, method = NULL) {
127129 invisible ()
128130}
129131
130- # Borrowed from pkgload::shim_system.file, with some modifications.
131- # Most notably, if the package isn't loaded via devtools, the package directory
132- # lookup is cached. Also, to keep the implementation simple, it doesn't support
133- # specification of lib.loc or mustWork
132+ # Borrowed from pkgload::shim_system.file, with some modifications. This behaves
133+ # like `system.file()`, except that (1) for packages loaded with
134+ # `devtools::load_all()`, it will return the path to files in the package's
135+ # inst/ directory, and (2) for other packages, the directory lookup is cached.
136+ # Also, to keep the implementation simple, it doesn't support specification of
137+ # lib.loc or mustWork.
134138system_file <- function (... , package = " base" ) {
135139 if (! devtools_loaded(package )) {
136140 return (system_file_cached(... , package = package ))
@@ -174,6 +178,11 @@ system_file <- function(..., package = "base") {
174178 normalizePath(files , winslash = " /" )
175179}
176180
181+ # A wrapper for `system.file()`, which caches the results, because
182+ # `system.file()` can be slow. Note that because of caching, if
183+ # `system_file_cached()` is called on a package that isn't installed, then the
184+ # package is installed, and then `system_file_cached()` is called again, it will
185+ # still return "".
177186system_file_cached <- local({
178187 pkg_dir_cache <- character ()
179188
0 commit comments