|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { inject, injectable } from 'inversify'; |
| 5 | +import { IRecommendedEnvironmentService } from './types'; |
| 6 | +import { PythonExtension } from '../../api/types'; |
| 7 | +import { IExtensionContext, Resource } from '../../common/types'; |
| 8 | +import { Uri, workspace } from 'vscode'; |
| 9 | +import { getWorkspaceStateValue, updateWorkspaceStateValue } from '../../common/persistentState'; |
| 10 | +import { traceError } from '../../logging'; |
| 11 | + |
| 12 | +const MEMENTO_KEY = 'userSelectedEnvPath'; |
| 13 | + |
| 14 | +@injectable() |
| 15 | +export class RecommendedEnvironmentService implements IRecommendedEnvironmentService { |
| 16 | + private api?: PythonExtension['environments']; |
| 17 | + constructor(@inject(IExtensionContext) private readonly extensionContext: IExtensionContext) {} |
| 18 | + |
| 19 | + registerEnvApi(api: PythonExtension['environments']) { |
| 20 | + this.api = api; |
| 21 | + } |
| 22 | + |
| 23 | + trackUserSelectedEnvironment(environmentPath: string | undefined, uri: Uri | undefined) { |
| 24 | + if (workspace.workspaceFolders?.length) { |
| 25 | + try { |
| 26 | + void updateWorkspaceStateValue(MEMENTO_KEY, getDataToStore(environmentPath, uri)); |
| 27 | + } catch (ex) { |
| 28 | + traceError('Failed to update workspace state for preferred environment', ex); |
| 29 | + } |
| 30 | + } else { |
| 31 | + void this.extensionContext.globalState.update(MEMENTO_KEY, environmentPath); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + getRecommededEnvironment( |
| 36 | + resource: Resource, |
| 37 | + ): |
| 38 | + | { environmentPath: string; reason: 'globalUserSelected' | 'workspaceUserSelected' | 'defaultRecommended' } |
| 39 | + | undefined { |
| 40 | + let workspaceState: string | undefined = undefined; |
| 41 | + try { |
| 42 | + workspaceState = getWorkspaceStateValue<string>(MEMENTO_KEY); |
| 43 | + } catch (ex) { |
| 44 | + traceError('Failed to get workspace state for preferred environment', ex); |
| 45 | + } |
| 46 | + |
| 47 | + if (workspace.workspaceFolders?.length && workspaceState) { |
| 48 | + const workspaceUri = ( |
| 49 | + (resource ? workspace.getWorkspaceFolder(resource)?.uri : undefined) || |
| 50 | + workspace.workspaceFolders[0].uri |
| 51 | + ).toString(); |
| 52 | + |
| 53 | + try { |
| 54 | + const existingJson: Record<string, string> = JSON.parse(workspaceState); |
| 55 | + const selectedEnvPath = existingJson[workspaceUri]; |
| 56 | + if (selectedEnvPath) { |
| 57 | + return { environmentPath: selectedEnvPath, reason: 'workspaceUserSelected' }; |
| 58 | + } |
| 59 | + } catch (ex) { |
| 60 | + traceError('Failed to parse existing workspace state value for preferred environment', ex); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + const globalSelectedEnvPath = this.extensionContext.globalState.get<string | undefined>(MEMENTO_KEY); |
| 65 | + if (globalSelectedEnvPath) { |
| 66 | + return { environmentPath: globalSelectedEnvPath, reason: 'globalUserSelected' }; |
| 67 | + } |
| 68 | + return this.api && workspace.isTrusted |
| 69 | + ? { |
| 70 | + environmentPath: this.api.getActiveEnvironmentPath(resource).path, |
| 71 | + reason: 'defaultRecommended', |
| 72 | + } |
| 73 | + : undefined; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function getDataToStore(environmentPath: string | undefined, uri: Uri | undefined): string | undefined { |
| 78 | + if (!workspace.workspaceFolders?.length) { |
| 79 | + return environmentPath; |
| 80 | + } |
| 81 | + const workspaceUri = ( |
| 82 | + (uri ? workspace.getWorkspaceFolder(uri)?.uri : undefined) || workspace.workspaceFolders[0].uri |
| 83 | + ).toString(); |
| 84 | + const existingData = getWorkspaceStateValue<string>(MEMENTO_KEY); |
| 85 | + if (!existingData) { |
| 86 | + return JSON.stringify(environmentPath ? { [workspaceUri]: environmentPath } : {}); |
| 87 | + } |
| 88 | + try { |
| 89 | + const existingJson: Record<string, string> = JSON.parse(existingData); |
| 90 | + if (environmentPath) { |
| 91 | + existingJson[workspaceUri] = environmentPath; |
| 92 | + } else { |
| 93 | + delete existingJson[workspaceUri]; |
| 94 | + } |
| 95 | + return JSON.stringify(existingJson); |
| 96 | + } catch (ex) { |
| 97 | + traceError('Failed to parse existing workspace state value for preferred environment', ex); |
| 98 | + return JSON.stringify({ |
| 99 | + [workspaceUri]: environmentPath, |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
0 commit comments