-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathprojectManager.ts
More file actions
273 lines (243 loc) · 11.2 KB
/
projectManager.ts
File metadata and controls
273 lines (243 loc) · 11.2 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
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;
// Debounce the updateProjects method to avoid excessive update calls
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();
}
}),
);
}
/**
*
* Gathers the projects which are configured in settings and all workspace roots.
* @returns An array of PythonProject objects representing the initial projects.
*/
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', []);
// Add the workspace root as a project if not already present
if (!newProjects.some((p) => p.uri.toString() === w.uri.toString())) {
newProjects.push(new PythonProjectsImpl(w.name, w.uri));
}
// For each override, resolve its path and add as a project if not already present
for (const o of overrides) {
let uriFromWorkspace: Uri | undefined = undefined;
// if override has a workspace property, resolve the path relative to that workspace
if (o.workspace) {
//
const workspaceFolder = workspaces.find((ws) => ws.name === o.workspace);
if (workspaceFolder) {
if (workspaceFolder.uri.toString() !== w.uri.toString()) {
continue; // skip if the workspace is not the same as the current workspace
}
uriFromWorkspace = Uri.file(path.resolve(workspaceFolder.uri.fsPath, o.path));
}
}
const uri = uriFromWorkspace ? uriFromWorkspace : Uri.file(path.resolve(w.uri.fsPath, o.path));
// Check if the project already exists in the newProjects array
if (!newProjects.some((p) => p.uri.toString() === uri.toString())) {
newProjects.push(new PythonProjectsImpl(o.path, uri));
}
}
}
return newProjects;
}
/**
* Get initial projects from the workspace(s) config settings
* then updates the internal _projects map to reflect the current state and
* fires the onDidChangeProjects event if there are any changes.
*/
private updateProjects(): void {
const newProjects: ProjectArray = this.getInitialProjects();
const existingProjects = Array.from(this._projects.values());
// Remove projects that are no longer in the workspace settings
const projectsToRemove = existingProjects.filter(
(w) => !newProjects.find((n) => n.uri.toString() === w.uri.toString()),
);
projectsToRemove.forEach((w) => this._projects.delete(w.uri.toString()));
// Add new projects that are in the workspace settings but not in the existing projects
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((currProject) => {
const workspaces = getWorkspaceFolders() ?? [];
const isRoot = workspaces.some((w) => w.uri.toString() === currProject.uri.toString());
if (isRoot) {
// for root projects, add setting if not default
if (envManagerId !== defaultEnvManager || pkgManagerId !== defaultPkgManager) {
edits.push({ project: currProject, envManager: envManagerId, packageManager: pkgManagerId });
}
} else {
// for non-root projects, always add setting
edits.push({ project: currProject, envManager: envManagerId, packageManager: pkgManagerId });
}
// handles adding the project to this._projects map
return this._projects.set(currProject.uri.toString(), currProject);
});
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()));
}
/**
* Update a project by removing the old one and adding a new one with updated properties.
* @param existingUri The URI of the project to update.
* @param newName The new name for the project (optional, defaults to old name).
* @param newUri The new URI for the project (optional, defaults to old URI).
* @param newOptions New options for the project (optional, merged with old options).
*/
async modifyProject(
existingUri: Uri,
newName?: string,
newUri?: Uri,
newOptions?: { description?: string; tooltip?: string | MarkdownString; iconPath?: IconPath },
): Promise<void> {
const project = this.get(existingUri);
if (!project) {
return;
}
// Remove the old project
this.remove(project);
// Prepare new values
const name = newName ?? project.name;
const uri = newUri ?? project.uri;
const options = {
description: newOptions?.description ?? project.description,
tooltip: newOptions?.tooltip ?? project.tooltip,
iconPath: newOptions?.iconPath ?? (project as PythonProjectsImpl).iconPath,
};
// Create and add the new project
const updatedProject = this.create(name, uri, options);
await this.add(updatedProject);
}
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;
}
/**
* Finds the single project that matches the given URI if it exists.
* @param uri The URI of the project to find.
* @returns The project with the given URI, or undefined if not found.
*/
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;
}
/**
* Checks if a given file or folder path (normalizedUriPath)
* is the same as, or is inside, a project path
* @normalizedProjectPath Project path to check against.
* @normalizedUriPath File or folder path to check.
* @returns true if the file or folder path is the same as or inside the project path, false otherwise.
*/
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());
}
}