Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft use_tidy_dependencies() #1423

Merged
merged 9 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
clipr (>= 0.3.0),
crayon,
curl (>= 2.7),
ellipsis,
desc (>= 1.3.0),
fs (>= 1.3.0),
gert (>= 1.0.2),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export(use_tibble)
export(use_tidy_ci)
export(use_tidy_coc)
export(use_tidy_contributing)
export(use_tidy_dependencies)
export(use_tidy_description)
export(use_tidy_eval)
export(use_tidy_github)
Expand Down
8 changes: 7 additions & 1 deletion R/roxygen.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,17 @@ roxygen_remind <- function() {
TRUE
}

roxygen_update_ns <- function() {
roxygen_update_ns <- function(load = is_interactive()) {
ui_done("Writing {ui_path('NAMESPACE')}")
utils::capture.output(
suppressMessages(roxygen2::roxygenise(proj_get(), "namespace"))
)

if (load) {
ui_done("Loading {project_name()}")
pkgload::load_all(quiet = TRUE)
}

TRUE
}

Expand Down
42 changes: 42 additions & 0 deletions R/tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' of the tidyverse conventions as possible, issues a few reminders, and
#' activates the new package.
#'
#' * `use_tidy_dependencies()`: sets up standard dependencies used by all
#' tidyverse packages (except packages that are designed to dependency free).
#'
#' * `use_tidy_description()`: puts fields in standard order and alphabetises
#' dependencies.
#'
Expand Down Expand Up @@ -101,6 +104,45 @@ use_tidy_description <- function() {
invisible(TRUE)
}

#' @export
#' @rdname tidyverse
use_tidy_dependencies <- function() {
check_has_package_doc("use_tidy_dependencies()")

use_dependency("rlang", "Imports")
use_dependency("ellipsis", "Imports")
use_dependency("lifecycle", "Imports")
use_dependency("cli", "Imports")
use_dependency("glue", "Imports")
use_dependency("withr", "Imports")


# standard imports
imports <- any(
roxygen_ns_append("@import rlang"),
roxygen_ns_append("@importFrom glue glue"),
roxygen_ns_append("@importFrom lifecycle deprecated")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we wouldn't import all of lifecycle?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for importing deprecated() is that you use it in arguments; for everything else I think it's better to be explicit.

)
if (imports) {
roxygen_update_ns()
}

# add badges; we don't need the details
ui_silence(use_lifecycle())


# If needed, copy in lightweight purrr compatibility layer
if (!desc::desc(proj_get())$has_dep("purrr")) {
use_directory("R")
url <- "https://raw.githubusercontent.com/r-lib/rlang/master/R/compat-purrr.R"
path <- file_temp()
utils::download.file(url, path, quiet = TRUE)
write_over("R/compat-purrr.R", read_utf8(path))
}

invisible()
}

#' @export
#' @rdname tidyverse
use_tidy_eval <- function() {
Expand Down
32 changes: 12 additions & 20 deletions R/use_import_from.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,15 @@ use_import_from <- function(package, fun, load = is_interactive()) {
}
check_is_package("use_import_from()")
check_uses_roxygen("use_import_from()")
check_has_package_doc("use_import_from()")

if (!check_has_package_doc()) {
return(invisible(FALSE))
}
purrr::walk2(package, fun, check_fun_exists)

use_dependency(package, "Imports")
changed <- roxygen_ns_append(glue("@importFrom {package} {fun}"))

if (changed) {
roxygen_update_ns()

if (load) {
ui_done("Loading {project_name()}")
pkgload::load_all(quiet = TRUE)
}
roxygen_update_ns(load)
}

invisible(changed)
Expand All @@ -56,23 +49,22 @@ check_fun_exists <- function(package, fun) {
ui_stop("Can't find {ui_code(name)}")
}

check_has_package_doc <- function() {
check_has_package_doc <- function(whos_asking) {
if (has_package_doc()) {
return(invisible(TRUE))
}

if (!is_interactive()) {
return(invisible(FALSE))
}

if (ui_yeah("
{ui_code('use_import_from()')} requires \\
package-level documentation. Would you like to add \\
it now?")) {
msg <- c(
"{ui_code(whos_asking)} requires package-level documentation.",
"Would you like to add it now?"
)
if (is_interactive() && ui_yeah(msg)) {
use_package_doc()
} else {
ui_todo("Run {ui_code('use_package_doc()')}")
return(invisible(FALSE))
ui_stop(c(
"{ui_code(whos_asking)} requires package docs",
"You can add it by running {ui_code('use_package_doc()')}"
))
}

invisible(TRUE)
Expand Down
3 changes: 2 additions & 1 deletion R/usethis-package.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#' @keywords internal
#' @importFrom glue glue glue_collapse glue_data
#' @importFrom glue glue_collapse glue_data
#' @importFrom purrr map map_chr map_lgl map_int
#' @import fs
#' @import rlang
"_PACKAGE"

## usethis namespace: start
#' @importFrom glue glue
#' @importFrom lifecycle deprecated
## usethis namespace: end
NULL
5 changes: 5 additions & 0 deletions man/tidyverse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/_snaps/tidyverse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# use_tidy_dependencies() isn't overly informative

Code
use_tidy_dependencies()
Message <message>
v Adding 'rlang' to Imports field in DESCRIPTION
v Adding 'ellipsis' to Imports field in DESCRIPTION
v Adding 'lifecycle' to Imports field in DESCRIPTION
v Adding 'cli' to Imports field in DESCRIPTION
v Adding 'glue' to Imports field in DESCRIPTION
v Adding 'withr' to Imports field in DESCRIPTION
v Adding '@import rlang' to 'R/tidydeps-package.R'
v Adding '@importFrom glue glue' to 'R/tidydeps-package.R'
v Adding '@importFrom lifecycle deprecated' to 'R/tidydeps-package.R'
v Writing 'NAMESPACE'
v Writing 'R/compat-purrr.R'

8 changes: 8 additions & 0 deletions tests/testthat/test-tidyverse.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ test_that("use_tidy_description() alphabetises dependencies and remotes", {
expect_gt(grep("r\\-lib\\/styler", desc), grep("jimhester\\/lintr", desc))
})

test_that("use_tidy_dependencies() isn't overly informative", {
create_local_package(fs::path_temp("tidydeps"))
use_package_doc()
withr::local_options(usethis.quiet = FALSE)

expect_snapshot(use_tidy_dependencies())
})

test_that("use_tidy_eval() inserts the template file and Imports rlang", {
skip_if_not_installed("roxygen2")

Expand Down