|
8 | 8 | # This file contains RPC functions for the packages pane. |
9 | 9 | # These functions are called via callMethod from the Positron R extension. |
10 | 10 |
|
11 | | -# Normalize a DESCRIPTION field value for display: collapse whitespace/newlines |
12 | | -# into single spaces so a multi-line Description fits on one line in the UI. |
13 | | -.ps.pkg_description_text <- function(value) { |
14 | | - if (is.null(value) || is.na(value)) { |
15 | | - return("") |
16 | | - } |
17 | | - value <- as.character(value) |
18 | | - value <- gsub("\\s+", " ", value, perl = TRUE) |
19 | | - trimws(value) |
20 | | -} |
21 | | - |
22 | | -# Normalize a Maintainer/Author value for display: collapse whitespace and strip |
23 | | -# trailing email addresses in angle brackets (e.g., "Hadley Wickham <h@posit.co>" |
24 | | -# becomes "Hadley Wickham"). |
25 | | -.ps.pkg_author_text <- function(value) { |
26 | | - if (is.null(value) || is.na(value)) { |
27 | | - return("") |
28 | | - } |
29 | | - value <- as.character(value) |
30 | | - value <- gsub("\\s+", " ", value, perl = TRUE) |
31 | | - value <- gsub("\\s*<[^>]+>", "", value, perl = TRUE) |
32 | | - trimws(value) |
33 | | -} |
34 | | - |
35 | | -# Build the package list via utils::installed.packages(), optionally scoped to a |
36 | | -# specific library path (used by the renv method). |
37 | 11 | .ps.pkg_list_installed <- function(lib.loc = NULL) { |
38 | 12 | ip <- utils::installed.packages( |
39 | 13 | lib.loc = lib.loc, |
40 | 14 | fields = c("Description", "Maintainer") |
41 | 15 | ) |
42 | | - lapply(seq_len(nrow(ip)), function(i) { |
43 | | - list( |
44 | | - id = paste0(ip[i, "Package"], "-", ip[i, "Version"]), |
45 | | - name = ip[i, "Package"], |
46 | | - displayName = ip[i, "Package"], |
47 | | - version = ip[i, "Version"], |
48 | | - description = .ps.pkg_description_text(ip[i, "Description"]), |
49 | | - author = .ps.pkg_author_text(ip[i, "Maintainer"]) |
50 | | - ) |
51 | | - }) |
| 16 | + |
| 17 | + name <- ip[, "Package"] |
| 18 | + version <- ip[, "Version"] |
| 19 | + id <- paste0(name, "-", version) |
| 20 | + # Collapse whitespace so a multi-line Description fits on one line in the UI. |
| 21 | + description <- trimws(gsub( |
| 22 | + "\\s+", |
| 23 | + " ", |
| 24 | + ifelse(is.na(ip[, "Description"]), "", ip[, "Description"]), |
| 25 | + perl = TRUE |
| 26 | + )) |
| 27 | + # Strip "<email>" markers so "Hadley Wickham <h@posit.co>" becomes "Hadley Wickham". |
| 28 | + author <- trimws(gsub( |
| 29 | + "\\s*<[^>]+>", |
| 30 | + "", |
| 31 | + gsub( |
| 32 | + "\\s+", |
| 33 | + " ", |
| 34 | + ifelse(is.na(ip[, "Maintainer"]), "", ip[, "Maintainer"]), |
| 35 | + perl = TRUE |
| 36 | + ), |
| 37 | + perl = TRUE |
| 38 | + )) |
| 39 | + |
| 40 | + unname(Map( |
| 41 | + list, |
| 42 | + id = id, |
| 43 | + name = name, |
| 44 | + displayName = name, |
| 45 | + version = version, |
| 46 | + description = description, |
| 47 | + author = author |
| 48 | + )) |
52 | 49 | } |
53 | 50 |
|
54 | | -# Return a list of installed packages |
| 51 | +# Return a list of installed packages. The pak/base/renv methods exist for |
| 52 | +# parity with install/update operations; for listing we always use |
| 53 | +# utils::installed.packages(), scoped to the renv library when requested. |
55 | 54 | #' @export |
56 | 55 | .ps.rpc.pkg_list <- function(method = c("pak", "base", "renv")) { |
57 | 56 | method <- match.arg(method) |
58 | | - switch( |
59 | | - method, |
60 | | - pak = { |
61 | | - old_opt <- options(pak.no_extra_messages = TRUE) |
62 | | - on.exit(options(old_opt), add = TRUE) |
63 | | - pkgs <- pak::lib_status() |
64 | | - lapply(seq_len(nrow(pkgs)), function(i) { |
65 | | - list( |
66 | | - id = paste0(pkgs$package[[i]], "-", pkgs$version[[i]]), |
67 | | - name = pkgs$package[[i]], |
68 | | - displayName = pkgs$package[[i]], |
69 | | - version = as.character(pkgs$version[[i]]), |
70 | | - description = .ps.pkg_description_text(pkgs$description[[i]]), |
71 | | - author = .ps.pkg_author_text(pkgs$maintainer[[i]]) |
72 | | - ) |
73 | | - }) |
74 | | - }, |
75 | | - base = .ps.pkg_list_installed(), |
76 | | - renv = .ps.pkg_list_installed(lib.loc = renv::paths$library()) |
77 | | - ) |
| 57 | + lib.loc <- if (method == "renv") renv::paths$library() else NULL |
| 58 | + .ps.pkg_list_installed(lib.loc = lib.loc) |
78 | 59 | } |
79 | 60 |
|
80 | 61 |
|
|
0 commit comments