|
| 1 | +#' Generate lockfile for application's environment reproducibility |
| 2 | +#' |
| 3 | +#' @param lockfile_path (`character`) path to the lockfile. |
| 4 | +#' |
| 5 | +#' @section Different ways of creating lockfile: |
| 6 | +#' `teal` leverages [renv::snapshot()], which offers multiple methods for lockfile creation. |
| 7 | +#' |
| 8 | +#' - **Working directory lockfile**: `teal`, by default, will create an `implicit` type lockfile that uses |
| 9 | +#' `renv::dependencies()` to detect all R packages in the current project's working directory. |
| 10 | +#' - **`DESCRIPTION`-based lockfile**: To generate a lockfile based on a `DESCRIPTION` file in your working |
| 11 | +#' directory, set `renv::settings$snapshot.type("explicit")`. The naming convention for `type` follows |
| 12 | +#' `renv::snapshot()`. For the `"explicit"` type, refer to `renv::settings$package.dependency.fields()` for the |
| 13 | +#' `DESCRIPTION` fields included in the lockfile. |
| 14 | +#' - **Custom files-based lockfile**: To specify custom files as the basis for the lockfile, set |
| 15 | +#' `renv::settings$snapshot.type("custom")` and configure the `renv.snapshot.filter` option. |
| 16 | +#' |
| 17 | +#' @section lockfile usage: |
| 18 | +#' After creating the lockfile, you can restore the application's environment using `renv::restore()`. |
| 19 | +#' |
| 20 | +#' @seealso [renv::snapshot()], [renv::restore()]. |
| 21 | +#' |
| 22 | +#' @return `NULL` |
| 23 | +#' |
| 24 | +#' @name module_teal_lockfile |
| 25 | +#' @rdname module_teal_lockfile |
| 26 | +#' |
| 27 | +#' @keywords internal |
| 28 | +NULL |
| 29 | + |
| 30 | +#' @rdname module_teal_lockfile |
| 31 | +ui_teal_lockfile <- function(id) { |
| 32 | + ns <- NS(id) |
| 33 | + shiny::tagList( |
| 34 | + tags$span("", id = ns("lockFileStatus")), |
| 35 | + shinyjs::disabled(downloadLink(ns("lockFileLink"), "Download lockfile")) |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +#' @rdname module_teal_lockfile |
| 40 | +srv_teal_lockfile <- function(id) { |
| 41 | + moduleServer(id, function(input, output, session) { |
| 42 | + logger::log_debug("Initialize srv_teal_lockfile.") |
| 43 | + enable_lockfile_download <- function() { |
| 44 | + shinyjs::html("lockFileStatus", "Application lockfile ready.") |
| 45 | + shinyjs::hide("lockFileStatus", anim = TRUE) |
| 46 | + shinyjs::enable("lockFileLink") |
| 47 | + output$lockFileLink <- shiny::downloadHandler( |
| 48 | + filename = function() { |
| 49 | + "renv.lock" |
| 50 | + }, |
| 51 | + content = function(file) { |
| 52 | + file.copy(lockfile_path, file) |
| 53 | + file |
| 54 | + }, |
| 55 | + contentType = "application/json" |
| 56 | + ) |
| 57 | + } |
| 58 | + disable_lockfile_download <- function() { |
| 59 | + warning("Lockfile creation failed.", call. = FALSE) |
| 60 | + shinyjs::html("lockFileStatus", "Lockfile creation failed.") |
| 61 | + shinyjs::hide("lockFileLink") |
| 62 | + } |
| 63 | + |
| 64 | + shiny::onStop(function() { |
| 65 | + if (file.exists(lockfile_path) && !shiny::isRunning()) { |
| 66 | + logger::log_debug("Removing lockfile after shutting down the app") |
| 67 | + file.remove(lockfile_path) |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + lockfile_path <- "teal_app.lock" |
| 72 | + mode <- getOption("teal.lockfile.mode", default = "") |
| 73 | + |
| 74 | + if (!(mode %in% c("auto", "enabled", "disabled"))) { |
| 75 | + stop("'teal.lockfile.mode' option can only be one of \"auto\", \"disabled\" or \"disabled\". ") |
| 76 | + } |
| 77 | + |
| 78 | + if (mode == "disabled") { |
| 79 | + logger::log_debug("'teal.lockfile.mode' option is set to 'disabled'. Hiding lockfile download button.") |
| 80 | + shinyjs::hide("lockFileLink") |
| 81 | + return(NULL) |
| 82 | + } |
| 83 | + |
| 84 | + if (file.exists(lockfile_path)) { |
| 85 | + logger::log_debug("Lockfile has already been created for this app - skipping automatic creation.") |
| 86 | + enable_lockfile_download() |
| 87 | + return(NULL) |
| 88 | + } |
| 89 | + |
| 90 | + if (mode == "auto" && .is_disabled_lockfile_scenario()) { |
| 91 | + logger::log_debug( |
| 92 | + "Automatic lockfile creation disabled. Execution scenario satisfies teal:::.is_disabled_lockfile_scenario()." |
| 93 | + ) |
| 94 | + shinyjs::hide("lockFileLink") |
| 95 | + return(NULL) |
| 96 | + } |
| 97 | + |
| 98 | + if (!.is_lockfile_deps_installed()) { |
| 99 | + warning("Automatic lockfile creation disabled. `mirai` and `renv` packages must be installed.") |
| 100 | + shinyjs::hide("lockFileLink") |
| 101 | + return(NULL) |
| 102 | + } |
| 103 | + |
| 104 | + # - Will be run only if the lockfile doesn't exist (see the if-s above) |
| 105 | + # - We render to the tempfile because the process might last after session is closed and we don't |
| 106 | + # want to make a "teal_app.renv" then. This is why we copy only during active session. |
| 107 | + process <- .teal_lockfile_process_invoke(lockfile_path) |
| 108 | + observeEvent(process$status(), { |
| 109 | + if (process$status() %in% c("initial", "running")) { |
| 110 | + shinyjs::html("lockFileStatus", "Creating lockfile...") |
| 111 | + } else if (process$status() == "success") { |
| 112 | + result <- process$result() |
| 113 | + if (any(grepl("Lockfile written to", result$out))) { |
| 114 | + logger::log_debug("Lockfile containing { length(result$res$Packages) } packages created.") |
| 115 | + if (any(grepl("(WARNING|ERROR):", result$out))) { |
| 116 | + warning("Lockfile created with warning(s) or error(s):", call. = FALSE) |
| 117 | + for (i in result$out) { |
| 118 | + warning(i, call. = FALSE) |
| 119 | + } |
| 120 | + } |
| 121 | + enable_lockfile_download() |
| 122 | + } else { |
| 123 | + disable_lockfile_download() |
| 124 | + } |
| 125 | + } else if (process$status() == "error") { |
| 126 | + disable_lockfile_download() |
| 127 | + } |
| 128 | + }) |
| 129 | + |
| 130 | + NULL |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +utils::globalVariables(c("opts", "sysenv", "libpaths", "wd", "lockfilepath", "run")) # needed for mirai call |
| 135 | +#' @rdname module_teal_lockfile |
| 136 | +.teal_lockfile_process_invoke <- function(lockfile_path) { |
| 137 | + mirai_obj <- NULL |
| 138 | + process <- shiny::ExtendedTask$new(function() { |
| 139 | + m <- mirai::mirai( |
| 140 | + { |
| 141 | + options(opts) |
| 142 | + do.call(Sys.setenv, sysenv) |
| 143 | + .libPaths(libpaths) |
| 144 | + setwd(wd) |
| 145 | + run(lockfile_path = lockfile_path) |
| 146 | + }, |
| 147 | + run = .renv_snapshot, |
| 148 | + lockfile_path = lockfile_path, |
| 149 | + opts = options(), |
| 150 | + libpaths = .libPaths(), |
| 151 | + sysenv = as.list(Sys.getenv()), |
| 152 | + wd = getwd() |
| 153 | + ) |
| 154 | + mirai_obj <<- m |
| 155 | + m |
| 156 | + }) |
| 157 | + |
| 158 | + shiny::onStop(function() { |
| 159 | + if (mirai::unresolved(mirai_obj)) { |
| 160 | + logger::log_debug("Terminating a running lockfile process...") |
| 161 | + mirai::stop_mirai(mirai_obj) # this doesn't stop running - renv will be created even if session is closed |
| 162 | + } |
| 163 | + }) |
| 164 | + |
| 165 | + suppressWarnings({ # 'package:stats' may not be available when loading |
| 166 | + process$invoke() |
| 167 | + }) |
| 168 | + |
| 169 | + logger::log_debug("Lockfile creation started based on { getwd() }.") |
| 170 | + |
| 171 | + process |
| 172 | +} |
| 173 | + |
| 174 | +#' @rdname module_teal_lockfile |
| 175 | +.renv_snapshot <- function(lockfile_path) { |
| 176 | + out <- utils::capture.output( |
| 177 | + res <- renv::snapshot( |
| 178 | + lockfile = lockfile_path, |
| 179 | + prompt = FALSE, |
| 180 | + force = TRUE, |
| 181 | + type = renv::settings$snapshot.type() # see the section "Different ways of creating lockfile" above here |
| 182 | + ) |
| 183 | + ) |
| 184 | + |
| 185 | + list(out = out, res = res) |
| 186 | +} |
| 187 | + |
| 188 | +#' @rdname module_teal_lockfile |
| 189 | +.is_lockfile_deps_installed <- function() { |
| 190 | + requireNamespace("mirai", quietly = TRUE) && requireNamespace("renv", quietly = TRUE) |
| 191 | +} |
| 192 | + |
| 193 | +#' @rdname module_teal_lockfile |
| 194 | +.is_disabled_lockfile_scenario <- function() { |
| 195 | + identical(Sys.getenv("CALLR_IS_RUNNING"), "true") || # inside callr process |
| 196 | + identical(Sys.getenv("TESTTHAT"), "true") || # inside devtools::test |
| 197 | + !identical(Sys.getenv("QUARTO_PROJECT_ROOT"), "") || # inside Quarto process |
| 198 | + ( |
| 199 | + ("CheckExEnv" %in% search()) || any(c("_R_CHECK_TIMINGS_", "_R_CHECK_LICENSE_") %in% names(Sys.getenv())) |
| 200 | + ) # inside R CMD CHECK |
| 201 | +} |
0 commit comments