forked from r-lib/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-mode.R
77 lines (64 loc) · 2.1 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
#' Activate and deactivate development mode.
#'
#' 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
#' @examples
#' \dontrun{
#' dev_mode()
#' dev_mode()
#' }
dev_mode <- local({
.prompt <- NULL
function(on = NULL, path = getOption("devtools.path")) {
lib_paths <- .libPaths()
path <- normalizePath(path, winslash = "/", mustWork = FALSE)
if (is.null(on)) {
on <- !(path %in% lib_paths)
}
if (on) {
if (!file.exists(path)) {
dir.create(path, recursive = TRUE, showWarnings = FALSE)
}
if (!file.exists(path)) {
stop("Failed to create ", path, call. = FALSE)
}
if (!is_library(path)) {
warning(path, " does not appear to be a library. ",
"Are sure you specified the correct directory?",
call. = FALSE
)
}
message("Dev mode: ON")
options(dev_path = path)
if (is.null(.prompt)) .prompt <<- getOption("prompt")
options(prompt = paste("d> "))
.libPaths(c(path, lib_paths))
} else {
message("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(path)) == 0) return(TRUE)
# otherwise check that the directories are compiled R directories -
# i.e. that they contain a Meta directory
dirs <- dir(path, full.names = TRUE)
dirs <- dirs[utils::file_test("-d", dirs)]
has_pkg_dir <- function(path) length(dir(path, pattern = "Meta")) > 0
help_dirs <- vapply(dirs, has_pkg_dir, logical(1))
all(help_dirs)
}