import * as fse from 'fs-extra' ; import * as os from 'os' ; import * as path from 'path' ; import { DebugConfiguration , TaskDefinition } from 'vscode' ; import { extInstallCommand , extInstallTaskName , func , gitignoreFileName , hostStartCommand , ProjectLanguage , pythonVenvSetting } from "../../../constants" ; import { pythonDebugConfig } from '../../../debug/PythonDebugProvider' ; import { ext } from '../../../extensionVariables' ; import { venvUtils } from '../../../utils/venvUtils' ; import { getFuncWatchProblemMatcher } from '../../../vsCodeConfig/settings' ; import { convertToFunctionsTaskLabel } from '../../../vsCodeConfig/tasks' ; import { IProjectWizardContext } from '../../createNewProject/IProjectWizardContext' ; import { IPythonVenvWizardContext } from '../../createNewProject/pythonSteps/IPythonVenvWizardContext' ; import { ScriptInitVSCodeStep } from './ScriptInitVSCodeStep' ; export class PythonInitVSCodeStep extends ScriptInitVSCodeStep { private _venvName : string | undefined ; protected async executeCore ( context ) { await super . executeCore ( context ) ; this . settings . push ( { key : 'scmDoBuildDuringDeployment' , value : true } ) ; this . _venvName = context . venvName ; if ( this . _venvName ) { this . settings . push ( { key : pythonVenvSetting , value : this . _venvName } ) ; await ensureVenvInFuncIgnore ( context . projectPath , this . _venvName ) ; } const gitignoreLines = [ '.python_packages' , '__pycache__' ] ; if ( this . _venvName ) { gitignoreLines . push ( this . _venvName ) ; } await ensureGitIgnoreContents ( context . projectPath , gitignoreLines ) ; } protected getDebugConfiguration ( ) { return pythonDebugConfig ; } protected getTasks ( language ) { const pipInstallLabel = convertToFunctionsTaskLabel ( 'pip install' ) ; const dependsOn = this . useFuncExtensionsInstall ? extInstallTaskName : this . _venvName ? pipInstallLabel : undefined ; const tasks = [ { type : func , command : hostStartCommand , problemMatcher : getFuncWatchProblemMatcher ( language ) , isBackground : true , dependsOn } ] ; if ( this . _venvName ) { if ( this . useFuncExtensionsInstall ) { tasks . push ( { type : func , command : extInstallCommand , dependsOn : pipInstallLabel , problemMatcher : [ ] } ) ; } const venvSettingReference = `\${config:${ ext . prefix }.${ pythonVenvSetting }}` ; function getPipInstallCommand ( platform ) { return venvUtils . convertToVenvPythonCommand ( 'pip install -r requirements.txt' , venvSettingReference , platform ) ; } tasks . push ( { label : pipInstallLabel , type : 'shell' , osx : { command : getPipInstallCommand ( 'darwin' ) } , windows : { command : getPipInstallCommand ( 'win32' ) } , linux : { command : getPipInstallCommand ( 'linux' ) } , problemMatcher : [ ] } ) ; } return tasks ; } protected getRecommendedExtensions ( ) { return [ 'ms-python.python' ] ; } } export async function ensureGitIgnoreContents ( projectPath , lines ) { const gitignorePath = path . join ( projectPath , gitignoreFileName ) ; if ( await fse . pathExists ( gitignorePath ) ) { let writeFile = false ; let gitignoreContents = ( await fse . readFile ( gitignorePath ) ) . toString ( ) ; for ( const line of lines ) { if ( ! gitignoreContents . includes ( line ) ) { gitignoreContents = gitignoreContents . concat ( `${ os . EOL }${ line }` ) ; writeFile = true ; } } if ( writeFile ) { await fse . writeFile ( gitignorePath , gitignoreContents ) ; } } } async function ensureVenvInFuncIgnore ( projectPath , venvName ) { const funcIgnorePath = path . join ( projectPath , '.funcignore' ) ; let funcIgnoreContents ; if ( await fse . pathExists ( funcIgnorePath ) ) { funcIgnoreContents = ( await fse . readFile ( funcIgnorePath ) ) . toString ( ) ; if ( funcIgnoreContents && ! funcIgnoreContents . includes ( venvName ) ) { funcIgnoreContents = funcIgnoreContents . concat ( `${ os . EOL }${ venvName }` ) ; } } if ( ! funcIgnoreContents ) { funcIgnoreContents = venvName ; } await fse . writeFile ( funcIgnorePath , funcIgnoreContents ) ; }