|
| 1 | +# Generated by staticimports; do not edit by hand. |
| 2 | +# ====================================================================== |
| 3 | +# Imported from pkg:staticimports |
| 4 | +# ====================================================================== |
| 5 | + |
| 6 | +# Borrowed from pkgload:::dev_meta, with some modifications. |
| 7 | +# Returns TRUE if `pkg` was loaded with `devtools::load_all()`. |
| 8 | +devtools_loaded <- function(pkg) { |
| 9 | + ns <- .getNamespace(pkg) |
| 10 | + if (is.null(ns) || is.null(ns$.__DEVTOOLS__)) { |
| 11 | + return(FALSE) |
| 12 | + } |
| 13 | + TRUE |
| 14 | +} |
| 15 | + |
| 16 | +get_package_version <- function(pkg) { |
| 17 | + # `utils::packageVersion()` can be slow, so first try the fast path of |
| 18 | + # checking if the package is already loaded. |
| 19 | + ns <- .getNamespace(pkg) |
| 20 | + if (is.null(ns)) { |
| 21 | + utils::packageVersion(pkg) |
| 22 | + } else { |
| 23 | + as.package_version(ns$.__NAMESPACE__.$spec[["version"]]) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +is_installed <- function(pkg, version = NULL) { |
| 28 | + installed <- isNamespaceLoaded(pkg) || nzchar(system_file_cached(package = pkg)) |
| 29 | + if (is.null(version)) { |
| 30 | + return(installed) |
| 31 | + } |
| 32 | + installed && isTRUE(get_package_version(pkg) >= version) |
| 33 | +} |
| 34 | + |
| 35 | +# Borrowed from pkgload::shim_system.file, with some modifications. This behaves |
| 36 | +# like `system.file()`, except that (1) for packages loaded with |
| 37 | +# `devtools::load_all()`, it will return the path to files in the package's |
| 38 | +# inst/ directory, and (2) for other packages, the directory lookup is cached. |
| 39 | +# Also, to keep the implementation simple, it doesn't support specification of |
| 40 | +# lib.loc or mustWork. |
| 41 | +system_file <- function(..., package = "base") { |
| 42 | + if (!devtools_loaded(package)) { |
| 43 | + return(system_file_cached(..., package = package)) |
| 44 | + } |
| 45 | + |
| 46 | + if (!is.null(names(list(...)))) { |
| 47 | + stop("All arguments other than `package` must be unnamed.") |
| 48 | + } |
| 49 | + |
| 50 | + # If package was loaded with devtools (the package loaded with load_all), |
| 51 | + # also search for files under inst/, and don't cache the results (it seems |
| 52 | + # more likely that the package path will change during the development |
| 53 | + # process) |
| 54 | + pkg_path <- find.package(package) |
| 55 | + |
| 56 | + # First look in inst/ |
| 57 | + files_inst <- file.path(pkg_path, "inst", ...) |
| 58 | + present_inst <- file.exists(files_inst) |
| 59 | + |
| 60 | + # For any files that weren't present in inst/, look in the base path |
| 61 | + files_top <- file.path(pkg_path, ...) |
| 62 | + present_top <- file.exists(files_top) |
| 63 | + |
| 64 | + # Merge them together. Here are the different possible conditions, and the |
| 65 | + # desired result. NULL means to drop that element from the result. |
| 66 | + # |
| 67 | + # files_inst: /inst/A /inst/B /inst/C /inst/D |
| 68 | + # present_inst: T T F F |
| 69 | + # files_top: /A /B /C /D |
| 70 | + # present_top: T F T F |
| 71 | + # result: /inst/A /inst/B /C NULL |
| 72 | + # |
| 73 | + files <- files_top |
| 74 | + files[present_inst] <- files_inst[present_inst] |
| 75 | + # Drop cases where not present in either location |
| 76 | + files <- files[present_inst | present_top] |
| 77 | + if (length(files) == 0) { |
| 78 | + return("") |
| 79 | + } |
| 80 | + # Make sure backslashes are replaced with slashes on Windows |
| 81 | + normalizePath(files, winslash = "/") |
| 82 | +} |
| 83 | + |
| 84 | +# A wrapper for `system.file()`, which caches the results, because |
| 85 | +# `system.file()` can be slow. Note that because of caching, if |
| 86 | +# `system_file_cached()` is called on a package that isn't installed, then the |
| 87 | +# package is installed, and then `system_file_cached()` is called again, it will |
| 88 | +# still return "". |
| 89 | +system_file_cached <- local({ |
| 90 | + pkg_dir_cache <- character() |
| 91 | + |
| 92 | + function(..., package = "base") { |
| 93 | + if (!is.null(names(list(...)))) { |
| 94 | + stop("All arguments other than `package` must be unnamed.") |
| 95 | + } |
| 96 | + |
| 97 | + not_cached <- is.na(match(package, names(pkg_dir_cache))) |
| 98 | + if (not_cached) { |
| 99 | + pkg_dir <- system.file(package = package) |
| 100 | + pkg_dir_cache[[package]] <<- pkg_dir |
| 101 | + } else { |
| 102 | + pkg_dir <- pkg_dir_cache[[package]] |
| 103 | + } |
| 104 | + |
| 105 | + file.path(pkg_dir, ...) |
| 106 | + } |
| 107 | +}) |
0 commit comments