-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathutils.ts
More file actions
309 lines (278 loc) · 12.5 KB
/
utils.ts
File metadata and controls
309 lines (278 loc) · 12.5 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
import * as fs from 'fs-extra';
import path from 'path';
import { commands, ConfigurationTarget, l10n, window, workspace } from 'vscode';
import { PythonCommandRunConfiguration, PythonEnvironment, PythonEnvironmentApi } from '../../api';
import { traceLog, traceVerbose } from '../../common/logging';
import { isWindows } from '../../common/utils/platformUtils';
import { ShellConstants } from '../../features/common/shellConstants';
import { getDefaultEnvManagerSetting, setDefaultEnvManagerBroken } from '../../features/settings/settingHelpers';
import { PythonProjectManager } from '../../internal.api';
import { Installable } from './types';
export function noop() {
// do nothing
}
/**
* In **contrast** to just checking `typeof` this will return `false` for `NaN`.
* @returns whether the provided parameter is a JavaScript Number or not.
*/
export function isNumber(obj: unknown): obj is number {
return typeof obj === 'number' && !isNaN(obj);
}
export function shortVersion(version: string): string {
const pattern = /(\d)\.(\d+)(?:\.(\d+)?)?/gm;
const match = pattern.exec(version);
if (match) {
if (match[3]) {
return `${match[1]}.${match[2]}.${match[3]}`;
}
return `${match[1]}.${match[2]}.x`;
}
return version;
}
export function isGreater(a: string | undefined, b: string | undefined): boolean {
if (!a && !b) {
return false;
}
if (!a) {
return false;
}
if (!b) {
return true;
}
try {
const aParts = a.split('.');
const bParts = b.split('.');
for (let i = 0; i < aParts.length; i++) {
if (i >= bParts.length) {
return true;
}
const aPart = parseInt(aParts[i], 10);
const bPart = parseInt(bParts[i], 10);
if (aPart > bPart) {
return true;
}
if (aPart < bPart) {
return false;
}
}
} catch {
return false;
}
return false;
}
export function sortEnvironments(collection: PythonEnvironment[]): PythonEnvironment[] {
return collection.sort((a, b) => {
if (a.version !== b.version) {
return isGreater(a.version, b.version) ? -1 : 1;
}
const value = a.name.localeCompare(b.name);
if (value !== 0) {
return value;
}
return a.environmentPath.fsPath.localeCompare(b.environmentPath.fsPath);
});
}
export function getLatest(collection: PythonEnvironment[]): PythonEnvironment | undefined {
if (collection.length === 0) {
return undefined;
}
let latest = collection[0];
for (const env of collection) {
if (isGreater(env.version, latest.version)) {
latest = env;
}
}
return latest;
}
export function mergePackages(common: Installable[], installed: string[]): Installable[] {
const notInCommon = installed.filter((pkg) => !common.some((c) => c.name === pkg));
return common
.concat(notInCommon.map((pkg) => ({ name: pkg, displayName: pkg })))
.sort((a, b) => a.name.localeCompare(b.name));
}
export function pathForGitBash(binPath: string): string {
return isWindows() ? binPath.replace(/\\/g, '/').replace(/^([a-zA-Z]):/, '/$1') : binPath;
}
/**
* Compares two semantic version strings. Support sonly simple 1.1.1 style versions.
* @param version1 First version
* @param version2 Second version
* @returns -1 if version1 < version2, 0 if equal, 1 if version1 > version2
*/
export function compareVersions(version1: string, version2: string): number {
const v1Parts = version1.split('.').map(Number);
const v2Parts = version2.split('.').map(Number);
for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) {
const v1Part = v1Parts[i] || 0;
const v2Part = v2Parts[i] || 0;
if (v1Part > v2Part) {
return 1;
}
if (v1Part < v2Part) {
return -1;
}
}
return 0;
}
export async function getShellActivationCommands(binDir: string): Promise<{
shellActivation: Map<string, PythonCommandRunConfiguration[]>;
shellDeactivation: Map<string, PythonCommandRunConfiguration[]>;
}> {
const shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
const shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
if (isWindows()) {
shellActivation.set('unknown', [{ executable: path.join(binDir, `activate`) }]);
shellDeactivation.set('unknown', [{ executable: path.join(binDir, `deactivate`) }]);
} else {
shellActivation.set('unknown', [{ executable: 'source', args: [path.join(binDir, `activate`)] }]);
shellDeactivation.set('unknown', [{ executable: 'deactivate' }]);
}
shellActivation.set(ShellConstants.SH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]);
shellDeactivation.set(ShellConstants.SH, [{ executable: 'deactivate' }]);
shellActivation.set(ShellConstants.BASH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]);
shellDeactivation.set(ShellConstants.BASH, [{ executable: 'deactivate' }]);
shellActivation.set(ShellConstants.GITBASH, [
{ executable: 'source', args: [pathForGitBash(path.join(binDir, `activate`))] },
]);
shellDeactivation.set(ShellConstants.GITBASH, [{ executable: 'deactivate' }]);
shellActivation.set(ShellConstants.ZSH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]);
shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'deactivate' }]);
shellActivation.set(ShellConstants.KSH, [{ executable: '.', args: [path.join(binDir, `activate`)] }]);
shellDeactivation.set(ShellConstants.KSH, [{ executable: 'deactivate' }]);
if (await fs.pathExists(path.join(binDir, 'Activate.ps1'))) {
shellActivation.set(ShellConstants.PWSH, [{ executable: '&', args: [path.join(binDir, `Activate.ps1`)] }]);
shellDeactivation.set(ShellConstants.PWSH, [{ executable: 'deactivate' }]);
} else if (await fs.pathExists(path.join(binDir, 'activate.ps1'))) {
shellActivation.set(ShellConstants.PWSH, [{ executable: '&', args: [path.join(binDir, `activate.ps1`)] }]);
shellDeactivation.set(ShellConstants.PWSH, [{ executable: 'deactivate' }]);
}
if (await fs.pathExists(path.join(binDir, 'activate.bat'))) {
shellActivation.set(ShellConstants.CMD, [{ executable: path.join(binDir, `activate.bat`) }]);
shellDeactivation.set(ShellConstants.CMD, [{ executable: path.join(binDir, `deactivate.bat`) }]);
}
if (await fs.pathExists(path.join(binDir, 'activate.csh'))) {
shellActivation.set(ShellConstants.CSH, [{ executable: 'source', args: [path.join(binDir, `activate.csh`)] }]);
shellDeactivation.set(ShellConstants.CSH, [{ executable: 'deactivate' }]);
shellActivation.set(ShellConstants.FISH, [{ executable: 'source', args: [path.join(binDir, `activate.csh`)] }]);
shellDeactivation.set(ShellConstants.FISH, [{ executable: 'deactivate' }]);
}
if (await fs.pathExists(path.join(binDir, 'activate.fish'))) {
shellActivation.set(ShellConstants.FISH, [
{ executable: 'source', args: [path.join(binDir, `activate.fish`)] },
]);
shellDeactivation.set(ShellConstants.FISH, [{ executable: 'deactivate' }]);
}
if (await fs.pathExists(path.join(binDir, 'activate.xsh'))) {
shellActivation.set(ShellConstants.XONSH, [
{ executable: 'source', args: [path.join(binDir, `activate.xsh`)] },
]);
shellDeactivation.set(ShellConstants.XONSH, [{ executable: 'deactivate' }]);
}
if (await fs.pathExists(path.join(binDir, 'activate.nu'))) {
shellActivation.set(ShellConstants.NU, [
{ executable: 'overlay', args: ['use', path.join(binDir, 'activate.nu')] },
]);
shellDeactivation.set(ShellConstants.NU, [{ executable: 'overlay', args: ['hide', 'activate'] }]);
}
return {
shellActivation,
shellDeactivation,
};
}
// Tracks if the broken defaultEnvManager error message has been shown this session
let hasShownBrokenDefaultEnvManagerError = false;
/**
* Checks if the given managerId is set as the default environment manager for the project.
* If so, marks the default manager as broken, refreshes environments, and shows an error message to the user.
* The error message offers to reset the setting, view the setting, or close.
* The error message is only shown once per session.
*
* @param managerId The environment manager id to check.
* @param projectManager The Python project manager instance.
* @param api The Python environment API instance.
*/
export async function notifyMissingManagerIfDefault(
managerId: string,
projectManager: PythonProjectManager,
api: PythonEnvironmentApi,
) {
const defaultEnvManager = getDefaultEnvManagerSetting(projectManager);
if (defaultEnvManager === managerId) {
if (hasShownBrokenDefaultEnvManagerError) {
return;
}
hasShownBrokenDefaultEnvManagerError = true;
setDefaultEnvManagerBroken(true);
await api.refreshEnvironments(undefined);
window
.showErrorMessage(
l10n.t(
"The default environment manager is set to '{0}', but the {1} executable could not be found.",
defaultEnvManager,
managerId.split(':')[1],
),
l10n.t('Reset setting'),
l10n.t('View setting'),
l10n.t('Close'),
)
.then(async (selection) => {
if (selection === 'Reset setting') {
const result = await removeFirstDefaultEnvManagerSettingDetailed(managerId);
if (!result.found) {
window
.showErrorMessage(
l10n.t(
"Could not find a setting for 'defaultEnvManager' set to '{0}' to reset.",
managerId,
),
l10n.t('Open settings'),
l10n.t('Close'),
)
.then((sel) => {
if (sel === 'Open settings') {
commands.executeCommand(
'workbench.action.openSettings',
'python-envs.defaultEnvManager',
);
}
});
}
}
if (selection === 'View setting') {
commands.executeCommand('workbench.action.openSettings', 'python-envs.defaultEnvManager');
}
});
}
}
/**
* Removes the first occurrence of 'defaultEnvManager' set to managerId, returns where it was removed, and logs the action.
* @param managerId The manager id to match and remove.
* @returns { found: boolean, scope?: string }
*/
export async function removeFirstDefaultEnvManagerSettingDetailed(
managerId: string,
): Promise<{ found: boolean; scope?: string }> {
const config = workspace.getConfiguration('python-envs');
const inspect = config.inspect('defaultEnvManager');
// Workspace folder settings (multi-root)
if (inspect?.workspaceFolderValue !== undefined && inspect.workspaceFolderValue === managerId) {
await config.update('defaultEnvManager', undefined, ConfigurationTarget.WorkspaceFolder);
traceLog("[python-envs] Removed 'defaultEnvManager' from Workspace Folder settings.");
return { found: true, scope: 'Workspace Folder' };
}
// Workspace settings
if (inspect?.workspaceValue !== undefined && inspect.workspaceValue === managerId) {
await config.update('defaultEnvManager', undefined, ConfigurationTarget.Workspace);
traceLog("[python-envs] Removed 'defaultEnvManager' from Workspace settings.");
return { found: true, scope: 'Workspace' };
}
// User/global settings
if (inspect?.globalValue !== undefined && inspect.globalValue === managerId) {
await config.update('defaultEnvManager', undefined, ConfigurationTarget.Global);
traceLog("[python-envs] Removed 'defaultEnvManager' from User/Global settings.");
return { found: true, scope: 'User/Global' };
}
// No matching setting found
traceVerbose(`[python-envs] Could not find 'defaultEnvManager' set to '${managerId}' in any scope.`);
return { found: false };
}