-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathhandle-lifecycle.R
More file actions
139 lines (123 loc) · 3.67 KB
/
Copy pathhandle-lifecycle.R
File metadata and controls
139 lines (123 loc) · 3.67 KB
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
# For each R file still containing lifecycle::deprecate_soft or lifecycle::deprecate_warn:
# advance the level with regex, document, test (stop on error so you can fix),
# prompt to continue, add NEWS item, commit.
already_committed <- system2(
"git",
c("diff", "--name-only", "main..HEAD"),
stdout = TRUE
)
already_committed <- basename(already_committed[grepl(
"^R/",
already_committed
)])
r_files <- sort(setdiff(
list.files(here::here("R"), pattern = "\\.R$", full.names = FALSE),
already_committed
))
for (f in r_files) {
path <- here::here("R", f)
lines <- readLines(path, warn = FALSE)
has_soft <- any(grepl("lifecycle::deprecate_soft", lines, fixed = TRUE))
has_warn <- any(grepl("lifecycle::deprecate_warn", lines, fixed = TRUE))
if (!has_soft && !has_warn) {
next
}
stem <- tools::file_path_sans_ext(f)
cli::cli_alert_info("Processing R/{f}")
# Extract function names before replacement for NEWS
extract_fns <- function(lines, level) {
hits <- grep(
paste0("lifecycle::", level),
lines,
fixed = TRUE,
value = TRUE
)
m <- regmatches(
hits,
regexpr(
paste0('lifecycle::', level, '\\([^,]+,\\s*"([^("]+)\\('),
hits,
perl = TRUE
)
)
sub(paste0('.*"'), "", m[nzchar(m)])
}
soft_fns <- extract_fns(lines, "deprecate_soft")
warn_fns <- extract_fns(lines, "deprecate_warn")
# Advance: warn -> stop first to avoid double-replacement
lines <- gsub(
"lifecycle::deprecate_warn",
"lifecycle::deprecate_stop",
lines,
fixed = TRUE
)
lines <- gsub(
"lifecycle::deprecate_soft",
"lifecycle::deprecate_warn",
lines,
fixed = TRUE
)
writeLines(lines, path)
devtools::document(quiet = TRUE)
# Tests — stop on failure so the user can fix
if (
file.exists(here::here("tests", "testthat", paste0("test-", stem, ".R")))
) {
callr::r(
function(pkg, filter) {
devtools::test(pkg = pkg, filter = filter, stop_on_failure = TRUE)
},
args = list(pkg = here::here(), filter = stem)
)
}
answer <- readline(paste0(
"Tests passed for R/",
f,
". Commit? [Enter = yes / n = stop] "
))
if (trimws(tolower(answer)) == "n") {
stop("Stopped at R/", f, ". Fix, then commit manually and re-run.")
}
# Add NEWS items
news_entries <- c(
if (length(soft_fns) > 0)
paste0("- `", soft_fns, "()` is now deprecated with a warning instead of a message."),
if (length(warn_fns) > 0)
paste0("- `", warn_fns, "()` is now defunct (errors instead of warning).")
)
news_lines <- readLines(here::here("NEWS.md"), warn = FALSE)
depr_idx <- which(news_lines == "## Deprecated and defunct")
if (length(depr_idx) > 0) {
insert_pos <- depr_idx[[1]] + 2L
new_news <- c(
news_lines[seq_len(insert_pos - 1L)],
news_entries,
news_lines[seq(insert_pos, length(news_lines))]
)
} else {
first_ver_idx <- which(grepl("^# igraph", news_lines))[[1]]
new_news <- c(
news_lines[seq_len(first_ver_idx)],
"",
"## Deprecated and defunct",
"",
news_entries,
"",
news_lines[seq(first_ver_idx + 1L, length(news_lines))]
)
}
writeLines(new_news, here::here("NEWS.md"))
commit_msg <- if (has_soft && has_warn) {
paste0("feat!: bump deprecated functions in R/", f)
} else if (has_soft) {
paste0("feat!: bump deprecate_soft to deprecate_warn in R/", f)
} else {
paste0("feat!: bump deprecate_warn to deprecate_stop in R/", f)
}
gert::git_add(
c(paste0("R/", f), "man", "NAMESPACE", "NEWS.md"),
repo = here::here()
)
gert::git_commit(commit_msg, repo = here::here())
cli::cli_alert_success("Committed R/{f}")
}