|
| 1 | +import * as path from 'path'; |
| 2 | +import { Uri } from 'vscode'; |
| 3 | +import { showQuickPickWithButtons } from '../../common/window.apis'; |
| 4 | +import { ProjectCreatorString } from '../../common/localize'; |
| 5 | +import { PythonProject, PythonProjectCreator, PythonProjectCreatorOptions } from '../../api'; |
| 6 | +import { PythonProjectManager } from '../../internal.api'; |
| 7 | +import { showErrorMessage } from '../../common/errors/utils'; |
| 8 | +import { findFiles } from '../../common/workspace.apis'; |
| 9 | + |
| 10 | +function getUniqueUri(uris: Uri[]): { |
| 11 | + label: string; |
| 12 | + description: string; |
| 13 | + uri: Uri; |
| 14 | +}[] { |
| 15 | + const files = uris.map((uri) => uri.fsPath).sort(); |
| 16 | + const dirs: Map<string, string> = new Map(); |
| 17 | + files.forEach((file) => { |
| 18 | + const dir = path.dirname(file); |
| 19 | + if (dirs.has(dir)) { |
| 20 | + return; |
| 21 | + } |
| 22 | + dirs.set(dir, file); |
| 23 | + }); |
| 24 | + return Array.from(dirs.entries()) |
| 25 | + .map(([dir, file]) => ({ |
| 26 | + label: path.basename(dir), |
| 27 | + description: file, |
| 28 | + uri: Uri.file(dir), |
| 29 | + })) |
| 30 | + .sort((a, b) => a.label.localeCompare(b.label)); |
| 31 | +} |
| 32 | + |
| 33 | +async function pickProjects(uris: Uri[]): Promise<Uri[] | undefined> { |
| 34 | + const items = getUniqueUri(uris); |
| 35 | + |
| 36 | + const selected = await showQuickPickWithButtons(items, { |
| 37 | + canPickMany: true, |
| 38 | + ignoreFocusOut: true, |
| 39 | + placeHolder: ProjectCreatorString.selectProjects, |
| 40 | + showBackButton: true, |
| 41 | + }); |
| 42 | + |
| 43 | + if (Array.isArray(selected)) { |
| 44 | + return selected.map((s) => s.uri); |
| 45 | + } else if (selected) { |
| 46 | + return [selected.uri]; |
| 47 | + } |
| 48 | + |
| 49 | + return undefined; |
| 50 | +} |
| 51 | + |
| 52 | +export class AutoFindProjects implements PythonProjectCreator { |
| 53 | + public readonly name = 'autoProjects'; |
| 54 | + public readonly displayName = ProjectCreatorString.autoFindProjects; |
| 55 | + public readonly description = ProjectCreatorString.autoFindProjectsDescription; |
| 56 | + |
| 57 | + constructor(private readonly pm: PythonProjectManager) {} |
| 58 | + |
| 59 | + async create(_options?: PythonProjectCreatorOptions): Promise<PythonProject | PythonProject[] | undefined> { |
| 60 | + const files = await findFiles('**/{pyproject.toml,setup.py}'); |
| 61 | + if (!files || files.length === 0) { |
| 62 | + setImmediate(() => { |
| 63 | + showErrorMessage('No projects found'); |
| 64 | + }); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const filtered = files.filter((uri) => { |
| 69 | + const p = this.pm.get(uri); |
| 70 | + if (p) { |
| 71 | + // If there ia already a project with the same path, skip it. |
| 72 | + // If there is a project with the same parent path, skip it. |
| 73 | + const np = path.normalize(p.uri.fsPath); |
| 74 | + const nf = path.normalize(uri.fsPath); |
| 75 | + const nfp = path.dirname(nf); |
| 76 | + return np !== nf && np !== nfp; |
| 77 | + } |
| 78 | + return true; |
| 79 | + }); |
| 80 | + |
| 81 | + if (filtered.length === 0) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const projects = await pickProjects(filtered); |
| 86 | + if (!projects || projects.length === 0) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + return projects.map((uri) => ({ |
| 91 | + name: path.basename(uri.fsPath), |
| 92 | + uri, |
| 93 | + })); |
| 94 | + } |
| 95 | +} |
0 commit comments