Debian Codegraph @

R/id.R

raw
1
2 renv_id_path <- function(project) {
3 file.path(project, "renv/project-id")
4 }
5
6 renv_id_generate <- function() {
7
8 methods <- list(
9 renv_id_generate_r,
10 renv_id_generate_kernel,
11 renv_id_generate_uuidgen,
12 renv_id_generate_cscript,
13 renv_id_generate_powershell,
14 renv_id_generate_csc
15 )
16
17 for (method in methods) {
18 id <- catch(method())
19 if (is.character(id) && length(id) == 1 && nzchar(id)) {
20 id <- toupper(id)
21 return(id)
22 }
23 }
24
25 stop("could not generate project id for this system")
26
27 }
28
29 renv_id_generate_kernel <- function() {
30
31 uuidpath <- "/proc/sys/kernel/random/uuid"
32 if (!file.exists(uuidpath)) {
33 fmt <- "%s does not exist on this operating system"
34 stopf(fmt, renv_path_pretty(uuidpath))
35 }
36
37 readLines(uuidpath, n = 1L, warn = FALSE)
38
39 }
40
41 renv_id_generate_uuidgen <- function() {
42
43 if (!nzchar(Sys.which("uuidgen"))) {
44 fmt <- "program %s does not exist on this system"
45 stopf(fmt, dQuote("uuidgen"))
46 }
47
48 system("uuidgen", intern = TRUE)
49
50 }
51
52 renv_id_generate_cscript <- function() {
53
54 if (!renv_platform_windows()) {
55 fmt <- "this method is only available on Windows"
56 stopf(fmt)
57 }
58
59 if (!nzchar(Sys.which("cscript.exe"))) {
60 fmt <- "could not find cscript.exe"
61 stopf(fmt)
62 }
63
64 # create temporary directory
65 dir <- renv_scope_tempfile("renv-id-")
66 dir.create(dir)
67
68 # move to it
69 renv_scope_wd(dir)
70
71 # write helper script
72 script <- c(
73 "set object = CreateObject(\"Scriptlet.TypeLib\")",
74 "WScript.StdOut.WriteLine object.GUID"
75 )
76
77 # invoke it
78 writeLines(script, con = "uuid.vbs")
79 args <- c("//NoLogo", "uuid.vbs")
80 id <- renv_system_exec("cscript.exe", args, "generating UUID")
81
82 # remove braces
83 gsub("(?:^\\{|\\}$)", "", id)
84
85 }
86
87 renv_id_generate_powershell <- function() {
88
89 if (!renv_platform_windows()) {
90 fmt <- "this method is only available on Windows"
91 stopf(fmt)
92 }
93
94 if (!nzchar(Sys.which("powershell.exe"))) {
95 fmt <- "could not find powershell.exe"
96 stopf(fmt)
97 }
98
99 command <- "[guid]::NewGuid().ToString()"
100 args <- c("-Command", shQuote(command))
101 renv_system_exec("powershell.exe", args, "generating UUID")
102
103 }
104
105 renv_id_generate_r <- function() {
106
107 if ("uuid" %in% loadedNamespaces())
108 return(uuid::UUIDgenerate())
109
110 libpaths <- c(
111 .libPaths(),
112 renv_libpaths_user(),
113 renv_libpaths_site(),
114 renv_libpaths_system()
115 )
116
117 if (!requireNamespace("uuid", lib.loc = libpaths, quietly = TRUE))
118 stop("could not load package 'uuid'")
119
120 id <- uuid::UUIDgenerate()
121 catchall(unloadNamespace("uuid"))
122 id
123
124 }
125
126 renv_id_generate_csc <- function() {
127
128 csc <- local({
129
130 csc <- Sys.which("csc.exe")
131 if (nzchar(csc))
132 return(csc)
133
134 frameworks <- file.path(
135 Sys.getenv("SYSTEMDRIVE", unset = "C:"),
136 "Windows/Microsoft.NET",
137 c("Framework", "Framework64")
138 )
139
140 versions <- list.files(frameworks, full.names = TRUE)
141 candidates <- file.path(versions, "csc.exe")
142 candidates[file.exists(candidates)]
143
144 })
145
146 if (empty(csc) || !file.exists(csc))
147 stop("could not find csc.exe")
148
149
150 code <- "
151 class GenerateUUID {
152 static void Main(string[] args) {
153 System.Console.WriteLine(System.Guid.NewGuid().ToString());
154 }
155 }
156 "
157
158 renv_scope_tempdir("renv-uuid-")
159 writeLines(code, con = "program.cs")
160
161 renv_system_exec(
162 csc[[1]],
163 c("/nologo", "/out:program.exe", "program.cs"),
164 "compiling uuid helper"
165 )
166
167 renv_system_exec("program.exe", character(), "generating uuid")
168
169 }