Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

rework file opening behavior #205

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions R/filepaths.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
make_filebase <- function(outfile = NULL, infile = NULL) {
if (is.null(outfile)) {
## work inside a new directory, within session temp directory
target_dir <- path_real(dir_create(file_temp("reprex")))
target_dir <- fs::path_real(fs::dir_create(fs::file_temp("reprex")))
## example: /private/var/.../.../.../reprex97d77de2835c/reprex
return(path(target_dir, "reprex"))
}
Expand All @@ -14,7 +14,7 @@ make_filebase <- function(outfile = NULL, infile = NULL) {
if (is.null(infile)) {
## outfile = NA, infile = NULL --> reprex in working directory
## example: reprexbfa165580676
path_file(file_temp("reprex"))
fs::path_file(fs::file_temp("reprex"))
} else {
## outfile = NA, infile = "sthg" --> follow infile's lead
## example: basename_of_infile
Expand Down Expand Up @@ -44,14 +44,14 @@ force_tempdir <- function(path) {
if (is_in_tempdir(path)) {
path
} else {
file_copy(path, path_temp(path_file(path)), overwrite = TRUE)
fs::file_copy(path, fs::path_temp(fs::path_file(path)), overwrite = TRUE)
}
}

is_in_tempdir <- function(path) {
identical(
path_real(path_temp()),
path_common(path_real(c(path, path_temp())))
fs::path_real(fs::path_temp()),
fs::path_common(fs::path_real(c(path, fs::path_temp())))
)
}

Expand Down
26 changes: 14 additions & 12 deletions R/reprex-undo.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#' interpreted as the path to a file containing reprex code. Otherwise,
#' assumed to hold reprex code as character vector. If not provided, the
#' clipboard is consulted for input.
#' @param outfile Optional basename for output file. When `NULL`, no file is
#' left behind. If `outfile = "foo"`, expect an output file in current working
#' @param outfile Optional basename for output file. When `NULL`
#' (default), reprex writes to temp files below the session temp directory.
#' If `outfile = "foo"`, expect an output file in current working
#' directory named `foo_clean.R`. If `outfile = NA`, expect on output file in
#' a location and with basename derived from `input`, if a path, or in
#' current working directory with basename derived from [tempfile()]
Expand Down Expand Up @@ -157,14 +158,11 @@ reprex_undo <- function(input = NULL,
)
comment <- arg_option(comment)

outfile_given <- !is.null(outfile)
infile <- if (where == "path") input else NULL
if (outfile_given) {
files <- make_filenames(make_filebase(outfile, infile), suffix = "clean")
r_file <- files[["r_file"]]
if (would_clobber(r_file)) {
return(invisible())
}
files <- make_filenames(make_filebase(outfile, infile), suffix = "clean")
r_file <- files[["r_file"]]
if (would_clobber(r_file)) {
return(invisible())
}

if (is_md) { ## reprex_invert
Expand All @@ -184,10 +182,14 @@ reprex_undo <- function(input = NULL,
clipr::write_clip(x_out)
message("Clean code is on the clipboard.")
}
if (outfile_given) {
writeLines(x_out, r_file)
message("Writing clean code as R script:\n * ", r_file)

writeLines(x_out, r_file)
message("Writing clean code as R script:\n * ", r_file)

if (interactive()) {
withr::defer(file_edit(r_file))
}

invisible(x_out)
}

Expand Down
2 changes: 1 addition & 1 deletion R/reprex.R
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ reprex <- function(x = NULL,
" * ", reprex_file
)
if (yep("Open the output file for manual copy?")) {
withr::defer(utils::file.edit(reprex_file))
withr::defer(file_edit(reprex_file))
}
}

Expand Down
8 changes: 8 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ collapse <- function(x, sep = "\n") {
backtick <- function(x) encodeString(x, quote = "`")

newline <- function(x) paste0(x, "\n")

file_edit <- function(...) {
if (exists("file.edit", envir = globalenv())) {
get("file.edit", envir = globalenv())(...)
} else {
utils::file.edit(...)
}
}
9 changes: 4 additions & 5 deletions man/reprex.Rd

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

13 changes: 7 additions & 6 deletions man/un-reprex.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ expect_error_free <- function(...) {

## set wd to session temp dir, execute testing code, restore previous wd
temporarily <- function(env = parent.frame()) {
withr::local_dir(path_temp(), .local_envir = env)
withr::local_dir(fs::path_temp(), .local_envir = env)
}

## call during interactive test development to fake being "in tests" and thereby
Expand Down