forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.ts
More file actions
294 lines (270 loc) · 13.1 KB
/
installer.ts
File metadata and controls
294 lines (270 loc) · 13.1 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import * as vscode from 'vscode';
import * as settings from './configSettings';
import { createDeferred, isNotInstalledError } from './helpers';
import { execPythonFile, getFullyQualifiedPythonInterpreterPath } from './utils';
import * as os from 'os';
import { Documentation } from './constants';
export enum Product {
pytest,
nosetest,
pylint,
flake8,
pep8,
pylama,
prospector,
pydocstyle,
yapf,
autopep8,
mypy,
unittest,
ctags,
rope
}
const ProductInstallScripts = new Map<Product, string[]>();
ProductInstallScripts.set(Product.autopep8, ['-m', 'pip', 'install', 'autopep8']);
ProductInstallScripts.set(Product.flake8, ['-m', 'pip', 'install', 'flake8']);
ProductInstallScripts.set(Product.mypy, ['-m', 'pip', 'install', 'mypy']);
ProductInstallScripts.set(Product.nosetest, ['-m', 'pip', 'install', 'nose']);
ProductInstallScripts.set(Product.pep8, ['-m', 'pip', 'install', 'pep8']);
ProductInstallScripts.set(Product.pylama, ['-m', 'pip', 'install', 'pylama']);
ProductInstallScripts.set(Product.prospector, ['-m', 'pip', 'install', 'prospector']);
ProductInstallScripts.set(Product.pydocstyle, ['-m', 'pip', 'install', 'pydocstyle']);
ProductInstallScripts.set(Product.pylint, ['-m', 'pip', 'install', 'pylint']);
ProductInstallScripts.set(Product.pytest, ['-m', 'pip', 'install', '-U', 'pytest']);
ProductInstallScripts.set(Product.yapf, ['-m', 'pip', 'install', 'yapf']);
ProductInstallScripts.set(Product.rope, ['-m', 'pip', 'install', 'rope']);
const ProductUninstallScripts = new Map<Product, string[]>();
ProductUninstallScripts.set(Product.autopep8, ['-m', 'pip', 'uninstall', 'autopep8', '--yes']);
ProductUninstallScripts.set(Product.flake8, ['-m', 'pip', 'uninstall', 'flake8', '--yes']);
ProductUninstallScripts.set(Product.mypy, ['-m', 'pip', 'uninstall', 'mypy', '--yes']);
ProductUninstallScripts.set(Product.nosetest, ['-m', 'pip', 'uninstall', 'nose', '--yes']);
ProductUninstallScripts.set(Product.pep8, ['-m', 'pip', 'uninstall', 'pep8', '--yes']);
ProductUninstallScripts.set(Product.pylama, ['-m', 'pip', 'uninstall', 'pylama', '--yes']);
ProductUninstallScripts.set(Product.prospector, ['-m', 'pip', 'uninstall', 'prospector', '--yes']);
ProductUninstallScripts.set(Product.pydocstyle, ['-m', 'pip', 'uninstall', 'pydocstyle', '--yes']);
ProductUninstallScripts.set(Product.pylint, ['-m', 'pip', 'uninstall', 'pylint', '--yes']);
ProductUninstallScripts.set(Product.pytest, ['-m', 'pip', 'uninstall', 'pytest', '--yes']);
ProductUninstallScripts.set(Product.yapf, ['-m', 'pip', 'uninstall', 'yapf', '--yes']);
ProductUninstallScripts.set(Product.rope, ['-m', 'pip', 'uninstall', 'rope', '--yes']);
export const ProductExecutableAndArgs = new Map<Product, { executable: string, args: string[] }>();
ProductExecutableAndArgs.set(Product.mypy, { executable: 'python', args: ['-m', 'mypy'] });
ProductExecutableAndArgs.set(Product.nosetest, { executable: 'python', args: ['-m', 'nose'] });
ProductExecutableAndArgs.set(Product.pylama, { executable: 'python', args: ['-m', 'pylama'] });
ProductExecutableAndArgs.set(Product.prospector, { executable: 'python', args: ['-m', 'prospector'] });
ProductExecutableAndArgs.set(Product.pylint, { executable: 'python', args: ['-m', 'pylint'] });
ProductExecutableAndArgs.set(Product.pytest, { executable: 'python', args: ['-m', 'pytest'] });
ProductExecutableAndArgs.set(Product.autopep8, { executable: 'python', args: ['-m', 'autopep8'] });
ProductExecutableAndArgs.set(Product.pep8, { executable: 'python', args: ['-m', 'pep8'] });
ProductExecutableAndArgs.set(Product.pydocstyle, { executable: 'python', args: ['-m', 'pydocstyle'] });
ProductExecutableAndArgs.set(Product.yapf, { executable: 'python', args: ['-m', 'yapf'] });
ProductExecutableAndArgs.set(Product.flake8, { executable: 'python', args: ['-m', 'flake8'] });
switch (os.platform()) {
case 'win32': {
// Nothing
break;
}
case 'darwin': {
ProductInstallScripts.set(Product.ctags, ['brew install ctags']);
}
default: {
ProductInstallScripts.set(Product.ctags, ['sudo apt-get install exuberant-ctags']);
}
}
export const Linters: Product[] = [
Product.flake8,
Product.pep8,
Product.pylama,
Product.prospector,
Product.pylint,
Product.mypy,
Product.pydocstyle
];
const Formatters: Product[] = [Product.autopep8, Product.yapf];
const TestFrameworks: Product[] = [Product.pytest, Product.nosetest, Product.unittest];
const ProductNames = new Map<Product, string>();
ProductNames.set(Product.autopep8, 'autopep8');
ProductNames.set(Product.flake8, 'flake8');
ProductNames.set(Product.mypy, 'mypy');
ProductNames.set(Product.nosetest, 'nosetest');
ProductNames.set(Product.pep8, 'pep8');
ProductNames.set(Product.pylama, 'pylama');
ProductNames.set(Product.prospector, 'prospector');
ProductNames.set(Product.pydocstyle, 'pydocstyle');
ProductNames.set(Product.pylint, 'pylint');
ProductNames.set(Product.pytest, 'py.test');
ProductNames.set(Product.yapf, 'yapf');
ProductNames.set(Product.rope, 'rope');
export const SettingToDisableProduct = new Map<Product, string>();
SettingToDisableProduct.set(Product.flake8, 'linting.flake8Enabled');
SettingToDisableProduct.set(Product.mypy, 'linting.mypyEnabled');
SettingToDisableProduct.set(Product.nosetest, 'unitTest.nosetestsEnabled');
SettingToDisableProduct.set(Product.pep8, 'linting.pep8Enabled');
SettingToDisableProduct.set(Product.pylama, 'linting.pylamaEnabled');
SettingToDisableProduct.set(Product.prospector, 'linting.prospectorEnabled');
SettingToDisableProduct.set(Product.pydocstyle, 'linting.pydocstyleEnabled');
SettingToDisableProduct.set(Product.pylint, 'linting.pylintEnabled');
SettingToDisableProduct.set(Product.pytest, 'unitTest.pyTestEnabled');
enum ProductType {
Linter,
Formatter,
TestFramework,
RefactoringLibrary,
WorkspaceSymbols
}
const ProductTypeNames = new Map<ProductType, string>();
ProductTypeNames.set(ProductType.Formatter, 'Formatter');
ProductTypeNames.set(ProductType.Linter, 'Linter');
ProductTypeNames.set(ProductType.RefactoringLibrary, 'Refactoring library');
ProductTypeNames.set(ProductType.TestFramework, 'Test Framework');
ProductTypeNames.set(ProductType.WorkspaceSymbols, 'Workspace Symbols');
const ProductTypes = new Map<Product, ProductType>();
ProductTypes.set(Product.flake8, ProductType.Linter);
ProductTypes.set(Product.mypy, ProductType.Linter);
ProductTypes.set(Product.pep8, ProductType.Linter);
ProductTypes.set(Product.prospector, ProductType.Linter);
ProductTypes.set(Product.pydocstyle, ProductType.Linter);
ProductTypes.set(Product.pylama, ProductType.Linter);
ProductTypes.set(Product.pylint, ProductType.Linter);
ProductTypes.set(Product.ctags, ProductType.WorkspaceSymbols);
ProductTypes.set(Product.nosetest, ProductType.TestFramework);
ProductTypes.set(Product.pytest, ProductType.TestFramework);
ProductTypes.set(Product.unittest, ProductType.TestFramework);
ProductTypes.set(Product.autopep8, ProductType.Formatter);
ProductTypes.set(Product.yapf, ProductType.Formatter);
ProductTypes.set(Product.rope, ProductType.RefactoringLibrary);
export class Installer implements vscode.Disposable {
private static terminal: vscode.Terminal;
private disposables: vscode.Disposable[] = [];
constructor(private outputChannel: vscode.OutputChannel = null) {
this.disposables.push(vscode.window.onDidCloseTerminal(term => {
if (term === Installer.terminal) {
Installer.terminal = null;
}
}));
}
public dispose() {
this.disposables.forEach(d => d.dispose());
}
promptToInstall(product: Product): Thenable<any> {
const productType = ProductTypes.get(product);
const productTypeName = ProductTypeNames.get(productType);
const productName = ProductNames.get(product);
const installOption = 'Install ' + productName;
const disableOption = 'Disable ' + productTypeName;
const disableOptionGlobally = `Disable ${productTypeName} globally`;
const alternateFormatter = product === Product.autopep8 ? 'yapf' : 'autopep8';
const useOtherFormatter = `Use '${alternateFormatter}' formatter`;
const options = [];
options.push(installOption);
if (productType === ProductType.Formatter) {
options.push(...[installOption, useOtherFormatter]);
}
if (SettingToDisableProduct.has(product)) {
options.push(...[disableOption, disableOptionGlobally]);
}
return vscode.window.showErrorMessage(`${productTypeName} ${productName} is not installed`, ...options).then(item => {
switch (item) {
case installOption: {
return this.install(product);
}
case disableOptionGlobally:
case disableOption: {
const global = item === disableOptionGlobally;
if (Linters.indexOf(product) >= 0) {
return disableLinter(product, global);
}
else {
const pythonConfig = vscode.workspace.getConfiguration('python');
const settingToDisable = SettingToDisableProduct.get(product);
return pythonConfig.update(settingToDisable, false, global);
}
}
case useOtherFormatter: {
const pythonConfig = vscode.workspace.getConfiguration('python');
return pythonConfig.update('formatting.provider', alternateFormatter);
}
case 'Help': {
return Promise.resolve();
}
}
});
}
install(product: Product): Promise<any> {
if (!this.outputChannel && !Installer.terminal) {
Installer.terminal = vscode.window.createTerminal('Python Installer');
}
if (product === Product.ctags && os.platform() === 'win32') {
vscode.commands.executeCommand('python.displayHelp', Documentation.Workspace.InstallOnWindows);
return Promise.resolve();
}
let installArgs = ProductInstallScripts.get(product);
let pipIndex = installArgs.indexOf('pip');
if (pipIndex > 0) {
installArgs = installArgs.slice();
let proxy = vscode.workspace.getConfiguration('http').get('proxy', '');
if (proxy.length > 0) {
installArgs.splice(2, 0, proxy);
installArgs.splice(2, 0, '--proxy');
}
}
if (this.outputChannel && installArgs[0] === '-m') {
// Errors are just displayed to the user
this.outputChannel.show();
return execPythonFile(settings.PythonSettings.getInstance().pythonPath, installArgs, vscode.workspace.rootPath, true, (data) => {
this.outputChannel.append(data);
});
}
else {
// When using terminal get the fully qualitified path
// Cuz people may launch vs code from terminal when they have activated the appropriate virtual env
// Problem is terminal doesn't use the currently activated virtual env
// Must have something to do with the process being launched in the terminal
return getFullyQualifiedPythonInterpreterPath()
.then(pythonPath => {
let installScript = installArgs.join(' ');
if (installArgs[0] === '-m') {
if (pythonPath.indexOf(' ') >= 0) {
installScript = `"${pythonPath}" ${installScript}`;
}
else {
installScript = `${pythonPath} ${installScript}`;
}
}
Installer.terminal.sendText(installScript);
Installer.terminal.show(false);
});
}
}
isInstalled(product: Product): Promise<boolean | undefined> {
return isProductInstalled(product);
}
uninstall(product: Product): Promise<any> {
return uninstallproduct(product);
}
}
export function disableLinter(product: Product, global?: boolean) {
const pythonConfig = vscode.workspace.getConfiguration('python');
const settingToDisable = SettingToDisableProduct.get(product);
if (vscode.workspace.rootPath) {
return pythonConfig.update(settingToDisable, false, global);
}
else {
return pythonConfig.update('linting.enabledWithoutWorkspace', false, true);
}
}
function isProductInstalled(product: Product): Promise<boolean | undefined> {
if (!ProductExecutableAndArgs.has(product)) {
return;
}
const prodExec = ProductExecutableAndArgs.get(product);
return execPythonFile(prodExec.executable, prodExec.args.concat(['--version']), vscode.workspace.rootPath, false)
.then(() => {
return true;
}).catch(reason => {
return !isNotInstalledError(reason);
});
}
function uninstallproduct(product: Product): Promise<any> {
const uninstallArgs = ProductUninstallScripts.get(product);
return execPythonFile('python', uninstallArgs, vscode.workspace.rootPath, false);
}