Skip to content

Commit

Permalink
Add option usethis.destdir (#1033)
Browse files Browse the repository at this point in the history
Fixes #1015
  • Loading branch information
malcolmbarrett committed Mar 20, 2020
1 parent 4cfa070 commit 261ca19
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
13 changes: 10 additions & 3 deletions R/course.R
Expand Up @@ -19,7 +19,10 @@
#' [create_download_url()] help create such URLs for GitHub, DropBox,
#' and Google Drive.
#' @param destdir The new folder is stored here. If `NULL`, defaults to user's
#' Desktop or some other conspicuous place.
#' Desktop or some other conspicuous place. You can also set a default
#' location using the option `usethis.destdir`, e.g.
#' `options(usethis.destdir = "a/good/dir")`, perhaps saved to your
#' `.Rprofile` with [`edit_r_profile()`]
#' @param cleanup Whether to delete the original ZIP file after unpacking its
#' contents. In an interactive setting, `NA` leads to a menu where user can
#' approve the deletion (or decline).
Expand Down Expand Up @@ -54,7 +57,7 @@ NULL
#' launched. Otherwise, the folder is opened in the file manager, e.g. Finder
#' or File Explorer.
#' @export
use_course <- function(url, destdir = NULL) {
use_course <- function(url, destdir = getOption("usethis.destdir")) {
url <- normalize_url(url)
destdir_not_specified <- is.null(destdir)
destdir <- user_path_prep(destdir %||% conspicuous_place())
Expand Down Expand Up @@ -112,7 +115,8 @@ use_zip <- function(url,
#' # as called inside use_course()
#' tidy_download(
#' url, ## after post-processing with normalize_url()
#' # conspicuous_place() = Desktop or home directory or working directory
#' # conspicuous_place() = `getOption('usethis.destdir')` or desktop or home
#' # directory or working directory
#' destdir = destdir %||% conspicuous_place()
#' )
#' ```
Expand Down Expand Up @@ -404,6 +408,9 @@ expand_github <- function(url) {
}

conspicuous_place <- function() {
destdir_opt <- getOption("usethis.destdir")
if (!is.null(destdir_opt)) return(path_tidy(destdir_opt))

Filter(dir_exists, c(
path_home("Desktop"),
path_home(),
Expand Down
5 changes: 4 additions & 1 deletion man/create_from_github.Rd

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

3 changes: 2 additions & 1 deletion man/use_course_details.Rd

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

7 changes: 5 additions & 2 deletions man/zip-utils.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-use-course.R
Expand Up @@ -138,6 +138,14 @@ test_that("conspicuous_place() returns a writeable directory", {
expect_true(file_access(x, mode = "write"))
})

test_that("conspicuous_place() uses `usethis.destdir` when set", {
tdestdir_temp <- path(tempdir(), "destdir_temp")
dir_create(tdestdir_temp)
withr::local_options(list(usethis.destdir = tdestdir_temp))
expect_error_free(x <- conspicuous_place())
expect_equal(path(tdestdir_temp), x)
})

test_that("check_is_zip() errors if MIME type is not 'application/zip'", {
skip("work this into a use_course test")
skip_on_cran()
Expand Down
2 changes: 2 additions & 0 deletions vignettes/articles/usethis-setup.Rmd
Expand Up @@ -65,6 +65,8 @@ Certain options are consulted by usethis and allow you to set personal defaults:
Git. Either "ssh" or "https". See the help for `git_protocol()` for more.
* `usethis.description`: named list of default DESCRIPTION fields for new
packages made with `usethis::create_package()`.
* `usethis.quiet`: if `TRUE`, prevent usethis from printing messages to the console.
* `usethis.destdir`: a default directory to use in `create_from_github()` and `use_course()`

Define any of these options in your `.Rprofile`, which can be opened for editing via `usethis::edit_r_profile()`. Here is example code:

Expand Down

0 comments on commit 261ca19

Please sign in to comment.