import { MessageItem } from 'vscode' ; import { callWithTelemetryAndErrorHandling , DialogResponses , IActionContext } from 'vscode-azureextensionui' ; import { funcVersionSetting , PackageManager } from '../constants' ; import { FuncVersion , tryParseFuncVersion } from '../FuncVersion' ; import { localize } from '../localize' ; import { cpUtils } from '../utils/cpUtils' ; import { openUrl } from '../utils/openUrl' ; import { getWorkspaceSetting } from '../vsCodeConfig/settings' ; import { getFuncCliPath , hasFuncCliSetting } from './getFuncCliPath' ; import { getFuncPackageManagers } from './getFuncPackageManagers' ; import { installFuncCoreTools } from './installFuncCoreTools' ; export async function validateFuncCoreToolsInstalled ( context , message , workspacePath ) { let input ; let installed = false ; const install = { title : localize ( 'install' , 'Install' ) } ; if ( hasFuncCliSetting ( ) ) { context . telemetry . properties . funcCliSource = 'setting' ; return true ; } await callWithTelemetryAndErrorHandling ( 'azureFunctions.validateFuncCoreToolsInstalled' , async ( innerContext ) => { innerContext . errorHandling . suppressDisplay = true ; if ( ! getWorkspaceSetting < boolean > ( 'validateFuncCoreTools' , workspacePath ) ) { innerContext . telemetry . properties . validateFuncCoreTools = 'false' ; installed = true ; } else if ( await funcToolsInstalled ( innerContext , workspacePath ) ) { installed = true ; } else { const items = [ ] ; const packageManagers = await getFuncPackageManagers ( false ) ; if ( packageManagers . length > 0 ) { items . push ( install ) ; } else { items . push ( DialogResponses . learnMore ) ; } input = await innerContext . ui . showWarningMessage ( message , { modal : true } , ... items ) ; innerContext . telemetry . properties . dialogResult = input . title ; if ( input === install ) { const version = tryParseFuncVersion ( getWorkspaceSetting ( funcVersionSetting , workspacePath ) ) ; await installFuncCoreTools ( innerContext , packageManagers , version ) ; installed = true ; } else if ( input === DialogResponses . learnMore ) { await openUrl ( getInstallUrl ( ) ) ; } } } ) ; if ( input === install && ! installed ) { if ( await context . ui . showWarningMessage ( localize ( 'failedInstallFuncTools' , 'The Azure Functions Core Tools installation has failed and will have to be installed manually.' ) , DialogResponses . learnMore ) === DialogResponses . learnMore ) { await openUrl ( getInstallUrl ( ) ) ; } } return installed ; } export async function funcToolsInstalled ( context , workspacePath ) { try { const funcCliPath = await getFuncCliPath ( context , workspacePath ) ; await cpUtils . executeCommand ( undefined , workspacePath , funcCliPath , '--version' ) ; return true ; } catch ( error ) { return false ; } } export function getInstallUrl ( ) { switch ( process . platform ) { case 'linux' : return 'https://aka.ms/AAb9zn8' ; case 'darwin' : return 'https://aka.ms/AAb9zn6' ; default : return 'https://aka.ms/Dqur4e' ; } }