-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathcommands.R
93 lines (86 loc) · 2.56 KB
/
commands.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
is_parsable <- Vectorize(function(x){
tryCatch({
parse(text = x)
TRUE
},
error = error_false
)
},
"x")
extract_filenames <- function(command){
if (!safe_grepl("'", command)){
return(character(0))
}
splits <- str_split(command, "'")[[1]]
splits[seq(from = 2, to = length(splits), by = 2)]
}
# This is the version of the command that is
# actually run in make(), not the version
# that is cached and treated as a dependency.
# It needs to (1) wrap the command in a function
# to protect the user's environment from side effects,
# and (2) call rlang::expr() to enable tidy evaluation
# features such as quasiquotation.
preprocess_command <- function(target, config){
text <- config$plan$command[config$plan$target == target] %>%
wrap_command
parse(text = text, keep.source = FALSE) %>%
eval(envir = config$envir)
}
# Use tidy evaluation to complete the contents of a command.
wrap_command <- function(command){
paste0("rlang::expr(local({\n", command, "\n}))")
}
# Can remove once we remove fetch_cache.
# We can remove fetch_cache once we allow the master process
# to optionally do all the caching.
localize <- function(command) {
paste0("local({\n", command, "\n})")
}
# This version of the command will be hashed and cached
# as a dependency. When the command changes nontrivially,
# drake will react. Otherwise, changes to whitespace or
# comments are just standardized away, and drake
# ignores them. Thus, superfluous builds are not triggered.
get_standardized_command <- function(target, config) {
config$plan$command[config$plan$target == target] %>%
standardize_command
}
# The old standardization command
# that relies on formatR.
# Eventually, we may move to styler,
# since it is now the preferred option for
# text tidying.
# The important thing for drake's standardization of commands
# is to stay stable here, not to be super correct.
# If styler's behavior changes a lot, it will
# put targets out of date.
standardize_command <- function(x) {
x <- language_to_text(x)
formatR::tidy_source(
source = NULL,
comment = FALSE,
blank = FALSE,
arrow = TRUE,
brace.newline = FALSE,
indent = 4,
output = FALSE,
text = as.character(x),
width.cutoff = 119
)$text.tidy %>%
paste(collapse = "\n") %>%
braces
}
language_to_text <- function(x){
if (is.expression(x)){
stopifnot(length(x) < 2)
x <- x[[1]]
}
if (is.expression(x) || is.language(x)){
for (attribute in c("srcref", "srcfile", "wholeSrcref")){
attr(x = x, which = attribute) <- NULL
}
x <- wide_deparse(x)
}
x
}