forked from microsoft/vscode-python-environments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectManager.ts
More file actions
193 lines (170 loc) · 7.48 KB
/
projectManager.ts
File metadata and controls
193 lines (170 loc) · 7.48 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
import * as path from 'path';
import { Disposable, EventEmitter, MarkdownString, Uri, workspace } from 'vscode';
import { IconPath, PythonProject } from '../api';
import { DEFAULT_ENV_MANAGER_ID, DEFAULT_PACKAGE_MANAGER_ID } from '../common/constants';
import { createSimpleDebounce } from '../common/utils/debounce';
import {
getConfiguration,
getWorkspaceFolders,
onDidChangeConfiguration,
onDidChangeWorkspaceFolders,
} from '../common/workspace.apis';
import { PythonProjectManager, PythonProjectSettings, PythonProjectsImpl } from '../internal.api';
import {
addPythonProjectSetting,
EditProjectSettings,
getDefaultEnvManagerSetting,
getDefaultPkgManagerSetting,
} from './settings/settingHelpers';
type ProjectArray = PythonProject[];
export class PythonProjectManagerImpl implements PythonProjectManager {
private disposables: Disposable[] = [];
private _projects = new Map<string, PythonProject>();
private readonly _onDidChangeProjects = new EventEmitter<ProjectArray | undefined>();
public readonly onDidChangeProjects = this._onDidChangeProjects.event;
private readonly updateDebounce = createSimpleDebounce(100, () => this.updateProjects());
initialize(): void {
this.add(this.getInitialProjects());
this.disposables.push(
this._onDidChangeProjects,
new Disposable(() => this._projects.clear()),
onDidChangeWorkspaceFolders(() => {
this.updateDebounce.trigger();
}),
onDidChangeConfiguration((e) => {
if (
e.affectsConfiguration('python-envs.defaultEnvManager') ||
e.affectsConfiguration('python-envs.pythonProjects') ||
e.affectsConfiguration('python-envs.defaultPackageManager')
) {
this.updateDebounce.trigger();
}
}),
);
}
private getInitialProjects(): ProjectArray {
const newProjects: ProjectArray = [];
const workspaces = getWorkspaceFolders() ?? [];
for (const w of workspaces) {
const config = getConfiguration('python-envs', w.uri);
const overrides = config.get<PythonProjectSettings[]>('pythonProjects', []);
newProjects.push(new PythonProjectsImpl(w.name, w.uri));
for (const o of overrides) {
const uri = Uri.file(path.resolve(w.uri.fsPath, o.path));
newProjects.push(new PythonProjectsImpl(o.path, uri));
}
}
return newProjects;
}
private updateProjects(): void {
const workspaces = getWorkspaceFolders() ?? [];
const existingProjects = Array.from(this._projects.values());
const newProjects: ProjectArray = [];
for (const w of workspaces) {
const config = getConfiguration('python-envs', w.uri);
const overrides = config.get<PythonProjectSettings[]>('pythonProjects', []);
newProjects.push(new PythonProjectsImpl(w.name, w.uri));
for (const o of overrides) {
const uri = Uri.file(path.resolve(w.uri.fsPath, o.path));
newProjects.push(new PythonProjectsImpl(o.path, uri));
}
}
const projectsToRemove = existingProjects.filter(
(w) => !newProjects.find((n) => n.uri.toString() === w.uri.toString()),
);
projectsToRemove.forEach((w) => this._projects.delete(w.uri.toString()));
const projectsToAdd = newProjects.filter(
(n) => !existingProjects.find((w) => w.uri.toString() === n.uri.toString()),
);
projectsToAdd.forEach((w) => this._projects.set(w.uri.toString(), w));
if (projectsToRemove.length > 0 || projectsToAdd.length > 0) {
this._onDidChangeProjects.fire(Array.from(this._projects.values()));
}
}
create(
name: string,
uri: Uri,
options?: { description?: string; tooltip?: string | MarkdownString; iconPath?: IconPath },
): PythonProject {
return new PythonProjectsImpl(name, uri, options);
}
async add(projects: PythonProject | ProjectArray): Promise<void> {
const _projects = Array.isArray(projects) ? projects : [projects];
if (_projects.length === 0) {
return;
}
const edits: EditProjectSettings[] = [];
const envManagerId = getDefaultEnvManagerSetting(this);
const pkgManagerId = getDefaultPkgManagerSetting(this);
const globalConfig = workspace.getConfiguration('python-envs', undefined);
const defaultEnvManager = globalConfig.get<string>('defaultEnvManager', DEFAULT_ENV_MANAGER_ID);
const defaultPkgManager = globalConfig.get<string>('defaultPackageManager', DEFAULT_PACKAGE_MANAGER_ID);
_projects.forEach((w) => {
// if the package manager and env manager are not the default ones, then add them to the edits
if (envManagerId !== defaultEnvManager || pkgManagerId !== defaultPkgManager) {
edits.push({ project: w, envManager: envManagerId, packageManager: pkgManagerId });
}
return this._projects.set(w.uri.toString(), w);
});
this._onDidChangeProjects.fire(Array.from(this._projects.values()));
if (edits.length > 0) {
await addPythonProjectSetting(edits);
}
}
remove(projects: PythonProject | ProjectArray): void {
const _projects = Array.isArray(projects) ? projects : [projects];
if (_projects.length === 0) {
return;
}
_projects.forEach((w) => this._projects.delete(w.uri.toString()));
this._onDidChangeProjects.fire(Array.from(this._projects.values()));
}
getProjects(uris?: Uri[]): ReadonlyArray<PythonProject> {
if (uris === undefined) {
return Array.from(this._projects.values());
} else {
const projects: PythonProject[] = [];
for (const uri of uris) {
const project = this.get(uri);
if (project !== undefined && !projects.includes(project)) {
projects.push(project);
}
}
return projects;
}
}
get(uri: Uri): PythonProject | undefined {
let pythonProject = this._projects.get(uri.toString());
if (!pythonProject) {
pythonProject = this.findProjectByUri(uri);
}
return pythonProject;
}
private findProjectByUri(uri: Uri): PythonProject | undefined {
const _projects = Array.from(this._projects.values()).sort((a, b) => b.uri.fsPath.length - a.uri.fsPath.length);
const normalizedUriPath = path.normalize(uri.fsPath);
for (const p of _projects) {
const normalizedProjectPath = path.normalize(p.uri.fsPath);
if (this.isUriMatching(normalizedUriPath, normalizedProjectPath)) {
return p;
}
}
return undefined;
}
private isUriMatching(normalizedUriPath: string, normalizedProjectPath: string): boolean {
if (normalizedProjectPath === normalizedUriPath) {
return true;
}
let parentPath = path.dirname(normalizedUriPath);
while (parentPath !== path.dirname(parentPath)) {
if (normalizedProjectPath === parentPath) {
return true;
}
parentPath = path.dirname(parentPath);
}
return false;
}
dispose() {
this.disposables.forEach((d) => d.dispose());
}
}