Skip to content

Commit 4802ba9

Browse files
add note about dependencies
1 parent 8062677 commit 4802ba9

File tree

4 files changed

+116
-56
lines changed

4 files changed

+116
-56
lines changed

Diff for: R/setup.R

+67-48
Original file line numberDiff line numberDiff line change
@@ -237,71 +237,79 @@ upstream_repo_url_is_outdated <- function() {
237237
#' * additional-deps-roxygenize: Code to paste into
238238
#' `.pre-commit-config.yaml` for the additional dependencies required by
239239
#' the roxygenize hook.
240+
#' * additional-deps-lintr: Code to paste into
241+
#' `.pre-commit-config.yaml` for the additional dependencies required by
242+
#' the lintr hook if you use `--load-package`.
240243
#' @param snippet Name of the snippet.
241244
#' @param open Whether or not to open the .pre-commit-config.yaml. The default
242245
#' is `TRUE` when working in RStudio. Otherwise, we recommend manually opening
243246
#' the file.
244247
#' @inheritParams fallback_doc
245248
#' @export
246-
snippet_generate <- function(snippet = "",
249+
snippet_generate <- function(snippet = "additional-deps-roxygenize",
247250
open = rstudioapi::isAvailable(),
248251
root = here::here()) {
249-
rlang::arg_match(snippet, c("additional-deps-roxygenize"))
250-
if (snippet == "additional-deps-roxygenize") {
251-
rlang::inform(paste(
252-
"Generating snippet using CRAN versions. If you need another source,",
253-
"specify with syntax that `renv::install()` understands (see examples in",
254-
"help file).",
255-
"\n"
256-
))
257-
deps <- desc::desc_get_deps()
258-
hard_dependencies <- deps[(deps$type %in% c("Depends", "Imports")), "package"] %>%
259-
setdiff("R")
260-
if (length(hard_dependencies) < 1) {
261-
cli::cli_alert_success(paste0(
262-
"According to {.code DESCRIPTION}`, there are no hard dependencies of ",
263-
"your package. You are set."
264-
))
265-
return()
266-
}
267-
hard_dependencies %>%
268-
snippet_generate_impl_additional_deps_roxygenize() %>%
269-
cat(sep = "")
270-
cat("\n")
271-
cli::cli_ul(paste0(
272-
"Replace the `id: roxygenize` key in `.pre-commit-config.yaml` with the ",
273-
"above code."
274-
))
275-
cli::cli_alert_info(paste0(
276-
"Note that CI services like {.url pre-commit.ci} have build-time ",
277-
"restrictions and installing the above dependencies may exceed those, ",
278-
"resulting in a timeout. In addition, system dependencies are not ",
279-
"supported for {.url pre-commit.ci}. See ",
280-
'{.code vignette("ci", package = "precommit")} for details and solutions.'
252+
snippet_generator <- if(snippet == 'additional-deps-roxygenize') {
253+
snippet_generate_impl_additional_deps_roxygenize
254+
} else if (snippet == 'additional-deps-lintr') {
255+
snippet_generate_impl_additional_deps_lintr
256+
} else {
257+
rlang::abort(paste0('Snippet "', snippet, '" not supported'))
258+
}
259+
rlang::inform(paste(
260+
"Generating snippet using CRAN versions. If you need another source,",
261+
"specify with syntax that `renv::install()` understands (see examples in",
262+
"help file).",
263+
"\n"
264+
))
265+
deps <- desc::desc_get_deps()
266+
hard_dependencies <- deps[(deps$type %in% c("Depends", "Imports")), "package"] %>%
267+
setdiff("R")
268+
if (length(hard_dependencies) < 1) {
269+
cli::cli_alert_success(paste0(
270+
"According to {.code DESCRIPTION}`, there are no hard dependencies of ",
271+
"your package. You are set."
281272
))
282-
remote_deps <- rlang::try_fetch(
283-
desc::desc_get_field("Remotes"),
284-
error = function(e) character()
285-
)
286-
if (length(remote_deps) > 0) {
287-
rlang::warn(paste0(
288-
"It seems you have remote dependencies in your `DESCRIPTION`. You ",
289-
"need to edit the above list manually to match the syntax `renv::install()` ",
290-
"understands, i.e. if you have in your `DESCRIPTION`
273+
return()
274+
}
275+
276+
hard_dependencies %>%
277+
snippet_generator() %>%
278+
cat(sep = "")
279+
cat("\n")
280+
cli::cli_ul(paste0(
281+
"Replace the `id: roxygenize` key in `.pre-commit-config.yaml` with the ",
282+
"above code."
283+
))
284+
cli::cli_alert_info(paste0(
285+
"Note that CI services like {.url pre-commit.ci} have build-time ",
286+
"restrictions and installing the above dependencies may exceed those, ",
287+
"resulting in a timeout. In addition, system dependencies are not ",
288+
"supported for {.url pre-commit.ci}. See ",
289+
'{.code vignette("ci", package = "precommit")} for details and solutions.'
290+
))
291+
remote_deps <- rlang::try_fetch(
292+
desc::desc_get_field("Remotes"),
293+
error = function(e) character()
294+
)
295+
if (length(remote_deps) > 0) {
296+
rlang::warn(paste0(
297+
"It seems you have remote dependencies in your `DESCRIPTION`. You ",
298+
"need to edit the above list manually to match the syntax `renv::install()` ",
299+
"understands, i.e. if you have in your `DESCRIPTION`
291300
292301
Imports:
293-
tidyr
302+
tidyr
294303
Remotes:
295-
tidyverse/tidyr@2fd80d5
304+
tidyverse/tidyr@2fd80d5
296305
297306
You need in your `.pre-commit-config.yaml`
298307
299-
additional_dependencies:
300-
- tidyverse/tidyr@2fd80d5
308+
additional_dependencies:
309+
- tidyverse/tidyr@2fd80d5
301310
302311
"
303-
))
304-
}
312+
))
305313
}
306314
if (open) {
307315
precommit::open_config(root)
@@ -318,3 +326,14 @@ snippet_generate_impl_additional_deps_roxygenize <- function(packages, with_vers
318326
# roxygen requires loading pkg -> add dependencies from DESCRIPTION
319327
additional_dependencies:\n", out)
320328
}
329+
330+
snippet_generate_impl_additional_deps_lintr <- function(packages, with_version = FALSE) {
331+
out <- paste0(
332+
" - ", packages, "\n",
333+
collapse = ""
334+
) %>%
335+
sort()
336+
paste0(" - id: lintr
337+
# lintr requires loading pkg -> add dependencies from DESCRIPTION
338+
additional_dependencies:\n", out)
339+
}

Diff for: man/snippet_generate.Rd

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: tests/testthat/test-setup.R

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
test_that("snippet generation works", {
1+
test_that("snippet generation works for roxygen", {
22
local_test_setup(
33
git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE
44
)
@@ -27,6 +27,36 @@ test_that("snippet generation works", {
2727
)
2828
})
2929

30+
31+
test_that("snippet generation works for lintr", {
32+
local_test_setup(
33+
git = FALSE, use_precommit = FALSE, package = TRUE, install_hooks = FALSE
34+
)
35+
usethis::use_package("R", "Depends", "3.6.0")
36+
expect_error(
37+
out <- capture_output(snippet_generate("additional-deps-lintr")),
38+
NA,
39+
)
40+
expect_equal(out, "")
41+
usethis::use_package("styler")
42+
expect_error(
43+
out <- capture_output(snippet_generate("additional-deps-lintr")),
44+
NA,
45+
)
46+
47+
expect_match(
48+
out, " - id: lintr\n.* - styler\n$",
49+
)
50+
desc::desc_set("Remotes", "r-lib/styler")
51+
expect_warning(
52+
out <- capture_output(snippet_generate("additional-deps-roxygenize")),
53+
"you have remote dependencies "
54+
)
55+
expect_match(
56+
out, " - id: roxygenize\n.* - styler\n$",
57+
)
58+
})
59+
3060
test_that("snippet generation only includes hard dependencies", {
3161
local_test_setup(
3262
git = FALSE, use_precommit = FALSE, package = TRUE,

Diff for: vignettes/available-hooks.Rmd

+14-6
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,21 @@ you should either make sure the version you use when you call {roxygen2}
270270
interactively matches the one from in {precommit} or simply not run
271271
{roxygen2} manually.
272272

273-
If you specify additional roclets through the `Roxygen:` field in
274-
`DESCRIPTION`, e.g. from [{pkgapi}](https://github.com/r-lib/pkgapi) you
275-
must specify the dependencies explicitly such that `renv::install()`
276-
understands it, e.g.
273+
Note that your package's dependencies must be listed in
274+
`additional_dependencies:` in your `.pre-commit-config.yaml` as shown
275+
below. You can generate these for easy c/p with
276+
`snippet_generate("additional-deps-roxygenize")` .
277+
278+
Similarly, if you specify additional roclets through the `Roxygen:`
279+
field in `DESCRIPTION`, e.g. from
280+
[{pkgapi}](https://github.com/r-lib/pkgapi) you must specify the
281+
dependencies explicitly such that `renv::install()` understands it.
277282

278283
```
279284
id: roxygenize
280285
additional_dependencies:
281286
- r-lib/pkgapi
287+
- dplyr
282288
```
283289

284290
**Arguments**
@@ -371,8 +377,10 @@ should set this with the field `verbose: true`.
371377
- `load-package`: When linting a package, {lintr} uses
372378
`pkgload::load_all()` to load the package before running
373379
`lintr::lint()`. Note that you need to list all dependencies of the
374-
package under `additional_dependencies:` in your
375-
`.pre-commit-config.yaml`.
380+
package under `additional_dependencies:` for the `lintr:`hook in
381+
your `.pre-commit-config.yaml`. You can generate the code snip o for
382+
c/p with `snippet_generate("additional-deps-lintr")`. This argument
383+
was added in 0.4.3.9005.
376384

377385
The `.lintr` config file as documented in the [`.lintr`
378386
documentation](https://github.com/r-lib/lintr#project-configuration) is

0 commit comments

Comments
 (0)