Debian Codegraph @

R/snapshot-auto.R

raw
1
2 # information about the project library; used to detect whether
3 # the library appears to have been modified or updated
4 the$library_info <- NULL
5
6 # are we forcing automatic snapshots?
7 the$auto_snapshot_forced <- FALSE
8
9 # did the last attempt at an automatic snapshot fail?
10 the$auto_snapshot_failed <- FALSE
11
12 # are we currently running an automatic snapshot?
13 the$auto_snapshot_running <- FALSE
14
15 # is the next automatic snapshot suppressed?
16 the$auto_snapshot_suppressed <- FALSE
17
18 # nocov start
19 renv_snapshot_auto <- function(project) {
20
21 # set some state so we know we're running
22 the$auto_snapshot_running <- TRUE
23 defer(the$auto_snapshot_running <- FALSE)
24
25 # passed pre-flight checks; snapshot the library
26 updated <- withCallingHandlers(
27
28 tryCatch(
29 renv_snapshot_auto_impl(project),
30 error = function(err) FALSE
31 ),
32
33 cancel = function() FALSE
34
35 )
36
37 if (updated) {
38 lockfile <- renv_path_aliased(renv_lockfile_path(project))
39 writef("- Automatic snapshot has updated '%s'.", lockfile)
40 }
41
42 invisible(updated)
43
44 }
45
46 renv_snapshot_auto_impl <- function(project) {
47
48 # validation messages can be noisy; turn off for auto snapshot
49 renv_scope_options(
50 renv.config.snapshot.validate = FALSE,
51 renv.verbose = FALSE
52 )
53
54 # file.info() can warn in some cases; silence those
55 renv_scope_options(warn = -1L)
56
57 # get current lockfile state
58 lockfile <- renv_paths_lockfile(project)
59 old <- file.info(lockfile, extra_cols = FALSE)$mtime
60
61 # perform snapshot without prompting
62 snapshot(project = project, prompt = FALSE)
63
64 # check for change in lockfile
65 new <- file.info(lockfile, extra_cols = FALSE)$mtime
66 !identical(old, new)
67
68 }
69
70 renv_snapshot_auto_enabled <- function(project = renv_project_get()) {
71
72 # respect override
73 if (the$auto_snapshot_forced)
74 return(TRUE)
75
76 # respect config setting
77 enabled <- config$auto.snapshot(project = project)
78 if (!enabled)
79 return(FALSE)
80
81 # only snapshot interactively
82 if (!interactive())
83 return(FALSE)
84
85 # only automatically snapshot the current project
86 if (!renv_project_loaded(project))
87 return(FALSE)
88
89 # don't auto-snapshot if the project hasn't been initialized
90 if (!renv_project_initialized(project = project))
91 return(FALSE)
92
93 # don't auto-snapshot if we don't have a library
94 library <- renv_paths_library(project = project)
95 if (!file.exists(library))
96 return(FALSE)
97
98 # don't auto-snapshot unless the active library is the project library
99 if (!renv_file_same(renv_libpaths_active(), library))
100 return(FALSE)
101
102 TRUE
103
104 }
105
106 renv_snapshot_auto_update <- function(project = renv_project_get() ) {
107
108 # check for enabled
109 if (!renv_snapshot_auto_enabled(project = project))
110 return(FALSE)
111
112 # get path to project library
113 libpath <- renv_paths_library(project = project)
114 if (!file.exists(libpath))
115 return(FALSE)
116
117 # list files + get file info for files in project library
118 info <- renv_file_info(libpath)
119
120 # only keep relevant fields
121 fields <- c("size", "mtime", "ctime")
122 new <- c(info[fields])
123
124 # update our cached info
125 old <- the$library_info
126 the$library_info <- new
127
128 # if we've suppressed the next automatic snapshot, bail here
129 if (the$auto_snapshot_suppressed) {
130 the$auto_snapshot_suppressed <- FALSE
131 return(FALSE)
132 }
133
134 # report if things have changed
135 !is.null(old) && !identical(old, new)
136
137 }
138
139 renv_snapshot_task <- function() {
140
141 # if the previous snapshot attempt failed, do nothing
142 if (the$auto_snapshot_failed)
143 return(FALSE)
144
145 # silence warnings in this scope
146 renv_scope_options(warn = -1L)
147
148 # attempt automatic snapshot, but disable on failure
149 tryCatch(
150 renv_snapshot_task_impl(),
151 error = function(cnd) {
152 caution("Error generating automatic snapshot: %s", conditionMessage(cnd))
153 caution("Automatic snapshots will be disabled. Use `renv::snapshot()` to manually update the lockfile.")
154 the$auto_snapshot_failed <- TRUE
155 }
156 )
157
158 }
159
160 renv_snapshot_task_impl <- function() {
161
162 # check for active renv project
163 project <- renv_project_get()
164 if (is.null(project))
165 return(invisible(FALSE))
166
167 # see if library state has updated
168 updated <- renv_snapshot_auto_update(project = project)
169 if (!updated)
170 return(invisible(FALSE))
171
172 # library has updated; perform auto snapshot
173 status <- renv_snapshot_auto(project = project)
174 ok <- identical(status, TRUE)
175
176 # return invisibly for snapshot tests
177 invisible(ok)
178
179 }
180
181 renv_snapshot_auto_suppress_next <- function() {
182
183 # if we're currently running an automatic snapshot, then nothing to do
184 if (the$auto_snapshot_running)
185 return()
186
187 # otherwise, set the suppressed flag
188 the$auto_snapshot_suppressed <- TRUE
189
190 }
191
192 # nocov end