@@ -9,11 +9,17 @@ import { inject, injectable } from 'inversify';
9
9
import { IExtensionSingleActivationService } from '../../../../../activation/types' ;
10
10
import { Commands } from '../../../../../common/constants' ;
11
11
import { IDisposableRegistry } from '../../../../../common/types' ;
12
- import { ITerminalServiceFactory } from '../../../../../common/terminal/types' ;
13
- import { ICommandManager } from '../../../../../common/application/types' ;
12
+ import { ICommandManager , ITerminalManager } from '../../../../../common/application/types' ;
14
13
import { sleep } from '../../../../../common/utils/async' ;
15
14
import { OSType } from '../../../../../common/utils/platform' ;
16
15
import { traceVerbose } from '../../../../../logging' ;
16
+ import { Interpreters } from '../../../../../common/utils/localize' ;
17
+
18
+ enum PackageManagers {
19
+ brew = 'brew' ,
20
+ apt = 'apt' ,
21
+ dnf = 'dnf' ,
22
+ }
17
23
18
24
/**
19
25
* Runs commands listed in walkthrough to install Python.
@@ -22,9 +28,15 @@ import { traceVerbose } from '../../../../../logging';
22
28
export class InstallPythonViaTerminal implements IExtensionSingleActivationService {
23
29
public readonly supportedWorkspaceTypes = { untrustedWorkspace : true , virtualWorkspace : false } ;
24
30
31
+ private readonly packageManagerCommands : Record < PackageManagers , string [ ] > = {
32
+ brew : [ 'brew install python3' ] ,
33
+ dnf : [ 'sudo dnf install python3' ] ,
34
+ apt : [ 'sudo apt-get update' , 'sudo apt-get install python3 python3-venv python3-pip' ] ,
35
+ } ;
36
+
25
37
constructor (
26
38
@inject ( ICommandManager ) private readonly commandManager : ICommandManager ,
27
- @inject ( ITerminalServiceFactory ) private readonly terminalServiceFactory : ITerminalServiceFactory ,
39
+ @inject ( ITerminalManager ) private readonly terminalManager : ITerminalManager ,
28
40
@inject ( IDisposableRegistry ) private readonly disposables : IDisposableRegistry ,
29
41
) { }
30
42
@@ -42,36 +54,49 @@ export class InstallPythonViaTerminal implements IExtensionSingleActivationServi
42
54
}
43
55
44
56
public async _installPythonOnUnix ( os : OSType . Linux | OSType . OSX ) : Promise < void > {
45
- const terminalService = this . terminalServiceFactory . getTerminalService ( { } ) ;
46
- const commands = await getCommands ( os ) ;
57
+ const commands = await this . getCommands ( os ) ;
58
+ const terminal = this . terminalManager . createTerminal ( {
59
+ name : 'Python' ,
60
+ message : commands . length ? undefined : Interpreters . installPythonTerminalMessage ,
61
+ } ) ;
62
+ terminal . show ( true ) ;
63
+ await waitForTerminalToStartup ( ) ;
47
64
for ( const command of commands ) {
48
- await terminalService . sendText ( command ) ;
65
+ terminal . sendText ( command ) ;
49
66
await waitForCommandToProcess ( ) ;
50
67
}
51
68
}
52
- }
53
69
54
- async function getCommands ( os : OSType . Linux | OSType . OSX ) {
55
- if ( os === OSType . OSX ) {
56
- return [ 'brew install python3' ] ;
70
+ private async getCommands ( os : OSType . Linux | OSType . OSX ) {
71
+ if ( os === OSType . OSX ) {
72
+ return this . packageManagerCommands [ PackageManagers . brew ] ;
73
+ }
74
+ return this . getCommandsForLinux ( ) ;
57
75
}
58
- return getCommandsForLinux ( ) ;
59
- }
60
76
61
- async function getCommandsForLinux ( ) {
62
- let isDnfAvailable = false ;
63
- try {
64
- const which = require ( 'which' ) as typeof whichTypes ;
65
- const resolvedPath = await which ( 'dnf' ) ;
66
- traceVerbose ( 'Resolved path to dnf module:' , resolvedPath ) ;
67
- isDnfAvailable = resolvedPath . trim ( ) . length > 0 ;
68
- } catch ( ex ) {
69
- traceVerbose ( 'Dnf not found' , ex ) ;
70
- isDnfAvailable = false ;
77
+ private async getCommandsForLinux ( ) {
78
+ for ( const packageManager of [ PackageManagers . apt , PackageManagers . dnf ] ) {
79
+ let isPackageAvailable = false ;
80
+ try {
81
+ const which = require ( 'which' ) as typeof whichTypes ;
82
+ const resolvedPath = await which ( packageManager ) ;
83
+ traceVerbose ( `Resolved path to ${ packageManager } module:` , resolvedPath ) ;
84
+ isPackageAvailable = resolvedPath . trim ( ) . length > 0 ;
85
+ } catch ( ex ) {
86
+ traceVerbose ( `${ packageManager } not found` , ex ) ;
87
+ isPackageAvailable = false ;
88
+ }
89
+ if ( isPackageAvailable ) {
90
+ return this . packageManagerCommands [ packageManager ] ;
91
+ }
92
+ }
93
+ return [ ] ;
71
94
}
72
- return isDnfAvailable
73
- ? [ 'sudo dnf install python3' ]
74
- : [ 'sudo apt-get update' , 'sudo apt-get install python3 python3-venv python3-pip' ] ;
95
+ }
96
+
97
+ async function waitForTerminalToStartup ( ) {
98
+ // Sometimes the terminal takes some time to start up before it can start accepting input.
99
+ await sleep ( 100 ) ;
75
100
}
76
101
77
102
async function waitForCommandToProcess ( ) {
0 commit comments