Skip to content

Commit 14dff76

Browse files
olivroyjennybcgithub-actions[bot]
authored
Add use_r_universe_badge() (#1994)
* Add use_r_universe_badge() function! * Avoid mention `use_github_actions()` deprecated. * Tweak man * adjustments to use `target_repo_spec()` * fix link + logic * Update NEWS.md * Apply suggestion from @github-actions[bot] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestion from @github-actions[bot] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestion from @github-actions[bot] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Use github_url() and also allow `repo_spec` to be provided * NEWS bullet * I guess it's lowercase "u"? R-universe? * Don't feature direct use of `use_badge()` for R-universe as an example These examples are not great, as it stands. But I don't think showing the use `use_badge()` to create one's on R-universe badge is the most obvious usage to add here. --------- Co-authored-by: Jennifer (Jenny) Bryan <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 21ecaa4 commit 14dff76

File tree

9 files changed

+134
-2
lines changed

9 files changed

+134
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
inst/doc
55
docs
66
internal
7+
8+
/.quarto/

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export(use_pkgdown_github_pages)
158158
export(use_posit_cloud_badge)
159159
export(use_proprietary_license)
160160
export(use_r)
161+
export(use_r_universe_badge)
161162
export(use_rcpp)
162163
export(use_rcpp_armadillo)
163164
export(use_rcpp_eigen)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* `use_package(min_version = FALSE)` is treated the same as when `min_version`
1010
is not specified (#2117, @salim-b).
1111

12+
* `use_r_universe_badge()` is a new function that creates a README badge indicating your package is available on [R-universe](https://r-universe.dev) and reporting the latest version (@olivroy, #1883).
13+
1214
* usethis's criteria for recognizing a project have expanded to include (#2133):
1315
- a `.vscode/` directory, which Positron or VS Code might create
1416
- a `_quarto.yml` file, typical of a Quarto project

R/badge.R

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
#' available on CRAN, powered by <https://www.r-pkg.org>
1818
#' * `use_lifecycle_badge()`: badge declares the developmental stage of a
1919
#' package according to <https://lifecycle.r-lib.org/articles/stages.html>.
20+
#' * `use_r_universe_badge()`: `r lifecycle::badge("experimental")` badge
21+
#' indicates what version of your package is available on [R-universe
22+
#' ](https://r-universe.dev/search/). It is assumed that you have already
23+
#' completed the
24+
#' [necessary R-universe setup](https://docs.r-universe.dev/publish/set-up.html).
2025
#' * `use_binder_badge()`: badge indicates that your repository can be launched
2126
#' in an executable environment on <https://mybinder.org/>
2227
#' * `use_posit_cloud_badge()`: badge indicates that your repository can be launched
2328
#' in a [Posit Cloud](https://posit.cloud) project
24-
#' * `use_rscloud_badge()`: `r lifecycle::badge("deprecated")`: Use
29+
#' * `use_rscloud_badge()`: `r lifecycle::badge("deprecated")` Use
2530
#' [use_posit_cloud_badge()] instead.
2631
#'
2732
#' @param badge_name Badge name. Used in error message and alt text
2833
#' @param href,src Badge link and image src
2934
#' @param stage Stage of the package lifecycle. One of "experimental",
3035
#' "stable", "superseded", or "deprecated".
36+
#' @eval param_repo_spec()
3137
#' @seealso [use_github_action()] helps with the setup of various continuous
3238
#' integration workflows, some of which will call these specialized badge
3339
#' helpers.
@@ -144,6 +150,35 @@ use_binder_badge <- function(ref = git_default_branch(), urlpath = NULL) {
144150

145151
invisible(TRUE)
146152
}
153+
#' @rdname badges
154+
#' @export
155+
use_r_universe_badge <- function(repo_spec = NULL) {
156+
check_is_package("use_r_universe_badge()")
157+
pkg <- project_name()
158+
159+
if (is.null(repo_spec)) {
160+
this_env <- current_call()
161+
tryCatch(
162+
github_url <- github_url(),
163+
error = function(e) {
164+
ui_abort(
165+
c(
166+
"x" = "Can't determine the R-universe owner of the {.pkg {pkg}} package.",
167+
"!" = "No GitHub URL found in DESCRIPTION or the Git remotes.",
168+
"i" = "Update the project configuration or provide an explicit {.arg repo_spec}."
169+
),
170+
call = this_env
171+
)
172+
}
173+
)
174+
repo_spec <- parse_repo_url(github_url)[["repo_spec"]]
175+
}
176+
177+
owner <- parse_repo_spec(repo_spec)[["owner"]]
178+
src <- glue("https://{owner}.r-universe.dev/{pkg}/badges/version")
179+
href <- glue("https://{owner}.r-universe.dev/{pkg}")
180+
use_badge("R-universe version", href, src)
181+
}
147182

148183
#' @rdname badges
149184
#' @param url A link to an existing [Posit Cloud](https://posit.cloud)

README.Rmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ knitr::opts_chunk$set(
2222
[![CRAN status](https://www.r-pkg.org/badges/version/usethis)](https://CRAN.R-project.org/package=usethis)
2323
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
2424
[![Codecov test coverage](https://codecov.io/gh/r-lib/usethis/graph/badge.svg)](https://app.codecov.io/gh/r-lib/usethis)
25+
[![R-universe version](https://r-lib.r-universe.dev/usethis/badges/version)](https://r-lib.r-universe.dev/usethis)
2526
<!-- badges: end -->
2627

2728
usethis is a workflow package: it automates repetitive tasks that arise during project setup and development, both for R packages and non-package projects.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ status](https://www.r-pkg.org/badges/version/usethis)](https://CRAN.R-project.or
1212
stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
1313
[![Codecov test
1414
coverage](https://codecov.io/gh/r-lib/usethis/graph/badge.svg)](https://app.codecov.io/gh/r-lib/usethis)
15+
[![R-universe
16+
version](https://r-lib.r-universe.dev/usethis/badges/version)](https://r-lib.r-universe.dev/usethis)
1517
<!-- badges: end -->
1618

1719
usethis is a workflow package: it automates repetitive tasks that arise

man/badges.Rd

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

tests/testthat/_snaps/badge.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,55 @@
77
! `stage` must be one of "experimental", "stable", "superseded", or "deprecated", not "eperimental".
88
i Did you mean "experimental"?
99

10+
# use_r_universe_badge() needs to know the owner
11+
12+
Code
13+
use_r_universe_badge()
14+
Condition
15+
Error in `use_r_universe_badge()`:
16+
x Can't determine the R-universe owner of the {TESTPKG} package.
17+
! No GitHub URL found in DESCRIPTION or the Git remotes.
18+
i Update the project configuration or provide an explicit `repo_spec`.
19+
20+
---
21+
22+
Code
23+
use_r_universe_badge("OWNER_DIRECT/SCRUBBED")
24+
Message
25+
! Can't find a README for the current project.
26+
i See `usethis::use_readme_rmd()` for help creating this file.
27+
i Badge link will only be printed to screen.
28+
[ ] Copy and paste the following lines into 'README':
29+
<!-- badges: start -->
30+
[![R-universe version](https://OWNER_DIRECT.r-universe.dev/{TESTPKG}/badges/version)](https://OWNER_DIRECT.r-universe.dev/{TESTPKG})
31+
<!-- badges: end -->
32+
33+
---
34+
35+
Code
36+
use_r_universe_badge()
37+
Message
38+
! Can't find a README for the current project.
39+
i See `usethis::use_readme_rmd()` for help creating this file.
40+
i Badge link will only be printed to screen.
41+
[ ] Copy and paste the following lines into 'README':
42+
<!-- badges: start -->
43+
[![R-universe version](https://OWNER_DESCRIPTION.r-universe.dev/{TESTPKG}/badges/version)](https://OWNER_DESCRIPTION.r-universe.dev/{TESTPKG})
44+
<!-- badges: end -->
45+
46+
---
47+
48+
Code
49+
use_r_universe_badge()
50+
Message
51+
! Can't find a README for the current project.
52+
i See `usethis::use_readme_rmd()` for help creating this file.
53+
i Badge link will only be printed to screen.
54+
[ ] Copy and paste the following lines into 'README':
55+
<!-- badges: start -->
56+
[![R-universe version](https://OWNER_ORIGIN.r-universe.dev/{TESTPKG}/badges/version)](https://OWNER_ORIGIN.r-universe.dev/{TESTPKG})
57+
<!-- badges: end -->
58+
1059
# use_posit_cloud_badge() handles bad and good input
1160

1261
Code

tests/testthat/test-badge.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ test_that("use_binder_badge() needs a github repository", {
2424
)
2525
})
2626

27+
test_that("use_r_universe_badge() needs to know the owner", {
28+
skip_if_no_git_user()
29+
local_interactive(FALSE)
30+
withr::local_options(usethis.quiet = FALSE)
31+
create_local_package()
32+
33+
expect_snapshot(
34+
error = TRUE,
35+
use_r_universe_badge(),
36+
transform = scrub_testpkg
37+
)
38+
39+
expect_snapshot(
40+
use_r_universe_badge("OWNER_DIRECT/SCRUBBED"),
41+
transform = scrub_testpkg
42+
)
43+
44+
desc::desc_set_urls("https://github.com/OWNER_DESCRIPTION/SCRUBBED")
45+
expect_snapshot(
46+
use_r_universe_badge(),
47+
transform = scrub_testpkg
48+
)
49+
50+
use_git()
51+
use_git_remote("origin", "https://github.com/OWNER_ORIGIN/SCRUBBED.git")
52+
expect_snapshot(
53+
use_r_universe_badge(),
54+
transform = scrub_testpkg
55+
)
56+
})
57+
2758
test_that("use_posit_cloud_badge() handles bad and good input", {
2859
create_local_project()
2960
expect_snapshot(use_posit_cloud_badge(), error = TRUE)

0 commit comments

Comments
 (0)