import * as vscode from 'vscode' ; import { DialogResponses , IActionContext } from 'vscode-azureextensionui' ; import { deploySubpathSetting , ProjectLanguage } from '../constants' ; import { localize } from '../localize' ; import { dotnetUtils } from '../utils/dotnetUtils' ; import { getWorkspaceSetting , updateWorkspaceSetting } from './settings' ; import { getTasks , ITask , updateTasks } from './tasks' ; export async function verifyTargetFramework ( projectLanguage , folder , projectPath , context ) { const settingKey = 'showTargetFrameworkWarning' ; if ( getWorkspaceSetting < boolean > ( settingKey ) ) { const projFiles = await dotnetUtils . getProjFiles ( context , projectLanguage , projectPath ) ; if ( projFiles . length === 1 ) { let targetFramework ; try { targetFramework = await dotnetUtils . getTargetFramework ( projFiles [ 0 ] ) ; } catch { return ; } const tasksResult = verifyTasksFramework ( folder , targetFramework ) ; const settingsResult = verifySettingsFramework ( folder . uri . fsPath , targetFramework ) ; const mismatchTargetFramework = ( tasksResult && tasksResult . mismatchTargetFramework ) || ( settingsResult && settingsResult . mismatchTargetFramework ) ; if ( mismatchTargetFramework ) { context . telemetry . properties . verifyConfigPrompt = 'updateTargetFramework' ; const message = localize ( 'mismatchTargetFramework' , 'The targetFramework "{0}" in your project file does not match the targetFramework "{1}" in your VS Code config.' , targetFramework , mismatchTargetFramework ) ; const update = { title : localize ( 'updateTargetFramework' , 'Update VS Code config' ) } ; const result = await context . ui . showWarningMessage ( message , update , DialogResponses . dontWarnAgain ) ; if ( result === DialogResponses . dontWarnAgain ) { context . telemetry . properties . verifyConfigResult = 'dontWarnAgain' ; await updateWorkspaceSetting ( settingKey , false , folder ) ; } else if ( result === update ) { context . telemetry . properties . verifyConfigResult = 'update' ; if ( tasksResult ) { await tasksResult . update ( ) ; } if ( settingsResult ) { await settingsResult . update ( ) ; } } } } } else { context . telemetry . properties . verifyConfigResult = 'suppressed' ; } } interface IVerifyFrameworkResult { mismatchTargetFramework : string ; update ( ) : Promise < void > ; } const targetFrameworkRegExp = /net(standard|coreapp)?[0-9.]+/i ; function verifyTasksFramework ( folder , projTargetFramework ) { let mismatchTargetFramework ; const tasks = getTasks ( folder ) ; for ( const task of tasks ) { if ( task . options && task . options . cwd ) { const matches = task . options . cwd . match ( targetFrameworkRegExp ) ; const targetFramework = matches && matches [ 0 ] ; if ( targetFramework && targetFramework . toLowerCase ( ) !== projTargetFramework . toLowerCase ( ) ) { mismatchTargetFramework = targetFramework ; task . options . cwd = task . options . cwd . replace ( targetFramework , projTargetFramework ) ; } } } if ( mismatchTargetFramework ) { return { mismatchTargetFramework , update : async ( ) : Promise < void > => { await updateTasks ( folder , tasks ) ; } } ; } return undefined ; } function verifySettingsFramework ( workspacePath , projTargetFramework ) { let deploySubPath = getWorkspaceSetting ( deploySubpathSetting , workspacePath ) ; if ( deploySubPath ) { const matches = deploySubPath . match ( targetFrameworkRegExp ) ; const targetFramework = matches && matches [ 0 ] ; if ( targetFramework && targetFramework . toLowerCase ( ) !== projTargetFramework . toLowerCase ( ) ) { deploySubPath = deploySubPath . replace ( targetFramework , projTargetFramework ) ; return { mismatchTargetFramework : targetFramework , update : async ( ) : Promise < void > => { await updateWorkspaceSetting ( deploySubpathSetting , deploySubPath , workspacePath ) ; } } ; } } return undefined ; }