| 1 |
|
| 2 |
#' Migrate a project from packrat to renv |
| 3 |
#' |
| 4 |
#' Migrate a project's infrastructure from packrat to renv. |
| 5 |
#' |
| 6 |
#' # Migration |
| 7 |
#' |
| 8 |
#' When migrating Packrat projects to renv, the set of components migrated |
| 9 |
#' can be customized using the `packrat` argument. The set of components that |
| 10 |
#' can be migrated are as follows: |
| 11 |
#' |
| 12 |
#' \tabular{ll}{ |
| 13 |
#' |
| 14 |
#' **Name** \tab **Description** \cr |
| 15 |
#' |
| 16 |
#' `lockfile` \tab |
| 17 |
#' Migrate the Packrat lockfile (`packrat/packrat.lock`) to the renv lockfile |
| 18 |
#' (`renv.lock`). \cr |
| 19 |
#' |
| 20 |
#' `sources` \tab |
| 21 |
#' Migrate package sources from the `packrat/src` folder to the renv |
| 22 |
#' sources folder. Currently, only CRAN packages are migrated to renv -- |
| 23 |
#' packages retrieved from other sources (e.g. GitHub) are ignored. |
| 24 |
#' \cr |
| 25 |
#' |
| 26 |
#' `library` \tab |
| 27 |
#' Migrate installed packages from the Packrat library to the renv project |
| 28 |
#' library. |
| 29 |
#' \cr |
| 30 |
#' |
| 31 |
#' `options` \tab |
| 32 |
#' Migrate compatible Packrat options to the renv project. |
| 33 |
#' \cr |
| 34 |
#' |
| 35 |
#' `cache` \tab |
| 36 |
#' Migrate packages from the Packrat cache to the renv cache. |
| 37 |
#' \cr |
| 38 |
#' |
| 39 |
#' } |
| 40 |
#' |
| 41 |
#' @inherit renv-params |
| 42 |
#' |
| 43 |
#' @param packrat Components of the Packrat project to migrate. See the default |
| 44 |
#' argument list for components of the Packrat project that can be migrated. |
| 45 |
#' Select a subset of those components for migration as appropriate. |
| 46 |
#' |
| 47 |
#' @export |
| 48 |
#' |
| 49 |
#' @examples |
| 50 |
#' \dontrun{ |
| 51 |
#' |
| 52 |
#' # migrate Packrat project infrastructure to renv |
| 53 |
#' renv::migrate() |
| 54 |
#' |
| 55 |
#' } |
| 56 |
migrate <- function( |
| 57 |
project = NULL, |
| 58 |
packrat = c("lockfile", "sources", "library", "options", "cache")) |
| 59 |
{ |
| 60 |
renv_consent_check() |
| 61 |
renv_scope_error_handler() |
| 62 |
|
| 63 |
project <- renv_project_resolve(project) |
| 64 |
renv_project_lock(project = project) |
| 65 |
|
| 66 |
project <- renv_path_normalize(project, mustWork = TRUE) |
| 67 |
if (file.exists(file.path(project, "packrat/packrat.lock"))) { |
| 68 |
packrat <- match.arg(packrat, several.ok = TRUE) |
| 69 |
renv_migrate_packrat(project, packrat) |
| 70 |
} |
| 71 |
|
| 72 |
invisible(project) |
| 73 |
} |
| 74 |
|
| 75 |
renv_migrate_packrat <- function(project = NULL, components = NULL) { |
| 76 |
project <- renv_project_resolve(project) |
| 77 |
|
| 78 |
if (!requireNamespace("packrat", quietly = TRUE)) |
| 79 |
stopf("migration requires the 'packrat' package to be installed") |
| 80 |
|
| 81 |
callbacks <- list( |
| 82 |
lockfile = renv_migrate_packrat_lockfile, |
| 83 |
sources = renv_migrate_packrat_sources, |
| 84 |
library = renv_migrate_packrat_library, |
| 85 |
options = renv_migrate_packrat_options, |
| 86 |
cache = renv_migrate_packrat_cache |
| 87 |
) |
| 88 |
|
| 89 |
components <- components %||% names(callbacks) |
| 90 |
callbacks <- callbacks[components] |
| 91 |
for (callback in callbacks) |
| 92 |
callback(project) |
| 93 |
|
| 94 |
renv_migrate_packrat_infrastructure(project) |
| 95 |
renv_imbue_impl(project) |
| 96 |
|
| 97 |
fmt <- "- Project '%s' has been migrated from Packrat to renv." |
| 98 |
writef(fmt, renv_path_aliased(project)) |
| 99 |
|
| 100 |
writef("- Consider deleting the project 'packrat' folder if it is no longer needed.") |
| 101 |
invisible(TRUE) |
| 102 |
} |
| 103 |
|
| 104 |
renv_migrate_packrat_lockfile <- function(project) { |
| 105 |
|
| 106 |
plock <- file.path(project, "packrat/packrat.lock") |
| 107 |
if (!file.exists(plock)) |
| 108 |
return(FALSE) |
| 109 |
|
| 110 |
# read the lockfile |
| 111 |
contents <- read(plock) |
| 112 |
splat <- strsplit(contents, "\n{2,}")[[1]] |
| 113 |
dcf <- lapply(splat, function(section) { |
| 114 |
renv_dcf_read(text = section) |
| 115 |
}) |
| 116 |
|
| 117 |
# split into header + package fields |
| 118 |
header <- dcf[[1]] |
| 119 |
records <- dcf[-1L] |
| 120 |
|
| 121 |
# parse the repositories |
| 122 |
repos <- getOption("repos") |
| 123 |
if (!is.null(header$Repos)) { |
| 124 |
parts <- strsplit(header$Repos, "\\s*,\\s*")[[1]] |
| 125 |
repos <- renv_properties_read(text = parts, delimiter = "=") |
| 126 |
} |
| 127 |
|
| 128 |
# fix-up some record fields for renv |
| 129 |
fields <- c("Package", "Version", "Source") |
| 130 |
records <- lapply(records, function(record) { |
| 131 |
|
| 132 |
# remove an old packrat hash |
| 133 |
record$Hash <- NULL |
| 134 |
|
| 135 |
# add RemoteType for GitHub records |
| 136 |
if (any(grepl("^Github", names(record)))) |
| 137 |
record$RemoteType <- "github" |
| 138 |
|
| 139 |
# remap '^Github'-style records to '^Remote' |
| 140 |
map <- c( |
| 141 |
"GithubRepo" = "RemoteRepo", |
| 142 |
"GithubUsername" = "RemoteUsername", |
| 143 |
"GithubRef" = "RemoteRef", |
| 144 |
"GithubSha1" = "RemoteSha", |
| 145 |
"GithubSHA1" = "RemoteSha", |
| 146 |
"GithubSubdir" = "RemoteSubdir" |
| 147 |
) |
| 148 |
names(record) <- remap(names(record), map) |
| 149 |
|
| 150 |
# keep only fields of interest |
| 151 |
keep <- c(fields, grep("^Remote", names(record), value = TRUE)) |
| 152 |
as.list(record[keep]) |
| 153 |
|
| 154 |
}) |
| 155 |
|
| 156 |
# pull out names for records |
| 157 |
names(records) <- extract_chr(records, "Package") |
| 158 |
|
| 159 |
# ensure renv is added |
| 160 |
records <- renv_snapshot_fixup_renv(records) |
| 161 |
|
| 162 |
# generate a blank lockfile |
| 163 |
lockfile <- structure(list(), class = "renv_lockfile") |
| 164 |
lockfile$R <- renv_lockfile_init_r(project) |
| 165 |
|
| 166 |
# update fields |
| 167 |
lockfile$R$Version <- header$RVersion |
| 168 |
lockfile$R$Repositories <- as.list(repos) |
| 169 |
renv_lockfile_records(lockfile) <- records |
| 170 |
|
| 171 |
# finish |
| 172 |
lockfile <- renv_lockfile_fini(lockfile, project) |
| 173 |
|
| 174 |
# write the lockfile |
| 175 |
lockpath <- renv_lockfile_path(project = project) |
| 176 |
renv_lockfile_write(lockfile, file = lockpath) |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
renv_migrate_packrat_sources <- function(project) { |
| 181 |
|
| 182 |
packrat <- asNamespace("packrat") |
| 183 |
srcdir <- packrat$srcDir(project = project) |
| 184 |
if (!file.exists(srcdir)) |
| 185 |
return(TRUE) |
| 186 |
|
| 187 |
pattern <- paste0( |
| 188 |
"^", # start |
| 189 |
"[^_]+", # package name |
| 190 |
"_", # separator |
| 191 |
"\\d+(?:[_.-]\\d+)*", # version |
| 192 |
"\\.tar\\.gz", # extension |
| 193 |
"$" # end |
| 194 |
) |
| 195 |
|
| 196 |
suffixes <- list.files( |
| 197 |
srcdir, |
| 198 |
pattern = pattern, |
| 199 |
recursive = TRUE |
| 200 |
) |
| 201 |
|
| 202 |
sources <- file.path(srcdir, suffixes) |
| 203 |
targets <- renv_paths_source("cran", suffixes) |
| 204 |
|
| 205 |
keep <- !file.exists(targets) |
| 206 |
sources <- sources[keep]; targets <- targets[keep] |
| 207 |
|
| 208 |
printf("- Migrating package sources from Packrat to renv ... ") |
| 209 |
copy <- renv_progress_callback(renv_file_copy, length(targets)) |
| 210 |
mapply(sources, targets, FUN = function(source, target) { |
| 211 |
ensure_parent_directory(target) |
| 212 |
copy(source, target) |
| 213 |
}) |
| 214 |
writef("Done!") |
| 215 |
|
| 216 |
TRUE |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
renv_migrate_packrat_library <- function(project) { |
| 221 |
|
| 222 |
packrat <- asNamespace("packrat") |
| 223 |
|
| 224 |
libdir <- packrat$libDir(project = project) |
| 225 |
if (!file.exists(libdir)) |
| 226 |
return(TRUE) |
| 227 |
|
| 228 |
sources <- list.files(libdir, full.names = TRUE) |
| 229 |
if (empty(sources)) |
| 230 |
return(TRUE) |
| 231 |
|
| 232 |
targets <- renv_paths_library(basename(sources), project = project) |
| 233 |
|
| 234 |
names(targets) <- sources |
| 235 |
targets <- targets[!file.exists(targets)] |
| 236 |
if (empty(targets)) { |
| 237 |
writef("- The renv library is already synchronized with the Packrat library.") |
| 238 |
return(TRUE) |
| 239 |
} |
| 240 |
|
| 241 |
# copy packages from Packrat to renv private library |
| 242 |
printf("- Migrating library from Packrat to renv ... ") |
| 243 |
ensure_parent_directory(targets) |
| 244 |
copy <- renv_progress_callback(renv_file_copy, length(targets)) |
| 245 |
enumerate(targets, copy) |
| 246 |
writef("Done!") |
| 247 |
|
| 248 |
# move packages into the cache |
| 249 |
if (renv_cache_config_enabled(project = project)) { |
| 250 |
printf("- Moving packages into the renv cache ... ") |
| 251 |
records <- lapply(targets, renv_description_read) |
| 252 |
sync <- renv_progress_callback(renv_cache_synchronize, length(targets)) |
| 253 |
lapply(records, sync, linkable = TRUE) |
| 254 |
writef("Done!") |
| 255 |
} |
| 256 |
|
| 257 |
TRUE |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
renv_migrate_packrat_options <- function(project) { |
| 262 |
|
| 263 |
packrat <- asNamespace("packrat") |
| 264 |
opts <- packrat$get_opts(project = project) |
| 265 |
|
| 266 |
settings$ignored.packages(opts$ignored.packages, project = project) |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
renv_migrate_packrat_cache <- function(project) { |
| 271 |
|
| 272 |
# find packages in the packrat cache |
| 273 |
packrat <- asNamespace("packrat") |
| 274 |
cache <- packrat$cacheLibDir() |
| 275 |
packages <- list.files(cache, full.names = TRUE) |
| 276 |
hashes <- list.files(packages, full.names = TRUE) |
| 277 |
sources <- list.files(hashes, full.names = TRUE) |
| 278 |
|
| 279 |
# sanity check: make sure the source folder is an R package |
| 280 |
ok <- file.exists(file.path(sources, "DESCRIPTION")) |
| 281 |
sources <- sources[ok] |
| 282 |
|
| 283 |
# construct cache target paths |
| 284 |
targets <- map_chr(sources, renv_cache_path) |
| 285 |
names(targets) <- sources |
| 286 |
|
| 287 |
# only copy to cache target paths that don't exist |
| 288 |
targets <- targets[!file.exists(targets)] |
| 289 |
if (empty(targets)) { |
| 290 |
writef("- The renv cache is already synchronized with the Packrat cache.") |
| 291 |
return(TRUE) |
| 292 |
} |
| 293 |
|
| 294 |
# cache each installed package |
| 295 |
if (renv_cache_config_enabled(project = project)) |
| 296 |
renv_migrate_packrat_cache_impl(targets) |
| 297 |
|
| 298 |
TRUE |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
renv_migrate_packrat_cache_impl <- function(targets) { |
| 303 |
|
| 304 |
# attempt to copy packages from Packrat to renv cache |
| 305 |
printf("- Migrating Packrat cache to renv cache ... ") |
| 306 |
ensure_parent_directory(targets) |
| 307 |
copy <- renv_progress_callback(renv_file_copy, length(targets)) |
| 308 |
|
| 309 |
result <- enumerate(targets, function(source, target) { |
| 310 |
status <- catch(copy(source, target)) |
| 311 |
broken <- inherits(status, "error") |
| 312 |
reason <- if (broken) conditionMessage(status) else "" |
| 313 |
list(source = source, target = target, broken = broken, reason = reason) |
| 314 |
}) |
| 315 |
|
| 316 |
writef("Done!") |
| 317 |
|
| 318 |
# report failures |
| 319 |
status <- bind(result) |
| 320 |
bad <- status[status$broken, ] |
| 321 |
if (nrow(bad) == 0) |
| 322 |
return(TRUE) |
| 323 |
|
| 324 |
bulletin( |
| 325 |
"The following packages could not be copied from the Packrat cache:", |
| 326 |
with(bad, sprintf("%s [%s]", format(source), reason)), |
| 327 |
"These packages may need to be reinstalled and re-cached." |
| 328 |
) |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
renv_migrate_packrat_infrastructure <- function(project) { |
| 333 |
unlink(file.path(project, ".Rprofile")) |
| 334 |
renv_infrastructure_write(project) |
| 335 |
writef("- renv support infrastructure has been written.") |
| 336 |
TRUE |
| 337 |
} |