Repository navigation
Expand file tree
/
Copy pathrPathResolver.ts
More file actions
230 lines (201 loc) · 7.39 KB
/
Copy pathrPathResolver.ts
File metadata and controls
230 lines (201 loc) · 7.39 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
import * as path from 'path';
import { ConfigurationReader, getMigratedSetting } from './configuration';
export const rExecutablePathSetting = 'executablePath';
export const rConsolePathSetting = 'consolePath';
export interface PathVariables {
userHome?: string;
workspaceFolder?: string;
fileWorkspaceFolder?: string;
fileDirname?: string;
}
export interface ExecutableResolution {
path?: string;
setting?: string;
quote?: '"' | '\'';
}
export interface RPathResolverDependencies {
getSetting: (setting: string) => string | undefined;
inspectSetting?: ConfigurationReader<string>['inspect'];
substituteVariables: (value: string) => string;
findExecutable: (name: string) => string | undefined;
pathExists: (value: string) => boolean;
getSystemR: () => Promise<string | undefined>;
}
export interface RPathResolverAdapterOptions<Resource> {
resource?: Resource;
getConfiguration: (resource?: Resource) => ConfigurationReader<string>;
substituteVariables: (value: string, resource?: Resource) => string;
findExecutable: (name: string) => string | undefined;
pathExists: (value: string) => boolean;
getSystemR: () => Promise<string | undefined>;
}
export interface SystemRResolverDependencies {
platform: NodeJS.Platform;
findExecutable: (name: string) => string | undefined;
getWindowsInstallPath: () => Promise<string | undefined>;
}
export function createRPathResolverDependencies<Resource>(
options: RPathResolverAdapterOptions<Resource>
): RPathResolverDependencies {
const currentConfig = options.getConfiguration(options.resource);
return {
getSetting: setting => currentConfig.get(setting),
inspectSetting: currentConfig.inspect?.bind(currentConfig),
substituteVariables: value => options.substituteVariables(value, options.resource),
findExecutable: options.findExecutable,
pathExists: options.pathExists,
getSystemR: options.getSystemR,
};
}
interface ConfiguredExecutableResolution extends ExecutableResolution {
setting: string;
}
export function substitutePathVariables(value: string, variables: PathVariables): string {
const substitutions: ReadonlyArray<[string, string | undefined]> = [
['${userHome}', variables.userHome],
['${workspaceFolder}', variables.workspaceFolder],
['${fileWorkspaceFolder}', variables.fileWorkspaceFolder],
['${fileDirname}', variables.fileDirname],
];
let result = value;
for (const [key, replacement] of substitutions) {
if (replacement) {
result = result.replaceAll(key, replacement);
}
}
return result;
}
export function selectWorkspaceFolder<T>(
workspaceFolders: readonly T[] | undefined,
activeFileWorkspaceFolder: T | undefined,
resourceWorkspaceFolder?: T
): T | undefined {
if (resourceWorkspaceFolder) {
return resourceWorkspaceFolder;
}
if (!workspaceFolders?.length) {
return undefined;
}
if (workspaceFolders.length === 1) {
return workspaceFolders[0];
}
return activeFileWorkspaceFolder ?? workspaceFolders[0];
}
export function findExecutableOnPath(
executableName: string,
platform: NodeJS.Platform,
pathValue: string | undefined,
exists: (value: string) => boolean
): string | undefined {
const pathImplementation = platform === 'win32' ? path.win32 : path.posix;
const delimiter = platform === 'win32' ? ';' : ':';
const extension = platform === 'win32' && !path.win32.extname(executableName) ? '.exe' : '';
for (const directory of pathValue?.split(delimiter) ?? []) {
const candidate = pathImplementation.join(directory, executableName + extension);
if (exists(candidate)) {
return candidate;
}
}
return undefined;
}
export async function resolveSystemR(dependencies: SystemRResolverDependencies): Promise<string | undefined> {
const fromPath = dependencies.findExecutable('R');
if (fromPath) {
return fromPath;
}
if (dependencies.platform === 'win32') {
const installPath = await dependencies.getWindowsInstallPath();
if (installPath) {
return path.win32.join(installPath, 'bin', 'R.exe');
}
}
return undefined;
}
export async function resolveBackgroundR(
dependencies: RPathResolverDependencies,
legacySetting: string
): Promise<ExecutableResolution> {
return resolveMigratedExecutable(dependencies, rExecutablePathSetting, legacySetting)
?? { path: await dependencies.getSystemR() };
}
export async function resolveConsoleR(
dependencies: RPathResolverDependencies,
legacySetting: string
): Promise<ExecutableResolution> {
return resolveMigratedExecutable(dependencies, rConsolePathSetting, legacySetting)
?? resolveWithFallback([rExecutablePathSetting], dependencies);
}
function resolveMigratedExecutable(
dependencies: RPathResolverDependencies,
canonicalKey: string,
legacyKey: string
): ConfiguredExecutableResolution | undefined {
const configured = getMigratedSetting<string>(
{
get: dependencies.getSetting,
inspect: dependencies.inspectSetting,
},
canonicalKey,
legacyKey,
value => Boolean(removeWrappingQuotes(value.trim()).value)
);
return configured && resolveConfiguredExecutable(configured.key, dependencies, configured.value);
}
export function formatRPath(
resolution: ExecutableResolution,
quote: boolean,
platform: NodeJS.Platform
): string | undefined {
if (!resolution.path || !quote) {
return resolution.path;
}
if (resolution.quote) {
if (platform === 'win32' && resolution.quote === '\'' && resolution.path.includes(' ')) {
return `"${resolution.path}"`;
}
return `${resolution.quote}${resolution.path}${resolution.quote}`;
}
return resolution.path.includes(' ') ? `"${resolution.path}"` : resolution.path;
}
async function resolveWithFallback(
settings: readonly string[],
dependencies: RPathResolverDependencies
): Promise<ExecutableResolution> {
for (const setting of settings) {
const configured = resolveConfiguredExecutable(setting, dependencies);
if (configured) {
return configured;
}
}
return { path: await dependencies.getSystemR() };
}
function resolveConfiguredExecutable(
setting: string,
dependencies: RPathResolverDependencies,
rawValue = dependencies.getSetting(setting)
): ConfiguredExecutableResolution | undefined {
if (!rawValue?.trim()) {
return undefined;
}
const substituted = dependencies.substituteVariables(rawValue).trim();
const { value, quote } = removeWrappingQuotes(substituted);
if (!value) {
return undefined;
}
const resolvedPath = isExecutableName(value)
? dependencies.findExecutable(value)
: dependencies.pathExists(value) ? value : undefined;
return { path: resolvedPath, setting, quote };
}
function removeWrappingQuotes(value: string): { value: string; quote?: '"' | '\'' } {
if (value.length >= 2) {
const first = value[0];
if ((first === '"' || first === '\'') && value.at(-1) === first) {
return { value: value.slice(1, -1), quote: first };
}
}
return { value };
}
function isExecutableName(value: string): boolean {
return !value.includes('/') && !value.includes('\\');
}