forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreterPathService.ts
More file actions
201 lines (188 loc) · 9.61 KB
/
interpreterPathService.ts
File metadata and controls
201 lines (188 loc) · 9.61 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as fs from 'fs-extra';
import { inject, injectable } from 'inversify';
import { ConfigurationChangeEvent, ConfigurationTarget, Event, EventEmitter, Uri } from 'vscode';
import { IWorkspaceService } from './application/types';
import { PythonSettings } from './configSettings';
import { isTestExecution } from './constants';
import { FileSystemPaths } from './platform/fs-paths';
import {
IDisposable,
IDisposableRegistry,
IInterpreterPathService,
InspectInterpreterSettingType,
InterpreterConfigurationScope,
IPersistentState,
IPersistentStateFactory,
IPythonSettings,
Resource
} from './types';
export const workspaceKeysForWhichTheCopyIsDone_Key = 'workspaceKeysForWhichTheCopyIsDone_Key';
export const workspaceFolderKeysForWhichTheCopyIsDone_Key = 'workspaceFolderKeysForWhichTheCopyIsDone_Key';
export const isGlobalSettingCopiedKey = 'isGlobalSettingCopiedKey';
export const defaultInterpreterPathSetting: keyof IPythonSettings = 'defaultInterpreterPath';
const CI_PYTHON_PATH = getCIPythonPath();
export function getCIPythonPath(): string {
if (process.env.CI_PYTHON_PATH && fs.existsSync(process.env.CI_PYTHON_PATH)) {
return process.env.CI_PYTHON_PATH;
}
return 'python';
}
@injectable()
export class InterpreterPathService implements IInterpreterPathService {
public get onDidChange(): Event<InterpreterConfigurationScope> {
return this._didChangeInterpreterEmitter.event;
}
public _didChangeInterpreterEmitter = new EventEmitter<InterpreterConfigurationScope>();
private fileSystemPaths: FileSystemPaths;
constructor(
@inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IDisposableRegistry) disposables: IDisposable[]
) {
disposables.push(this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)));
this.fileSystemPaths = FileSystemPaths.withDefaults();
}
public async onDidChangeConfiguration(event: ConfigurationChangeEvent) {
if (event.affectsConfiguration(`python.${defaultInterpreterPathSetting}`)) {
this._didChangeInterpreterEmitter.fire({ uri: undefined, configTarget: ConfigurationTarget.Global });
}
}
public inspect(resource: Resource): InspectInterpreterSettingType {
resource = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService).uri;
let workspaceFolderSetting: IPersistentState<string | undefined> | undefined;
let workspaceSetting: IPersistentState<string | undefined> | undefined;
if (resource) {
workspaceFolderSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
this.getSettingKey(resource, ConfigurationTarget.WorkspaceFolder),
undefined
);
workspaceSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
this.getSettingKey(resource, ConfigurationTarget.Workspace),
undefined
);
}
const globalValue = this.workspaceService.getConfiguration('python')!.inspect<string>('defaultInterpreterPath')!
.globalValue;
return {
globalValue,
workspaceFolderValue: workspaceFolderSetting?.value,
workspaceValue: workspaceSetting?.value
};
}
public get(resource: Resource): string {
const settings = this.inspect(resource);
return (
settings.workspaceFolderValue ||
settings.workspaceValue ||
settings.globalValue ||
(isTestExecution() ? CI_PYTHON_PATH : 'python')
);
}
public async update(
resource: Resource,
configTarget: ConfigurationTarget,
pythonPath: string | undefined
): Promise<void> {
resource = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService).uri;
if (configTarget === ConfigurationTarget.Global) {
const pythonConfig = this.workspaceService.getConfiguration('python');
const globalValue = pythonConfig.inspect<string>('defaultInterpreterPath')!.globalValue;
if (globalValue !== pythonPath) {
await pythonConfig.update('defaultInterpreterPath', pythonPath, true);
this._didChangeInterpreterEmitter.fire({ uri: undefined, configTarget });
}
return;
}
if (!resource) {
throw new Error('Cannot update workspace settings as no workspace is opened');
}
const settingKey = this.getSettingKey(resource, configTarget);
const persistentSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
settingKey,
undefined
);
if (persistentSetting.value !== pythonPath) {
await persistentSetting.updateValue(pythonPath);
this._didChangeInterpreterEmitter.fire({ uri: resource, configTarget });
}
}
public getSettingKey(
resource: Uri,
configTarget: ConfigurationTarget.Workspace | ConfigurationTarget.WorkspaceFolder
): string {
let settingKey: string;
const folderKey = this.workspaceService.getWorkspaceFolderIdentifier(resource);
if (configTarget === ConfigurationTarget.WorkspaceFolder) {
settingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${folderKey}`;
} else {
settingKey = this.workspaceService.workspaceFile
? `WORKSPACE_INTERPRETER_PATH_${this.fileSystemPaths.normCase(
this.workspaceService.workspaceFile.fsPath
)}`
: // Only a single folder is opened, use fsPath of the folder as key
`WORKSPACE_FOLDER_INTERPRETER_PATH_${folderKey}`;
}
return settingKey;
}
public async copyOldInterpreterStorageValuesToNew(resource: Resource): Promise<void> {
resource = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService).uri;
const oldSettings = this.workspaceService.getConfiguration('python', resource).inspect<string>('pythonPath')!;
await Promise.all([
this._copyWorkspaceFolderValueToNewStorage(resource, oldSettings.workspaceFolderValue),
this._copyWorkspaceValueToNewStorage(resource, oldSettings.workspaceValue),
this._moveGlobalSettingValueToNewStorage(oldSettings.globalValue)
]);
}
public async _copyWorkspaceFolderValueToNewStorage(resource: Resource, value: string | undefined): Promise<void> {
// Copy workspace folder setting into the new storage if it hasn't been copied already
const workspaceFolderKey = this.workspaceService.getWorkspaceFolderIdentifier(resource);
const flaggedWorkspaceFolderKeysStorage = this.persistentStateFactory.createGlobalPersistentState<string[]>(
workspaceFolderKeysForWhichTheCopyIsDone_Key,
[]
);
const flaggedWorkspaceFolderKeys = flaggedWorkspaceFolderKeysStorage.value;
const shouldUpdateWorkspaceFolderSetting = !flaggedWorkspaceFolderKeys.includes(workspaceFolderKey);
if (shouldUpdateWorkspaceFolderSetting) {
await this.update(resource, ConfigurationTarget.WorkspaceFolder, value);
await flaggedWorkspaceFolderKeysStorage.updateValue([workspaceFolderKey, ...flaggedWorkspaceFolderKeys]);
}
}
public async _copyWorkspaceValueToNewStorage(resource: Resource, value: string | undefined): Promise<void> {
// Copy workspace setting into the new storage if it hasn't been copied already
const workspaceKey = this.workspaceService.workspaceFile
? this.fileSystemPaths.normCase(this.workspaceService.workspaceFile.fsPath)
: undefined;
if (!workspaceKey) {
return;
}
const flaggedWorkspaceKeysStorage = this.persistentStateFactory.createGlobalPersistentState<string[]>(
workspaceKeysForWhichTheCopyIsDone_Key,
[]
);
const flaggedWorkspaceKeys = flaggedWorkspaceKeysStorage.value;
const shouldUpdateWorkspaceSetting = !flaggedWorkspaceKeys.includes(workspaceKey);
if (shouldUpdateWorkspaceSetting) {
await this.update(resource, ConfigurationTarget.Workspace, value);
await flaggedWorkspaceKeysStorage.updateValue([workspaceKey, ...flaggedWorkspaceKeys]);
}
}
public async _moveGlobalSettingValueToNewStorage(value: string | undefined) {
// Move global setting into the new storage if it hasn't been moved already
const isGlobalSettingCopiedStorage = this.persistentStateFactory.createGlobalPersistentState<boolean>(
isGlobalSettingCopiedKey,
false
);
const shouldUpdateGlobalSetting = !isGlobalSettingCopiedStorage.value;
if (shouldUpdateGlobalSetting) {
await this.update(undefined, ConfigurationTarget.Global, value);
// Make sure to delete the original setting after copying it
await this.workspaceService
.getConfiguration('python')
.update('pythonPath', undefined, ConfigurationTarget.Global);
await isGlobalSettingCopiedStorage.updateValue(true);
}
}
}