Skip to content

Commit d9ab32e

Browse files
hadleyjennybc
andauthored
Review dev_sitrep() (#2669)
* Review `dev_sitrep()` This doesn't seem that useful any more, but doesn't also seem too objectionable. I just did some minor polishing. Fixes #2664 * Stop importing memoise::memoise() * Report on packages that are behind AND ahead * We need this @importFrom after all --------- Co-authored-by: Jenny Bryan <jenny.f.bryan@gmail.com>
1 parent 713809d commit d9ab32e

6 files changed

Lines changed: 244 additions & 73 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
^[.]?air[.]toml$
2525
^\.vscode$
2626
^\.claude$
27+
^scratch\.R$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ script.R
1414
tests/*/*/.gitignore
1515
docs/
1616
*.key
17+
scratch.R

R/sitrep.R

Lines changed: 98 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Supress R CMD check note
1+
# Suppress R CMD check note. memoise is used at build time!
22
#' @importFrom memoise memoise
33
NULL
44

@@ -71,28 +71,25 @@ check_for_rstudio_updates <- function(
7171
)
7272
}
7373

74-
.r_release <- function() {
75-
R_system_version(rversions::r_release()$version)
74+
r_release <- function() {
75+
R_system_version(rversions::resolve("release")$version)
7676
}
7777

78-
r_release <- memoise::memoise(.r_release)
79-
8078
#' Report package development situation
8179
#'
82-
#' @template devtools
83-
#' @inheritParams pkgbuild::has_build_tools
84-
#' @description `dev_sitrep()` reports
85-
#' * If R is up to date
86-
#' * If RStudio is up to date
87-
#' * If compiler build tools are installed and available for use
88-
#' * If devtools and its dependencies are up to date
89-
#' * If the package's dependencies are up to date
80+
#' @description
81+
#' Call this function if things seem weird and you're not sure
82+
#' what's wrong or how to fix it. It reports:
9083
#'
91-
#' @description Call this function if things seem weird and you're not sure
92-
#' what's wrong or how to fix it. If this function returns no output
93-
#' everything should be ready for package development.
84+
#' * If R is up to date.
85+
#' * If RStudio or Positron is up to date.
86+
#' * If compiler build tools are installed and available for use.
87+
#' * If devtools and its dependencies are up to date.
88+
#' * If the package's dependencies are up to date.
9489
#'
9590
#' @return A named list, with S3 class `dev_sitrep` (for printing purposes).
91+
#' @template devtools
92+
#' @inheritParams pkgbuild::has_build_tools
9693
#' @export
9794
#' @examples
9895
#' \dontrun{
@@ -112,9 +109,9 @@ dev_sitrep <- function(pkg = ".", debug = FALSE) {
112109
has_build_tools = has_build_tools,
113110
rtools_path = if (has_build_tools) pkgbuild::rtools_path(),
114111
devtools_version = utils::packageVersion("devtools"),
115-
devtools_deps = outdated_deps(pak::pkg_deps("devtools", dependencies = NA)),
112+
devtools_deps = compare_deps(pak::pkg_deps("devtools", dependencies = NA)),
116113
pkg_deps = if (!is.null(pkg)) {
117-
outdated_deps(pak::local_dev_deps(pkg$path, dependencies = TRUE))
114+
compare_deps(pak::local_dev_deps(pkg$path, dependencies = TRUE))
118115
},
119116
rstudio_version = if (is_rstudio_running()) rstudioapi::getVersion(),
120117
rstudio_msg = if (!is_positron()) check_for_rstudio_updates()
@@ -130,7 +127,7 @@ new_dev_sitrep <- function(
130127
has_build_tools = TRUE,
131128
rtools_path = NULL,
132129
devtools_version = utils::packageVersion("devtools"),
133-
devtools_deps = data.frame(package = character(), diff = numeric()),
130+
devtools_deps = NULL,
134131
pkg_deps = NULL,
135132
rstudio_version = NULL,
136133
rstudio_msg = NULL
@@ -162,60 +159,84 @@ print.dev_sitrep <- function(x, ...) {
162159
kv_line("version", x$r_version)
163160
kv_line("path", x$r_path, path = TRUE)
164161
if (x$r_version < x$r_release_version) {
162+
all_ok <- FALSE
165163
cli::cli_bullets(c(
166164
"!" = "{.field R} is out of date ({.val {x$r_version}} vs {.val {x$r_release_version}})"
167165
))
168-
all_ok <- FALSE
169166
}
170167

171168
if (x$is_windows) {
172169
cli::cli_rule("Rtools")
173170
if (x$has_build_tools) {
174171
kv_line("path", x$rtools_path, path = TRUE)
175172
} else {
173+
all_ok <- FALSE
176174
cli::cli_bullets(c(
177175
"!" = "{.field Rtools} is not installed.",
178176
" " = "Download and install it from: {.url https://cloud.r-project.org/bin/windows/Rtools/}"
179177
))
180178
}
181-
all_ok <- FALSE
182179
}
183180

184181
if (!is.null(x$rstudio_version)) {
185182
cli::cli_rule(if (is_positron()) "Positron" else "RStudio")
186183
kv_line("version", x$rstudio_version)
187184

188185
if (!is.null(x$rstudio_msg)) {
189-
cli::cli_bullets(c("!" = "{x$rstudio_msg}"))
190186
all_ok <- FALSE
187+
cli::cli_bullets(c("!" = "{x$rstudio_msg}"))
191188
}
192189
}
193190

194191
cli::cli_rule("devtools")
195192
kv_line("version", x$devtools_version)
196193

197-
devtools_deps_old <- x$devtools_deps$diff < 0
198-
if (any(devtools_deps_old)) {
199-
cli::cli_bullets(c(
200-
"!" = "{.pkg devtools} or its dependencies out of date:",
201-
" " = "{.val {x$devtools_deps$package[devtools_deps_old]}}",
202-
" " = "Update them with {.code pak::pak(\"devtools\")}"
203-
))
194+
devtools_not_ok <- any(x$devtools_deps$status != "ok")
195+
if (devtools_not_ok) {
204196
all_ok <- FALSE
197+
198+
behind <- x$devtools_deps[x$devtools_deps$status == "behind", ]
199+
if (nrow(behind) > 0) {
200+
cli::cli_bullets(c(
201+
"!" = "{.field devtools} or its dependencies are out of date.",
202+
" " = "Update them with {.code pak::pak(\"devtools\").}"
203+
))
204+
cli::cli_verbatim(paste(" ", dep_labels(behind)))
205+
}
206+
207+
ahead <- x$devtools_deps[x$devtools_deps$status == "ahead", ]
208+
if (nrow(ahead) > 0) {
209+
cli::cli_bullets(c(
210+
"i" = "{.field devtools} or its dependencies are installed from a dev version, FYI:"
211+
))
212+
cli::cli_verbatim(paste(" ", dep_labels(ahead)))
213+
}
205214
}
206215

207216
cli::cli_rule("dev package")
208217
kv_line("package", x$pkg$package)
209218
kv_line("path", x$pkg$path, path = TRUE)
210219

211-
pkg_deps_old <- x$pkg_deps$diff < 0
212-
if (any(pkg_deps_old)) {
213-
cli::cli_bullets(c(
214-
"!" = "{.field {x$pkg$package}} dependencies out of date:",
215-
" " = "{.val {x$pkg_deps$package[pkg_deps_old]}}",
216-
" " = "Update them with {.code pak::local_install_dev_deps()}"
217-
))
220+
dev_pkg_not_ok <- any(x$pkg_deps$status != "ok")
221+
if (dev_pkg_not_ok) {
218222
all_ok <- FALSE
223+
224+
behind <- x$pkg_deps[x$pkg_deps$status == "behind", ]
225+
if (nrow(behind) > 0) {
226+
cli::cli_bullets(c(
227+
"!" = "{.field {x$pkg$package}} dependencies are out of date.",
228+
" " = "Update them with {.code pak::local_install_dev_deps()}."
229+
))
230+
cli::cli_verbatim(paste(" ", dep_labels(behind)))
231+
}
232+
233+
ahead <- x$pkg_deps[x$pkg_deps$status == "ahead", ]
234+
if (nrow(ahead) > 0) {
235+
cli::cli_bullets(c(
236+
"i" = "{.field {x$pkg$package}} dependencies are installed from a dev version, FYI:"
237+
))
238+
cli::cli_verbatim(paste(" ", dep_labels(ahead)))
239+
}
219240
}
220241

221242
if (all_ok) {
@@ -225,10 +246,9 @@ print.dev_sitrep <- function(x, ...) {
225246
invisible(x)
226247
}
227248

228-
229249
# Helpers -----------------------------------------------------------------
230250

231-
outdated_deps <- function(deps) {
251+
compare_deps <- function(deps) {
232252
installed <- vapply(
233253
deps$package,
234254
function(p) {
@@ -239,18 +259,53 @@ outdated_deps <- function(deps) {
239259
},
240260
character(1)
241261
)
242-
diff <- mapply(
243-
function(inst, avail) {
262+
status <- mapply(
263+
function(inst, latest) {
244264
if (is.na(inst)) {
245-
return(-1L)
265+
return("behind")
246266
}
247-
as.integer(utils::compareVersion(inst, avail))
267+
switch(
268+
as.character(utils::compareVersion(inst, latest)),
269+
"-1" = "behind",
270+
"0" = "ok",
271+
"1" = "ahead"
272+
)
248273
},
249274
installed,
250275
deps$version,
251276
USE.NAMES = FALSE
252277
)
253-
data.frame(package = deps$package, diff = diff)
278+
data.frame(
279+
package = deps$package,
280+
latest = deps$version,
281+
installed = installed,
282+
status = status
283+
)
284+
}
285+
286+
dep_labels <- function(deps) {
287+
labels <- mapply(
288+
format_dep_line,
289+
format(deps$package, justify = "left"),
290+
deps$installed,
291+
deps$latest,
292+
deps$status,
293+
USE.NAMES = FALSE
294+
)
295+
stats::setNames(labels, rep(" ", length(labels)))
296+
}
297+
298+
format_dep_line <- function(package, installed, latest, status) {
299+
if (is.na(installed)) {
300+
paste0(package, " (not installed)")
301+
} else {
302+
status_styled <- if (status == "behind") {
303+
cli::col_red(status)
304+
} else {
305+
cli::col_cyan(status)
306+
}
307+
paste0(package, " (", status_styled, ": ", installed, " vs ", latest, ")")
308+
}
254309
}
255310

256311
kv_line <- function(key, value, path = FALSE) {

man/dev_sitrep.Rd

Lines changed: 7 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/sitrep.md

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,43 @@
3838
* path: '/usr/lib/R'
3939
-- devtools ------------------------------------------------
4040
* version: 2.4.6
41-
! devtools or its dependencies out of date:
42-
"cli"
43-
Update them with `pak::pak("devtools")`
41+
! devtools or its dependencies are out of date.
42+
Update them with `pak::pak("devtools").`
43+
cli (behind: 0.5.0 vs 1.0.0)
44+
-- dev package ---------------------------------------------
45+
* package: <unset>
46+
* path: <unset>
47+
48+
# print warns about missing devtools deps
49+
50+
Code
51+
print(x)
52+
Message
53+
-- R -------------------------------------------------------
54+
* version: 4.4.0
55+
* path: '/usr/lib/R'
56+
-- devtools ------------------------------------------------
57+
* version: 2.4.6
58+
! devtools or its dependencies are out of date.
59+
Update them with `pak::pak("devtools").`
60+
somepkg (not installed)
61+
-- dev package ---------------------------------------------
62+
* package: <unset>
63+
* path: <unset>
64+
65+
# print notes dev versions of devtools deps
66+
67+
Code
68+
print(x)
69+
Message
70+
-- R -------------------------------------------------------
71+
* version: 4.4.0
72+
* path: '/usr/lib/R'
73+
-- devtools ------------------------------------------------
74+
* version: 2.4.6
75+
i devtools or its dependencies are installed from a dev
76+
version, FYI:
77+
usethis (ahead: 3.2.1.9000 vs 3.2.1)
4478
-- dev package ---------------------------------------------
4579
* package: <unset>
4680
* path: <unset>
@@ -58,9 +92,26 @@
5892
-- dev package ---------------------------------------------
5993
* package: "mypkg"
6094
* path: '/tmp/mypkg'
61-
! mypkg dependencies out of date:
62-
"dplyr" and "tidyr"
63-
Update them with `pak::local_install_dev_deps()`
95+
! mypkg dependencies are out of date.
96+
Update them with `pak::local_install_dev_deps()`.
97+
dplyr (behind: 1.0.0 vs 1.1.0)
98+
tidyr (behind: 1.0.0 vs 1.1.0)
99+
100+
# print notes dev versions of package deps
101+
102+
Code
103+
print(x)
104+
Message
105+
-- R -------------------------------------------------------
106+
* version: 4.4.0
107+
* path: '/usr/lib/R'
108+
-- devtools ------------------------------------------------
109+
* version: 2.4.6
110+
-- dev package ---------------------------------------------
111+
* package: "mypkg"
112+
* path: '/tmp/mypkg'
113+
i mypkg dependencies are installed from a dev version, FYI:
114+
usethis (ahead: 3.2.1.9000 vs 3.2.1)
64115

65116
# print shows RStudio update message
66117

0 commit comments

Comments
 (0)