Debian Codegraph @

R/lockfile-api.R

raw
1
2 # NOTE: These functions are used by the 'dockerfiler' package, even though
3 # they are not exported. We retain these functions here just to avoid issues
4 # during CRAN submission. We'll consider removing them in a future release.
5
6 renv_lockfile_api <- function(lockfile = NULL) {
7
8 .lockfile <- lockfile
9 .self <- new.env(parent = emptyenv())
10
11 .self$repos <- function(..., .repos = NULL) {
12
13 if (nargs() == 0) {
14 repos <- .lockfile$R$Repositories
15 return(repos)
16 }
17
18 repos <- .repos %||% list(...)
19 if (is.null(names(repos)) || "" %in% names(repos))
20 stop("repositories must all be named", call. = FALSE)
21
22 .lockfile$R$Repositories <<- as.list(convert(repos, "character"))
23 invisible(.self)
24
25 }
26
27 .self$version <- function(..., .version = NULL) {
28
29 if (nargs() == 0) {
30 version <- .lockfile$R$Version
31 return(version)
32 }
33
34 version <- .version %||% c(...)
35
36 if (length(version) > 1) {
37 stop("Version should be length 1 character e.g. `\"3.6.3\"`")
38 }
39
40 .lockfile$R$Version <<- version
41 invisible(.self)
42
43 }
44
45 .self$add <- function(..., .list = NULL) {
46
47 records <- renv_lockfile_records(.lockfile)
48
49 dots <- .list %||% list(...)
50 enumerate(dots, function(package, remote) {
51 resolved <- renv_remotes_resolve(remote)
52 records[[package]] <<- resolved
53 })
54
55 renv_lockfile_records(.lockfile) <<- records
56 invisible(.self)
57
58 }
59
60 .self$remove <- function(packages) {
61 records <- renv_lockfile_records(.lockfile) %>% omit(packages)
62 renv_lockfile_records(.lockfile) <<- records
63 invisible(.self)
64 }
65
66 .self$write <- function(file = stdout()) {
67 renv_lockfile_write(.lockfile, file = file)
68 invisible(.self)
69 }
70
71 .self$data <- function() {
72 .lockfile
73 }
74
75 class(.self) <- "renv_lockfile_api"
76 .self
77
78 }
79
80 #' Programmatically Create and Modify a Lockfile
81 #'
82 #' **NOTE: `lockfile()` is now internal, please use the \link{lockfiles}
83 #' API.**
84 #'
85 #' This function provides an API for creating and modifying `renv` lockfiles.
86 #' This can be useful when you'd like to programmatically generate or modify
87 #' a lockfile -- for example, because you want to update or change a package
88 #' record in an existing lockfile.
89 #'
90 #' @inheritParams renv-params
91 #'
92 #' @param file The path to an existing lockfile. When no lockfile is provided,
93 #' a new one will be created based on the current project context. If you
94 #' want to create a blank lockfile, use `file = NA` instead.
95 #'
96 #' @seealso \code{\link{lockfiles}}, for a description of the structure of an
97 #' `renv` lockfile.
98 #'
99 #' @examples
100 #'
101 #' \dontrun{
102 #'
103 #' lock <- lockfile("renv.lock")
104 #'
105 #' # set the repositories for a lockfile
106 #' lock$repos(CRAN = "https://cran.r-project.org")
107 #'
108 #' # depend on digest 0.6.22
109 #' lock$add(digest = "digest@@0.6.22")
110 #'
111 #' # write to file
112 #' lock$write("renv.lock")
113 #'
114 #' }
115 #'
116 #' @keywords internal
117 #' @rdname lockfile-api
118 #' @name lockfile-api
119 #'
120 lockfile <- function(file = NULL, project = NULL) {
121 project <- renv_project_resolve(project)
122 renv_scope_error_handler()
123
124 lock <- if (is.null(file)) {
125
126 renv_lockfile_create(
127 project = project,
128 libpaths = renv_libpaths_all(),
129 type = settings$snapshot.type(project = project)
130 )
131
132 } else if (is.na(file)) {
133
134 renv_lockfile_init(project)
135
136 } else {
137
138 renv_lockfile_read(file = file)
139
140 }
141
142 renv_lockfile_api(lock)
143
144 }