1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
+
3
4
'use strict' ;
5
+
4
6
import '../../extensions' ;
5
7
6
8
import { inject , injectable } from 'inversify' ;
7
9
import * as path from 'path' ;
8
10
import { Uri } from 'vscode' ;
9
11
10
- import { ICondaLocatorService , ICondaService } from '../../../interpreter/contracts' ;
12
+ import { IComponentAdapter , ICondaLocatorService , ICondaService } from '../../../interpreter/contracts' ;
11
13
import { IPlatformService } from '../../platform/types' ;
12
- import { IConfigurationService } from '../../types' ;
14
+ import { IConfigurationService , IExperimentService } from '../../types' ;
13
15
import { ITerminalActivationCommandProvider , TerminalShellType } from '../types' ;
14
16
import { IServiceContainer } from '../../../ioc/types' ;
17
+ import { inDiscoveryExperiment } from '../../experiments/helpers' ;
15
18
16
19
// Version number of conda that requires we call activate with 'conda activate' instead of just 'activate'
17
20
const CondaRequiredMajor = 4 ;
@@ -28,12 +31,15 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
28
31
@inject ( IPlatformService ) private platform : IPlatformService ,
29
32
@inject ( IConfigurationService ) private configService : IConfigurationService ,
30
33
@inject ( IServiceContainer ) private serviceContainer : IServiceContainer ,
34
+ @inject ( IExperimentService ) private experimentService : IExperimentService ,
35
+ @inject ( IComponentAdapter ) private pyenvs : IComponentAdapter ,
31
36
) { }
32
37
33
38
/**
34
39
* Is the given shell supported for activating a conda env?
35
40
*/
36
- public isShellSupported ( _targetShell : TerminalShellType ) : boolean {
41
+ // eslint-disable-next-line class-methods-use-this
42
+ public isShellSupported ( ) : boolean {
37
43
return true ;
38
44
}
39
45
@@ -44,7 +50,7 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
44
50
resource : Uri | undefined ,
45
51
targetShell : TerminalShellType ,
46
52
) : Promise < string [ ] | undefined > {
47
- const pythonPath = this . configService . getSettings ( resource ) . pythonPath ;
53
+ const { pythonPath } = this . configService . getSettings ( resource ) ;
48
54
return this . getActivationCommandsForInterpreter ( pythonPath , targetShell ) ;
49
55
}
50
56
@@ -56,10 +62,12 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
56
62
pythonPath : string ,
57
63
targetShell : TerminalShellType ,
58
64
) : Promise < string [ ] | undefined > {
59
- const condaLocatorService = this . serviceContainer . get < ICondaLocatorService > ( ICondaLocatorService ) ;
65
+ const condaLocatorService = ( await inDiscoveryExperiment ( this . experimentService ) )
66
+ ? this . pyenvs
67
+ : this . serviceContainer . get < ICondaLocatorService > ( ICondaLocatorService ) ;
60
68
const envInfo = await condaLocatorService . getCondaEnvironment ( pythonPath ) ;
61
69
if ( ! envInfo ) {
62
- return ;
70
+ return undefined ;
63
71
}
64
72
65
73
const condaEnv = envInfo . name . length > 0 ? envInfo . name : envInfo . path ;
@@ -75,7 +83,7 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
75
83
versionInfo . minor >= CondaRequiredMinorForPowerShell &&
76
84
( targetShell === TerminalShellType . powershell || targetShell === TerminalShellType . powershellCore )
77
85
) {
78
- return this . getPowershellCommands ( condaEnv ) ;
86
+ return _getPowershellCommands ( condaEnv ) ;
79
87
}
80
88
if ( versionInfo . minor >= CondaRequiredMinor ) {
81
89
// New version.
@@ -91,23 +99,22 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
91
99
switch ( targetShell ) {
92
100
case TerminalShellType . powershell :
93
101
case TerminalShellType . powershellCore :
94
- return this . getPowershellCommands ( condaEnv ) ;
102
+ return _getPowershellCommands ( condaEnv ) ;
95
103
96
104
// TODO: Do we really special-case fish on Windows?
97
105
case TerminalShellType . fish :
98
- return this . getFishCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
106
+ return getFishCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
99
107
100
108
default :
101
109
if ( this . platform . isWindows ) {
102
110
return this . getWindowsCommands ( condaEnv ) ;
103
- } else {
104
- return this . getUnixCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
105
111
}
112
+ return getUnixCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
106
113
}
107
114
}
108
115
109
116
public async getWindowsActivateCommand ( ) : Promise < string > {
110
- let activateCmd : string = 'activate' ;
117
+ let activateCmd = 'activate' ;
111
118
112
119
const condaExePath = await this . condaService . getCondaFile ( ) ;
113
120
@@ -125,28 +132,25 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
125
132
const activate = await this . getWindowsActivateCommand ( ) ;
126
133
return [ `${ activate } ${ condaEnv . toCommandArgument ( ) } ` ] ;
127
134
}
128
- /**
129
- * The expectation is for the user to configure Powershell for Conda.
130
- * Hence we just send the command `conda activate ...`.
131
- * This configuration is documented on Conda.
132
- * Extension will not attempt to work around issues by trying to setup shell for user.
133
- *
134
- * @param {string } condaEnv
135
- * @returns {(Promise<string[] | undefined>) }
136
- * @memberof CondaActivationCommandProvider
137
- */
138
- public async getPowershellCommands ( condaEnv : string ) : Promise < string [ ] | undefined > {
139
- return [ `conda activate ${ condaEnv . toCommandArgument ( ) } ` ] ;
140
- }
135
+ }
141
136
142
- public async getFishCommands ( condaEnv : string , condaFile : string ) : Promise < string [ ] | undefined > {
143
- // https://github.com/conda/conda/blob/be8c08c083f4d5e05b06bd2689d2cd0d410c2ffe/shell/etc/fish/conf.d/conda.fish#L18-L28
144
- return [ `${ condaFile . fileToCommandArgument ( ) } activate ${ condaEnv . toCommandArgument ( ) } ` ] ;
145
- }
137
+ /**
138
+ * The expectation is for the user to configure Powershell for Conda.
139
+ * Hence we just send the command `conda activate ...`.
140
+ * This configuration is documented on Conda.
141
+ * Extension will not attempt to work around issues by trying to setup shell for user.
142
+ */
143
+ export async function _getPowershellCommands ( condaEnv : string ) : Promise < string [ ] | undefined > {
144
+ return [ `conda activate ${ condaEnv . toCommandArgument ( ) } ` ] ;
145
+ }
146
146
147
- public async getUnixCommands ( condaEnv : string , condaFile : string ) : Promise < string [ ] | undefined > {
148
- const condaDir = path . dirname ( condaFile ) ;
149
- const activateFile = path . join ( condaDir , 'activate' ) ;
150
- return [ `source ${ activateFile . fileToCommandArgument ( ) } ${ condaEnv . toCommandArgument ( ) } ` ] ;
151
- }
147
+ async function getFishCommands ( condaEnv : string , condaFile : string ) : Promise < string [ ] | undefined > {
148
+ // https://github.com/conda/conda/blob/be8c08c083f4d5e05b06bd2689d2cd0d410c2ffe/shell/etc/fish/conf.d/conda.fish#L18-L28
149
+ return [ `${ condaFile . fileToCommandArgument ( ) } activate ${ condaEnv . toCommandArgument ( ) } ` ] ;
150
+ }
151
+
152
+ async function getUnixCommands ( condaEnv : string , condaFile : string ) : Promise < string [ ] | undefined > {
153
+ const condaDir = path . dirname ( condaFile ) ;
154
+ const activateFile = path . join ( condaDir , 'activate' ) ;
155
+ return [ `source ${ activateFile . fileToCommandArgument ( ) } ${ condaEnv . toCommandArgument ( ) } ` ] ;
152
156
}
0 commit comments