forked from Novartis/tidymodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_module.R
329 lines (295 loc) · 7.4 KB
/
add_module.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#' Create a module
#'
#' This function creates a `{tm}` module class inside the current folder.
#'
#' @param name The class name of the module.
#' @param path Where to created the file. Default is `getwd()`. The function will add `R` to the path if the sub-folder exists.
#' @param prefix filename prefix. Default is `tm`. Set to `NULL`` to disable.
#' @param inherit Parent module class. Default is TidyModule.
#' @param open Should the file be opened?
#' @param dir_create Creates the directory if it doesn't exist, default is `TRUE`.
#' @param export Logical. Should the module be exported? Default is `FALSE`.
#' @note As a convention, this function will automatically capitalize the first character of the `name` argument.
#'
#' @importFrom cli cat_bullet
#' @importFrom utils file.edit
#' @importFrom fs path_abs path file_create
#' @importFrom snippr snippets_read
#'
#' @export
add_module <- function(
name,
inherit = "TidyModule",
path = getwd(),
prefix = "tm",
open = TRUE,
dir_create = TRUE,
export = FALSE
){
name <- file_path_sans_ext(name)
# Capitalize
name <- paste0(toupper(substring(name,1,1)),substring(name,2))
dir_created <- create_if_needed(
fs::path(path), type = "directory"
)
if (!dir_created){
cat_red_bullet(
"File not added (needs a valid directory)"
)
return(invisible(FALSE))
}
if(dir.exists(fs::path(path, "R")))
path <- fs::path(path, "R")
old <- setwd(path_abs(path))
on.exit(setwd(old))
where <- fs::path(
paste0(ifelse(is.null(prefix),"",paste0(prefix,"_")), name, ".R")
)
if(!check_file_exist(where)){
cat_red_bullet(
"File not created (already exists)"
)
return(invisible(FALSE))
}
# make sure the provided parent module is valid
import <- NULL
parent <- inherit
# TidyModule object
if(is(parent,"TidyModule")){
parent <- class(parent)[1]
}
# Load the class generator from the name
if(class(parent) == "character")
tryCatch(
{
parent <- eval(parse(text = parent))
},
error = function(e){
cat_red_bullet(
paste0("Could not find module defined with 'inherit' = ",inherit)
)
return(invisible(FALSE))
}
)
# Retrieve package dependency and parent module name
if(is(parent,"R6ClassGenerator")){
clist <- get_R6CG_list(parent)
if("TidyModule" %in% clist){
import <- environmentName(parent$parent_env)
if(import == "R_GlobalEnv")
import <- NULL
parent <- clist[1]
}else{
cat_red_bullet(
paste0("Could not find module defined with 'inherit' = ",deparse(substitute(inherit)))
)
return(invisible(FALSE))
}
}
# Retrieve content from package snippet
file_content <- snippr::snippets_read(path = system.file("rstudio/r.snippets",package = "tidymodules"))$tm.mod.new
file_content <- unlist(strsplit(file_content,"\\n"))
for(l in 1:length(file_content)){
# remove $ escapes \\
file_content[l] <- sub("\\$","$",file_content[l],fixed = TRUE)
# remove tabs
file_content[l] <- sub("\t","",file_content[l])
# remove snippet placeholders
file_content[l] <- gsub("\\$\\{\\d+:(\\w+)\\}","%\\1",file_content[l])
# remove cursor pointer
file_content[l] <- sub("\\$\\{0\\}","",file_content[l])
# substitute module name
if(grepl("MyModule",file_content[l])){
file_content[l] <- gsub("MyModule","s",file_content[l])
file_content[l] <- sprintf(file_content[l],name)
}
# substitute parent module
if(grepl("TidyModule",file_content[l])){
file_content[l] <- gsub("TidyModule","s",file_content[l])
file_content[l] <- sprintf(file_content[l],parent)
}
# manage export
if (grepl("@export",file_content[l])){
if(!export)
file_content[l] <- "#' @noRd "
if(!is.null(import))
file_content[l] <- paste0("#'\n#' @import ",import,"\n",file_content[l])
}
}
writeLines(file_content,where,sep = "\n")
cat_created(fs::path(path,where))
open_or_go_to(where, open)
}
# bunch of utility functions copied from golem
# WILL FACILITATE MIGRATING THIS FUNCTION TO GOLEM
#' @importFrom utils menu
yesno <- function (...)
{
cat(paste0(..., collapse = ""))
menu(c("Yes", "No")) == 1
}
#' @importFrom fs file_exists
check_file_exist <- function(file){
res <- TRUE
if (file_exists(file)){
cat_orange_bullet(file)
res <- yesno("This file already exists, override?")
}
return(res)
}
#' @importFrom fs dir_create file_create
create_if_needed <- function(
path,
type = c("file", "directory"),
content = NULL
){
type <- match.arg(type)
# Check if file or dir already exist
if (type == "file"){
dont_exist <- file_not_exist(path)
} else if (type == "directory"){
dont_exist <- dir_not_exist(path)
}
# If it doesn't exist, ask if we are allowed
# to create it
if (dont_exist){
ask <- yesno(
sprintf(
"The %s %s doesn't exist, create?",
basename(path),
type
)
)
# Return early if the user doesn't allow
if (!ask) {
return(FALSE)
} else {
# Create the file
if (type == "file"){
if(dir_not_exist(dirname(path)))
dir_create(dirname(path), recurse = TRUE)
file_create(path)
write(content, path, append = TRUE)
} else if (type == "directory"){
dir_create(path, recurse = TRUE)
}
}
}
# TRUE means that file exists (either
# created or already there)
return(TRUE)
}
#' @importFrom cli cat_bullet
cat_green_tick <- function(...){
cat_bullet(
...,
bullet = "tick",
bullet_col = "green"
)
}
#' @importFrom cli cat_bullet
cat_red_bullet <- function(...){
cat_bullet(
...,
bullet = "bullet",
bullet_col = "red"
)
}
#' @importFrom cli cat_bullet
cat_orange_bullet <- function(...){
cat_bullet(
...,
bullet = "bullet",
bullet_col = "orange"
)
}
#' @importFrom cli cat_bullet
cat_info <- function(...){
cat_bullet(
...,
bullet = "arrow_right",
bullet_col = "grey"
)
}
cat_exists <- function(where){
cat_red_bullet(
sprintf(
"%s already exists, skipping the copy.",
path_file(where)
)
)
cat_info(
sprintf(
"If you want replace it, remove the %s file first.",
path_file(where)
)
)
}
cat_created <- function(
where,
file = "File"
){
cat_green_tick(
sprintf(
"%s created at %s",
file,
where
)
)
}
open_or_go_to <- function(
where,
open
){
if (
rstudioapi::isAvailable() &&
open &&
rstudioapi::hasFun("navigateToFile")
){
rstudioapi::navigateToFile(where)
} else {
cat_red_bullet(
sprintf(
"Go to %s",
where
)
)
}
}
desc_exist <- function(pkg){
file_exists(
paste0(pkg, "/DESCRIPTION")
)
}
file_created_dance <- function(
where,
fun,
pkg,
dir,
name,
open
){
cat_created(where)
fun(pkg, dir, name)
open_or_go_to(where, open)
}
if_not_null <- function(x, ...){
if (! is.null(x)){
force(...)
}
}
set_name <- function(x, y){
names(x) <- y
x
}
# FROM tools::file_path_sans_ext() & tools::file_ext
file_path_sans_ext <- function(x){
sub("([^.]+)\\.[[:alnum:]]+$", "\\1", x)
}
file_ext <- function (x) {
pos <- regexpr("\\.([[:alnum:]]+)$", x)
ifelse(pos > -1L, substring(x, pos + 1L), "")
}
#' @importFrom fs dir_exists file_exists
dir_not_exist <- Negate(dir_exists)
file_not_exist <- Negate(file_exists)