Skip to content

Commit 79e244f

Browse files
committed
use_standalone() beyond packages
- Closes #2087 Thanks @jennybc for help in person during the tidy dev day 2025 Also, claude code helped with a few things: - Fix pluralization error with `cli::qty()` - See examples of messages in this repo - Use `usethis.quiet = TRUE`
1 parent 7a71a95 commit 79e244f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

R/use_standalone.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ use_standalone <- function(repo_spec, file = NULL, ref = NULL, host = NULL) {
7979

8080
dependencies <- standalone_dependencies(lines, path)
8181

82+
if (!is_package()) {
83+
imports <- dependencies$imports
84+
n <- cli::qty(length(imports$pkg))
85+
ui_bullets(c(
86+
"!" = "You'll need the following {n} package{?s}: {.pkg {imports$pkg}}."
87+
))
88+
return(invisible())
89+
}
90+
8291
for (dependency in dependencies$deps) {
8392
use_standalone(repo_spec, dependency, ref = ref, host = host)
8493
}

tests/testthat/_snaps/use_standalone.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
[4] "# ----------------------------------------------------------------------"
6565
[5] "#"
6666

67+
# from a non-package project throws an informative message
68+
69+
Code
70+
use_standalone("r-lib/rlang", "types-check", ref = "73182fe94")
71+
Message
72+
v Writing 'R/import-standalone-types-check.R'.
73+
! You'll need the following package: rlang.
74+
6775
# can offer choices
6876

6977
Code

tests/testthat/test-use_standalone.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ test_that("can import standalone file with dependencies", {
5454
expect_match(imports, "rlang")
5555
})
5656

57+
test_that("from a non-package project throws an informative message", {
58+
skip_if_offline("github.com")
59+
create_local_project()
60+
withr::local_options(list(usethis.quiet = FALSE))
61+
62+
# NOTE: Check ref after r-lib/rlang@standalone-dep has been merged
63+
expect_snapshot(
64+
use_standalone("r-lib/rlang", "types-check", ref = "73182fe94")
65+
)
66+
})
67+
5768
test_that("can use full github url", {
5869
skip_if_offline("github.com")
5970
create_local_package()

0 commit comments

Comments
 (0)