|
| 1 | +extra_dev_pkg <- c( |
| 2 | + "renv", "fusen", "devtools", |
| 3 | + "roxygen2", "usethis", "pkgload", |
| 4 | + "testthat", "remotes", "covr", |
| 5 | + "attachment", "pak", "dockerfiler", |
| 6 | + "pkgdown" |
| 7 | +) |
| 8 | + |
| 9 | +#' Create reproducible environments for your R projects with {renv} |
| 10 | +#' |
| 11 | +#' @description |
| 12 | +#' `r lifecycle::badge("experimental")` |
| 13 | +#' |
| 14 | +#' Tool to create and maintain renv.lock files. |
| 15 | +#' The idea is to have 2 distinct files, one for development and the other for deployment. |
| 16 | +#' Indeed, although packages like {attachment} or {pkgload} must be installed to develop, |
| 17 | +#' they are not necessary in your project, package or Shiny application. |
| 18 | +#' |
| 19 | +#' |
| 20 | +#' @param path Path to your current package source folder |
| 21 | +#' @param dev_pkg Package development toolbox you need. Use `_default` |
| 22 | +#' (with underscore before to avoid confusing with a package name), to |
| 23 | +#' use the default list. Use `NULL` for no extra package. |
| 24 | +#' Use `attachment:::extra_dev_pkg` for the list. |
| 25 | +#' @param folder_to_include Folder to scan to detect development packages |
| 26 | +#' @param output Path and name of the file created, default is `./renv.lock` |
| 27 | +#' @param install_if_missing Logical. Install missing packages. `TRUE` by default |
| 28 | +#' @param document Logical. Whether to run [att_amend_desc()] before |
| 29 | +#' detecting packages in DESCRIPTION. |
| 30 | +#' @param ... Other arguments to pass to [renv::snapshot()] |
| 31 | +#' |
| 32 | +#' @return a renv.lock file |
| 33 | +#' |
| 34 | +#' |
| 35 | +#' @importFrom cli cat_bullet |
| 36 | +#' @export |
| 37 | +#' |
| 38 | +#' @examples |
| 39 | +#' \dontrun{ |
| 40 | +#' create_renv_for_dev() |
| 41 | +#' create_renv_for_dev(dev_pkg = "attachment") |
| 42 | +#' create_renv_for_prod() |
| 43 | +#' } |
| 44 | +create_renv_for_dev <- function(path = ".", |
| 45 | + dev_pkg = "_default", |
| 46 | + folder_to_include = c("dev", "data-raw"), |
| 47 | + output = "renv.lock", |
| 48 | + install_if_missing = TRUE, |
| 49 | + document = TRUE, |
| 50 | + ...) { |
| 51 | + |
| 52 | + if (!requireNamespace("renv")) { |
| 53 | + stop("'renv' is required. Please install it before.") |
| 54 | + } |
| 55 | + |
| 56 | + path <- normalizePath(path) |
| 57 | + |
| 58 | + if (!is.null(dev_pkg) && "_default" %in% dev_pkg) { |
| 59 | + cli::cli_alert_info( |
| 60 | + paste('`dev_pkg = _default` includes: ', |
| 61 | + paste(extra_dev_pkg, collapse = ", "))) |
| 62 | + dev_pkg <- c(extra_dev_pkg, dev_pkg[dev_pkg != "_default"]) |
| 63 | + } |
| 64 | + |
| 65 | + if (isTRUE(document)) { |
| 66 | + att_amend_desc(path) |
| 67 | + } |
| 68 | + |
| 69 | + pkg_list <- |
| 70 | + c( |
| 71 | + att_from_description(path = file.path(path, "DESCRIPTION")), |
| 72 | + dev_pkg |
| 73 | + ) |
| 74 | + |
| 75 | + # Extra folders |
| 76 | + folder_to_include_relative <- folder_to_include |
| 77 | + folder_to_include <- file.path(path, folder_to_include) |
| 78 | + folder_exists <- dir.exists(folder_to_include) |
| 79 | + |
| 80 | + if (any(!folder_exists)) { |
| 81 | + cli::cli_alert_info( |
| 82 | + paste( |
| 83 | + "There is no directory named: ", |
| 84 | + paste(folder_to_include_relative[!folder_exists], collapse = ", "), |
| 85 | + ". This is removed from the exploration." |
| 86 | + ) |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + if (any(folder_exists)) { |
| 91 | + folder_to_include <- folder_to_include[folder_exists] |
| 92 | + |
| 93 | + # folder_to_include <- folder_to_include[dir.exists(file.path(path, folder_to_include))] |
| 94 | + |
| 95 | + from_r_script <- att_from_rscripts(folder_to_include) |
| 96 | + from_rmd <- att_from_rmds(folder_to_include) |
| 97 | + |
| 98 | + pkg_list <- unique(c(pkg_list, from_r_script, from_rmd)) |
| 99 | + } |
| 100 | + |
| 101 | + # Install |
| 102 | + if (install_if_missing) { |
| 103 | + install_if_missing(pkg_list) |
| 104 | + } |
| 105 | + |
| 106 | + cli::cat_bullet( |
| 107 | + sprintf("create renv.lock at %s", output), |
| 108 | + bullet = "tick", |
| 109 | + bullet_col = "green" |
| 110 | + ) |
| 111 | + |
| 112 | + renv::snapshot( |
| 113 | + packages = pkg_list, |
| 114 | + lockfile = output, |
| 115 | + prompt = FALSE, |
| 116 | + ... |
| 117 | + # type = "packages" |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | + if (!file.exists(output)) { |
| 122 | + stop("error during renv.lock creation") |
| 123 | + } |
| 124 | + |
| 125 | + output |
| 126 | +} |
| 127 | + |
| 128 | +#' @export |
| 129 | +#' @rdname create_renv_for_dev |
| 130 | +create_renv_for_prod <- function(path = ".", output = "renv.lock.prod", dev_pkg = "remotes", ...) { |
| 131 | + create_renv_for_dev( |
| 132 | + path = path, |
| 133 | + dev_pkg = dev_pkg, |
| 134 | + folder_to_include = NULL, |
| 135 | + output = output, |
| 136 | + ... |
| 137 | + ) |
| 138 | +} |
| 139 | + |
0 commit comments