Skip to content

Commit

Permalink
renv updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jayhesselberth committed Jun 22, 2023
1 parent 8603b1c commit 604567e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 64 deletions.
8 changes: 2 additions & 6 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2495,18 +2495,14 @@
},
"renv": {
"Package": "renv",
"Version": "0.17.3-84",
"Version": "0.17.3-88",
"Source": "GitHub",
"RemoteType": "github",
"RemoteHost": "api.github.com",
"RemoteUsername": "rstudio",
"RemoteRepo": "renv",
"RemoteRef": "main",
"RemoteSha": "1a0dd23ff61f7e0d2a3dc7a6e7b7e36a4b6c5de0",
"Requirements": [
"utils"
],
"Hash": "9d2265127154a72f4bf918b6f67af77e"
"RemoteSha": "bfb5356541694c987232ae7ceedac992efbdbdca"
},
"reprex": {
"Package": "reprex",
Expand Down
150 changes: 92 additions & 58 deletions renv/activate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local({

# the requested version of renv
version <- "1a0dd23ff61f7e0d2a3dc7a6e7b7e36a4b6c5de0"
version <- "bfb5356541694c987232ae7ceedac992efbdbdca"

# the project directory
project <- getwd()
Expand Down Expand Up @@ -59,10 +59,6 @@ local({
unloadNamespace("renv")

# load bootstrap tools
startswith <- function(string, prefix) {
substring(string, 1, nchar(prefix)) == prefix
}

`%||%` <- function(x, y) {
if (is.null(x)) y else x
}
Expand All @@ -80,30 +76,61 @@ local({

}

header <- function(label,
...,
prefix = "#",
suffix = "-",
n = min(getOption("width"), 78))
{
label <- sprintf(label, ...)
n <- max(n - nchar(label) - nchar(prefix) - 2L, 8L)
if (n <= 0)
return(paste(prefix, label))

tail <- paste(rep.int(suffix, n), collapse = "")
paste0(prefix, " ", label, " ", tail)

}

startswith <- function(string, prefix) {
substring(string, 1, nchar(prefix)) == prefix
}

bootstrap <- function(version, library) {

# load failed; inform user we're about to bootstrap
catf("Bootstrapping renv %s:", version)
friendly <- version
if (nchar(friendly) >= 40)
friendly <- sprintf("[rstudio/renv@%s]", substring(version, 1L, 7L))

section <- header(sprintf("Bootstrapping renv %s", friendly))
catf(section)

# attempt to download renv
catf("- Downloading renv ... ", appendLF = FALSE)
withCallingHandlers(
tarball <- renv_bootstrap_download(version),
error = function(err) {
catf("FAILED")
stop("failed to download:\n", conditionMessage(err))
}
)
catf("OK")
on.exit(unlink(tarball), add = TRUE)

# now attempt to install
catf("- Installing renv ... ", appendLF = FALSE)
withCallingHandlers(
status <- renv_bootstrap_install(version, tarball, library),
error = function(err) {
catf("FAILED")
stop("failed to install:\n", conditionMessage(err))
}
)
catf("OK")

# add empty line to break up bootstrapping from normal output
catf("\n")
catf("")

return(invisible())
}
Expand Down Expand Up @@ -260,8 +287,6 @@ local({
type <- spec$type
repos <- spec$repos

catf("* Downloading %s from <%s> ... ", type, repos, appendLF = FALSE)

baseurl <- utils::contrib.url(repos = repos, type = type)
ext <- if (identical(type, "source"))
".tar.gz"
Expand All @@ -278,13 +303,10 @@ local({
condition = identity
)

if (inherits(status, "condition")) {
catf("FAILED")
if (inherits(status, "condition"))
return(FALSE)
}

# report success and return
catf("OK")
destfile

}
Expand Down Expand Up @@ -341,23 +363,18 @@ local({
urls <- file.path(repos, "src/contrib/Archive/renv", name)
destfile <- file.path(tempdir(), name)

catf("* Downloading from archive ... ", appendLF = FALSE)

for (url in urls) {

status <- tryCatch(
renv_bootstrap_download_impl(url, destfile),
condition = identity
)

if (identical(status, 0L)) {
catf("OK")
if (identical(status, 0L))
return(destfile)
}

}

catf("FAILED")
return(FALSE)

}
Expand Down Expand Up @@ -389,7 +406,7 @@ local({

}

catf("* Using local tarball '%s'", tarball)
catf("- Using local tarball '%s'.", tarball)
tarball

}
Expand All @@ -416,8 +433,6 @@ local({
on.exit(do.call(base::options, saved), add = TRUE)
}

catf("* Downloading from GitHub ... ", appendLF = FALSE)

url <- file.path("https://api.github.com/repos/rstudio/renv/tarball", version)
name <- sprintf("renv_%s.tar.gz", version)
destfile <- file.path(tempdir(), name)
Expand All @@ -427,14 +442,11 @@ local({
condition = identity
)

if (!identical(status, 0L)) {
catf("FAILED")
if (!identical(status, 0L))
return(FALSE)
}

renv_bootstrap_download_augment(destfile)

catf("OK")
return(destfile)

}
Expand Down Expand Up @@ -507,42 +519,36 @@ local({
renv_bootstrap_install <- function(version, tarball, library) {

# attempt to install it into project library
catf("* Installing ... ", appendLF = FALSE)
dir.create(library, showWarnings = FALSE, recursive = TRUE)

output <- renv_bootstrap_install_impl(library, tarball)

# check for successful install
status <- attr(output, "status")
if (is.numeric(status) && !identical(status, 0L)) {
catf("FAILED")

header <- "Error installing renv:"
lines <- paste(rep.int("=", nchar(header)), collapse = "")
text <- c(header, lines, output)
writeLines(text, con = stdout())
stop("Failed")
} else {
catf("OK")
}
if (is.null(status) || identical(status, 0L))
return(status)

status
# an error occurred; report it
header <- "installation of renv failed"
lines <- paste(rep.int("=", nchar(header)), collapse = "")
text <- paste(c(header, lines, output), collapse = "\n")
stop(text)

}

renv_bootstrap_install_impl <- function(library, tarball) {

# invoke using system2 so we can capture and report output
bin <- R.home("bin")
exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"
r <- file.path(bin, exe)
R <- file.path(bin, exe)

args <- c(
"--vanilla", "CMD", "INSTALL", "--no-multiarch",
"-l", shQuote(path.expand(library)),
shQuote(path.expand(tarball))
)

system2(r, args, stdout = TRUE, stderr = TRUE)
system2(R, args, stdout = TRUE, stderr = TRUE)

}

Expand Down Expand Up @@ -964,6 +970,35 @@ local({
}
}

renv_bootstrap_run <- function(version, libpath) {

# perform bootstrap
bootstrap(version, libpath)

# exit early if we're just testing bootstrap
if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA)))
return(TRUE)

# try again to load
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
return(renv::load())
}

# failed to download or load renv; warn the user
msg <- c(
"Failed to find an renv installation: the project will not be loaded.",
"Use `renv::activate()` to re-initialize the project."
)

warning(paste(msg, collapse = "\n"), call. = FALSE)

}


renv_bootstrap_in_rstudio <- function() {
commandArgs()[[1]] == "RStudio"
}

renv_json_read <- function(file = NULL, text = NULL) {

jlerr <- NULL
Expand Down Expand Up @@ -1106,24 +1141,23 @@ local({
if (renv_bootstrap_load(project, libpath, version))
return(TRUE)

# perform bootstrap
bootstrap(version, libpath)
if (renv_bootstrap_in_rstudio()) {
setHook("rstudio.sessionInit", function(...) {
renv_bootstrap_run(version, libpath)

# exit early if we're just testing bootstrap
if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA)))
return(TRUE)

# try again to load
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
return(renv::load())
# Work around buglet in RStudio if hook uses readline
tryCatch(
{
tools <- as.environment("tools:rstudio")
tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE)
},
error = function(cnd) {}
)
})
} else {
renv_bootstrap_run(version, libpath)
}

# failed to download or load renv; warn the user
msg <- c(
"Failed to find an renv installation: the project will not be loaded.",
"Use `renv::activate()` to re-initialize the project."
)

warning(paste(msg, collapse = "\n"), call. = FALSE)
invisible()

})

0 comments on commit 604567e

Please sign in to comment.