1+ import * as vscode from 'vscode' ;
2+ import * as settings from './configSettings' ;
13
2- export enum product {
4+ export enum Product {
35 pytest ,
46 nosetest ,
57 pylint ,
@@ -8,17 +10,122 @@ export enum product {
810 prospector ,
911 pydocstyle ,
1012 yapf ,
11- autopep8
13+ autopep8 ,
14+ mypy ,
15+ unittest
1216}
1317
14- interface installIScript {
15- windows : string ;
16- mac : string ;
17- linux : string ;
18- }
18+ const ProductInstallScripts = new Map < Product , string > ( ) ;
19+ ProductInstallScripts . set ( Product . autopep8 , 'pip install autopep8' ) ;
20+ ProductInstallScripts . set ( Product . flake8 , 'pip install flake8' ) ;
21+ ProductInstallScripts . set ( Product . mypy , 'pip install mypy-lang' ) ;
22+ ProductInstallScripts . set ( Product . nosetest , 'pip install nose' ) ;
23+ ProductInstallScripts . set ( Product . pep8 , 'pip install pep8' ) ;
24+ ProductInstallScripts . set ( Product . prospector , 'pip install prospector' ) ;
25+ ProductInstallScripts . set ( Product . pydocstyle , 'pip install pydocstyle' ) ;
26+ ProductInstallScripts . set ( Product . pylint , 'pip install pylint' ) ;
27+ ProductInstallScripts . set ( Product . pytest , 'pip install -U pytest' ) ;
28+ ProductInstallScripts . set ( Product . yapf , 'pip install yapf' ) ;
29+
30+
31+ const Linters : Product [ ] = [ Product . flake8 , Product . pep8 , Product . prospector , Product . pylint , Product . mypy , Product . pydocstyle ] ;
32+ const Formatters : Product [ ] = [ Product . autopep8 , Product . yapf ] ;
33+ const TestFrameworks : Product [ ] = [ Product . pytest , Product . nosetest ] ;
34+
35+ const ProductNames = new Map < Product , string > ( ) ;
36+ ProductNames . set ( Product . autopep8 , 'autopep8' ) ;
37+ ProductNames . set ( Product . flake8 , 'flake8' ) ;
38+ ProductNames . set ( Product . mypy , 'mypy' ) ;
39+ ProductNames . set ( Product . nosetest , 'nosetest' ) ;
40+ ProductNames . set ( Product . pep8 , 'pep8' ) ;
41+ ProductNames . set ( Product . prospector , 'prospector' ) ;
42+ ProductNames . set ( Product . pydocstyle , 'pydocstyle' ) ;
43+ ProductNames . set ( Product . pylint , 'pylint' ) ;
44+ ProductNames . set ( Product . pytest , 'py.test' ) ;
45+ ProductNames . set ( Product . yapf , 'yapf' ) ;
46+
47+ const SettingToDisableProduct = new Map < Product , string > ( ) ;
48+ SettingToDisableProduct . set ( Product . autopep8 , '' ) ;
49+ SettingToDisableProduct . set ( Product . flake8 , 'linting.flake8Enabled' ) ;
50+ SettingToDisableProduct . set ( Product . mypy , 'linting.mypyEnabled' ) ;
51+ SettingToDisableProduct . set ( Product . nosetest , 'unitTest.nosetestsEnabled' ) ;
52+ SettingToDisableProduct . set ( Product . pep8 , 'linting.pep8Enabled' ) ;
53+ SettingToDisableProduct . set ( Product . prospector , 'linting.prospectorEnabled' ) ;
54+ SettingToDisableProduct . set ( Product . pydocstyle , 'linting.pydocstyleEnabled' ) ;
55+ SettingToDisableProduct . set ( Product . pylint , 'linting.pylintEnabled' ) ;
56+ SettingToDisableProduct . set ( Product . pytest , 'unitTest.pyTestEnabled' ) ;
57+ SettingToDisableProduct . set ( Product . yapf , 'yapf' ) ;
58+
59+ export class Installer {
60+ private static terminal : vscode . Terminal ;
61+ private disposables : vscode . Disposable [ ] = [ ] ;
62+ constructor ( ) {
63+ this . disposables . push ( vscode . window . onDidCloseTerminal ( term => {
64+ if ( term === Installer . terminal ) {
65+ Installer . terminal = null ;
66+ }
67+ } ) ) ;
68+ }
69+ public dispose ( ) {
70+ this . disposables . forEach ( d => d . dispose ( ) ) ;
71+ }
72+
73+ promptToInstall ( product : Product ) {
74+ let productType = Linters . indexOf ( product ) >= 0 ? 'Linter' : ( Formatters . indexOf ( product ) >= 0 ? 'Formatter' : 'Test Framework' ) ;
75+ const productName = ProductNames . get ( product ) ;
76+
77+ const installOption = 'Install ' + productName ;
78+ const disableOption = 'Disable this ' + productType ;
79+ const alternateFormatter = product === Product . autopep8 ? 'yapf' : 'autopep8' ;
80+ const useOtherFormatter = `Use '${ alternateFormatter } ' formatter` ;
81+ const options = [ ] ;
82+ if ( Formatters . indexOf ( product ) === - 1 ) {
83+ options . push ( ...[ installOption , disableOption , 'Help' ] ) ;
84+ }
85+ else {
86+ options . push ( ...[ installOption , useOtherFormatter , 'Help' ] ) ;
87+ }
88+ vscode . window . showErrorMessage ( `${ productType } ${ productName } is not installed` , ...options ) . then ( item => {
89+ switch ( item ) {
90+ case installOption : {
91+ this . installProduct ( product ) ;
92+ break ;
93+ }
94+ case disableOption : {
95+ const pythonConfig = vscode . workspace . getConfiguration ( 'python' ) ;
96+ const settingToDisable = SettingToDisableProduct . get ( product ) ;
97+ pythonConfig . update ( settingToDisable , false ) ;
98+ break ;
99+ }
100+ case useOtherFormatter : {
101+ const pythonConfig = vscode . workspace . getConfiguration ( 'python' ) ;
102+ pythonConfig . update ( 'formatting.provider' , alternateFormatter ) ;
103+ break ;
104+ }
105+ case 'Help' : {
106+ break ;
107+ }
108+ }
109+ } ) ;
110+ }
19111
20- const productInstallScripts = new Map < product , installIScript > ( ) ;
112+ installProduct ( product : Product ) {
113+ if ( ! Installer . terminal ) {
114+ Installer . terminal = vscode . window . createTerminal ( 'Python Installer' ) ;
115+ }
21116
22- export function promptToInstall ( prod :product ) {
117+ let installScript = ProductInstallScripts . get ( product ) ;
118+ if ( installScript . startsWith ( 'pip install' ) ) {
119+ const pythonPath = settings . PythonSettings . getInstance ( ) . pythonPath ;
120+ if ( pythonPath . indexOf ( ' ' ) >= 0 ) {
121+ installScript = `"${ pythonPath } " -m ${ installScript } ` ;
122+ }
123+ else {
124+ installScript = `${ pythonPath } -m ${ installScript } ` ;
125+ }
126+ }
23127
128+ Installer . terminal . sendText ( installScript ) ;
129+ Installer . terminal . show ( false ) ;
130+ }
24131}
0 commit comments