forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
237 lines (210 loc) · 9.29 KB
/
utils.ts
File metadata and controls
237 lines (210 loc) · 9.29 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
import * as fs from 'fs-extra';
import path from 'path';
import { commands, ConfigurationTarget, window, workspace } from 'vscode';
import { PythonCommandRunConfiguration, PythonEnvironment, PythonEnvironmentApi } from '../../api';
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
}
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,
};
}
export async function notifyMissingManagerIfDefault(
managerId: string,
projectManager: PythonProjectManager,
api: PythonEnvironmentApi,
) {
const defaultEnvManager = getDefaultEnvManagerSetting(projectManager);
if (defaultEnvManager === managerId) {
setDefaultEnvManagerBroken(true);
await api.refreshEnvironments(undefined);
window
.showErrorMessage(
`The default environment manager is set to '${defaultEnvManager}', but the ${
managerId.split(':')[1]
} executable could not be found.`,
'Reset setting',
'View setting',
'Close',
)
.then((selection) => {
if (selection === 'Reset setting') {
// Remove the setting from all scopes
const config = workspace.getConfiguration('python-envs');
const inspect = config.inspect('defaultEnvManager');
if (inspect?.workspaceValue !== undefined) {
// Remove from workspace settings
config.update('defaultEnvManager', undefined, ConfigurationTarget.Workspace);
} else if (inspect?.globalValue !== undefined) {
// Remove from user settings
config.update('defaultEnvManager', undefined, ConfigurationTarget.Global);
}
}
if (selection === 'View setting') {
commands.executeCommand('workbench.action.openSettings', 'python-envs.defaultEnvManager');
}
});
}
}