import { URI } from 'vscode-uri' ; import { Connection } from 'vscode-languageserver' ; import { withInterpreter , asyncExec } from './misc' ; import { getAnsibleCommandExecPath } from './execPath' ; import { WorkspaceFolderContext } from '../services/workspaceManager' ; import { ExtensionSettings } from '../interfaces/extensionSettings' ; export class CommandRunner { private connection : Connection ; private context : WorkspaceFolderContext ; private settings : ExtensionSettings ; constructor ( connection , context , settings ) { this . connection = connection ; this . context = context ; this . settings = settings ; } public async runCommand ( executable , args , workingDirectory ? , mountPaths ? ) { let executablePath ; let command ; let runEnv ; const isEEEnabled = this . settings . executionEnvironment . enabled ; const interpreterPath = isEEEnabled ? 'python3' : this . settings . python . interpreterPath ; if ( executable . startsWith ( 'ansible' ) ) { executablePath = isEEEnabled ? executable : getAnsibleCommandExecPath ( executable , this . settings ) ; } else { executablePath = executable ; } if ( ! isEEEnabled ) { [ command , runEnv ] = withInterpreter ( executablePath , args , interpreterPath , this . settings . python . activationScript ) ; } else { const executionEnvironment = await this . context . executionEnvironment ; command = executionEnvironment . wrapContainerArgs ( `${ executable } ${ args }` , mountPaths ) ; runEnv = undefined ; } const currentWorkingDirectory = workingDirectory ? workingDirectory : URI . parse ( this . context . workspaceFolder . uri ) . path ; const result = await asyncExec ( command , { encoding : 'utf-8' , cwd : currentWorkingDirectory , env : runEnv , } ) ; return result ; } param executable returns public async getExecutablePath ( executable ) { try { const executablePath = await this . runCommand ( 'which' , executable ) ; return executablePath . stdout . trim ( ) ; } catch ( error ) { console . log ( error ) ; } try { const executablePath = await this . runCommand ( 'whereis' , executable ) ; const outParts = executablePath . stdout . split ( ':' ) ; return outParts . length >= 2 ? outParts [ 1 ] . trim ( ) : undefined ; } catch ( error ) { console . log ( error ) ; } } }