@@ -57,75 +57,70 @@ export function throwIfCancelled() {
57
57
}
58
58
}
59
59
60
- /*
61
- * This function searches for a .class file in each opened module. Then it runs javap on the found .class file to get the JDK version
62
- * for the file, and sets the version in the state variable. Only JDK8 and JDK11 are supported.
63
- */
64
- export async function getValidModules ( ) {
60
+ export async function getOpenProjects ( ) {
65
61
const folders = vscode . workspace . workspaceFolders
66
- const validModules : vscode . QuickPickItem [ ] = [ ]
67
62
if ( folders === undefined ) {
68
63
vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
69
- throw Error ( 'No Java projects found since no projects are open' )
64
+ throw new ToolkitError ( 'No Java projects found since no projects are open' , { code : 'NoOpenProjects' } )
70
65
}
71
- let containsSupportedJava = false // workspace must contain Java 8 or Java 11 code for this to be true
72
- let containsPomXml = false // workspace must contain a 'pom.xml' file for this to be true
73
- let failureReason = 'NoJavaProjectsAvailable'
66
+ const openProjects : vscode . QuickPickItem [ ] = [ ]
74
67
for ( const folder of folders ) {
75
- const compiledJavaFiles = await vscode . workspace . findFiles (
76
- new vscode . RelativePattern ( folder , '**/*.class' ) ,
77
- '**/node_modules/**' ,
78
- 1
79
- )
80
- if ( compiledJavaFiles . length < 1 ) {
81
- continue
82
- }
83
- const classFilePath = compiledJavaFiles [ 0 ] . fsPath
84
- const baseCommand = 'javap'
85
- const args = [ '-v' , classFilePath ]
86
- const spawnResult = spawnSync ( baseCommand , args , { shell : false , encoding : 'utf-8' } )
87
-
88
- if ( spawnResult . error || spawnResult . status !== 0 ) {
89
- failureReason = 'CouldNotRunJavaCommand'
90
- continue // if cannot get Java version, move on to other projects in workspace
91
- }
92
- const majorVersionIndex = spawnResult . stdout . indexOf ( 'major version: ' )
93
- const javaVersion = spawnResult . stdout . slice ( majorVersionIndex + 15 , majorVersionIndex + 17 ) . trim ( )
94
- if ( javaVersion === CodeWhispererConstants . JDK8VersionNumber ) {
95
- transformByQState . setSourceJDKVersionToJDK8 ( )
96
- containsSupportedJava = true
97
- } else if ( javaVersion === CodeWhispererConstants . JDK11VersionNumber ) {
98
- transformByQState . setSourceJDKVersionToJDK11 ( )
99
- containsSupportedJava = true
100
- } else {
101
- continue
102
- }
103
- const buildFile = await vscode . workspace . findFiles (
104
- new vscode . RelativePattern ( folder , '**/pom.xml' ) , // only supporting projects with a pom.xml for now
105
- '**/node_modules/**' ,
106
- 1
107
- )
108
- if ( buildFile . length < 1 ) {
109
- checkIfGradle ( folder )
110
- continue
111
- } else {
112
- containsPomXml = true
113
- }
114
- validModules . push ( { label : folder . name , description : folder . uri . fsPath } )
68
+ openProjects . push ( {
69
+ label : folder . name ,
70
+ description : folder . uri . fsPath ,
71
+ } )
72
+ }
73
+ return openProjects
74
+ }
75
+
76
+ /*
77
+ * This function searches for a .class file in the selected project. Then it runs javap on the found .class file to get the JDK version
78
+ * for the project, and sets the version in the state variable. Only JDK8 and JDK11 are supported. It also ensure a pom.xml file is found,
79
+ * since only the Maven build system is supported for now.
80
+ */
81
+ export async function validateProjectSelection ( project : vscode . QuickPickItem ) {
82
+ const projectPath = project . description
83
+ const compiledJavaFiles = await vscode . workspace . findFiles (
84
+ new vscode . RelativePattern ( projectPath ! , '**/*.class' ) ,
85
+ '**/node_modules/**' ,
86
+ 1
87
+ )
88
+ if ( compiledJavaFiles . length < 1 ) {
89
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
90
+ throw new ToolkitError ( 'No Java projects found' , { code : 'NoJavaProjectsAvailable' } )
115
91
}
116
- if ( ! containsSupportedJava ) {
92
+ const classFilePath = compiledJavaFiles [ 0 ] . fsPath
93
+ const baseCommand = 'javap'
94
+ const args = [ '-v' , classFilePath ]
95
+ const spawnResult = spawnSync ( baseCommand , args , { shell : false , encoding : 'utf-8' } )
96
+
97
+ if ( spawnResult . error || spawnResult . status !== 0 ) {
117
98
vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
118
- throw new ToolkitError ( 'No Java projects found ' , { code : failureReason } )
99
+ throw new ToolkitError ( 'Unable to determine Java version ' , { code : 'CannotDetermineJavaVersion' } )
119
100
}
120
- if ( ! containsPomXml ) {
121
- vscode . window . showErrorMessage ( CodeWhispererConstants . noPomXmlFoundMessage , { modal : true } )
122
- throw new ToolkitError ( 'No build file found' , { code : 'CouldNotFindPomXml' } )
101
+ const majorVersionIndex = spawnResult . stdout . indexOf ( 'major version: ' )
102
+ const javaVersion = spawnResult . stdout . slice ( majorVersionIndex + 15 , majorVersionIndex + 17 ) . trim ( )
103
+ if ( javaVersion === CodeWhispererConstants . JDK8VersionNumber ) {
104
+ transformByQState . setSourceJDKVersionToJDK8 ( )
105
+ } else if ( javaVersion === CodeWhispererConstants . JDK11VersionNumber ) {
106
+ transformByQState . setSourceJDKVersionToJDK11 ( )
123
107
} else {
124
- telemetry . amazonq_codeTransformInvoke . record ( {
125
- codeTransform_ProjectType : 'maven' ,
126
- } )
108
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noSupportedJavaProjectsFoundMessage , { modal : true } )
109
+ throw new ToolkitError ( 'Project selected is not Java 8 or Java 11' , { code : 'UnsupportedJavaVersion' } )
127
110
}
128
- return validModules
111
+ const buildFile = await vscode . workspace . findFiles (
112
+ new vscode . RelativePattern ( projectPath ! , '**/pom.xml' ) ,
113
+ '**/node_modules/**' ,
114
+ 1
115
+ )
116
+ if ( buildFile . length < 1 ) {
117
+ await checkIfGradle ( projectPath ! )
118
+ vscode . window . showErrorMessage ( CodeWhispererConstants . noPomXmlFoundMessage , { modal : true } )
119
+ throw new ToolkitError ( 'No valid Maven build file found' , { code : 'CouldNotFindPomXml' } )
120
+ }
121
+ telemetry . amazonq_codeTransformInvoke . record ( {
122
+ codeTransform_ProjectType : 'maven' ,
123
+ } )
129
124
}
130
125
131
126
export function getSha256 ( fileName : string ) {
@@ -384,14 +379,14 @@ export async function pollTransformationJob(jobId: string, validStates: string[]
384
379
return status
385
380
}
386
381
387
- async function checkIfGradle ( folder : vscode . WorkspaceFolder ) {
388
- const gradleBuildFiles = await vscode . workspace . findFiles (
389
- new vscode . RelativePattern ( folder , '**/build.gradle' ) ,
382
+ async function checkIfGradle ( projectPath : string ) {
383
+ const gradleBuildFile = await vscode . workspace . findFiles (
384
+ new vscode . RelativePattern ( projectPath , '**/build.gradle' ) ,
390
385
'**/node_modules/**' ,
391
386
1
392
387
)
393
388
394
- if ( gradleBuildFiles . length > 1 ) {
389
+ if ( gradleBuildFile . length > 0 ) {
395
390
telemetry . amazonq_codeTransformInvoke . record ( {
396
391
codeTransform_ProjectType : 'gradle' ,
397
392
} )
0 commit comments