Debian Codegraph @

R/modify.R

raw
1
2 #' Modify a Lockfile
3 #'
4 #' Modify a project's lockfile, either interactively or non-interactively.
5 #'
6 #' After edit, if the lockfile edited is associated with the active project, any
7 #' state-related changes (e.g. to \R repositories) will be updated in the
8 #' current session.
9 #'
10 #' @inherit renv-params
11 #'
12 #' @param changes A list of changes to be merged into the lockfile.
13 #' When `NULL` (the default), the lockfile is instead opened for
14 #' interactive editing.
15 #'
16 #' @export
17 #'
18 #' @examples
19 #' \dontrun{
20 #'
21 #' # modify an existing lockfile
22 #' if (interactive())
23 #' renv::modify()
24 #'
25 #' }
26 modify <- function(project = NULL, changes = NULL) {
27 renv_scope_error_handler()
28 project <- renv_project_resolve(project)
29 renv_project_lock(project = project)
30 renv_modify_impl(project, changes)
31 invisible(project)
32 }
33
34 renv_modify_impl <- function(project, changes) {
35
36 lockfile <- if (is.null(changes))
37 renv_modify_interactive(project)
38 else
39 renv_modify_noninteractive(project, changes)
40
41 if (renv_project_loaded(project))
42 renv_modify_fini(lockfile)
43
44 }
45
46 renv_modify_interactive <- function(project) {
47
48 # check for interactive session
49 if (!interactive())
50 stop("can't modify lockfile in non-interactive session")
51
52 # resolve path to lockfile
53 lockpath <- renv_lockfile_path(project)
54 if (!file.exists(lockpath))
55 stopf("lockfile '%s' does not exist", renv_path_aliased(lockpath))
56
57 # copy the lockfile to a temporary file
58 dir <- renv_scope_tempfile("renv-lockfile-")
59 ensure_directory(dir)
60
61 templock <- file.path(dir, "renv.lock")
62 file.copy(lockpath, templock)
63
64 # edit the temporary lockfile
65 renv_file_edit(templock)
66
67 # check that the new lockfile can be read
68 withCallingHandlers(
69 lockfile <- catch(renv_lockfile_read(file = templock)),
70 error = function(cnd) {
71 stop(lines(
72 "renv was unable to parse the modified lockfile:",
73 conditionMessage(cnd),
74 "Your changes will be discarded"
75 ))
76 }
77 )
78
79 lockfile
80
81 }
82
83 renv_modify_noninteractive <- function(project, changes) {
84
85 # resolve path to lockfile
86 lockpath <- renv_lockfile_path(project)
87 if (!file.exists(lockpath))
88 stopf("lockfile '%s' does not exist", renv_path_aliased(lockpath))
89
90 # read it
91 lockfile <- renv_lockfile_read(file = lockpath)
92
93 # merge changes
94 merged <- overlay(lockfile, changes)
95
96 # write updated lockfile to a temporary file
97 templock <- renv_scope_tempfile("renv-lock-")
98 renv_lockfile_write(merged, file = templock)
99
100 # try reading it once more
101 newlock <- renv_lockfile_read(file = templock)
102 if (!identical(merged, newlock))
103 stop("modify produced an invalid lockfile")
104
105 # overwrite the original lockfile
106 file.rename(templock, lockpath)
107
108 # finish up
109 merged
110
111 }
112
113 renv_modify_fini <- function(lockfile) {
114
115 # synchronize relevant changes into the session
116 repos <- lockfile$R$Repositories
117 options(repos = convert(repos, "character"))
118
119 }