From 7864a5796adf69fed7718ed1d9990f0a322558a1 Mon Sep 17 00:00:00 2001 From: Jakob Gepp Date: Tue, 6 Feb 2024 16:29:15 +0100 Subject: [PATCH] bump to 1.4.2 - fix error in get_network with duplicated inner functions names --- .github/workflows/lints.yml | 6 +- DESCRIPTION | 4 +- NEWS.md | 10 + R/get_network.R | 131 ++++++++- README.md | 2 +- man/get_network.Rd | 5 +- misc/update_DESCRIPTION_NEWS.R | 8 + renv.lock | 497 ++++++++++++++++++++------------ renv/.gitignore | 1 + renv/activate.R | 508 ++++++++++++++++++++++++--------- renv/settings.json | 19 ++ 11 files changed, 861 insertions(+), 330 deletions(-) create mode 100644 renv/settings.json diff --git a/.github/workflows/lints.yml b/.github/workflows/lints.yml index 1c2939c..5e5310e 100644 --- a/.github/workflows/lints.yml +++ b/.github/workflows/lints.yml @@ -6,11 +6,13 @@ jobs: runs-on: macOS-latest if: "!contains(github.event.head_commit.message, 'skip ci')" env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} LINTR_COMMENT_BOT: true + steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-r@master + - uses: r-lib/actions/setup-r@v2 - name: Query dependencies run: | diff --git a/DESCRIPTION b/DESCRIPTION index 152570c..48f54be 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: helfRlein Title: R Helper Functions -Version: 1.4.1 +Version: 1.4.2 Authors@R: c( person("Jakob", "Gepp", , "jakob.gepp@statworx.com", role = c("cre", "aut")), person("Daniel", "Luettgau", , "daniel.luettgau@statworx.com", role = "aut"), @@ -35,4 +35,4 @@ Imports: igraph (>= 1.1.2), rstudioapi, utils -Date: 2023-07-23 +Date: 2024-02-06 diff --git a/NEWS.md b/NEWS.md index 470809a..311871e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,13 @@ +## version 1.4.2 + +--- + + +### Bugfixes + +- fix error in get_network with duplicated inner functions names + + ## version 1.4.1 --- diff --git a/R/get_network.R b/R/get_network.R index 7be582b..de38163 100644 --- a/R/get_network.R +++ b/R/get_network.R @@ -18,6 +18,7 @@ #' @param exclude a vector with folder's or function's names, that are excluded #' from the network creation. This is done by a regex, so it will remove #' everything that contains these words. +#' @param verbose a boolean setting the debugging prints. #' #' @return #' Returns an object with the adjacency matrix \code{$matrix} and @@ -36,6 +37,7 @@ #' #' TODO: maybe return plot #' +#' #' @examples #' \dontrun{ #' net <- get_network(dir = "R/", simplify = TRUE) @@ -53,10 +55,11 @@ get_network <- function(dir = NULL, simplify = FALSE, all_scripts = NULL, use_internals = TRUE, - exclude = NULL) { + exclude = NULL, + verbose = FALSE) { # check if dir exists - if (!is.null(dir) && !dir.exists(dir)) { + if (!is.null(dir) && all(!dir.exists(dir))) { stop(paste0(dir, " does not exists")) } @@ -68,7 +71,7 @@ get_network <- function(dir = NULL, full.names = TRUE) - # removing files with exlude input + # removing files with exclude input if (!is.null(exclude)) { keep <- !grepl(pattern = paste0("(", paste0(exclude, collapse = ")|("), @@ -81,7 +84,31 @@ get_network <- function(dir = NULL, stop("no files with the given pattern") } - folder <- dirname(gsub(paste0(dir, "/"), "", files_path)) + common_base_path <- function(paths) { + # Split each path into its components + split_paths <- strsplit(paths, "/") + + # Find the common path + common_path <- Reduce(function(x, y) { + # Get the length of the shorter vector + min_length <- min(length(x), length(y)) + # Only compare the elements up to the length of the shorter vector + common <- x[seq_len(min_length)] == y[seq_len(min_length)] + # If there's a FALSE in common, only keep the elements before it + if (any(!common)) x[seq_len(which(!common)[1] - 1)] + else x + }, split_paths) + + # Combine the common path components back into a single string + common_path <- paste(common_path, collapse = "/") + + return(common_path) + } + + dir_base <- common_base_path(paths = dir) + + + folder <- dirname(gsub(paste0(dir_base, "/"), "", files_path)) # get all scripts all_scripts <- lapply(files_path, readLines, warn = FALSE) @@ -98,7 +125,12 @@ get_network <- function(dir = NULL, folder <- rep(".", length(all_scripts)) } - # check for emtpy scripts + if (verbose) { + print(paste0("found ", length(all_scripts), " scripts")) + print("length of folder: ", length(folder)) + } + + # check for empty scripts indx <- sapply(all_scripts, length) == 0 if (any(indx)) { warning(paste0("removing empty scritps: ", @@ -115,8 +147,12 @@ get_network <- function(dir = NULL, } # remove method / functions that start with [ - # otherwise the regex will be messed up later + # otherwise the regular expression will be messed up later keep <- !startsWith(names(all_scripts), "[") + if (verbose) { + print(paste0("remove method / functions that start with [: ", + sum(!keep))) + } all_scripts <- all_scripts[keep] folder <- folder[keep] @@ -155,6 +191,17 @@ get_network <- function(dir = NULL, scripts <- all_scripts[-index_functions] folder_scripts <- folder[-index_functions] + if (verbose) { + print(c( + paste0("found ", length(index_functions), + " scripts containing: '", + paste0(variations, collapse = "', '"), "'"), + paste0("main_functions: ", length(main_functions), + " in folder_main: ", length(folder_main)), + paste0("scripts: ", length(scripts), + " in folder_scripts: ", length(folder_scripts)) + )) + } # get subfunctions getsubindex <- function(funlist, @@ -221,6 +268,7 @@ get_network <- function(dir = NULL, sub_index <- tmp$sub_index internal <- tmp$internal + sub_functions <- mapply(function(i, s) { lapply(seq_len(nrow(s)), function(t) i[s[t, 1]:s[t, 2]]) @@ -229,9 +277,17 @@ get_network <- function(dir = NULL, sub_functions <- do.call("c", sub_functions) # folder for sub_functions - folder_index <- which(names(sub_index) %in% names(main_functions)) + folder_index <- which(names(main_functions) %in% names(sub_index)) folder_sub <- rep(folder_main[folder_index], sapply(sub_index, nrow)) + if (verbose) { + print(paste0( + "check length: ", length(sub_functions), " sub-functions in ", + length(folder_sub), " folders" + + )) + } + def_sub_functions <- unlist(lapply(seq_along(sub_functions), function(x) sub_functions[[x]][1])) @@ -253,11 +309,21 @@ get_network <- function(dir = NULL, all_folder <- folder_main } + if (verbose) { + print(paste0( + "check length: ", length(all_functions), " all_functions in ", + length(all_folder), " all_folder" + )) + } + # remove duplicates index <- !duplicated(all_functions) all_functions <- all_functions[index] all_folder <- all_folder[index] + if (verbose) { + print(paste0("number of duplicated functions: ", sum(!index))) + } dup_names <- duplicated(names(all_functions)) if (any(dup_names)) { @@ -289,6 +355,13 @@ get_network <- function(dir = NULL, all_files <- c(all_functions, scripts) all_folder <- c(all_folder, folder_scripts) + if (verbose) { + print(paste0( + "check length: ", length(all_files), " all_files in ", + length(all_folder), " all_folder" + )) + } + # check if there are functions if (length(all_files) == 0) { warning("no functions found") @@ -362,6 +435,13 @@ get_network <- function(dir = NULL, function(x) clean_functions[[x]][keep_lines[[x]]]) names(clean_functions) <- names(all_files) + if (verbose) { + print(paste0( + "check length: ", length(clean_functions), " clean_functions in ", + length(all_folder), " all_folder" + )) + } + # remove duplicated names dub_rows <- !duplicated(names(clean_functions)) if (!all(dub_rows)) { @@ -383,25 +463,53 @@ get_network <- function(dir = NULL, network <- as.data.frame(do.call(rbind, network)) + if (verbose) { + print(paste0( + "initial network has ", nrow(network), " rows and ", + ncol(network), " cols" + )) + } + # adjust networks rows and columns names(network) <- gsub("\\\\\\(", "", names(network)) - new_collumns <- rownames(network)[ + new_columns <- rownames(network)[ which(!rownames(network) %in% colnames(network))] new_rows <- colnames(network)[ which(!colnames(network) %in% rownames(network))] - network[, new_collumns] <- 0 + network[, new_columns] <- 0 network[new_rows, ] <- 0 network <- network[rownames(network)] + if (verbose) { + print(c( + paste0("adding ", length(new_rows), " new rows"), + paste0("adding ", length(new_columns), " new cols"), + paste0("adjusted network has ", nrow(network), " rows and ", + ncol(network), " cols") + )) + } + + # adjust lines, folders old_names <- names(lines) lines <- c(lines, rep(0, length(new_rows))) names(lines) <- c(old_names, new_rows) + if (verbose) { + print(paste0("check length: ", length(lines), " lines")) + } + + + # remove duplicated functions within def_functions2 + if (sum(duplicated(def_functions2)) > 0 && verbose) { + print(paste0("There are ", sum(duplicated(def_functions2)), + " inner functions with the same name.", + " Keeping only the first")) + } tmp_index <- unlist(lapply( new_rows, function(y) { - which(lapply(def_functions2, function(x) x == y) == TRUE) + which(lapply(def_functions2, function(x) x == y) == TRUE)[1] } )) if (length(tmp_index) == 0) { @@ -409,6 +517,9 @@ get_network <- function(dir = NULL, } all_folder <- c(all_folder, all_folder[tmp_index]) + if (verbose) { + print(paste0("check length: ", length(all_folder), " all_folder")) + } # simplify - removing functions with no connections if (simplify) { diff --git a/README.md b/README.md index 9320b15..35e5152 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# helfRlein - 1.4.1 +# helfRlein - 1.4.2 | branch | master | dev | | ------------- | ------ | ---- | diff --git a/man/get_network.Rd b/man/get_network.Rd index 3394185..69d7855 100644 --- a/man/get_network.Rd +++ b/man/get_network.Rd @@ -11,7 +11,8 @@ get_network( simplify = FALSE, all_scripts = NULL, use_internals = TRUE, - exclude = NULL + exclude = NULL, + verbose = FALSE ) } \arguments{ @@ -35,6 +36,8 @@ functions. The default is \code{TRUE}.} \item{exclude}{a vector with folder's or function's names, that are excluded from the network creation. This is done by a regex, so it will remove everything that contains these words.} + +\item{verbose}{a boolean setting the debugging prints.} } \value{ Returns an object with the adjacency matrix \code{$matrix} and diff --git a/misc/update_DESCRIPTION_NEWS.R b/misc/update_DESCRIPTION_NEWS.R index 8d05ec3..e470f14 100644 --- a/misc/update_DESCRIPTION_NEWS.R +++ b/misc/update_DESCRIPTION_NEWS.R @@ -445,6 +445,14 @@ my_news$add_bullet(c("get_network can now handle files with only comments", "fix get_network internal list handling")) +# bugfix in get_network --------------------------------------------------- + +my_desc$bump_version("patch") +my_news$add_version(my_desc$get_version()) +my_news$add_subtitle("Bugfixes") +my_news$add_bullet(c("fix error in get_network with duplicated inner functions names")) + + # save everything --------------------------------------------------------- diff --git a/renv.lock b/renv.lock index 69cc4c0..8ee648b 100644 --- a/renv.lock +++ b/renv.lock @@ -14,188 +14,212 @@ "Version": "1.4-1", "Source": "Repository", "Repository": "CRAN", - "Hash": "699c47c606293bdfbc9fd78a93c9c8fe", "Requirements": [ - "lattice" - ] + "R", + "graphics", + "grid", + "lattice", + "methods", + "stats", + "utils" + ], + "Hash": "699c47c606293bdfbc9fd78a93c9c8fe" }, "R6": { "Package": "R6", "Version": "2.5.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "470851b6d5d0ac559e9d01bb352b4021", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "470851b6d5d0ac559e9d01bb352b4021" }, "askpass": { "Package": "askpass", "Version": "1.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "e8a22846fff485f0be3770c2da758713", "Requirements": [ "sys" - ] + ], + "Hash": "e8a22846fff485f0be3770c2da758713" }, "backports": { "Package": "backports", "Version": "1.4.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "c39fbec8a30d23e721980b8afb31984c", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "c39fbec8a30d23e721980b8afb31984c" }, "brew": { "Package": "brew", "Version": "1.0-7", "Source": "Repository", "Repository": "CRAN", - "Hash": "38875ea52350ff4b4c03849fc69736c8", - "Requirements": [] + "Hash": "38875ea52350ff4b4c03849fc69736c8" }, "brio": { "Package": "brio", "Version": "1.1.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "976cf154dfb043c012d87cddd8bca363", - "Requirements": [] + "Hash": "976cf154dfb043c012d87cddd8bca363" }, "cachem": { "Package": "cachem", "Version": "1.0.6", "Source": "Repository", "Repository": "CRAN", - "Hash": "648c5b3d71e6a37e3043617489a0a0e9", "Requirements": [ "fastmap", "rlang" - ] + ], + "Hash": "648c5b3d71e6a37e3043617489a0a0e9" }, "callr": { "Package": "callr", "Version": "3.7.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "461aa75a11ce2400245190ef5d3995df", "Requirements": [ "R6", - "processx" - ] + "processx", + "utils" + ], + "Hash": "461aa75a11ce2400245190ef5d3995df" }, "cli": { "Package": "cli", "Version": "3.3.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "23abf173c2b783dcc43379ab9bba00ee", "Requirements": [ - "glue" - ] + "R", + "glue", + "utils" + ], + "Hash": "23abf173c2b783dcc43379ab9bba00ee" }, "clipr": { "Package": "clipr", "Version": "0.8.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "3f038e5ac7f41d4ac41ce658c85e3042", - "Requirements": [] + "Requirements": [ + "utils" + ], + "Hash": "3f038e5ac7f41d4ac41ce658c85e3042" }, "codetools": { "Package": "codetools", "Version": "0.2-18", "Source": "Repository", "Repository": "CRAN", - "Hash": "019388fc48e48b3da0d3a76ff94608a8", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "019388fc48e48b3da0d3a76ff94608a8" }, "commonmark": { "Package": "commonmark", "Version": "1.8.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "2ba81b120c1655ab696c935ef33ea716", - "Requirements": [] + "Hash": "2ba81b120c1655ab696c935ef33ea716" }, "cpp11": { "Package": "cpp11", "Version": "0.4.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "fa53ce256cd280f468c080a58ea5ba8c", - "Requirements": [] + "Hash": "fa53ce256cd280f468c080a58ea5ba8c" }, "crayon": { "Package": "crayon", "Version": "1.5.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "8dc45fd8a1ee067a92b85ef274e66d6a", - "Requirements": [] + "Requirements": [ + "grDevices", + "methods", + "utils" + ], + "Hash": "8dc45fd8a1ee067a92b85ef274e66d6a" }, "credentials": { "Package": "credentials", "Version": "1.3.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "93762d0a34d78e6a025efdbfb5c6bb41", "Requirements": [ "askpass", "curl", "jsonlite", "openssl", "sys" - ] + ], + "Hash": "93762d0a34d78e6a025efdbfb5c6bb41" }, "curl": { "Package": "curl", "Version": "4.3.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "022c42d49c28e95d69ca60446dbabf88", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "022c42d49c28e95d69ca60446dbabf88" }, "cyclocomp": { "Package": "cyclocomp", "Version": "1.1.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "53cbed70a2f7472d48fb6aef08442f25", "Requirements": [ "callr", "crayon", "desc", "remotes", "withr" - ] + ], + "Hash": "53cbed70a2f7472d48fb6aef08442f25" }, "data.table": { "Package": "data.table", "Version": "1.14.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "36b67b5adf57b292923f5659f5f0c853", - "Requirements": [] + "Requirements": [ + "R", + "methods" + ], + "Hash": "36b67b5adf57b292923f5659f5f0c853" }, "desc": { "Package": "desc", "Version": "1.4.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "eebd27ee58fcc58714eedb7aa07d8ad1", "Requirements": [ + "R", "R6", "cli", - "rprojroot" - ] + "rprojroot", + "utils" + ], + "Hash": "eebd27ee58fcc58714eedb7aa07d8ad1" }, "devtools": { "Package": "devtools", "Version": "2.4.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "fc35e13bb582e5fe6f63f3d647a4cbe5", "Requirements": [ + "R", "callr", "cli", "desc", @@ -213,77 +237,98 @@ "rstudioapi", "rversions", "sessioninfo", + "stats", "testthat", + "tools", "usethis", + "utils", "withr" - ] + ], + "Hash": "fc35e13bb582e5fe6f63f3d647a4cbe5" }, "diffobj": { "Package": "diffobj", "Version": "0.3.5", "Source": "Repository", "Repository": "CRAN", - "Hash": "bcaa8b95f8d7d01a5dedfd959ce88ab8", "Requirements": [ - "crayon" - ] + "R", + "crayon", + "methods", + "stats", + "tools", + "utils" + ], + "Hash": "bcaa8b95f8d7d01a5dedfd959ce88ab8" }, "digest": { "Package": "digest", "Version": "0.6.29", "Source": "Repository", "Repository": "CRAN", - "Hash": "cf6b206a045a684728c3267ef7596190", - "Requirements": [] + "Requirements": [ + "R", + "utils" + ], + "Hash": "cf6b206a045a684728c3267ef7596190" }, "ellipsis": { "Package": "ellipsis", "Version": "0.3.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "bb0eec2fe32e88d9e2836c2f73ea2077", "Requirements": [ + "R", "rlang" - ] + ], + "Hash": "bb0eec2fe32e88d9e2836c2f73ea2077" }, "evaluate": { "Package": "evaluate", "Version": "0.15", "Source": "Repository", "Repository": "CRAN", - "Hash": "699a7a93d08c962d9f8950b2d7a227f1", - "Requirements": [] + "Requirements": [ + "R", + "methods" + ], + "Hash": "699a7a93d08c962d9f8950b2d7a227f1" }, "fansi": { "Package": "fansi", "Version": "1.0.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "83a8afdbe71839506baa9f90eebad7ec", - "Requirements": [] + "Requirements": [ + "R", + "grDevices", + "utils" + ], + "Hash": "83a8afdbe71839506baa9f90eebad7ec" }, "fastmap": { "Package": "fastmap", "Version": "1.1.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "77bd60a6157420d4ffa93b27cf6a58b8", - "Requirements": [] + "Hash": "77bd60a6157420d4ffa93b27cf6a58b8" }, "fs": { "Package": "fs", "Version": "1.5.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "7c89603d81793f0d5486d91ab1fc6f1d", - "Requirements": [] + "Requirements": [ + "R", + "methods" + ], + "Hash": "7c89603d81793f0d5486d91ab1fc6f1d" }, "gert": { "Package": "gert", "Version": "1.6.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "98c014c4c933f23ea5a0321a4d0b588b", "Requirements": [ "askpass", "credentials", @@ -291,138 +336,162 @@ "rstudioapi", "sys", "zip" - ] + ], + "Hash": "98c014c4c933f23ea5a0321a4d0b588b" }, "gh": { "Package": "gh", "Version": "1.3.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "38c2580abbda249bd6afeec00d14f531", "Requirements": [ "cli", "gitcreds", "httr", "ini", "jsonlite" - ] + ], + "Hash": "38c2580abbda249bd6afeec00d14f531" }, "gitcreds": { "Package": "gitcreds", "Version": "0.1.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "f3aefccc1cc50de6338146b62f115de8", - "Requirements": [] + "Hash": "f3aefccc1cc50de6338146b62f115de8" }, "glue": { "Package": "glue", "Version": "1.6.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "4f2596dfb05dac67b9dc558e5c6fba2e", - "Requirements": [] + "Requirements": [ + "R", + "methods" + ], + "Hash": "4f2596dfb05dac67b9dc558e5c6fba2e" }, "highr": { "Package": "highr", "Version": "0.9", "Source": "Repository", "Repository": "CRAN", - "Hash": "8eb36c8125038e648e5d111c0d7b2ed4", "Requirements": [ + "R", "xfun" - ] + ], + "Hash": "8eb36c8125038e648e5d111c0d7b2ed4" }, "httr": { "Package": "httr", "Version": "1.4.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "88d1b310583777edf01ccd1216fb0b2b", "Requirements": [ + "R", "R6", "curl", "jsonlite", "mime", "openssl" - ] + ], + "Hash": "88d1b310583777edf01ccd1216fb0b2b" }, "igraph": { "Package": "igraph", "Version": "1.3.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "48e890b2ccaeb5cdce18f14781ae76d6", "Requirements": [ "Matrix", + "grDevices", + "graphics", "magrittr", - "pkgconfig" - ] + "methods", + "pkgconfig", + "stats", + "utils" + ], + "Hash": "48e890b2ccaeb5cdce18f14781ae76d6" }, "ini": { "Package": "ini", "Version": "0.3.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "6154ec2223172bce8162d4153cda21f7", - "Requirements": [] + "Hash": "6154ec2223172bce8162d4153cda21f7" }, "jsonlite": { "Package": "jsonlite", "Version": "1.8.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "d07e729b27b372429d42d24d503613a0", - "Requirements": [] + "Requirements": [ + "methods" + ], + "Hash": "d07e729b27b372429d42d24d503613a0" }, "knitr": { "Package": "knitr", "Version": "1.39", "Source": "Repository", "Repository": "CRAN", - "Hash": "029ab7c4badd3cf8af69016b2ba27493", "Requirements": [ + "R", "evaluate", "highr", + "methods", "stringr", + "tools", "xfun", "yaml" - ] + ], + "Hash": "029ab7c4badd3cf8af69016b2ba27493" }, "lattice": { "Package": "lattice", "Version": "0.20-45", "Source": "Repository", "Repository": "CRAN", - "Hash": "b64cdbb2b340437c4ee047a1f4c4377b", - "Requirements": [] + "Requirements": [ + "R", + "grDevices", + "graphics", + "grid", + "stats", + "utils" + ], + "Hash": "b64cdbb2b340437c4ee047a1f4c4377b" }, "lazyeval": { "Package": "lazyeval", "Version": "0.2.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "d908914ae53b04d4c0c0fd72ecc35370", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "d908914ae53b04d4c0c0fd72ecc35370" }, "lifecycle": { "Package": "lifecycle", "Version": "1.0.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "a6b6d352e3ed897373ab19d8395c98d0", "Requirements": [ + "R", "glue", "rlang" - ] + ], + "Hash": "a6b6d352e3ed897373ab19d8395c98d0" }, "lintr": { "Package": "lintr", "Version": "3.0.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "1214604176fb93fdcac030fc5d2177d9", "Requirements": [ + "R", "backports", "codetools", "crayon", @@ -432,73 +501,81 @@ "jsonlite", "knitr", "rex", + "stats", + "utils", "xml2", "xmlparsedata" - ] + ], + "Hash": "1214604176fb93fdcac030fc5d2177d9" }, "magrittr": { "Package": "magrittr", "Version": "2.0.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "7ce2733a9826b3aeb1775d56fd305472", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "7ce2733a9826b3aeb1775d56fd305472" }, "memoise": { "Package": "memoise", "Version": "2.0.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "e2817ccf4a065c5d9d7f2cfbe7c1d78c", "Requirements": [ "cachem", "rlang" - ] + ], + "Hash": "e2817ccf4a065c5d9d7f2cfbe7c1d78c" }, "mime": { "Package": "mime", "Version": "0.12", "Source": "Repository", "Repository": "CRAN", - "Hash": "18e9c28c1d3ca1560ce30658b22ce104", - "Requirements": [] + "Requirements": [ + "tools" + ], + "Hash": "18e9c28c1d3ca1560ce30658b22ce104" }, "newsmd": { "Package": "newsmd", "Version": "0.4.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "b693abeac4853be8a55c5146547626e4", "Requirements": [ + "R", "R6" - ] + ], + "Hash": "b693abeac4853be8a55c5146547626e4" }, "openssl": { "Package": "openssl", "Version": "2.0.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "6d3bef2e305f55c705c674653c7d7d3d", "Requirements": [ "askpass" - ] + ], + "Hash": "6d3bef2e305f55c705c674653c7d7d3d" }, "origin": { "Package": "origin", "Version": "0.5.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "322d909c7ae0f1446895c6b73e863ecb", "Requirements": [ + "R", "rstudioapi" - ] + ], + "Hash": "322d909c7ae0f1446895c6b73e863ecb" }, "pillar": { "Package": "pillar", "Version": "1.7.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "51dfc97e1b7069e9f7e6f83f3589c22e", "Requirements": [ "cli", "crayon", @@ -508,16 +585,18 @@ "lifecycle", "rlang", "utf8", + "utils", "vctrs" - ] + ], + "Hash": "51dfc97e1b7069e9f7e6f83f3589c22e" }, "pkgbuild": { "Package": "pkgbuild", "Version": "1.3.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "66d2adfed274daf81ccfe77d974c3b9b", "Requirements": [ + "R", "R6", "callr", "cli", @@ -526,92 +605,102 @@ "prettyunits", "rprojroot", "withr" - ] + ], + "Hash": "66d2adfed274daf81ccfe77d974c3b9b" }, "pkgconfig": { "Package": "pkgconfig", "Version": "2.0.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "01f28d4278f15c76cddbea05899c5d6f", - "Requirements": [] + "Requirements": [ + "utils" + ], + "Hash": "01f28d4278f15c76cddbea05899c5d6f" }, "pkgload": { "Package": "pkgload", "Version": "1.2.4", "Source": "Repository", "Repository": "CRAN", - "Hash": "7533cd805940821bf23eaf3c8d4c1735", "Requirements": [ "cli", "crayon", "desc", + "methods", "rlang", "rprojroot", "rstudioapi", + "utils", "withr" - ] + ], + "Hash": "7533cd805940821bf23eaf3c8d4c1735" }, "praise": { "Package": "praise", "Version": "1.0.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "a555924add98c99d2f411e37e7d25e9f", - "Requirements": [] + "Hash": "a555924add98c99d2f411e37e7d25e9f" }, "prettyunits": { "Package": "prettyunits", "Version": "1.1.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "95ef9167b75dde9d2ccc3c7528393e7e", - "Requirements": [] + "Hash": "95ef9167b75dde9d2ccc3c7528393e7e" }, "processx": { "Package": "processx", "Version": "3.6.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "a11891e28c1f1e5ddd773ba1b8c07cf6", "Requirements": [ + "R", "R6", - "ps" - ] + "ps", + "utils" + ], + "Hash": "a11891e28c1f1e5ddd773ba1b8c07cf6" }, "ps": { "Package": "ps", "Version": "1.7.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "8b93531308c01ad0e56d9eadcc0c4fcd", - "Requirements": [] + "Requirements": [ + "R", + "utils" + ], + "Hash": "8b93531308c01ad0e56d9eadcc0c4fcd" }, "purrr": { "Package": "purrr", "Version": "0.3.4", "Source": "Repository", "Repository": "CRAN", - "Hash": "97def703420c8ab10d8f0e6c72101e02", "Requirements": [ + "R", "magrittr", "rlang" - ] + ], + "Hash": "97def703420c8ab10d8f0e6c72101e02" }, "rappdirs": { "Package": "rappdirs", "Version": "0.3.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "5e3c5dc0b071b21fa128676560dbe94d", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "5e3c5dc0b071b21fa128676560dbe94d" }, "rcmdcheck": { "Package": "rcmdcheck", "Version": "1.4.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "8f25ebe2ec38b1f2aef3b0d2ef76f6c4", "Requirements": [ "R6", "callr", @@ -623,61 +712,74 @@ "prettyunits", "rprojroot", "sessioninfo", + "utils", "withr", "xopen" - ] + ], + "Hash": "8f25ebe2ec38b1f2aef3b0d2ef76f6c4" }, "rematch2": { "Package": "rematch2", "Version": "2.1.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "76c9e04c712a05848ae7a23d2f170a40", "Requirements": [ "tibble" - ] + ], + "Hash": "76c9e04c712a05848ae7a23d2f170a40" }, "remotes": { "Package": "remotes", "Version": "2.4.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "227045be9aee47e6dda9bb38ac870d67", - "Requirements": [] + "Requirements": [ + "R", + "methods", + "stats", + "tools", + "utils" + ], + "Hash": "227045be9aee47e6dda9bb38ac870d67" }, "renv": { "Package": "renv", - "Version": "0.15.5", + "Version": "1.0.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "6a38294e7d12f5d8e656b08c5bd8ae34", - "Requirements": [] + "Requirements": [ + "utils" + ], + "Hash": "41b847654f567341725473431dd0d5ab" }, "rex": { "Package": "rex", "Version": "1.2.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "ae34cd56890607370665bee5bd17812f", "Requirements": [ "lazyeval" - ] + ], + "Hash": "ae34cd56890607370665bee5bd17812f" }, "rlang": { "Package": "rlang", "Version": "1.0.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "04884d9a75d778aca22c7154b8333ec9", - "Requirements": [] + "Requirements": [ + "R", + "utils" + ], + "Hash": "04884d9a75d778aca22c7154b8333ec9" }, "roxygen2": { "Package": "roxygen2", "Version": "7.2.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "b390c1d54fcd977cda48588e6172daba", "Requirements": [ + "R", "R6", "brew", "cli", @@ -686,87 +788,100 @@ "desc", "digest", "knitr", + "methods", "pkgload", "purrr", "rlang", "stringi", "stringr", + "utils", "withr", "xml2" - ] + ], + "Hash": "b390c1d54fcd977cda48588e6172daba" }, "rprojroot": { "Package": "rprojroot", "Version": "2.0.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "1de7ab598047a87bba48434ba35d497d", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "1de7ab598047a87bba48434ba35d497d" }, "rstudioapi": { "Package": "rstudioapi", "Version": "0.13", "Source": "Repository", "Repository": "CRAN", - "Hash": "06c85365a03fdaf699966cc1d3cf53ea", - "Requirements": [] + "Hash": "06c85365a03fdaf699966cc1d3cf53ea" }, "rversions": { "Package": "rversions", "Version": "2.1.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "f88fab00907b312f8b23ec13e2d437cb", "Requirements": [ "curl", + "utils", "xml2" - ] + ], + "Hash": "f88fab00907b312f8b23ec13e2d437cb" }, "sessioninfo": { "Package": "sessioninfo", "Version": "1.2.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "3f9796a8d0a0e8c6eb49a4b029359d1f", "Requirements": [ - "cli" - ] + "R", + "cli", + "tools", + "utils" + ], + "Hash": "3f9796a8d0a0e8c6eb49a4b029359d1f" }, "stringi": { "Package": "stringi", "Version": "1.7.6", "Source": "Repository", "Repository": "CRAN", - "Hash": "bba431031d30789535745a9627ac9271", - "Requirements": [] + "Requirements": [ + "R", + "stats", + "tools", + "utils" + ], + "Hash": "bba431031d30789535745a9627ac9271" }, "stringr": { "Package": "stringr", "Version": "1.4.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "0759e6b6c0957edb1311028a49a35e76", "Requirements": [ + "R", "glue", "magrittr", "stringi" - ] + ], + "Hash": "0759e6b6c0957edb1311028a49a35e76" }, "sys": { "Package": "sys", "Version": "3.4", "Source": "Repository", "Repository": "CRAN", - "Hash": "b227d13e29222b4574486cfcbde077fa", - "Requirements": [] + "Hash": "b227d13e29222b4574486cfcbde077fa" }, "testthat": { "Package": "testthat", "Version": "3.1.4", "Source": "Repository", "Repository": "CRAN", - "Hash": "f76c2a02d0fdc24aa7a47ea34261a6e3", "Requirements": [ + "R", "R6", "brio", "callr", @@ -779,39 +894,45 @@ "jsonlite", "lifecycle", "magrittr", + "methods", "pkgload", "praise", "processx", "ps", "rlang", + "utils", "waldo", "withr" - ] + ], + "Hash": "f76c2a02d0fdc24aa7a47ea34261a6e3" }, "tibble": { "Package": "tibble", "Version": "3.1.7", "Source": "Repository", "Repository": "CRAN", - "Hash": "08415af406e3dd75049afef9552e7355", "Requirements": [ + "R", "ellipsis", "fansi", "lifecycle", "magrittr", + "methods", "pillar", "pkgconfig", "rlang", + "utils", "vctrs" - ] + ], + "Hash": "08415af406e3dd75049afef9552e7355" }, "usethis": { "Package": "usethis", "Version": "2.1.6", "Source": "Repository", "Repository": "CRAN", - "Hash": "a67a22c201832b12c036cc059f1d137d", "Requirements": [ + "R", "cli", "clipr", "crayon", @@ -828,112 +949,130 @@ "rlang", "rprojroot", "rstudioapi", + "stats", + "utils", "whisker", "withr", "yaml" - ] + ], + "Hash": "a67a22c201832b12c036cc059f1d137d" }, "utf8": { "Package": "utf8", "Version": "1.2.2", "Source": "Repository", "Repository": "CRAN", - "Hash": "c9c462b759a5cc844ae25b5942654d13", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "c9c462b759a5cc844ae25b5942654d13" }, "vctrs": { "Package": "vctrs", "Version": "0.4.1", "Source": "Repository", "Repository": "CRAN", - "Hash": "8b54f22e2a58c4f275479c92ce041a57", "Requirements": [ + "R", "cli", "glue", "rlang" - ] + ], + "Hash": "8b54f22e2a58c4f275479c92ce041a57" }, "waldo": { "Package": "waldo", "Version": "0.4.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "035fba89d0c86e2113120f93301b98ad", "Requirements": [ "cli", "diffobj", "fansi", "glue", + "methods", "rematch2", "rlang", "tibble" - ] + ], + "Hash": "035fba89d0c86e2113120f93301b98ad" }, "whisker": { "Package": "whisker", "Version": "0.4", "Source": "Repository", "Repository": "CRAN", - "Hash": "ca970b96d894e90397ed20637a0c1bbe", - "Requirements": [] + "Hash": "ca970b96d894e90397ed20637a0c1bbe" }, "withr": { "Package": "withr", "Version": "2.5.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "c0e49a9760983e81e55cdd9be92e7182", - "Requirements": [] + "Requirements": [ + "R", + "grDevices", + "graphics", + "stats" + ], + "Hash": "c0e49a9760983e81e55cdd9be92e7182" }, "xfun": { "Package": "xfun", "Version": "0.31", "Source": "Repository", "Repository": "CRAN", - "Hash": "a318c6f752b8dcfe9fb74d897418ab2b", - "Requirements": [] + "Requirements": [ + "stats", + "tools" + ], + "Hash": "a318c6f752b8dcfe9fb74d897418ab2b" }, "xml2": { "Package": "xml2", "Version": "1.3.3", "Source": "Repository", "Repository": "CRAN", - "Hash": "40682ed6a969ea5abfd351eb67833adc", - "Requirements": [] + "Requirements": [ + "R", + "methods" + ], + "Hash": "40682ed6a969ea5abfd351eb67833adc" }, "xmlparsedata": { "Package": "xmlparsedata", "Version": "1.0.5", "Source": "Repository", "Repository": "CRAN", - "Hash": "45e4bf3c46476896e821fc0a408fb4fc", - "Requirements": [] + "Requirements": [ + "R" + ], + "Hash": "45e4bf3c46476896e821fc0a408fb4fc" }, "xopen": { "Package": "xopen", "Version": "1.0.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "6c85f015dee9cc7710ddd20f86881f58", "Requirements": [ + "R", "processx" - ] + ], + "Hash": "6c85f015dee9cc7710ddd20f86881f58" }, "yaml": { "Package": "yaml", "Version": "2.3.5", "Source": "Repository", "Repository": "CRAN", - "Hash": "458bb38374d73bf83b1bb85e353da200", - "Requirements": [] + "Hash": "458bb38374d73bf83b1bb85e353da200" }, "zip": { "Package": "zip", "Version": "2.2.0", "Source": "Repository", "Repository": "CRAN", - "Hash": "c7eef2996ac270a18c2715c997a727c5", - "Requirements": [] + "Hash": "c7eef2996ac270a18c2715c997a727c5" } } } diff --git a/renv/.gitignore b/renv/.gitignore index 35af5c5..bed446c 100644 --- a/renv/.gitignore +++ b/renv/.gitignore @@ -1,3 +1,4 @@ +sandbox/ local/ cellar/ library/ diff --git a/renv/activate.R b/renv/activate.R index 72c0818..cb5401f 100644 --- a/renv/activate.R +++ b/renv/activate.R @@ -2,11 +2,27 @@ local({ # the requested version of renv - version <- "0.15.5" + version <- "1.0.3" + attr(version, "sha") <- NULL # the project directory project <- getwd() + # use start-up diagnostics if enabled + diagnostics <- Sys.getenv("RENV_STARTUP_DIAGNOSTICS", unset = "FALSE") + if (diagnostics) { + start <- Sys.time() + profile <- tempfile("renv-startup-", fileext = ".Rprof") + utils::Rprof(profile) + on.exit({ + utils::Rprof(NULL) + elapsed <- signif(difftime(Sys.time(), start, units = "auto"), digits = 2L) + writeLines(sprintf("- renv took %s to run the autoloader.", format(elapsed))) + writeLines(sprintf("- Profile: %s", profile)) + print(utils::summaryRprof(profile)) + }, add = TRUE) + } + # figure out whether the autoloader is enabled enabled <- local({ @@ -60,21 +76,75 @@ local({ # load bootstrap tools `%||%` <- function(x, y) { - if (is.environment(x) || length(x)) x else y + if (is.null(x)) y else x + } + + catf <- function(fmt, ..., appendLF = TRUE) { + + quiet <- getOption("renv.bootstrap.quiet", default = FALSE) + if (quiet) + return(invisible()) + + msg <- sprintf(fmt, ...) + cat(msg, file = stdout(), sep = if (appendLF) "\n" else "") + + invisible(msg) + + } + + 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) { + friendly <- renv_bootstrap_version_friendly(version) + section <- header(sprintf("Bootstrapping renv %s", friendly)) + catf(section) + # attempt to download renv - tarball <- tryCatch(renv_bootstrap_download(version), error = identity) - if (inherits(tarball, "error")) - stop("failed to download renv ", version) + 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 - status <- tryCatch(renv_bootstrap_install(version, tarball, library), error = identity) - if (inherits(status, "error")) - stop("failed to install renv ", version) + 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("") + + return(invisible()) } renv_bootstrap_tests_running <- function() { @@ -83,28 +153,32 @@ local({ renv_bootstrap_repos <- function() { + # get CRAN repository + cran <- getOption("renv.repos.cran", "https://cloud.r-project.org") + # check for repos override repos <- Sys.getenv("RENV_CONFIG_REPOS_OVERRIDE", unset = NA) - if (!is.na(repos)) + if (!is.na(repos)) { + + # check for RSPM; if set, use a fallback repository for renv + rspm <- Sys.getenv("RSPM", unset = NA) + if (identical(rspm, repos)) + repos <- c(RSPM = rspm, CRAN = cran) + return(repos) + } + # check for lockfile repositories repos <- tryCatch(renv_bootstrap_repos_lockfile(), error = identity) if (!inherits(repos, "error") && length(repos)) return(repos) - # if we're testing, re-use the test repositories - if (renv_bootstrap_tests_running()) - return(getOption("renv.tests.repos")) - # retrieve current repos repos <- getOption("repos") # ensure @CRAN@ entries are resolved - repos[repos == "@CRAN@"] <- getOption( - "renv.repos.cran", - "https://cloud.r-project.org" - ) + repos[repos == "@CRAN@"] <- cran # add in renv.bootstrap.repos if set default <- c(FALLBACK = "https://cloud.r-project.org") @@ -143,33 +217,34 @@ local({ renv_bootstrap_download <- function(version) { - # if the renv version number has 4 components, assume it must - # be retrieved via github - nv <- numeric_version(version) - components <- unclass(nv)[[1]] - - # if this appears to be a development version of 'renv', we'll - # try to restore from github - dev <- length(components) == 4L - - # begin collecting different methods for finding renv - methods <- c( - renv_bootstrap_download_tarball, - if (dev) - renv_bootstrap_download_github - else c( - renv_bootstrap_download_cran_latest, - renv_bootstrap_download_cran_archive + sha <- attr(version, "sha", exact = TRUE) + + methods <- if (!is.null(sha)) { + + # attempting to bootstrap a development version of renv + c( + function() renv_bootstrap_download_tarball(sha), + function() renv_bootstrap_download_github(sha) + ) + + } else { + + # attempting to bootstrap a release version of renv + c( + function() renv_bootstrap_download_tarball(version), + function() renv_bootstrap_download_cran_latest(version), + function() renv_bootstrap_download_cran_archive(version) ) - ) + + } for (method in methods) { - path <- tryCatch(method(version), error = identity) + path <- tryCatch(method(), error = identity) if (is.character(path) && file.exists(path)) return(path) } - stop("failed to download renv ", version) + stop("All download methods failed") } @@ -185,43 +260,75 @@ local({ if (fixup) mode <- "w+b" - utils::download.file( + args <- list( url = url, destfile = destfile, mode = mode, quiet = TRUE ) + if ("headers" %in% names(formals(utils::download.file))) + args$headers <- renv_bootstrap_download_custom_headers(url) + + do.call(utils::download.file, args) + } - renv_bootstrap_download_cran_latest <- function(version) { + renv_bootstrap_download_custom_headers <- function(url) { - spec <- renv_bootstrap_download_cran_latest_find(version) + headers <- getOption("renv.download.headers") + if (is.null(headers)) + return(character()) + + if (!is.function(headers)) + stopf("'renv.download.headers' is not a function") + + headers <- headers(url) + if (length(headers) == 0L) + return(character()) + + if (is.list(headers)) + headers <- unlist(headers, recursive = FALSE, use.names = TRUE) - message("* Downloading renv ", version, " ... ", appendLF = FALSE) + ok <- + is.character(headers) && + is.character(names(headers)) && + all(nzchar(names(headers))) + if (!ok) + stop("invocation of 'renv.download.headers' did not return a named character vector") + + headers + + } + + renv_bootstrap_download_cran_latest <- function(version) { + + spec <- renv_bootstrap_download_cran_latest_find(version) type <- spec$type repos <- spec$repos - info <- tryCatch( - utils::download.packages( - pkgs = "renv", - destdir = tempdir(), - repos = repos, - type = type, - quiet = TRUE - ), + baseurl <- utils::contrib.url(repos = repos, type = type) + ext <- if (identical(type, "source")) + ".tar.gz" + else if (Sys.info()[["sysname"]] == "Windows") + ".zip" + else + ".tgz" + name <- sprintf("renv_%s%s", version, ext) + url <- paste(baseurl, name, sep = "/") + + destfile <- file.path(tempdir(), name) + status <- tryCatch( + renv_bootstrap_download_impl(url, destfile), condition = identity ) - if (inherits(info, "condition")) { - message("FAILED") + if (inherits(status, "condition")) return(FALSE) - } # report success and return - message("OK (downloaded ", type, ")") - info[1, 2] + destfile } @@ -277,8 +384,6 @@ local({ urls <- file.path(repos, "src/contrib/Archive/renv", name) destfile <- file.path(tempdir(), name) - message("* Downloading renv ", version, " ... ", appendLF = FALSE) - for (url in urls) { status <- tryCatch( @@ -286,14 +391,11 @@ local({ condition = identity ) - if (identical(status, 0L)) { - message("OK") + if (identical(status, 0L)) return(destfile) - } } - message("FAILED") return(FALSE) } @@ -307,8 +409,7 @@ local({ return() # allow directories - info <- file.info(tarball, extra_cols = FALSE) - if (identical(info$isdir, TRUE)) { + if (dir.exists(tarball)) { name <- sprintf("renv_%s.tar.gz", version) tarball <- file.path(tarball, name) } @@ -317,7 +418,7 @@ local({ if (!file.exists(tarball)) { # let the user know we weren't able to honour their request - fmt <- "* RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist." + fmt <- "- RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist." msg <- sprintf(fmt, tarball) warning(msg) @@ -326,10 +427,7 @@ local({ } - fmt <- "* Bootstrapping with tarball at path '%s'." - msg <- sprintf(fmt, tarball) - message(msg) - + catf("- Using local tarball '%s'.", tarball) tarball } @@ -356,8 +454,6 @@ local({ on.exit(do.call(base::options, saved), add = TRUE) } - message("* Downloading renv ", version, " 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) @@ -367,26 +463,105 @@ local({ condition = identity ) - if (!identical(status, 0L)) { - message("FAILED") + if (!identical(status, 0L)) return(FALSE) - } - message("OK") + renv_bootstrap_download_augment(destfile) + return(destfile) } + # Add Sha to DESCRIPTION. This is stop gap until #890, after which we + # can use renv::install() to fully capture metadata. + renv_bootstrap_download_augment <- function(destfile) { + sha <- renv_bootstrap_git_extract_sha1_tar(destfile) + if (is.null(sha)) { + return() + } + + # Untar + tempdir <- tempfile("renv-github-") + on.exit(unlink(tempdir, recursive = TRUE), add = TRUE) + untar(destfile, exdir = tempdir) + pkgdir <- dir(tempdir, full.names = TRUE)[[1]] + + # Modify description + desc_path <- file.path(pkgdir, "DESCRIPTION") + desc_lines <- readLines(desc_path) + remotes_fields <- c( + "RemoteType: github", + "RemoteHost: api.github.com", + "RemoteRepo: renv", + "RemoteUsername: rstudio", + "RemotePkgRef: rstudio/renv", + paste("RemoteRef: ", sha), + paste("RemoteSha: ", sha) + ) + writeLines(c(desc_lines[desc_lines != ""], remotes_fields), con = desc_path) + + # Re-tar + local({ + old <- setwd(tempdir) + on.exit(setwd(old), add = TRUE) + + tar(destfile, compression = "gzip") + }) + invisible() + } + + # Extract the commit hash from a git archive. Git archives include the SHA1 + # hash as the comment field of the tarball pax extended header + # (see https://www.kernel.org/pub/software/scm/git/docs/git-archive.html) + # For GitHub archives this should be the first header after the default one + # (512 byte) header. + renv_bootstrap_git_extract_sha1_tar <- function(bundle) { + + # open the bundle for reading + # We use gzcon for everything because (from ?gzcon) + # > Reading from a connection which does not supply a 'gzip' magic + # > header is equivalent to reading from the original connection + conn <- gzcon(file(bundle, open = "rb", raw = TRUE)) + on.exit(close(conn)) + + # The default pax header is 512 bytes long and the first pax extended header + # with the comment should be 51 bytes long + # `52 comment=` (11 chars) + 40 byte SHA1 hash + len <- 0x200 + 0x33 + res <- rawToChar(readBin(conn, "raw", n = len)[0x201:len]) + + if (grepl("^52 comment=", res)) { + sub("52 comment=", "", res) + } else { + NULL + } + } + renv_bootstrap_install <- function(version, tarball, library) { # attempt to install it into project library - message("* Installing renv ", version, " ... ", 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.null(status) || identical(status, 0L)) + return(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", @@ -394,19 +569,7 @@ local({ shQuote(path.expand(tarball)) ) - output <- system2(r, args, stdout = TRUE, stderr = TRUE) - message("Done!") - - # check for successful install - status <- attr(output, "status") - if (is.numeric(status) && !identical(status, 0L)) { - header <- "Error installing renv:" - lines <- paste(rep.int("=", nchar(header)), collapse = "") - text <- c(header, lines, output) - writeLines(text, con = stderr()) - } - - status + system2(R, args, stdout = TRUE, stderr = TRUE) } @@ -616,34 +779,62 @@ local({ } - renv_bootstrap_validate_version <- function(version) { + renv_bootstrap_validate_version <- function(version, description = NULL) { - loadedversion <- utils::packageDescription("renv", fields = "Version") - if (version == loadedversion) - return(TRUE) + # resolve description file + # + # avoid passing lib.loc to `packageDescription()` below, since R will + # use the loaded version of the package by default anyhow. note that + # this function should only be called after 'renv' is loaded + # https://github.com/rstudio/renv/issues/1625 + description <- description %||% packageDescription("renv") - # assume four-component versions are from GitHub; three-component - # versions are from CRAN - components <- strsplit(loadedversion, "[.-]")[[1]] - remote <- if (length(components) == 4L) - paste("rstudio/renv", loadedversion, sep = "@") + # check whether requested version 'version' matches loaded version of renv + sha <- attr(version, "sha", exact = TRUE) + valid <- if (!is.null(sha)) + renv_bootstrap_validate_version_dev(sha, description) else - paste("renv", loadedversion, sep = "@") + renv_bootstrap_validate_version_release(version, description) + + if (valid) + return(TRUE) + + # the loaded version of renv doesn't match the requested version; + # give the user instructions on how to proceed + remote <- if (!is.null(description[["RemoteSha"]])) { + paste("rstudio/renv", description[["RemoteSha"]], sep = "@") + } else { + paste("renv", description[["Version"]], sep = "@") + } + + # display both loaded version + sha if available + friendly <- renv_bootstrap_version_friendly( + version = description[["Version"]], + sha = description[["RemoteSha"]] + ) fmt <- paste( "renv %1$s was loaded from project library, but this project is configured to use renv %2$s.", - "Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.", - "Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.", + "- Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.", + "- Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.", sep = "\n" ) - - msg <- sprintf(fmt, loadedversion, version, remote) - warning(msg, call. = FALSE) + catf(fmt, friendly, renv_bootstrap_version_friendly(version), remote) FALSE } + renv_bootstrap_validate_version_dev <- function(version, description) { + expected <- description[["RemoteSha"]] + is.character(expected) && startswith(expected, version) + } + + renv_bootstrap_validate_version_release <- function(version, description) { + expected <- description[["Version"]] + is.character(expected) && identical(expected, version) + } + renv_bootstrap_hash_text <- function(text) { hashfile <- tempfile("renv-hash-") @@ -663,6 +854,12 @@ local({ # warn if the version of renv loaded does not match renv_bootstrap_validate_version(version) + # execute renv load hooks, if any + hooks <- getHook("renv::autoload") + for (hook in hooks) + if (is.function(hook)) + tryCatch(hook(), error = warnify) + # load the project renv::load(project) @@ -678,7 +875,7 @@ local({ return(profile) # check for a profile file (nothing to do if it doesn't exist) - path <- renv_bootstrap_paths_renv("profile", profile = FALSE) + path <- renv_bootstrap_paths_renv("profile", profile = FALSE, project = project) if (!file.exists(path)) return(NULL) @@ -802,12 +999,78 @@ local({ } + renv_bootstrap_version_friendly <- function(version, shafmt = NULL, sha = NULL) { + sha <- sha %||% attr(version, "sha", exact = TRUE) + parts <- c(version, sprintf(shafmt %||% " [sha: %s]", substring(sha, 1L, 7L))) + paste(parts, collapse = "") + } + + renv_bootstrap_exec <- function(project, libpath, version) { + if (!renv_bootstrap_load(project, libpath, version)) + renv_bootstrap_run(version, libpath) + } + + 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(project = getwd())) + } + + # 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_json_read <- function(file = NULL, text = NULL) { + jlerr <- NULL + + # if jsonlite is loaded, use that instead + if ("jsonlite" %in% loadedNamespaces()) { + + json <- catch(renv_json_read_jsonlite(file, text)) + if (!inherits(json, "error")) + return(json) + + jlerr <- json + + } + + # otherwise, fall back to the default JSON reader + json <- catch(renv_json_read_default(file, text)) + if (!inherits(json, "error")) + return(json) + + # report an error + if (!is.null(jlerr)) + stop(jlerr) + else + stop(json) + + } + + renv_json_read_jsonlite <- function(file = NULL, text = NULL) { text <- paste(text %||% read(file), collapse = "\n") + jsonlite::fromJSON(txt = text, simplifyVector = FALSE) + } + + renv_json_read_default <- function(file = NULL, text = NULL) { # find strings in the JSON + text <- paste(text %||% read(file), collapse = "\n") pattern <- '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]' locs <- gregexpr(pattern, text, perl = TRUE)[[1]] @@ -838,8 +1101,9 @@ local({ # transform the JSON into something the R parser understands transformed <- replaced - transformed <- gsub("[[{]", "list(", transformed) - transformed <- gsub("[]}]", ")", transformed) + transformed <- gsub("{}", "`names<-`(list(), character())", transformed, fixed = TRUE) + transformed <- gsub("[[{]", "list(", transformed, perl = TRUE) + transformed <- gsub("[]}]", ")", transformed, perl = TRUE) transformed <- gsub(":", "=", transformed, fixed = TRUE) text <- paste(transformed, collapse = "\n") @@ -908,35 +1172,9 @@ local({ # construct full libpath libpath <- file.path(root, prefix) - # attempt to load - if (renv_bootstrap_load(project, libpath, version)) - return(TRUE) - - # load failed; inform user we're about to bootstrap - prefix <- paste("# Bootstrapping renv", version) - postfix <- paste(rep.int("-", 77L - nchar(prefix)), collapse = "") - header <- paste(prefix, postfix) - message(header) - - # 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)) { - message("* Successfully installed and loaded renv ", version, ".") - 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." - ) + # run bootstrap code + renv_bootstrap_exec(project, libpath, version) - warning(paste(msg, collapse = "\n"), call. = FALSE) + invisible() }) diff --git a/renv/settings.json b/renv/settings.json new file mode 100644 index 0000000..2472d63 --- /dev/null +++ b/renv/settings.json @@ -0,0 +1,19 @@ +{ + "bioconductor.version": [], + "external.libraries": [], + "ignored.packages": [], + "package.dependency.fields": [ + "Imports", + "Depends", + "LinkingTo" + ], + "ppm.enabled": null, + "ppm.ignored.urls": [], + "r.version": [], + "snapshot.type": "implicit", + "use.cache": true, + "vcs.ignore.cellar": true, + "vcs.ignore.library": true, + "vcs.ignore.local": true, + "vcs.manage.ignores": true +}