11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
3+
34'use strict' ;
5+
46import '../../extensions' ;
57
68import { inject , injectable } from 'inversify' ;
79import * as path from 'path' ;
810import { Uri } from 'vscode' ;
911
10- import { ICondaLocatorService , ICondaService } from '../../../interpreter/contracts' ;
12+ import { IComponentAdapter , ICondaLocatorService , ICondaService } from '../../../interpreter/contracts' ;
1113import { IPlatformService } from '../../platform/types' ;
12- import { IConfigurationService } from '../../types' ;
14+ import { IConfigurationService , IExperimentService } from '../../types' ;
1315import { ITerminalActivationCommandProvider , TerminalShellType } from '../types' ;
1416import { IServiceContainer } from '../../../ioc/types' ;
17+ import { inDiscoveryExperiment } from '../../experiments/helpers' ;
1518
1619// Version number of conda that requires we call activate with 'conda activate' instead of just 'activate'
1720const CondaRequiredMajor = 4 ;
@@ -28,12 +31,15 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
2831 @inject ( IPlatformService ) private platform : IPlatformService ,
2932 @inject ( IConfigurationService ) private configService : IConfigurationService ,
3033 @inject ( IServiceContainer ) private serviceContainer : IServiceContainer ,
34+ @inject ( IExperimentService ) private experimentService : IExperimentService ,
35+ @inject ( IComponentAdapter ) private pyenvs : IComponentAdapter ,
3136 ) { }
3237
3338 /**
3439 * Is the given shell supported for activating a conda env?
3540 */
36- public isShellSupported ( _targetShell : TerminalShellType ) : boolean {
41+ // eslint-disable-next-line class-methods-use-this
42+ public isShellSupported ( ) : boolean {
3743 return true ;
3844 }
3945
@@ -44,7 +50,7 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
4450 resource : Uri | undefined ,
4551 targetShell : TerminalShellType ,
4652 ) : Promise < string [ ] | undefined > {
47- const pythonPath = this . configService . getSettings ( resource ) . pythonPath ;
53+ const { pythonPath } = this . configService . getSettings ( resource ) ;
4854 return this . getActivationCommandsForInterpreter ( pythonPath , targetShell ) ;
4955 }
5056
@@ -56,10 +62,12 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
5662 pythonPath : string ,
5763 targetShell : TerminalShellType ,
5864 ) : 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 ) ;
6068 const envInfo = await condaLocatorService . getCondaEnvironment ( pythonPath ) ;
6169 if ( ! envInfo ) {
62- return ;
70+ return undefined ;
6371 }
6472
6573 const condaEnv = envInfo . name . length > 0 ? envInfo . name : envInfo . path ;
@@ -75,7 +83,7 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
7583 versionInfo . minor >= CondaRequiredMinorForPowerShell &&
7684 ( targetShell === TerminalShellType . powershell || targetShell === TerminalShellType . powershellCore )
7785 ) {
78- return this . getPowershellCommands ( condaEnv ) ;
86+ return _getPowershellCommands ( condaEnv ) ;
7987 }
8088 if ( versionInfo . minor >= CondaRequiredMinor ) {
8189 // New version.
@@ -91,23 +99,22 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
9199 switch ( targetShell ) {
92100 case TerminalShellType . powershell :
93101 case TerminalShellType . powershellCore :
94- return this . getPowershellCommands ( condaEnv ) ;
102+ return _getPowershellCommands ( condaEnv ) ;
95103
96104 // TODO: Do we really special-case fish on Windows?
97105 case TerminalShellType . fish :
98- return this . getFishCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
106+ return getFishCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
99107
100108 default :
101109 if ( this . platform . isWindows ) {
102110 return this . getWindowsCommands ( condaEnv ) ;
103- } else {
104- return this . getUnixCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
105111 }
112+ return getUnixCommands ( condaEnv , await this . condaService . getCondaFile ( ) ) ;
106113 }
107114 }
108115
109116 public async getWindowsActivateCommand ( ) : Promise < string > {
110- let activateCmd : string = 'activate' ;
117+ let activateCmd = 'activate' ;
111118
112119 const condaExePath = await this . condaService . getCondaFile ( ) ;
113120
@@ -125,28 +132,25 @@ export class CondaActivationCommandProvider implements ITerminalActivationComman
125132 const activate = await this . getWindowsActivateCommand ( ) ;
126133 return [ `${ activate } ${ condaEnv . toCommandArgument ( ) } ` ] ;
127134 }
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+ }
141136
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+ }
146146
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 ( ) } ` ] ;
152156}
0 commit comments