-
Notifications
You must be signed in to change notification settings - Fork 129
/
use_utils.R
143 lines (132 loc) · 3 KB
/
use_utils.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#' Use the utils files
#'
#' \describe{
#' \item{use_utils_ui}{Copies the golem_utils_ui.R to the R folder.}
#' \item{use_utils_server}{Copies the golem_utils_server.R to the R folder.}
#' }
#'
#' @inheritParams add_module
#'
#' @export
#' @rdname utils_files
#'
#' @importFrom cli cat_bullet
#' @importFrom utils capture.output
#' @importFrom usethis use_testthat
#'
#' @return Used for side-effects.
use_utils_ui <- function(pkg = get_golem_wd(),
with_test = FALSE) {
added <- use_utils(
file_name = "golem_utils_ui.R",
folder_name = "R",
pkg = pkg
)
if (added) {
cat_green_tick("Utils UI added")
if (with_test) {
if (!isTRUE(dir.exists("tests"))) {
use_testthat()
}
pth <- path(
pkg, "tests", "testthat", "test-golem_utils_ui.R"
)
if (
file.exists(pth)
) {
file_already_there_dance(
where = pth,
open_file = FALSE
)
} else {
use_utils_test_ui()
}
}
}
}
#' @export
#' @rdname utils_files
use_utils_test_ui <- function(pkg = get_golem_wd()) {
added <- use_utils(
file_name = "test-golem_utils_ui.R",
folder_name = "tests/testthat",
pkg = pkg
)
if (added) {
cat_green_tick("Tests on utils_ui added")
}
}
#' @export
#' @rdname utils_files
use_utils_server <- function(pkg = get_golem_wd(),
with_test = FALSE) {
added <- use_utils(
file_name = "golem_utils_server.R",
folder_name = "R",
pkg = pkg
)
if (added) {
cat_green_tick("Utils server added")
if (with_test) {
if (!isTRUE(dir.exists("tests"))) {
use_testthat()
}
pth <- path(
pkg, "tests", "testthat", "test-golem_utils_server.R"
)
if (
file.exists(pth)
) {
file_already_there_dance(
where = pth,
open_file = FALSE
)
} else {
use_utils_test_server()
}
}
}
}
use_utils_test_ <- function(pkg = get_golem_wd(),
type = c("ui", "server")) {
type <- match.arg(type)
added <- use_utils(
file_name = sprintf("test-golem_utils_%s.R", type),
folder_name = "tests/testthat",
pkg = pkg
)
if (added) {
cat_green_tick("Tests on utils_server added")
}
}
#' @export
#' @rdname utils_files
use_utils_test_ui <- function(pkg = get_golem_wd()) {
use_utils_test_(pkg, "ui")
}
#' @export
#' @rdname utils_files
use_utils_test_server <- function(pkg = get_golem_wd()) {
use_utils_test_(pkg, "server")
}
#' @importFrom fs file_copy path_abs path_file
use_utils <- function(file_name,
folder_name,
pkg = get_golem_wd()) {
old <- setwd(
path_abs(pkg)
)
on.exit(setwd(old))
where <- path(path_abs(pkg), folder_name, file_name)
if (file_exists(where)) {
cat_exists(where)
return(FALSE)
} else {
file_copy(
path = golem_sys("utils", file_name),
new_path = where
)
cat_created(where)
return(TRUE)
}
}