forked from REditorSupport/vscode-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
394 lines (335 loc) · 12.8 KB
/
Copy pathutil.ts
File metadata and controls
394 lines (335 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
'use strict';
import { existsSync, PathLike, readFile } from 'fs-extra';
import * as fs from 'fs';
import winreg = require('winreg');
import * as path from 'path';
import * as vscode from 'vscode';
import * as cp from 'child_process';
import { rGuestService, isGuestSession } from './liveShare';
import { extensionContext } from './extension';
export function config(): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration('r');
}
function getRfromEnvPath(platform: string) {
let splitChar = ':';
let fileExtension = '';
if (platform === 'win32') {
splitChar = ';';
fileExtension = '.exe';
}
const os_paths: string[] | string = process.env.PATH.split(splitChar);
for (const os_path of os_paths) {
const os_r_path: string = path.join(os_path, 'R' + fileExtension);
if (fs.existsSync(os_r_path)) {
return os_r_path;
}
}
return '';
}
export async function getRpathFromSystem(): Promise<string> {
let rpath = '';
const platform: string = process.platform;
rpath ||= getRfromEnvPath(platform);
if ( !rpath && platform === 'win32') {
// Find path from registry
try {
const key = new winreg({
hive: winreg.HKLM,
key: '\\Software\\R-Core\\R',
});
const item: winreg.RegistryItem = await new Promise((c, e) =>
key.get('InstallPath', (err, result) => err === null ? c(result) : e(err)));
rpath = path.join(item.value, 'bin', 'R.exe');
} catch (e) {
rpath = '';
}
}
return rpath;
}
export function getRPathConfigEntry(term: boolean = false): string {
const trunc = (term ? 'rterm' : 'rpath');
const platform = (
process.platform === 'win32' ? 'windows' :
process.platform === 'darwin' ? 'mac' :
'linux'
);
return `${trunc}.${platform}`;
}
export async function getRpath(quote = false, overwriteConfig?: string): Promise<string> {
let rpath = '';
// try the config entry specified in the function arg:
if (overwriteConfig) {
rpath = config().get<string>(overwriteConfig);
}
// try the os-specific config entry for the rpath:
const configEntry = getRPathConfigEntry();
rpath ||= config().get<string>(configEntry);
// read from path/registry:
rpath ||= await getRpathFromSystem();
// represent all invalid paths (undefined, '', null) as undefined:
rpath ||= undefined;
if (!rpath) {
// inform user about missing R path:
void vscode.window.showErrorMessage(`Cannot find R to use for help, package installation etc. Change setting r.${configEntry} to R path.`);
} else if (quote && /^[^'"].* .*[^'"]$/.exec(rpath)) {
// if requested and rpath contains spaces, add quotes:
rpath = `"${rpath}"`;
} else if (process.platform === 'win32' && /^'.* .*'$/.exec(rpath)) {
// replace single quotes with double quotes on windows
rpath = rpath.replace(/^'(.*)'$/, '"$1"');
}
return rpath;
}
export async function getRterm(): Promise<string | undefined> {
const configEntry = getRPathConfigEntry(true);
let rpath = config().get<string>(configEntry);
rpath ||= await getRpathFromSystem();
if (rpath !== '') {
return rpath;
}
void vscode.window.showErrorMessage(`Cannot find R for creating R terminal. Change setting r.${configEntry} to R path.`);
return undefined;
}
export function ToRStringLiteral(s: string, quote: string): string {
if (s === undefined) {
return 'NULL';
}
return (quote +
s.replace(/\\/g, '\\\\')
.replace(/"""/g, `\\${quote}`)
.replace(/\\n/g, '\\n')
.replace(/\\r/g, '\\r')
.replace(/\\t/g, '\\t')
.replace(/\\b/g, '\\b')
.replace(/\\a/g, '\\a')
.replace(/\\f/g, '\\f')
.replace(/\\v/g, '\\v') +
quote);
}
export async function delay(ms: number): Promise<unknown> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function checkForSpecialCharacters(text: string): boolean {
return !/[~`!#$%^&*+=\-[\]\\';,/{}|\\":<>?\s]/g.test(text);
}
export function checkIfFileExists(filePath: string): boolean {
return existsSync(filePath);
}
// Drop-in replacement for fs-extra.readFile (),
// passes to guest service if the caller is a guest
// This can be used wherever fs.readFile() is used,
// particularly if a guest can access the function
//
// If it is a guest, the guest service requests the host
// to read the file, and pass back its contents to the guest
export function readContent(file: PathLike | number): Promise<Buffer>;
export function readContent(file: PathLike | number, encoding: string): Promise<string>;
export function readContent(file: PathLike | number, encoding?: string): Promise<string | Buffer> {
if (isGuestSession) {
return encoding === undefined ? rGuestService.requestFileContent(file) : rGuestService.requestFileContent(file, encoding);
} else {
return encoding === undefined ? readFile(file) : readFile(file, encoding);
}
}
export async function saveDocument(document: vscode.TextDocument): Promise<boolean> {
if (document.isUntitled) {
void vscode.window.showErrorMessage('Document is unsaved. Please save and retry running R command.');
return false;
}
const isSaved: boolean = document.isDirty ? (await document.save()) : true;
if (!isSaved) {
void vscode.window.showErrorMessage('Cannot run R command: document could not be saved.');
return false;
}
return true;
}
// shows a quick pick asking the user for confirmation
// returns true if the user confirms, false if they cancel or dismiss the quickpick
export async function getConfirmation(prompt: string, confirmation?: string, detail?: string): Promise<boolean> {
confirmation ||= 'Yes';
const items: vscode.QuickPickItem[] = [
{
label: confirmation,
detail: detail
},
{
label: 'Cancel'
}
];
const answer = await vscode.window.showQuickPick(items, {
placeHolder: prompt
});
return answer === items[0];
}
// executes a given command as shell task
// is more transparent thatn background processes without littering the integrated terminals
// is not intended for actual user interaction
export async function executeAsTask(name: string, command: string, args?: string[]): Promise<void> {
const taskDefinition = { type: 'shell' };
const quotedArgs = args.map<vscode.ShellQuotedString>(arg => { return { value: arg, quoting: vscode.ShellQuoting.Weak }; });
const taskExecution = new vscode.ShellExecution(
command,
quotedArgs
);
const task = new vscode.Task(
taskDefinition,
vscode.TaskScope.Global,
name,
'R',
taskExecution,
[]
);
const taskExecutionRunning = await vscode.tasks.executeTask(task);
const taskDonePromise = new Promise<void>((resolve) => {
vscode.tasks.onDidEndTask(e => {
if (e.execution === taskExecutionRunning) {
resolve();
}
});
});
return await taskDonePromise;
}
// executes a callback and shows a 'busy' progress bar during the execution
// synchronous callbacks are converted to async to properly render the progress bar
// default location is in the help pages tree view
export async function doWithProgress<T>(cb: (token?: vscode.CancellationToken, progress?: vscode.Progress<T>) => T | Promise<T>, location: string | vscode.ProgressLocation = 'rHelpPages', title?: string, cancellable?: boolean): Promise<T> {
const location2 = (typeof location === 'string' ? { viewId: location } : location);
const options: vscode.ProgressOptions = {
location: location2,
cancellable: cancellable ?? false,
title: title
};
let ret: T;
await vscode.window.withProgress(options, async (progress, token) => {
const retPromise = new Promise<T>((resolve) => setTimeout(() => {
const ret = cb(token, progress);
resolve(ret);
}));
ret = await retPromise;
});
return ret;
}
// get the URL of a CRAN website
// argument path is optional and should be relative to the cran root
// currently the CRAN root url is hardcoded, this could be replaced by reading
// the url from config, R, or both
export async function getCranUrl(path?: string, cwd?: string): Promise<string> {
const defaultCranUrl = 'https://cran.r-project.org/';
// get cran URL from R. Returns empty string if option is not set.
const baseUrl = await executeRCommand('cat(getOption(\'repos\')[\'CRAN\'])', undefined, cwd);
let url: string;
try {
url = new URL(path, baseUrl).toString();
} catch (e) {
url = new URL(path, defaultCranUrl).toString();
}
return url;
}
// executes an R command returns its output to stdout
// uses a regex to filter out output generated e.g. by code in .Rprofile
// returns the provided fallBack when the command failes
//
// WARNING: Cannot handle double quotes in the R command! (e.g. `print("hello world")`)
// Single quotes are ok.
//
export async function executeRCommand(rCommand: string, fallBack?: string, cwd?: string): Promise<string | undefined> {
const lim = '---vsc---';
const re = new RegExp(`${lim}(.*)${lim}`, 'ms');
const args = [
'--silent',
'--slave',
'--no-save',
'--no-restore',
];
const rPath = await getRpath(true);
const options: cp.ExecSyncOptionsWithStringEncoding = {
cwd: cwd,
encoding: 'utf-8'
};
const cmd = (
`${rPath} ${args.join(' ')} -e "cat('${lim}')" -e "${rCommand}" -e "cat('${lim}')"`
);
let ret: string = undefined;
try {
const stdout = cp.execSync(cmd, options);
ret = stdout.replace(re, '$1');
} catch (e) {
if (fallBack) {
ret = fallBack;
} else {
console.warn(e);
}
}
return ret;
}
// This class is a wrapper around Map<string, any> that implements vscode.Memento
// Can be used in place of vscode.ExtensionContext.globalState or .workspaceState when no caching is desired
export class DummyMemento implements vscode.Memento {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
items = new Map<string, any>()
public get<T>(key: string, defaultValue?: T): T | undefined {
if (this.items.has(key)) {
return <T>this.items.get(key) || defaultValue;
} else {
return defaultValue;
}
}
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
public async update(key: string, value: any): Promise<void> {
this.items.set(key, value);
}
public keys(): readonly string[] {
return Object.keys(this.items);
}
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
export async function setContext(key: string, value: any): Promise<void> {
await vscode.commands.executeCommand(
'setContext', key, value
);
}
// Helper function used to convert raw text files to html
export function escapeHtml(source: string): string {
const entityMap = new Map<string, string>(Object.entries({
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
'/': '/'
}));
return String(source).replace(/[&<>"'/]/g, (s: string) => entityMap.get(s) || '');
}
// creates a directory if it doesn't exist,
// returns the input string
export function getDir(dirPath: string): string {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
return dirPath;
}
export class UriIcon {
dark: vscode.Uri;
light: vscode.Uri;
constructor(id: string) {
const extIconPath = extensionContext.asAbsolutePath('images/icons');
this.dark = vscode.Uri.file(path.join(extIconPath, 'dark', id + '.svg'));
this.light = vscode.Uri.file(path.join(extIconPath, 'light', id + '.svg'));
}
}
/**
* As Disposable.
*
* Create a dispose method for any given object, and push it to the
* extension subscriptions array
*
* @param {T} toDispose - the object to add dispose to
* @param {Function} disposeFunction - the method called when the object is disposed
* @returns returned object is considered types T and vscode.Disposable
*/
export function asDisposable<T>(toDispose: T, disposeFunction: (...args: unknown[]) => unknown): T & vscode.Disposable {
type disposeType = T & vscode.Disposable;
(toDispose as disposeType).dispose = () => disposeFunction();
extensionContext.subscriptions.push(toDispose as disposeType);
return toDispose as disposeType;
}