3
3
4
4
package software.aws.toolkits.jetbrains.services.codemodernizer
5
5
6
+ import com.intellij.execution.configurations.GeneralCommandLine
7
+ import com.intellij.execution.process.ProcessNotCreatedException
8
+ import com.intellij.execution.util.ExecUtil
6
9
import com.intellij.openapi.application.ApplicationInfo
7
10
import com.intellij.openapi.module.ModuleUtil
8
11
import com.intellij.openapi.project.Project
12
+ import com.intellij.openapi.util.SystemInfo
9
13
import com.intellij.openapi.vfs.VfsUtilCore
10
14
import com.intellij.openapi.vfs.VirtualFile
11
15
import com.intellij.openapi.vfs.VirtualFileManager
12
16
import com.intellij.serviceContainer.AlreadyDisposedException
17
+ import org.jetbrains.idea.maven.project.MavenProjectsManager
18
+ import org.jetbrains.idea.maven.utils.MavenUtil
13
19
import org.jetbrains.plugins.gradle.settings.GradleSettings
20
+ import org.slf4j.LoggerFactory
14
21
import software.amazon.awssdk.awscore.exception.AwsServiceException
15
22
import software.amazon.awssdk.core.exception.SdkClientException
16
23
import software.amazon.awssdk.services.codewhispererruntime.model.CodeWhispererRuntimeException
@@ -26,7 +33,9 @@ import software.aws.toolkits.core.TokenConnectionSettings
26
33
import software.aws.toolkits.core.utils.WaiterUnrecoverableException
27
34
import software.aws.toolkits.core.utils.Waiters.waitUntil
28
35
import software.aws.toolkits.core.utils.createParentDirectories
36
+ import software.aws.toolkits.core.utils.error
29
37
import software.aws.toolkits.core.utils.exists
38
+ import software.aws.toolkits.core.utils.warn
30
39
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
31
40
import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection
32
41
import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenProvider
@@ -84,6 +93,8 @@ val TERMINAL_STATES = setOf(
84
93
TransformationStatus .COMPLETED ,
85
94
)
86
95
96
+ private val LOG = LoggerFactory .getLogger(" CodeTransformUtils" )
97
+
87
98
fun String.toVirtualFile () = VirtualFileManager .getInstance().findFileByUrl(VfsUtilCore .pathToUrl(this ))
88
99
fun Project.moduleFor (path : String ) = ModuleUtil .findModuleForFile(
89
100
path.toVirtualFile() ? : throw RuntimeException (" File not found $path " ),
@@ -285,3 +296,55 @@ fun isCodeModernizerAvailable(project: Project): Boolean {
285
296
}
286
297
287
298
fun isGradleProject (project : Project ) = ! GradleSettings .getInstance(project).linkedProjectsSettings.isEmpty()
299
+
300
+ fun getJavaVersionFromProjectSetting (project : Project ): String? = project.tryGetJdk()?.toString()
301
+
302
+ fun getMavenVersions (project : Project ): String {
303
+ fun getVersion (mavenCommand : String ): String? {
304
+ try {
305
+ val commandLine = GeneralCommandLine (listOf (mavenCommand, " -v" ))
306
+ .withWorkDirectory(project.basePath)
307
+ .withRedirectErrorStream(true )
308
+ val output = ExecUtil .execAndGetOutput(commandLine)
309
+ if (output.exitCode == 0 ) {
310
+ return parseMavenVersion(output.stdout)
311
+ } else {
312
+ LOG .error { " Failed to fetch $mavenCommand version: ${output.stdout} " }
313
+ }
314
+ } catch (e: ProcessNotCreatedException ) {
315
+ LOG .warn { " $mavenCommand not set up" }
316
+ } catch (e: Exception ) {
317
+ LOG .error(e) { " Failed to fetch $mavenCommand version" }
318
+ }
319
+ return null
320
+ }
321
+
322
+ // Get local maven version
323
+ val localMavenVersion: String? = getVersion(" mvn" )
324
+
325
+ // Get wrapper maven version
326
+ val mvnw = if (SystemInfo .isWindows) " ./mvnw.cmd" else " ./mvnw"
327
+ val wrapperMavenVersion: String? = getVersion(mvnw)
328
+
329
+ // Get user's Maven setting (using bundled vs local vs wrapper)
330
+ val mavenSettings = MavenProjectsManager .getInstance(project).getGeneralSettings()
331
+ val mavenHome = mavenSettings.getMavenHome()
332
+ // Need to detect bundled Maven version that come with IDEA
333
+ // The utility returns "Use Maven wrapper" if using wrapper, "Bundled (Maven 3)" if using Bundled Maven, otherwise the local maven version.
334
+ val userMavenSetting = MavenUtil .getMavenVersion(mavenHome) ? : mavenHome
335
+
336
+ return " $wrapperMavenVersion (mvnw) -- $localMavenVersion (mvn) -- user setting: $userMavenSetting "
337
+ }
338
+
339
+ private fun parseMavenVersion (output : String? ): String? {
340
+ if (output == null ) return null
341
+ val mavenVersionIndex = output.indexOf(" Apache Maven" )
342
+ if (mavenVersionIndex == - 1 ) return null
343
+ return try {
344
+ val mavenVersionString = output.slice(IntRange (mavenVersionIndex + 13 , output.length - 1 ))
345
+ mavenVersionString.slice(IntRange (0 , output.indexOf(' ' ) - 1 ))
346
+ } catch (e: StringIndexOutOfBoundsException ) {
347
+ LOG .error(e) { " Failed to parse Maven version from output: $output " }
348
+ null
349
+ }
350
+ }
0 commit comments