import * as path from 'path' ; import { coerce as semVerCoerce , SemVer } from 'semver' ; import { IActionContext } from 'vscode-azureextensionui' ; import { ext } from "../../extensionVariables" ; import { FuncVersion } from '../../FuncVersion' ; import { localize } from '../../localize' ; import { cpUtils } from "../../utils/cpUtils" ; export async function executeDotnetTemplateCommand ( context , version , projTemplateKey , workingDirectory , operation , ... args ) { const framework = await getFramework ( context , workingDirectory ) ; const jsonDllPath = ext . context . asAbsolutePath ( path . join ( 'resources' , 'dotnetJsonCli' , framework , 'Microsoft.TemplateEngine.JsonCli.dll' ) ) ; return await cpUtils . executeCommand ( undefined , workingDirectory , 'dotnet' , cpUtils . wrapArgInQuotes ( jsonDllPath ) , '--templateDir' , cpUtils . wrapArgInQuotes ( getDotnetTemplateDir ( context , version , projTemplateKey ) ) , '--operation' , operation , ... args ) ; } export function getDotnetItemTemplatePath ( context , version , projTemplateKey ) { return path . join ( getDotnetTemplateDir ( context , version , projTemplateKey ) , 'item.nupkg' ) ; } export function getDotnetProjectTemplatePath ( context , version , projTemplateKey ) { return path . join ( getDotnetTemplateDir ( context , version , projTemplateKey ) , 'project.nupkg' ) ; } export function getDotnetTemplateDir ( context , version , projTemplateKey ) { const templateProvider = ext . templateProvider . get ( context ) ; return path . join ( ext . context . globalStoragePath , templateProvider . templateSource || '' , version , projTemplateKey ) ; } export async function validateDotnetInstalled ( context ) { await getFramework ( context , undefined ) ; } let cachedFramework ; async function getFramework ( context , workingDirectory ) { if ( ! cachedFramework ) { let versions = '' ; try { versions += await cpUtils . executeCommand ( undefined , workingDirectory , 'dotnet' , '--version' ) ; } catch { } try { versions += await cpUtils . executeCommand ( undefined , workingDirectory , 'dotnet' , '--list-sdks' ) ; } catch { } const netVersions = [ '6.0' , '5.0' , '3.1' ] ; const semVersions = netVersions . map ( v => semVerCoerce ( v ) as SemVer ) ; let pickedVersion ; for ( const semVersion of semVersions ) { const regExp = new RegExp ( `^\\s*${ semVersion . major }\\.${ semVersion . minor }\\.[0-9]+(\\s|$)` , 'm' ) ; if ( regExp . test ( versions ) ) { pickedVersion = semVersion ; break ; } } if ( ! pickedVersion ) { for ( const semVersion of semVersions ) { const regExp = new RegExp ( `^\\s*${ semVersion . major }\\.${ semVersion . minor }\\.` , 'm' ) ; if ( regExp . test ( versions ) ) { pickedVersion = semVersion ; break ; } } } if ( ! pickedVersion ) { context . errorHandling . suppressReportIssue = true ; throw new Error ( localize ( 'noMatchingFramework' , 'You must have the [.NET Core SDK](https://aka.ms/AA4ac70) installed to perform this operation. See [here](https://aka.ms/AA1tpij) for supported versions.' ) ) ; } else { cachedFramework = `${ pickedVersion . major < 4 ? 'netcoreapp' : 'net' }${ pickedVersion . major }.${ pickedVersion . minor }` ; } } return cachedFramework ; }