| 1 |
|
| 2 |
# whether or not we're already trying to restart the session |
| 3 |
the$restarting <- FALSE |
| 4 |
|
| 5 |
renv_restart_request <- function(project = NULL, reason = "", ...) { |
| 6 |
|
| 7 |
project <- renv_project_resolve(project) |
| 8 |
|
| 9 |
# if we're running in RStudio, explicitly open the project |
| 10 |
# if it differs from the current project |
| 11 |
if (renv_rstudio_available()) { |
| 12 |
status <- renv_restart_request_rstudio(project, reason, ...) |
| 13 |
return(invisible(status)) |
| 14 |
} |
| 15 |
|
| 16 |
renv_restart_request_default(project, reason, ...) |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
renv_restart_request_default <- function(project, reason, ...) { |
| 21 |
|
| 22 |
# use 'restart' helper defined by front-end (if any) |
| 23 |
restart <- getOption("restart") |
| 24 |
if (is.function(restart)) |
| 25 |
return(renv_restart_invoke(restart)) |
| 26 |
|
| 27 |
# otherwise, ask the user to restart |
| 28 |
if (interactive()) { |
| 29 |
fmt <- "- %s -- please restart the R session." |
| 30 |
writef(fmt, sprintf(reason, ...)) |
| 31 |
} |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
renv_restart_request_rstudio <- function(project, reason, ...) { |
| 36 |
|
| 37 |
# if we're running tests, don't restart |
| 38 |
if (renv_tests_running()) |
| 39 |
return(renv_restart_request_default(project, reason, ...)) |
| 40 |
|
| 41 |
# if we don't have a tools env, bail |
| 42 |
tools <- catch(as.environment("tools:rstudio")) |
| 43 |
if (inherits(tools, "error")) |
| 44 |
return(renv_restart_request_default(project, reason, ...)) |
| 45 |
|
| 46 |
# if RStudio is too old, use default restart impl |
| 47 |
old <- |
| 48 |
is.null(tools$.rs.getProjectDirectory) || |
| 49 |
is.null(tools$.rs.api.openProject) |
| 50 |
|
| 51 |
if (old) |
| 52 |
return(renv_restart_request_default(project, reason, ...)) |
| 53 |
|
| 54 |
# if the requested project matches the current project, just |
| 55 |
# restart the R session -- but note that we cannot respect |
| 56 |
# the 'restart' option here as the version RStudio uses |
| 57 |
# tries to preserve session state that we need to change. |
| 58 |
# |
| 59 |
# https://github.com/rstudio/renv/issues/1530 |
| 60 |
projdir <- tools$.rs.getProjectDirectory() %||% "" |
| 61 |
if (renv_file_same(projdir, project)) { |
| 62 |
restart <- getOption("renv.restart.function", default = function() { |
| 63 |
tools$.rs.api.executeCommand("restartR", quiet = TRUE) |
| 64 |
}) |
| 65 |
return(renv_restart_invoke(restart)) |
| 66 |
} |
| 67 |
|
| 68 |
# otherwise, explicitly open the new project |
| 69 |
renv_restart_invoke(function() { |
| 70 |
invisible(tools$.rs.api.openProject(project, newSession = FALSE)) |
| 71 |
}) |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
renv_restart_invoke <- function(callback) { |
| 76 |
|
| 77 |
# avoid multiple attempts to restart in a single call, just in case |
| 78 |
if (!the$restarting) { |
| 79 |
the$restarting <- TRUE |
| 80 |
callback() |
| 81 |
} |
| 82 |
|
| 83 |
} |