forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackageManagement.ts
More file actions
79 lines (76 loc) · 3.93 KB
/
packageManagement.ts
File metadata and controls
79 lines (76 loc) · 3.93 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
import { window } from 'vscode';
import { traceInfo } from '../common/logging';
import { getCallingExtension } from '../common/utils/frameUtils';
import { getConfiguration } from '../common/workspace.apis';
import { SettingsPackageTrust, promptForInstallPermissions, promptForAlwaysAsk } from './utils';
export enum InstallPermissionEnum {
AlwaysAllow = 'alwaysAllow',
AlwaysAsk = 'alwaysAsk',
InstallNoConfigure = 'installNoConfigure',
Cancel = 'cancel',
}
export enum SimpleResponseEnum {
YesInstall = 'yesInstall',
NoInstall = 'noInstall',
Cancel = 'cancel',
}
export async function packageManagementFlow(packages: string[]): Promise<void> {
// what does it mean to return, will we tell the calling extension about it?
//check to see if pkg was already installed?
const callingExtension = getCallingExtension();
traceInfo(`Python API: Installing packages for extension: '${callingExtension}'`);
const config = getConfiguration('python-envs');
let extPkgTrustConfig: SettingsPackageTrust | undefined =
config.get<SettingsPackageTrust>('allowAutoPackageManagement');
let callingExtensionTrustLevel;
let isConfigured = true;
if (extPkgTrustConfig === undefined) {
// TODO:s THIS DOESN'T WORK
// no package trust config, default to alwaysAsk
callingExtensionTrustLevel = InstallPermissionEnum.AlwaysAsk;
isConfigured = false;
} else {
// check for package trust settings
callingExtensionTrustLevel = extPkgTrustConfig[callingExtension];
if (callingExtensionTrustLevel === undefined) {
// no specific package trust settings, checking wildcard in config
callingExtensionTrustLevel = extPkgTrustConfig['*'];
if (callingExtensionTrustLevel === undefined) {
// no wildcard in config, default to alwaysAsk
callingExtensionTrustLevel = InstallPermissionEnum.AlwaysAsk;
isConfigured = false;
}
}
}
traceInfo(`package trust settings for '${callingExtension}' is ${callingExtensionTrustLevel}`);
if (!isConfigured) {
// calling extension has no config, user has no wildcard setup
// prompt user to "alwaysAsk" or "alwaysAllow"
const selectedOption = await promptForInstallPermissions(callingExtension, packages.join(', '));
if (selectedOption === InstallPermissionEnum.Cancel) {
// user cancelled the prompt, exit
window.showErrorMessage(`Installation of ${packages.join(', ')} was canceled by the user.`);
return Promise.reject('User cancelled the package installation.');
}
if (selectedOption !== InstallPermissionEnum.InstallNoConfigure) {
// meaning the user selected "alwaysAsk" or "alwaysAllow", update the config
const newExtTrustConfig = { ...extPkgTrustConfig, [callingExtension]: selectedOption };
config.update('allowAutoPackageManagement', newExtTrustConfig, true);
}
} else {
// user has already configured package trust settings for this extension
if (callingExtensionTrustLevel === InstallPermissionEnum.AlwaysAsk) {
traceInfo('Installation is pending user confirmation due to permission settings.');
// prompt user to allow or deny package installation
const simpleResponse = await promptForAlwaysAsk(callingExtension, packages.join(', '));
if (simpleResponse === SimpleResponseEnum.NoInstall || simpleResponse === SimpleResponseEnum.Cancel) {
// user cancelled the prompt, exit
window.showErrorMessage(`Installation of ${packages.join(', ')} was canceled by the user.`);
return Promise.reject('User cancelled the package installation.');
}
}
// if callingExtensionTrustLevel is 'alwaysAllow' just continue to install
}
// actually install the packages
return Promise.resolve();
}