55import * as vscode from 'vscode'
66import path from 'path'
77import { tempDirPath , testGenerationLogsDir } from '../../shared/filesystemUtilities'
8- import { getLogger } from '../../shared/logger/logger'
8+ import { getLogger , getNullLogger } from '../../shared/logger/logger'
99import * as CodeWhispererConstants from '../models/constants'
10- import { ToolkitError } from '../../shared/errors'
1110import { fs } from '../../shared/fs/fs'
12- import { getLoggerForScope } from '../service/securityScanHandler'
1311import { runtimeLanguageContext } from './runtimeLanguageContext'
1412import { CodewhispererLanguage } from '../../shared/telemetry/telemetry.gen'
1513import {
@@ -43,6 +41,11 @@ export interface ZipMetadata {
4341 language : CodewhispererLanguage | undefined
4442}
4543
44+ export interface GenerateZipOptions {
45+ includeGitDiffHeader ?: boolean
46+ silent ?: boolean
47+ }
48+
4649export const ZipConstants = {
4750 newlineRegex : / \r ? \n / ,
4851 gitignoreFilename : '.gitignore' ,
@@ -110,7 +113,7 @@ export class ZipUtil {
110113 if ( ZipUtil . aboveByteLimit ( this . _totalSize , 'file' ) ) {
111114 throw new FileSizeExceededError ( )
112115 }
113- const zipFilePath = this . getZipDirPath ( FeatureUseCase . CODE_SCAN ) + CodeWhispererConstants . codeScanZipExt
116+ const zipFilePath = this . getZipDirPath ( ) + CodeWhispererConstants . codeScanZipExt
114117 await zip . finalizeToFile ( zipFilePath )
115118 return zipFilePath
116119 }
@@ -209,7 +212,7 @@ export class ZipUtil {
209212 throw new NoSourceFilesError ( )
210213 }
211214 this . _language = [ ...languageCount . entries ( ) ] . reduce ( ( a , b ) => ( b [ 1 ] > a [ 1 ] ? b : a ) ) [ 0 ]
212- const zipFilePath = this . getZipDirPath ( useCase ) + CodeWhispererConstants . codeScanZipExt
215+ const zipFilePath = this . getZipDirPath ( ) + CodeWhispererConstants . codeScanZipExt
213216 await zip . finalizeToFile ( zipFilePath )
214217 return zipFilePath
215218 }
@@ -231,7 +234,7 @@ export class ZipUtil {
231234 : vscode . workspace . workspaceFolders
232235 ) as CurrentWsFolders
233236 }
234-
237+ // TODO: remove
235238 private getExcludePatterns ( useCase : FeatureUseCase ) {
236239 return useCase === FeatureUseCase . TEST_GENERATION
237240 ? [ ...CodeWhispererConstants . testGenExcludePatterns , ...defaultExcludePatterns ]
@@ -347,37 +350,27 @@ export class ZipUtil {
347350 return vscode . workspace . textDocuments . some ( ( document ) => document . uri . fsPath === uri . fsPath && document . isDirty )
348351 }
349352
350- public getZipDirPath ( useCase : FeatureUseCase ) : string {
353+ public getZipDirPath ( ) : string {
351354 if ( this . _zipDir === '' ) {
352- // const prefix =
353- // useCase === FeatureUseCase.TEST_GENERATION
354- // ? CodeWhispererConstants.TestGenerationTruncDirPrefix
355- // : CodeWhispererConstants.codeScanTruncDirPrefix
356-
357355 this . _zipDir = path . join ( this . _tmpDir , `${ this . _zipDirPrefix } _${ this . _timestamp } ` )
358356 }
359357 return this . _zipDir
360358 }
361359
362360 public async generateZip (
363361 uri : vscode . Uri | undefined ,
364- scope : CodeWhispererConstants . CodeAnalysisScope
362+ zipType : ZipType ,
363+ options ?: GenerateZipOptions
365364 ) : Promise < ZipMetadata > {
366365 try {
367- const zipDirPath = this . getZipDirPath ( FeatureUseCase . CODE_SCAN )
368- let zipFilePath : string
369- if (
370- scope === CodeWhispererConstants . CodeAnalysisScope . FILE_AUTO ||
371- scope === CodeWhispererConstants . CodeAnalysisScope . FILE_ON_DEMAND
372- ) {
373- zipFilePath = await this . zipFile ( uri , scope === CodeWhispererConstants . CodeAnalysisScope . FILE_AUTO )
374- } else if ( scope === CodeWhispererConstants . CodeAnalysisScope . PROJECT ) {
375- zipFilePath = await this . zipProject ( FeatureUseCase . CODE_SCAN )
376- } else {
377- throw new ToolkitError ( `Unknown code analysis scope: ${ scope } ` )
378- }
366+ const zipDirPath = this . getZipDirPath ( )
367+ const zipFilePath = await ( zipType === 'file'
368+ ? this . zipFile ( uri , options ?. includeGitDiffHeader )
369+ : this . zipProject ( FeatureUseCase . CODE_SCAN ) )
379370
380- getLoggerForScope ( scope ) . debug ( `Picked source files: [${ [ ...this . _pickedSourceFiles ] . join ( ', ' ) } ]` )
371+ if ( ! options ?. silent ) {
372+ getLogger ( ) . debug ( `Picked source files: [${ [ ...this . _pickedSourceFiles ] . join ( ', ' ) } ]` )
373+ }
381374 const zipFileSize = ( await fs . stat ( zipFilePath ) ) . size
382375 return {
383376 rootDir : zipDirPath ,
@@ -397,7 +390,7 @@ export class ZipUtil {
397390
398391 public async generateZipTestGen ( projectPath : string , initialExecution : boolean ) : Promise < ZipMetadata > {
399392 try {
400- const zipDirPath = this . getZipDirPath ( FeatureUseCase . TEST_GENERATION )
393+ const zipDirPath = this . getZipDirPath ( )
401394
402395 const metadataDir = path . join ( zipDirPath , 'utgRequiredArtifactsDir' )
403396
@@ -440,8 +433,8 @@ export class ZipUtil {
440433 }
441434 }
442435 // TODO: Refactor this
443- public async removeTmpFiles ( zipMetadata : ZipMetadata , scope ?: CodeWhispererConstants . CodeAnalysisScope ) {
444- const logger = getLoggerForScope ( scope )
436+ public async removeTmpFiles ( zipMetadata : ZipMetadata , silent ?: boolean ) {
437+ const logger = silent ? getNullLogger ( ) : getLogger ( )
445438 logger . verbose ( `Cleaning up temporary files...` )
446439 await fs . delete ( zipMetadata . zipFilePath , { force : true } )
447440 await fs . delete ( zipMetadata . rootDir , { recursive : true , force : true } )
@@ -454,31 +447,27 @@ interface GitDiffOptions {
454447 projectPath : string
455448 projectName : string
456449 filepath ?: string
457- scope ?: CodeWhispererConstants . CodeAnalysisScope
450+ zipType ?: ZipType
458451}
459452
460- async function getGitDiffContentForProjects (
461- projectPaths : string [ ] ,
462- filepath ?: string ,
463- scope ?: CodeWhispererConstants . CodeAnalysisScope
464- ) {
453+ async function getGitDiffContentForProjects ( projectPaths : string [ ] , filepath ?: string , zipType ?: ZipType ) {
465454 let gitDiffContent = ''
466455 for ( const projectPath of projectPaths ) {
467456 const projectName = path . basename ( projectPath )
468457 gitDiffContent += await getGitDiffContent ( {
469458 projectPath,
470459 projectName,
471460 filepath,
472- scope ,
461+ zipType ,
473462 } )
474463 }
475464 return gitDiffContent
476465}
477466
478467async function getGitDiffContent ( options : GitDiffOptions ) : Promise < string > {
479- const { projectPath, projectName, filepath : filePath , scope } = options
480- const isProjectScope = scope === CodeWhispererConstants . CodeAnalysisScope . PROJECT
468+ const { projectPath, projectName, filepath : filePath } = options
481469
470+ const isProjectScope = options . zipType === 'project'
482471 const untrackedFilesString = await getGitUntrackedFiles ( projectPath )
483472 const untrackedFilesArray = untrackedFilesString ?. trim ( ) ?. split ( '\n' ) ?. filter ( Boolean )
484473
0 commit comments