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