Skip to content

Commit acac3f9

Browse files
krlmlrjennybc
andauthored
Improvements to standalone header (#1903)
* Add ref and host to standalone header * Add code to generate the standalone * Make some more changes * Dogfood it * Add NEWS bullet * Use fs * Update a snapshot --------- Co-authored-by: Jenny Bryan <[email protected]>
1 parent 2dab4c2 commit acac3f9

File tree

6 files changed

+139
-25
lines changed

6 files changed

+139
-25
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# usethis (development version)
22

3+
* `use_standalone()` inserts an improved header that includes the code needed to
4+
update the standalone file (@krlmlr, #1903).
5+
36
* `use_release_issue()` and `use_upkeep()` behave better when the user has a
47
fork. The user is asked just once to choose between `origin` and `upstream` as
58
the target repo (#2023).

R/import-standalone-obj-type.R

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
# Standalone file: do not edit by hand
2-
# Source: <https://github.com/r-lib/rlang/blob/main/R/standalone-obj-type.R>
2+
# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-obj-type.R
3+
# Generated by: usethis::use_standalone("r-lib/rlang", "obj-type")
34
# ----------------------------------------------------------------------
45
#
56
# ---
67
# repo: r-lib/rlang
78
# file: standalone-obj-type.R
8-
# last-updated: 2022-10-04
9+
# last-updated: 2024-02-14
910
# license: https://unlicense.org
1011
# imports: rlang (>= 1.1.0)
1112
# ---
1213
#
1314
# ## Changelog
1415
#
16+
# 2024-02-14:
17+
# - `obj_type_friendly()` now works for S7 objects.
18+
#
19+
# 2023-05-01:
20+
# - `obj_type_friendly()` now only displays the first class of S3 objects.
21+
#
22+
# 2023-03-30:
23+
# - `stop_input_type()` now handles `I()` input literally in `arg`.
24+
#
1525
# 2022-10-04:
1626
# - `obj_type_friendly(value = TRUE)` now shows numeric scalars
1727
# literally.
@@ -65,7 +75,7 @@ obj_type_friendly <- function(x, value = TRUE) {
6575
if (inherits(x, "quosure")) {
6676
type <- "quosure"
6777
} else {
68-
type <- paste(class(x), collapse = "/")
78+
type <- class(x)[[1L]]
6979
}
7080
return(sprintf("a <%s> object", type))
7181
}
@@ -261,19 +271,19 @@ vec_type_friendly <- function(x, length = FALSE) {
261271
#' Return OO type
262272
#' @param x Any R object.
263273
#' @return One of `"bare"` (for non-OO objects), `"S3"`, `"S4"`,
264-
#' `"R6"`, or `"R7"`.
274+
#' `"R6"`, or `"S7"`.
265275
#' @noRd
266276
obj_type_oo <- function(x) {
267277
if (!is.object(x)) {
268278
return("bare")
269279
}
270280

271-
class <- inherits(x, c("R6", "R7_object"), which = TRUE)
281+
class <- inherits(x, c("R6", "S7_object"), which = TRUE)
272282

273283
if (class[[1]]) {
274284
"R6"
275285
} else if (class[[2]]) {
276-
"R7"
286+
"S7"
277287
} else if (isS4(x)) {
278288
"S4"
279289
} else {
@@ -315,10 +325,15 @@ stop_input_type <- function(x,
315325
if (length(what)) {
316326
what <- oxford_comma(what)
317327
}
328+
if (inherits(arg, "AsIs")) {
329+
format_arg <- identity
330+
} else {
331+
format_arg <- cli$format_arg
332+
}
318333

319334
message <- sprintf(
320335
"%s must be %s, not %s.",
321-
cli$format_arg(arg),
336+
format_arg(arg),
322337
what,
323338
obj_type_friendly(x, value = show_value)
324339
)

R/import-standalone-types-check.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Standalone file: do not edit by hand
2-
# Source: <https://github.com/r-lib/rlang/blob/main/R/standalone-types-check.R>
2+
# Source: https://github.com/r-lib/rlang/blob/HEAD/R/standalone-types-check.R
3+
# Generated by: usethis::use_standalone("r-lib/rlang", "types-check")
34
# ----------------------------------------------------------------------
45
#
56
# ---

R/use_standalone.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use_standalone <- function(repo_spec, file = NULL, ref = NULL, host = NULL) {
7474
dest_path <- path("R", as_standalone_dest_file(file))
7575

7676
lines <- read_github_file(repo_spec, path = src_path, ref = ref, host = host)
77-
lines <- c(standalone_header(repo_spec, src_path), lines)
77+
lines <- c(standalone_header(repo_spec, src_path, ref, host), lines)
7878
write_over(proj_path(dest_path), lines, overwrite = TRUE)
7979

8080
dependencies <- standalone_dependencies(lines, path)
@@ -156,10 +156,22 @@ as_standalone_dest_file <- function(file) {
156156
gsub("standalone-", "import-standalone-", file)
157157
}
158158

159-
standalone_header <- function(repo_spec, path) {
159+
standalone_header <- function(repo_spec, path, ref = NULL, host = NULL) {
160+
ref_string <- ref %||% "HEAD"
161+
host_string <- host %||% "https://github.com"
162+
source_comment <-
163+
glue("# Source: {host_string}/{repo_spec}/blob/{ref_string}/{path}")
164+
165+
path_string <- path_ext_remove(sub("^standalone-", "", path_file(path)))
166+
ref_string <- if (is.null(ref)) "" else glue(', ref = "{ref}"')
167+
host_string <- if (is.null(host) || host == "https://github.com") "" else glue(', host = "{host}"')
168+
code_hint <- glue('usethis::use_standalone("{repo_spec}", "{path_string}"{ref_string}{host_string})')
169+
generated_comment <- glue('# Generated by: {code_hint}')
170+
160171
c(
161172
"# Standalone file: do not edit by hand",
162-
glue("# Source: <https://github.com/{repo_spec}/blob/main/{path}>"),
173+
source_comment,
174+
generated_comment,
163175
paste0("# ", strrep("-", 72 - 2)),
164176
"#"
165177
)

tests/testthat/_snaps/use_standalone.md

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
1+
# standalone_header() works with various inputs
2+
3+
Code
4+
standalone_header("OWNER/REPO", "R/standalone-foo.R")
5+
Output
6+
[1] "# Standalone file: do not edit by hand"
7+
[2] "# Source: https://github.com/OWNER/REPO/blob/HEAD/R/standalone-foo.R"
8+
[3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\")"
9+
[4] "# ----------------------------------------------------------------------"
10+
[5] "#"
11+
12+
---
13+
14+
Code
15+
standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah")
16+
Output
17+
[1] "# Standalone file: do not edit by hand"
18+
[2] "# Source: https://github.com/OWNER/REPO/blob/blah/R/standalone-foo.R"
19+
[3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\")"
20+
[4] "# ----------------------------------------------------------------------"
21+
[5] "#"
22+
23+
---
24+
25+
Code
26+
standalone_header("OWNER/REPO", "R/standalone-foo.R", host = "https://github.com")
27+
Output
28+
[1] "# Standalone file: do not edit by hand"
29+
[2] "# Source: https://github.com/OWNER/REPO/blob/HEAD/R/standalone-foo.R"
30+
[3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\")"
31+
[4] "# ----------------------------------------------------------------------"
32+
[5] "#"
33+
34+
---
35+
36+
Code
37+
standalone_header("OWNER/REPO", "R/standalone-foo.R", host = "https://github.acme.com")
38+
Output
39+
[1] "# Standalone file: do not edit by hand"
40+
[2] "# Source: https://github.acme.com/OWNER/REPO/blob/HEAD/R/standalone-foo.R"
41+
[3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", host = \"https://github.acme.com\")"
42+
[4] "# ----------------------------------------------------------------------"
43+
[5] "#"
44+
45+
---
46+
47+
Code
48+
standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah", host = "https://github.com")
49+
Output
50+
[1] "# Standalone file: do not edit by hand"
51+
[2] "# Source: https://github.com/OWNER/REPO/blob/blah/R/standalone-foo.R"
52+
[3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\")"
53+
[4] "# ----------------------------------------------------------------------"
54+
[5] "#"
55+
56+
---
57+
58+
Code
59+
standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah", host = "https://github.acme.com")
60+
Output
61+
[1] "# Standalone file: do not edit by hand"
62+
[2] "# Source: https://github.acme.com/OWNER/REPO/blob/blah/R/standalone-foo.R"
63+
[3] "# Generated by: usethis::use_standalone(\"OWNER/REPO\", \"foo\", ref = \"blah\", host = \"https://github.acme.com\")"
64+
[4] "# ----------------------------------------------------------------------"
65+
[5] "#"
66+
167
# can offer choices
268

369
Code
@@ -12,16 +78,6 @@
1278
! `file` is absent, but must be supplied.
1379
i Possible options are cli, downstream-deps, lazyeval, lifecycle, linked-version, obj-type, purrr, rlang, s3-register, sizes, types-check, vctrs, or zeallot.
1480

15-
# header provides useful summary
16-
17-
Code
18-
standalone_header("r-lib/usethis", "R/standalone-test.R")
19-
Output
20-
[1] "# Standalone file: do not edit by hand"
21-
[2] "# Source: <https://github.com/r-lib/usethis/blob/main/R/standalone-test.R>"
22-
[3] "# ----------------------------------------------------------------------"
23-
[4] "#"
24-
2581
# can extract imports
2682

2783
Code

tests/testthat/test-use_standalone.R

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
test_that("standalone_header() works with various inputs", {
2+
expect_snapshot(
3+
standalone_header("OWNER/REPO", "R/standalone-foo.R")
4+
)
5+
expect_snapshot(
6+
standalone_header("OWNER/REPO", "R/standalone-foo.R", ref = "blah")
7+
)
8+
expect_snapshot(
9+
standalone_header(
10+
"OWNER/REPO", "R/standalone-foo.R", host = "https://github.com"
11+
)
12+
)
13+
expect_snapshot(
14+
standalone_header(
15+
"OWNER/REPO", "R/standalone-foo.R", host = "https://github.acme.com"
16+
)
17+
)
18+
expect_snapshot(
19+
standalone_header(
20+
"OWNER/REPO", "R/standalone-foo.R",
21+
ref = "blah", host = "https://github.com"
22+
)
23+
)
24+
expect_snapshot(
25+
standalone_header(
26+
"OWNER/REPO", "R/standalone-foo.R",
27+
ref = "blah", host = "https://github.acme.com"
28+
)
29+
)
30+
})
31+
132
test_that("can import standalone file with dependencies", {
233
skip_if_offline("github.com")
334
create_local_package()
@@ -40,10 +71,6 @@ test_that("can offer choices", {
4071
})
4172
})
4273

43-
test_that("header provides useful summary", {
44-
expect_snapshot(standalone_header("r-lib/usethis", "R/standalone-test.R"))
45-
})
46-
4774
test_that("can extract dependencies", {
4875
extract_deps <- function(deps) {
4976
out <- standalone_dependencies(c("# ---", deps, "# ---"), "test.R")

0 commit comments

Comments
 (0)