|
1 | 1 | import { QuickInputButtons, TaskExecution, TaskRevealKind, Terminal, Uri } from 'vscode';
|
2 |
| -import { |
3 |
| - EnvironmentManagers, |
4 |
| - InternalEnvironmentManager, |
5 |
| - InternalPackageManager, |
6 |
| - ProjectCreators, |
7 |
| - PythonProjectManager, |
8 |
| -} from '../internal.api'; |
9 |
| -import { traceError, traceInfo, traceVerbose } from '../common/logging'; |
10 | 2 | import {
|
11 | 3 | CreateEnvironmentOptions,
|
12 | 4 | PythonEnvironment,
|
13 | 5 | PythonEnvironmentApi,
|
14 |
| - PythonProject, |
15 | 6 | PythonProjectCreator,
|
| 7 | + PythonProjectCreatorOptions, |
16 | 8 | } from '../api';
|
17 |
| -import * as path from 'path'; |
| 9 | +import { traceError, traceInfo, traceVerbose } from '../common/logging'; |
18 | 10 | import {
|
19 |
| - setEnvironmentManager, |
20 |
| - setPackageManager, |
21 |
| - addPythonProjectSetting, |
22 |
| - removePythonProjectSetting, |
23 |
| - getDefaultEnvManagerSetting, |
24 |
| - getDefaultPkgManagerSetting, |
25 |
| - EditProjectSettings, |
26 |
| -} from './settings/settingHelpers'; |
27 |
| - |
28 |
| -import { getAbsolutePath } from '../common/utils/fileNameUtils'; |
| 11 | + EnvironmentManagers, |
| 12 | + InternalEnvironmentManager, |
| 13 | + InternalPackageManager, |
| 14 | + ProjectCreators, |
| 15 | + PythonProjectManager, |
| 16 | +} from '../internal.api'; |
| 17 | +import { removePythonProjectSetting, setEnvironmentManager, setPackageManager } from './settings/settingHelpers'; |
| 18 | + |
| 19 | +import { clipboardWriteText } from '../common/env.apis'; |
| 20 | +import {} from '../common/errors/utils'; |
| 21 | +import { pickEnvironment } from '../common/pickers/environments'; |
| 22 | +import { pickCreator, pickEnvironmentManager, pickPackageManager } from '../common/pickers/managers'; |
| 23 | +import { pickProject, pickProjectMany } from '../common/pickers/projects'; |
| 24 | +import { activeTextEditor, showErrorMessage } from '../common/window.apis'; |
| 25 | +import { quoteArgs } from './execution/execUtils'; |
29 | 26 | import { runAsTask } from './execution/runAsTask';
|
| 27 | +import { runInTerminal } from './terminal/runInTerminal'; |
| 28 | +import { TerminalManager } from './terminal/terminalManager'; |
30 | 29 | import {
|
31 | 30 | EnvManagerTreeItem,
|
32 |
| - PackageRootTreeItem, |
33 |
| - PythonEnvTreeItem, |
34 |
| - ProjectItem, |
35 |
| - ProjectEnvironment, |
36 |
| - ProjectPackageRootTreeItem, |
37 |
| - GlobalProjectItem, |
38 | 31 | EnvTreeItemKind,
|
| 32 | + GlobalProjectItem, |
| 33 | + PackageRootTreeItem, |
39 | 34 | PackageTreeItem,
|
| 35 | + ProjectEnvironment, |
| 36 | + ProjectItem, |
40 | 37 | ProjectPackage,
|
| 38 | + ProjectPackageRootTreeItem, |
| 39 | + PythonEnvTreeItem, |
41 | 40 | } from './views/treeViewItems';
|
42 |
| -import { pickEnvironment } from '../common/pickers/environments'; |
43 |
| -import { pickEnvironmentManager, pickPackageManager, pickCreator } from '../common/pickers/managers'; |
44 |
| -import { pickProject, pickProjectMany } from '../common/pickers/projects'; |
45 |
| -import { TerminalManager } from './terminal/terminalManager'; |
46 |
| -import { runInTerminal } from './terminal/runInTerminal'; |
47 |
| -import { quoteArgs } from './execution/execUtils'; |
48 |
| -import {} from '../common/errors/utils'; |
49 |
| -import { activeTextEditor, showErrorMessage } from '../common/window.apis'; |
50 |
| -import { clipboardWriteText } from '../common/env.apis'; |
51 | 41 |
|
52 | 42 | export async function refreshManagerCommand(context: unknown): Promise<void> {
|
53 | 43 | if (context instanceof EnvManagerTreeItem) {
|
@@ -338,93 +328,61 @@ export async function setPackageManagerCommand(em: EnvironmentManagers, wm: Pyth
|
338 | 328 | }
|
339 | 329 | }
|
340 | 330 |
|
341 |
| -export async function addPythonProject( |
| 331 | +/** |
| 332 | + * Creates a new Python project using a selected PythonProjectCreator. |
| 333 | + * |
| 334 | + * This function calls create on the selected creator and handles the creation process. Will return |
| 335 | + * without doing anything if the resource is a ProjectItem, as the project is already created. |
| 336 | + * |
| 337 | + * @param resource - The resource to use for project creation (can be a Uri(s), ProjectItem(s), or undefined). |
| 338 | + * @param wm - The PythonProjectManager instance for managing projects. |
| 339 | + * @param em - The EnvironmentManagers instance for managing environments. |
| 340 | + * @param pc - The ProjectCreators instance for accessing available project creators. |
| 341 | + * @returns A promise that resolves when the project has been created, or void if cancelled or invalid. |
| 342 | + */ |
| 343 | +export async function addPythonProjectCommand( |
342 | 344 | resource: unknown,
|
343 | 345 | wm: PythonProjectManager,
|
344 | 346 | em: EnvironmentManagers,
|
345 | 347 | pc: ProjectCreators,
|
346 |
| -): Promise<PythonProject | PythonProject[] | undefined> { |
| 348 | +): Promise<void> { |
347 | 349 | if (wm.getProjects().length === 0) {
|
348 | 350 | showErrorMessage('Please open a folder/project before adding a workspace');
|
349 | 351 | return;
|
350 | 352 | }
|
351 |
| - |
352 |
| - if (resource instanceof Uri) { |
353 |
| - const uri = resource as Uri; |
354 |
| - const envManagerId = getDefaultEnvManagerSetting(wm, uri); |
355 |
| - const pkgManagerId = getDefaultPkgManagerSetting( |
356 |
| - wm, |
357 |
| - uri, |
358 |
| - em.getEnvironmentManager(envManagerId)?.preferredPackageManagerId, |
359 |
| - ); |
360 |
| - const pw = wm.create(path.basename(uri.fsPath), uri); |
361 |
| - await addPythonProjectSetting([{ project: pw, envManager: envManagerId, packageManager: pkgManagerId }]); |
362 |
| - return pw; |
363 |
| - } |
364 |
| - |
365 |
| - if (resource === undefined || resource instanceof ProjectItem) { |
366 |
| - const creator: PythonProjectCreator | undefined = await pickCreator(pc.getProjectCreators()); |
367 |
| - if (!creator) { |
| 353 | + if (resource instanceof Array) { |
| 354 | + for (const r of resource) { |
| 355 | + await addPythonProjectCommand(r, wm, em, pc); |
368 | 356 | return;
|
369 | 357 | }
|
| 358 | + } |
| 359 | + if (resource instanceof ProjectItem) { |
| 360 | + // If the context is a ProjectItem, project is already created. Just add it to the package manager project list. |
| 361 | + wm.add(resource.project); |
| 362 | + return; |
| 363 | + } |
| 364 | + let options: PythonProjectCreatorOptions | undefined; |
370 | 365 |
|
371 |
| - let results: PythonProject | PythonProject[] | Uri | Uri[] | undefined; |
372 |
| - try { |
373 |
| - results = await creator.create(); |
374 |
| - if (results === undefined) { |
375 |
| - return; |
376 |
| - } |
377 |
| - } catch (ex) { |
378 |
| - if (ex === QuickInputButtons.Back) { |
379 |
| - return addPythonProject(resource, wm, em, pc); |
380 |
| - } |
381 |
| - throw ex; |
382 |
| - } |
383 |
| - |
384 |
| - if ( |
385 |
| - results instanceof Uri || |
386 |
| - (Array.isArray(results) && results.length > 0 && results.every((r) => r instanceof Uri)) |
387 |
| - ) { |
388 |
| - // the results are Uris, which means they aren't projects and shouldn't be added |
389 |
| - return; |
390 |
| - } |
391 |
| - results = results as PythonProject | PythonProject[]; |
392 |
| - |
393 |
| - if (!Array.isArray(results)) { |
394 |
| - results = [results]; |
395 |
| - } |
396 |
| - |
397 |
| - if (Array.isArray(results)) { |
398 |
| - if (results.length === 0) { |
399 |
| - return; |
400 |
| - } |
401 |
| - } |
402 |
| - |
403 |
| - const projects: PythonProject[] = []; |
404 |
| - const edits: EditProjectSettings[] = []; |
| 366 | + if (resource instanceof Uri) { |
| 367 | + // Use resource as the URI for the project if it is a URI. |
| 368 | + options = { |
| 369 | + name: resource.fsPath, |
| 370 | + rootUri: resource, |
| 371 | + }; |
| 372 | + } |
405 | 373 |
|
406 |
| - for (const result of results) { |
407 |
| - const uri = await getAbsolutePath(result.uri.fsPath); |
408 |
| - if (!uri) { |
409 |
| - traceError(`Path does not belong to any opened workspace: ${result.uri.fsPath}`); |
410 |
| - continue; |
411 |
| - } |
| 374 | + const creator: PythonProjectCreator | undefined = await pickCreator(pc.getProjectCreators()); |
| 375 | + if (!creator) { |
| 376 | + return; |
| 377 | + } |
412 | 378 |
|
413 |
| - const envManagerId = getDefaultEnvManagerSetting(wm, uri); |
414 |
| - const pkgManagerId = getDefaultPkgManagerSetting( |
415 |
| - wm, |
416 |
| - uri, |
417 |
| - em.getEnvironmentManager(envManagerId)?.preferredPackageManagerId, |
418 |
| - ); |
419 |
| - const pw = wm.create(path.basename(uri.fsPath), uri); |
420 |
| - projects.push(pw); |
421 |
| - edits.push({ project: pw, envManager: envManagerId, packageManager: pkgManagerId }); |
| 379 | + try { |
| 380 | + await creator.create(options); |
| 381 | + } catch (ex) { |
| 382 | + if (ex === QuickInputButtons.Back) { |
| 383 | + return addPythonProjectCommand(resource, wm, em, pc); |
422 | 384 | }
|
423 |
| - await addPythonProjectSetting(edits); |
424 |
| - return projects; |
425 |
| - } else { |
426 |
| - // If the context is not a Uri or ProjectItem, rerun function with undefined context |
427 |
| - await addPythonProject(undefined, wm, em, pc); |
| 385 | + throw ex; |
428 | 386 | }
|
429 | 387 | }
|
430 | 388 |
|
|
0 commit comments