3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import { transformByQState , TransformByQStoppedError , ZipManifest } from '../models/model'
6
+ import { BuildSystem , transformByQState , TransformByQStoppedError , ZipManifest } from '../models/model'
7
7
import * as codeWhisperer from '../client/codewhisperer'
8
8
import * as crypto from 'crypto'
9
9
import { getLogger } from '../../shared/logger'
@@ -64,7 +64,7 @@ export function throwIfCancelled() {
64
64
export async function getOpenProjects ( ) {
65
65
const folders = vscode . workspace . workspaceFolders
66
66
if ( folders === undefined ) {
67
- vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
67
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage )
68
68
throw new ToolkitError ( 'No Java projects found since no projects are open' , { code : 'NoOpenProjects' } )
69
69
}
70
70
const openProjects : vscode . QuickPickItem [ ] = [ ]
@@ -84,13 +84,24 @@ export async function getOpenProjects() {
84
84
*/
85
85
export async function validateProjectSelection ( project : vscode . QuickPickItem ) {
86
86
const projectPath = project . description
87
+ const buildSystem = await checkBuildSystem ( projectPath ! )
88
+ if ( buildSystem !== BuildSystem . Maven ) {
89
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noPomXmlFoundMessage )
90
+ telemetry . codeTransform_isDoubleClickedToTriggerInvalidProject . emit ( {
91
+ codeTransformSessionId : codeTransformTelemetryState . getSessionId ( ) ,
92
+ codeTransformPreValidationError : 'NonMavenProject' ,
93
+ result : MetadataResult . Fail ,
94
+ reason : buildSystem === BuildSystem . Gradle ? buildSystem : 'NoPomFileFound' ,
95
+ } )
96
+ throw new ToolkitError ( 'No valid Maven build file found' , { code : 'CouldNotFindPomXml' } )
97
+ }
87
98
const compiledJavaFiles = await vscode . workspace . findFiles (
88
99
new vscode . RelativePattern ( projectPath ! , '**/*.class' ) ,
89
100
'**/node_modules/**' ,
90
101
1
91
102
)
92
103
if ( compiledJavaFiles . length < 1 ) {
93
- vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
104
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage )
94
105
telemetry . codeTransform_isDoubleClickedToTriggerInvalidProject . emit ( {
95
106
codeTransformSessionId : codeTransformTelemetryState . getSessionId ( ) ,
96
107
codeTransformPreValidationError : 'NoJavaProject' ,
@@ -105,7 +116,7 @@ export async function validateProjectSelection(project: vscode.QuickPickItem) {
105
116
const spawnResult = spawnSync ( baseCommand , args , { shell : false , encoding : 'utf-8' } )
106
117
107
118
if ( spawnResult . error || spawnResult . status !== 0 ) {
108
- vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
119
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage )
109
120
telemetry . codeTransform_isDoubleClickedToTriggerInvalidProject . emit ( {
110
121
codeTransformSessionId : codeTransformTelemetryState . getSessionId ( ) ,
111
122
codeTransformPreValidationError : 'NoJavaProject' ,
@@ -124,7 +135,7 @@ export async function validateProjectSelection(project: vscode.QuickPickItem) {
124
135
} else if ( javaVersion === CodeWhispererConstants . JDK11VersionNumber ) {
125
136
transformByQState . setSourceJDKVersionToJDK11 ( )
126
137
} else {
127
- vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
138
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage )
128
139
telemetry . codeTransform_isDoubleClickedToTriggerInvalidProject . emit ( {
129
140
codeTransformSessionId : codeTransformTelemetryState . getSessionId ( ) ,
130
141
codeTransformPreValidationError : 'UnsupportedJavaVersion' ,
@@ -133,30 +144,6 @@ export async function validateProjectSelection(project: vscode.QuickPickItem) {
133
144
} )
134
145
throw new ToolkitError ( 'Project selected is not Java 8 or Java 11' , { code : 'UnsupportedJavaVersion' } )
135
146
}
136
- const buildFile = await vscode . workspace . findFiles (
137
- new vscode . RelativePattern ( projectPath ! , 'pom.xml' ) , // check for pom.xml in root directory only
138
- '**/node_modules/**' ,
139
- 1
140
- )
141
- if ( buildFile . length < 1 ) {
142
- const buildType = await checkIfGradle ( projectPath ! )
143
- vscode . window . showErrorMessage ( CodeWhispererConstants . noPomXmlFoundMessage , { modal : true } )
144
- if ( buildType === 'Gradle' ) {
145
- telemetry . codeTransform_isDoubleClickedToTriggerInvalidProject . emit ( {
146
- codeTransformSessionId : codeTransformTelemetryState . getSessionId ( ) ,
147
- codeTransformPreValidationError : 'NonMavenProject' ,
148
- result : MetadataResult . Fail ,
149
- reason : buildType ,
150
- } )
151
- }
152
- telemetry . codeTransform_isDoubleClickedToTriggerInvalidProject . emit ( {
153
- codeTransformSessionId : codeTransformTelemetryState . getSessionId ( ) ,
154
- codeTransformPreValidationError : 'NoPom' ,
155
- result : MetadataResult . Fail ,
156
- reason : 'CouldNotFindPomXml' ,
157
- } )
158
- throw new ToolkitError ( 'No valid Maven build file found' , { code : 'CouldNotFindPomXml' } )
159
- }
160
147
}
161
148
162
149
export function getSha256 ( fileName : string ) {
@@ -188,7 +175,6 @@ export async function uploadArtifactToS3(fileName: string, resp: CreateUploadUrl
188
175
const sha256 = getSha256 ( fileName )
189
176
const headersObj = getHeadersObj ( sha256 , resp . kmsKeyArn )
190
177
191
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
192
178
throwIfCancelled ( )
193
179
try {
194
180
const apiStartTime = Date . now ( )
@@ -255,7 +241,6 @@ export async function stopJob(jobId: string) {
255
241
256
242
export async function uploadPayload ( payloadFileName : string ) {
257
243
const sha256 = getSha256 ( payloadFileName )
258
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
259
244
throwIfCancelled ( )
260
245
try {
261
246
const apiStartTime = Date . now ( )
@@ -290,14 +275,25 @@ export async function uploadPayload(payloadFileName: string) {
290
275
}
291
276
}
292
277
293
- function getFilesRecursively ( dir : string ) : string [ ] {
278
+ /**
279
+ * Gets all files in dir. We use this method to get the source code, then we run a mvn command to
280
+ * copy over dependencies into their own folder, then we use this method again to get those
281
+ * dependencies. If isDependenciesFolder is true, then we are getting all the files
282
+ * of the dependencies which were copied over by the previously-run mvn command, in which case
283
+ * we DO want to include any dependencies that may happen to be named "target", hence the check
284
+ * in the first part of the IF statement. The point of excluding folders named target is that
285
+ * "target" is also the name of the folder where .class files, large JARs, etc. are stored after
286
+ * building, and we do not want these included in the ZIP so we exclude these when calling
287
+ * getFilesRecursively on the source code folder.
288
+ */
289
+ function getFilesRecursively ( dir : string , isDependenciesFolder : boolean ) : string [ ] {
294
290
const entries = fs . readdirSync ( dir , { withFileTypes : true } )
295
291
const files = entries . flatMap ( entry => {
296
292
const res = path . resolve ( dir , entry . name )
297
- // exclude 'target' directory from ZIP due to issues in backend
293
+ // exclude 'target' directory from ZIP (except if zipping dependencies) due to issues in backend
298
294
if ( entry . isDirectory ( ) ) {
299
- if ( entry . name !== 'target' ) {
300
- return getFilesRecursively ( res )
295
+ if ( isDependenciesFolder || entry . name !== 'target' ) {
296
+ return getFilesRecursively ( res , isDependenciesFolder )
301
297
} else {
302
298
return [ ]
303
299
}
@@ -324,7 +320,7 @@ function getProjectDependencies(modulePath: string): string[] {
324
320
const spawnResult = spawnSync ( baseCommand , args , { cwd : modulePath , shell : false , encoding : 'utf-8' } )
325
321
326
322
if ( spawnResult . error || spawnResult . status !== 0 ) {
327
- vscode . window . showErrorMessage ( CodeWhispererConstants . dependencyErrorMessage , { modal : true } )
323
+ vscode . window . showErrorMessage ( CodeWhispererConstants . dependencyErrorMessage )
328
324
getLogger ( ) . error ( 'CodeTransform: Error in running Maven command = ' )
329
325
// Maven command can still go through and still return an error. Won't be caught in spawnResult.error in this case
330
326
if ( spawnResult . error ) {
@@ -339,13 +335,11 @@ function getProjectDependencies(modulePath: string): string[] {
339
335
}
340
336
341
337
export async function zipCode ( modulePath : string ) {
342
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
343
338
throwIfCancelled ( )
344
339
const zipStartTime = Date . now ( )
345
340
const sourceFolder = modulePath
346
- const sourceFiles = getFilesRecursively ( sourceFolder )
341
+ const sourceFiles = getFilesRecursively ( sourceFolder , false )
347
342
348
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
349
343
throwIfCancelled ( )
350
344
351
345
let dependencyFolderInfo : string [ ] = [ ]
@@ -359,7 +353,6 @@ export async function zipCode(modulePath: string) {
359
353
const dependencyFolderPath = ! mavenFailed ? dependencyFolderInfo [ 0 ] : ''
360
354
const dependencyFolderName = ! mavenFailed ? dependencyFolderInfo [ 1 ] : ''
361
355
362
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
363
356
throwIfCancelled ( )
364
357
365
358
const zip = new AdmZip ( )
@@ -371,12 +364,11 @@ export async function zipCode(modulePath: string) {
371
364
zip . addLocalFile ( file , path . dirname ( paddedPath ) )
372
365
}
373
366
374
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
375
367
throwIfCancelled ( )
376
368
377
369
let dependencyFiles : string [ ] = [ ]
378
370
if ( ! mavenFailed && fs . existsSync ( dependencyFolderPath ) ) {
379
- dependencyFiles = getFilesRecursively ( dependencyFolderPath )
371
+ dependencyFiles = getFilesRecursively ( dependencyFolderPath , true )
380
372
}
381
373
382
374
if ( ! mavenFailed && dependencyFiles . length > 0 ) {
@@ -395,7 +387,6 @@ export async function zipCode(modulePath: string) {
395
387
}
396
388
zip . addFile ( 'manifest.json' , Buffer . from ( JSON . stringify ( zipManifest ) , 'utf-8' ) )
397
389
398
- await sleep ( 2000 ) // pause to give time to recognize potential cancellation
399
390
throwIfCancelled ( )
400
391
401
392
const tempFilePath = path . join ( os . tmpdir ( ) , 'zipped-code.zip' )
@@ -605,11 +596,14 @@ export async function pollTransformationJob(jobId: string, validStates: string[]
605
596
return status
606
597
}
607
598
608
- async function checkIfGradle ( projectPath : string ) {
599
+ export async function checkBuildSystem ( projectPath : string ) {
600
+ const mavenBuildFilePath = path . join ( projectPath , 'pom.xml' )
601
+ if ( fs . existsSync ( mavenBuildFilePath ) ) {
602
+ return BuildSystem . Maven
603
+ }
609
604
const gradleBuildFilePath = path . join ( projectPath , 'build.gradle' )
610
605
if ( fs . existsSync ( gradleBuildFilePath ) ) {
611
- return 'Gradle'
612
- } else {
613
- return 'Unknown'
606
+ return BuildSystem . Gradle
614
607
}
608
+ return BuildSystem . Unknown
615
609
}
0 commit comments