-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpipManager.ts
More file actions
138 lines (129 loc) · 5.01 KB
/
pipManager.ts
File metadata and controls
138 lines (129 loc) · 5.01 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
import {
CancellationError,
Event,
EventEmitter,
LogOutputChannel,
MarkdownString,
ProgressLocation,
ThemeIcon,
window,
} from 'vscode';
import { Disposable } from 'vscode-jsonrpc';
import {
DidChangePackagesEventArgs,
IconPath,
Package,
PackageChangeKind,
PackageManagementOptions,
PackageManager,
PythonEnvironment,
PythonEnvironmentApi,
} from '../../api';
import { getWorkspacePackagesToInstall } from './pipUtils';
import { managePackages, refreshPackages } from './utils';
import { VenvManager } from './venvManager';
function getChanges(before: Package[], after: Package[]): { kind: PackageChangeKind; pkg: Package }[] {
const changes: { kind: PackageChangeKind; pkg: Package }[] = [];
before.forEach((pkg) => {
changes.push({ kind: PackageChangeKind.remove, pkg });
});
after.forEach((pkg) => {
changes.push({ kind: PackageChangeKind.add, pkg });
});
return changes;
}
export class PipPackageManager implements PackageManager, Disposable {
private readonly _onDidChangePackages = new EventEmitter<DidChangePackagesEventArgs>();
onDidChangePackages: Event<DidChangePackagesEventArgs> = this._onDidChangePackages.event;
private packages: Map<string, Package[]> = new Map();
constructor(
private readonly api: PythonEnvironmentApi,
public readonly log: LogOutputChannel,
private readonly venv: VenvManager,
) {
this.name = 'pip';
this.displayName = 'Pip';
this.description = 'This package manager for python installs using pip.';
this.tooltip = new MarkdownString('This package manager for python installs using `pip`.');
this.iconPath = new ThemeIcon('python');
}
readonly name: string;
readonly displayName?: string;
readonly description?: string;
readonly tooltip?: string | MarkdownString;
readonly iconPath?: IconPath;
async manage(environment: PythonEnvironment, options: PackageManagementOptions): Promise<void> {
let toInstall: string[] = [...(options.install ?? [])];
let toUninstall: string[] = [...(options.uninstall ?? [])];
if (toInstall.length === 0 && toUninstall.length === 0) {
const projects = this.venv.getProjectsByEnvironment(environment);
const result = await getWorkspacePackagesToInstall(this.api, options, projects, environment, this.log);
if (result) {
toInstall = result.install;
toUninstall = result.uninstall;
} else {
return;
}
}
const manageOptions = {
...options,
install: toInstall,
uninstall: toUninstall,
};
await window.withProgress(
{
location: ProgressLocation.Notification,
title: 'Installing packages',
cancellable: true,
},
async (_progress, token) => {
try {
const before = this.packages.get(environment.envId.id) ?? [];
const after = await managePackages(environment, manageOptions, this.api, this, token);
const changes = getChanges(before, after);
this.packages.set(environment.envId.id, after);
this._onDidChangePackages.fire({ environment, manager: this, changes });
} catch (e) {
if (e instanceof CancellationError) {
throw e;
}
this.log.error('Error managing packages', e);
setImmediate(async () => {
const result = await window.showErrorMessage('Error managing packages', 'View Output');
if (result === 'View Output') {
this.log.show();
}
});
throw e;
}
},
);
}
async refresh(environment: PythonEnvironment): Promise<void> {
await window.withProgress(
{
location: ProgressLocation.Window,
title: 'Refreshing packages',
},
async () => {
const before = this.packages.get(environment.envId.id) ?? [];
const after = await refreshPackages(environment, this.api, this);
const changes = getChanges(before, after);
this.packages.set(environment.envId.id, after);
if (changes.length > 0) {
this._onDidChangePackages.fire({ environment, manager: this, changes });
}
},
);
}
async getPackages(environment: PythonEnvironment): Promise<Package[] | undefined> {
if (!this.packages.has(environment.envId.id)) {
await this.refresh(environment);
}
return this.packages.get(environment.envId.id);
}
dispose(): void {
this._onDidChangePackages.dispose();
this.packages.clear();
}
}