-
Notifications
You must be signed in to change notification settings - Fork 258
/
proj.R
94 lines (81 loc) 路 2.2 KB
/
proj.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
90
91
92
93
94
proj <- new.env(parent = emptyenv())
proj_crit <- function() {
rprojroot::has_file(".here") |
rprojroot::is_rstudio_project |
rprojroot::is_r_package |
rprojroot::is_git_root |
rprojroot::is_remake_project |
rprojroot::is_projectile_project
}
proj_find <- function(path = ".") {
tryCatch(
rprojroot::find_root(proj_crit(), path = path),
error = function(e) NULL
)
}
is_proj <- function(path = ".") !is.null(proj_find(path))
is_package <- function(base_path = proj_get()) {
res <- tryCatch(
rprojroot::find_package_root_file(path = base_path),
error = function(e) NULL
)
!is.null(res)
}
check_is_package <- function(whos_asking = NULL) {
if (is_package()) {
return(invisible())
}
message <- paste0(
"Project ", value(project_name()), " is not an R package."
)
if (!is.null(whos_asking)) {
message <- paste0(
code(whos_asking),
" is designed to work with packages. ",
message
)
}
stop(message, call. = FALSE)
}
#' Get and set currently active project
#'
#' When attached, usethis uses rprojroot to find the project root of the
#' current working directory. It establishes the project root by looking for
#' for a `.here` file, an RStudio project, a package `DESCRIPTION`, a
#' `remake.yml`, or a `.projectile` file. It then stores the project directory
#' for use for the remainder of the session. If needed, you can manually
#' override by running `proj_set()`.
#'
#' @param path Path to set.
#' @param force If `TRUE`, uses this path without checking if any parent
#' directories are existing projects.
#' @keywords internal
#' @export
proj_get <- function() {
if (!is.null(proj$cur)) {
return(proj$cur)
}
# Try current wd
proj_set(".")
if (!is.null(proj$cur)) {
return(proj$cur)
}
stop(
"Current working directory, ", value(normalizePath(".")), ", ",
" does not appear to be inside a project or package.",
call. = FALSE
)
}
#' @export
#' @rdname proj_get
proj_set <- function(path = ".", force = FALSE) {
old <- proj$cur
if (force) {
proj$cur <- path
} else {
proj$cur <- proj_find(path)
}
invisible(old)
}
## check vectorizaion
proj_path <- function(...) file.path(proj_get(), ...)