-
Notifications
You must be signed in to change notification settings - Fork 761
/
Copy pathdev-mode.R
89 lines (75 loc) · 2.66 KB
/
dev-mode.R
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
88
89
#' Activate and deactivate development mode
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' We no longer recommend `dev_mode()` and it will be removed in a future
#' release of devtools. Instead, we now rely on [load_all()] to test drive an
#' in-development package. If you really like the idea of corralling
#' experimental packages in a special library, you might enjoy
#' `withr::local_libpaths()`. If you are concerned about different projects
#' interfering with each other through the use of a shared library, consider
#' using the [renv package](https://rstudio.github.io/renv/).
#'
#' Original description: When activated, `dev_mode` creates a new library for
#' storing installed packages. This new library is automatically created when
#' `dev_mode` is activated if it does not already exist. This allows you to test
#' development packages in a sandbox, without interfering with the other
#' packages you have installed.
#'
#' @param on turn dev mode on (`TRUE`) or off (`FALSE`). If omitted will guess
#' based on whether or not `path` is in [.libPaths()]
#' @param path directory to library.
#' @export
#' @keywords internal
#' @examples
#' \dontrun{
#' dev_mode()
#' dev_mode()
#' }
dev_mode <- local({
.prompt <- NULL
function(on = NULL, path = getOption("devtools.path")) {
lifecycle::deprecate_warn("2.4.5", "dev_mode()")
lib_paths <- .libPaths()
path <- path_real(path)
if (is.null(on)) {
on <- !(path %in% lib_paths)
}
if (on) {
if (!file_exists(path)) {
dir_create(path)
}
if (!file_exists(path)) {
cli::cli_abort("Failed to create {.path {path}}")
}
if (!is_library(path)) {
cli::cli_warn(c(
"{.path {path}} does not appear to be a library.",
"Are sure you specified the correct directory?"
))
}
cli::cli_inform(c(v = "Dev mode: ON"))
options(dev_path = path)
if (is.null(.prompt)) .prompt <<- getOption("prompt")
options(prompt = paste("d> "))
.libPaths(c(path, lib_paths))
} else {
cli::cli_inform(c(v = "Dev mode: OFF"))
options(dev_path = NULL)
if (!is.null(.prompt)) options(prompt = .prompt)
.prompt <<- NULL
.libPaths(setdiff(lib_paths, path))
}
}
})
is_library <- function(path) {
# empty directories can be libraries
if (length(dir_ls(path)) == 0) return(TRUE)
# otherwise check that the directories are compiled R directories -
# i.e. that they contain a Meta directory
dirs <- dir_ls(path, type = "directory")
has_pkg_dir <- function(path) length(dir_ls(path, regexp = "Meta")) > 0
help_dirs <- vapply(dirs, has_pkg_dir, logical(1))
all(help_dirs)
}