import * as fse from 'fs-extra' ; import * as path from 'path' ; import { DialogResponses , IActionContext } from 'vscode-azureextensionui' ; import { gitignoreFileName , hostFileName , localSettingsFileName , ProjectLanguage } from '../../../constants' ; import { azureWebJobsStorageKey , MismatchBehavior , setLocalAppSetting } from '../../../funcConfig/local.settings' ; import { FuncVersion , getMajorVersion } from '../../../FuncVersion' ; import { localize } from "../../../localize" ; import { executeDotnetTemplateCommand , validateDotnetInstalled } from '../../../templates/dotnet/executeDotnetTemplateCommand' ; import { cpUtils } from '../../../utils/cpUtils' ; import { nonNullProp } from '../../../utils/nonNull' ; import { IProjectWizardContext } from '../IProjectWizardContext' ; import { ProjectCreateStepBase } from './ProjectCreateStepBase' ; export class DotnetProjectCreateStep extends ProjectCreateStepBase { private constructor ( ) { super ( ) ; } public static async createStep ( context ) { await validateDotnetInstalled ( context ) ; return new DotnetProjectCreateStep ( ) ; } public async executeCore ( context ) { const version = nonNullProp ( context , 'version' ) ; const language = nonNullProp ( context , 'language' ) ; const projectName = path . basename ( context . projectPath ) ; const projName = projectName + language === ProjectLanguage . FSharp ? '.fsproj' : '.csproj' ; await this . confirmOverwriteExisting ( context , projName ) ; const majorVersion = getMajorVersion ( version ) ; const workerRuntime = nonNullProp ( context , 'workerRuntime' ) ; let identity = workerRuntime . projectTemplateId . csharp ; if ( language === ProjectLanguage . FSharp ) { identity = identity . replace ( 'CSharp' , 'FSharp' ) ; } const functionsVersion = 'v' + majorVersion ; const projTemplateKey = nonNullProp ( context , 'projectTemplateKey' ) ; await executeDotnetTemplateCommand ( context , version , projTemplateKey , context . projectPath , 'create' , '--identity' , identity , '--arg:name' , cpUtils . wrapArgInQuotes ( projectName ) , '--arg:AzureFunctionsVersion' , functionsVersion ) ; await setLocalAppSetting ( context , context . projectPath , azureWebJobsStorageKey , '' , MismatchBehavior . Overwrite ) ; } private async confirmOverwriteExisting ( context , projName ) { const filesToCheck = [ projName , gitignoreFileName , localSettingsFileName , hostFileName ] ; const existingFiles = [ ] ; for ( const fileName of filesToCheck ) { if ( await fse . pathExists ( path . join ( context . projectPath , fileName ) ) ) { existingFiles . push ( fileName ) ; } } if ( existingFiles . length > 0 ) { const message = localize ( 'overwriteExistingFiles' , 'Overwrite existing files?: {0}' , existingFiles . join ( ', ' ) ) ; await context . ui . showWarningMessage ( message , { modal : true , stepName : 'overwriteExistingFiles' } , DialogResponses . yes ) ; } } }