Debian Codegraph @

R/plan.R

raw
1
2 #' Plan package installations
3 #'
4 #' `renv::plan()` resolves the packages required by a project and generates
5 #' a lockfile, without installing any packages. This allows you to see what
6 #' `renv::restore()` would do before actually performing the installation.
7 #'
8 #' Because packages are not installed, the generated lockfile is based on
9 #' package metadata from the configured repositories (i.e. the `PACKAGES`
10 #' file). This means the lockfile may contain only a subset of the fields
11 #' that would be present in a lockfile generated by [renv::snapshot()],
12 #' which reads the full `DESCRIPTION` from installed packages. The
13 #' generated lockfile is still suitable for use with [renv::restore()].
14 #'
15 #' @inheritParams renv-params
16 #'
17 #' @param packages The packages to be included in the plan. When `NULL`
18 #' (the default), all packages currently used in the project will be
19 #' included, as determined by [renv::dependencies()].
20 #'
21 #' @param lockfile The path where the generated lockfile should be written.
22 #' Use `NULL` to skip writing the lockfile.
23 #'
24 #' @return The generated lockfile, invisibly.
25 #'
26 #' @examples
27 #' \dontrun{
28 #'
29 #' # plan the packages required by the current project
30 #' renv::plan()
31 #'
32 #' # plan a specific set of packages
33 #' renv::plan(packages = c("dplyr", "ggplot2"))
34 #'
35 #' }
36 #' @export
37 plan <- function(packages = NULL,
38 ...,
39 dependencies = NULL,
40 lockfile = paths$lockfile(project = project),
41 project = NULL)
42 {
43 renv_consent_check()
44 renv_scope_error_handler()
45 renv_dots_check(...)
46
47 project <- renv_project_resolve(project)
48 renv_project_lock(project = project)
49
50 # resolve the set of packages to plan for
51 requested <- packages
52 packages <- packages %||% renv_plan_packages(project)
53
54 # expand dependency fields
55 fields <- renv_dependencies_fields(dependencies, project = project)
56
57 # get available packages database
58 dbs <- available_packages(type = "source")
59 if (is.null(dbs))
60 stop("no package repositories are available")
61
62 db <- renv_available_packages_flatten(dbs)
63
64 # warn about user-requested packages not available in repositories
65 renv_checkout_report(requested, db)
66
67 # resolve the full recursive dependency tree
68 packages <- renv_checkout_resolve(packages, db, fields, project = project)
69
70 # build enriched lockfile records from available packages metadata
71 records <- renv_checkout_records(packages, db)
72
73 # assemble the lockfile
74 new <- renv_lockfile_init(project)
75 new$Packages <- records
76 new <- renv_lockfile_fini(new, project)
77 class(new) <- "renv_lockfile"
78
79 # compare against existing lockfile if one exists
80 renv_plan_report(new, fields)
81
82 # write the lockfile
83 if (!is.null(lockfile)) {
84 ensure_parent_directory(lockfile)
85 renv_lockfile_write(new, file = lockfile)
86 }
87
88 invisible(new)
89
90 }
91
92 renv_plan_packages <- function(project) {
93 renv_dependencies_impl(
94 project,
95 field = "Package",
96 dev = TRUE
97 )
98 }
99
100 renv_plan_report <- function(new, fields) {
101
102 records <- renv_lockfile_records(new)
103 if (empty(records)) {
104 writef("- No packages found for this project.")
105 return(invisible())
106 }
107
108 writef("The following dependency tree will be encoded in the lockfile:\n")
109 roots <- renv_graph_roots(records, fields)
110 renv_graph_print(records, roots, fields)
111
112 }