forked from r-lib/usethis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And generalise block_append() to uniquify and sort. Fixes r-lib#1361. Co-authored-by: Hadley Wickham <[email protected]>
- Loading branch information
1 parent
aa81353
commit 4819d19
Showing
15 changed files
with
232 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ Suggests: | |
knitr, | ||
magick, | ||
mockr, | ||
pkgload, | ||
rmarkdown, | ||
roxygen2, | ||
spelling (>= 1.2), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' Import a function from another package | ||
#' | ||
#' @description | ||
#' `use_import_from()` imports a function from another package by adding the | ||
#' roxygen2 `@importFrom` tag to the package-level documentation (which can be | ||
#' created with [`use_package_doc()`]). Importing a function from another | ||
#' package allows you to refer to it without a namespace (e.g., `fun()` instead | ||
#' of `package::fun()`). | ||
#' | ||
#' `use_import_from()` also re-documents the NAMESPACE, and re-load the current | ||
#' package. This ensures that `fun` is immediately available in your development | ||
#' session. | ||
#' | ||
#' @param package Package name | ||
#' @param fun A vector of function names | ||
#' @param load Logical. Re-load with [`pkgload::load_all()`]? | ||
#' @return | ||
#' Invisibly, `TRUE` if the package document has changed, `FALSE` if not. | ||
#' @export | ||
#' @examples | ||
#' \dontrun{ | ||
#' use_import_from("usethis", "ui_todo") | ||
#' } | ||
use_import_from <- function(package, fun, load = is_interactive()) { | ||
if (!is_string(package)) { | ||
ui_stop("{ui_code('package')} must be a single string") | ||
} | ||
check_is_package("use_import_from()") | ||
check_uses_roxygen("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) | ||
} | ||
} | ||
|
||
invisible(changed) | ||
} | ||
|
||
check_fun_exists <- function(package, fun) { | ||
if (exists(fun, envir = asNamespace(package))) { | ||
return() | ||
} | ||
name <- paste0(package, "::", fun, "()") | ||
ui_stop("Can't find {ui_code(name)}") | ||
} | ||
|
||
check_has_package_doc <- function() { | ||
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?")) { | ||
use_package_doc() | ||
} else { | ||
ui_todo("Run {ui_code('use_package_doc()')}") | ||
return(invisible(FALSE)) | ||
} | ||
|
||
invisible(TRUE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
#' @keywords internal | ||
"_PACKAGE" | ||
|
||
# The following block is used by usethis to automatically manage | ||
# roxygen namespace tags. Modify with care! | ||
## usethis namespace: start | ||
## usethis namespace: end | ||
NULL |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# use_import_from() adds one line for each function | ||
|
||
Code | ||
roxygen_ns_show() | ||
Output | ||
[1] "#' @importFrom tibble deframe" "#' @importFrom tibble enframe" | ||
[3] "#' @importFrom tibble tibble" | ||
|
||
# use_import_from() generates helpful errors | ||
|
||
Code | ||
use_import_from(1) | ||
Error <usethis_error> | ||
`package` must be a single string | ||
Code | ||
use_import_from(c("tibble", "rlang")) | ||
Error <usethis_error> | ||
`package` must be a single string | ||
Code | ||
use_import_from("tibble", "pool_noodle") | ||
Error <usethis_error> | ||
Can't find `tibble::pool_noodle()` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
test_that("block_append() only writes unique lines", { | ||
|
||
path <- withr::local_tempfile() | ||
writeLines(block_create(), path) | ||
|
||
block_append("---", c("x", "y"), path) | ||
block_append("---", c("y", "x"), path) | ||
expect_equal(block_show(path), c("x", "y")) | ||
}) | ||
|
||
test_that("block_append() can sort, if requested", { | ||
path <- withr::local_tempfile() | ||
writeLines(block_create(), path) | ||
|
||
block_append("---", c("z", "y"), path) | ||
block_append("---", "x", path, sort = TRUE) | ||
expect_equal(block_show(path), c("x", "y", "z")) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
test_that("use_import_from() imports the related package & adds line to package doc", { | ||
create_local_package() | ||
use_package_doc() | ||
use_import_from("tibble", "tibble") | ||
|
||
expect_equal(trimws(desc::desc_get("Imports", proj_get()))[[1]], "tibble") | ||
expect_equal(roxygen_ns_show(), "#' @importFrom tibble tibble") | ||
}) | ||
|
||
test_that("use_import_from() adds one line for each function", { | ||
create_local_package() | ||
use_package_doc() | ||
use_import_from("tibble", c("tibble", "enframe", "deframe")) | ||
|
||
expect_snapshot(roxygen_ns_show()) | ||
}) | ||
|
||
test_that("use_import_from() generates helpful errors", { | ||
create_local_package() | ||
use_package_doc() | ||
|
||
expect_snapshot(error = TRUE, { | ||
use_import_from(1) | ||
use_import_from(c("tibble", "rlang")) | ||
|
||
use_import_from("tibble", "pool_noodle") | ||
}) | ||
}) |