Skip to content

Commit 1aa0d2c

Browse files
dhasani23David Hasani
andauthored
fix(CodeTransform): smart select Java version (#4129)
* fix(CodeTransform): smart select Java version * add unit tests * fix: fix if condition * comment out tests * fix: Java version selection bug * remove print statements * fix detekt issues * fix detekt (again) * fix detektMain task failure * add back test --------- Co-authored-by: David Hasani <[email protected]>
1 parent 9aa26a3 commit 1aa0d2c

File tree

13 files changed

+163
-119
lines changed

13 files changed

+163
-119
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "feature",
3+
"description" : "CodeTransform: smart select Java version of project"
4+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mockito = "5.10.0"
2121
mockitoKotlin = "5.2.1"
2222
mockk = "1.13.8"
2323
node-gradle = "7.0.1"
24-
telemetryGenerator = "1.0.182"
24+
telemetryGenerator = "1.0.186"
2525
testLogger = "3.1.0"
2626
testRetry = "1.5.2"
2727
# test-only; platform provides slf4j transitively at runtime. <233, 1.7.36; >=233, 2.0.9

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/ArtifactHandler.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import software.aws.toolkits.jetbrains.core.coroutines.projectCoroutineScope
2121
import software.aws.toolkits.jetbrains.services.codemodernizer.client.GumbyClient
2222
import software.aws.toolkits.jetbrains.services.codemodernizer.model.CodeModernizerArtifact
2323
import software.aws.toolkits.jetbrains.services.codemodernizer.model.JobId
24+
import software.aws.toolkits.jetbrains.services.codemodernizer.state.CodeModernizerSessionState
2425
import software.aws.toolkits.jetbrains.services.codemodernizer.state.CodeTransformTelemetryState
2526
import software.aws.toolkits.jetbrains.services.codemodernizer.summary.CodeModernizerSummaryEditorProvider
2627
import software.aws.toolkits.jetbrains.utils.notifyInfo
@@ -147,13 +148,15 @@ class ArtifactHandler(private val project: Project, private val clientAdaptor: G
147148
if (dialog.showAndGet()) {
148149
CodetransformTelemetry.vcsViewerSubmitted(
149150
codeTransformSessionId = CodeTransformTelemetryState.instance.getSessionId(),
150-
codeTransformJobId = jobId.id
151+
codeTransformJobId = jobId.id,
152+
codeTransformStatus = CodeModernizerSessionState.getInstance(project).currentJobStatus.toString()
151153
)
152154
} else {
153155
CodetransformTelemetry.vcsViewerCanceled(
154156
codeTransformPatchViewerCancelSrcComponents = CodeTransformPatchViewerCancelSrcComponents.CancelButton,
155157
codeTransformSessionId = CodeTransformTelemetryState.instance.getSessionId(),
156-
codeTransformJobId = jobId.id
158+
codeTransformJobId = jobId.id,
159+
codeTransformStatus = CodeModernizerSessionState.getInstance(project).currentJobStatus.toString()
157160
)
158161
}
159162
}

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/CodeModernizerManager.kt

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import com.intellij.notification.NotificationAction
77
import com.intellij.openapi.Disposable
88
import com.intellij.openapi.application.ApplicationManager
99
import com.intellij.openapi.application.runInEdt
10-
import com.intellij.openapi.application.runReadAction
1110
import com.intellij.openapi.components.PersistentStateComponent
1211
import com.intellij.openapi.components.RoamingType
1312
import com.intellij.openapi.components.State
1413
import com.intellij.openapi.components.Storage
1514
import com.intellij.openapi.components.service
1615
import com.intellij.openapi.project.Project
17-
import com.intellij.openapi.project.modules
18-
import com.intellij.openapi.projectRoots.JavaSdkVersion
1916
import com.intellij.openapi.roots.ProjectRootManager
2017
import com.intellij.openapi.util.Disposer
2118
import com.intellij.openapi.vfs.VirtualFile
@@ -70,7 +67,6 @@ import software.aws.toolkits.telemetry.CodeTransformPreValidationError
7067
import software.aws.toolkits.telemetry.CodeTransformStartSrcComponents
7168
import software.aws.toolkits.telemetry.CodetransformTelemetry
7269
import software.aws.toolkits.telemetry.Result
73-
import java.io.File
7470
import java.time.Instant
7571
import java.util.concurrent.atomic.AtomicBoolean
7672
import javax.swing.Icon
@@ -91,10 +87,6 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
9187
}
9288
}
9389
private val supportedBuildFileNames = listOf(MAVEN_CONFIGURATION_FILE_NAME)
94-
private val supportedJavaMappings = mapOf(
95-
JavaSdkVersion.JDK_1_8 to setOf(JavaSdkVersion.JDK_17),
96-
JavaSdkVersion.JDK_11 to setOf(JavaSdkVersion.JDK_17),
97-
)
9890
private val isModernizationInProgress = AtomicBoolean(false)
9991
private val isResumingJob = AtomicBoolean(false)
10092

@@ -142,19 +134,7 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
142134
)
143135
)
144136
}
145-
val supportedModules = getSupportedModulesInProject().toSet()
146-
val validProjectJdk = project.getSupportedJavaMappingsForProject(supportedJavaMappings).isNotEmpty()
147-
if (supportedModules.isEmpty() && !validProjectJdk) {
148-
return ValidationResult(
149-
false,
150-
message("codemodernizer.notification.warn.invalid_project.description.reason.invalid_jdk_versions", supportedJavaMappings.keys.joinToString()),
151-
InvalidTelemetryReason(
152-
CodeTransformPreValidationError.UnsupportedJavaVersion,
153-
project.tryGetJdk().toString()
154-
)
155-
)
156-
}
157-
val validatedBuildFiles = getSupportedBuildFilesInProject()
137+
val validatedBuildFiles = project.getSupportedBuildModules(supportedBuildFileNames)
158138
return if (validatedBuildFiles.isNotEmpty()) {
159139
ValidationResult(true, validatedBuildFiles = validatedBuildFiles)
160140
} else {
@@ -275,7 +255,6 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
275255
fun getCustomerSelection(validatedBuildFiles: List<VirtualFile>): CustomerSelection? = PreCodeTransformUserDialog(
276256
project,
277257
validatedBuildFiles,
278-
supportedJavaMappings,
279258
).create()
280259

281260
private fun notifyJobFailure(failureReason: String?, retryable: Boolean) {
@@ -442,7 +421,9 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
442421
fun tryResumeJob() = projectCoroutineScope(project).launch {
443422
try {
444423
val notYetResumed = isResumingJob.compareAndSet(false, true)
445-
if (!notYetResumed) return@launch
424+
if (!notYetResumed) {
425+
return@launch
426+
}
446427

447428
LOG.info { "Attempting to resume job, current state is: $managerState" }
448429
if (!managerState.flags.getOrDefault(StateFlags.IS_ONGOING, false)) return@launch
@@ -649,57 +630,6 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
649630
managerState.flags.putAll(state.flags)
650631
}
651632

652-
private fun findBuildFiles(sourceFolder: File): List<File> {
653-
/**
654-
* For every dir, check if any supported build files (pom.xml etc) exists.
655-
* If found store it and stop further recursion.
656-
*/
657-
val buildFiles = mutableListOf<File>()
658-
sourceFolder.walkTopDown()
659-
.maxDepth(5)
660-
.onEnter { currentDir ->
661-
supportedBuildFileNames.forEach {
662-
val maybeSupportedFile = currentDir.resolve(MAVEN_CONFIGURATION_FILE_NAME)
663-
if (maybeSupportedFile.exists()) {
664-
buildFiles.add(maybeSupportedFile)
665-
return@onEnter false
666-
}
667-
}
668-
return@onEnter true
669-
}.forEach {
670-
// noop, collects the sequence
671-
}
672-
return buildFiles
673-
}
674-
675-
private fun getSupportedBuildFilesInProject(): List<VirtualFile> {
676-
/**
677-
* Strategy:
678-
* 1. Find folders with pom.xml or build.gradle.kts or build.gradle
679-
* 2. Filter out subdirectories
680-
*/
681-
val projectRootManager = ProjectRootManager.getInstance(project)
682-
val probableProjectRoot = project.basePath?.toVirtualFile() // May point to only one intellij module (the first opened one)
683-
val probableContentRoots = projectRootManager.contentRoots.toMutableSet() // May not point to the topmost folder of modules
684-
probableContentRoots.add(probableProjectRoot) // dedupe
685-
val topLevelRoots = filterOnlyParentFiles(probableContentRoots)
686-
val detectedBuildFiles = topLevelRoots.flatMap { root ->
687-
findBuildFiles(root.toNioPath().toFile()).mapNotNull { it.path.toVirtualFile() }
688-
}
689-
690-
val supportedModules = getSupportedModulesInProject().toSet()
691-
val validProjectJdk = project.getSupportedJavaMappingsForProject(supportedJavaMappings).isNotEmpty()
692-
return detectedBuildFiles.filter {
693-
val moduleOfFile = runReadAction { projectRootManager.fileIndex.getModuleForFile(it) }
694-
return@filter (moduleOfFile in supportedModules) || (moduleOfFile == null && validProjectJdk)
695-
}
696-
}
697-
698-
private fun getSupportedModulesInProject() = project.modules.filter {
699-
val moduleJdk = it.tryGetJdk(project) ?: return@filter false
700-
moduleJdk in supportedJavaMappings
701-
}
702-
703633
fun warnUnsupportedProject(reason: String?) {
704634
addCodeModernizeUI(true)
705635
val maybeUnknownReason = reason ?: message("codemodernizer.notification.warn.invalid_project.description.reason.unknown")

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/CodeModernizerSession.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import java.util.concurrent.CancellationException
4545
import java.util.concurrent.atomic.AtomicBoolean
4646

4747
const val ZIP_SOURCES_PATH = "sources"
48+
const val BUILD_LOG_PATH = "build-logs.txt"
4849
const val UPLOAD_ZIP_MANIFEST_VERSION = 1.0F
4950

5051
class CodeModernizerSession(

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/CodeTransformUtils.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.execution.util.ExecUtil
99
import com.intellij.openapi.application.ApplicationInfo
1010
import com.intellij.openapi.module.ModuleUtil
1111
import com.intellij.openapi.project.Project
12+
import com.intellij.openapi.projectRoots.JavaSdkVersion
1213
import com.intellij.openapi.util.SystemInfo
1314
import com.intellij.openapi.vfs.VfsUtilCore
1415
import com.intellij.openapi.vfs.VirtualFile
@@ -44,9 +45,13 @@ import software.aws.toolkits.jetbrains.core.gettingstarted.editor.ActiveConnecti
4445
import software.aws.toolkits.jetbrains.core.gettingstarted.editor.BearerTokenFeatureSet
4546
import software.aws.toolkits.jetbrains.core.gettingstarted.editor.checkBearerConnectionValidity
4647
import software.aws.toolkits.jetbrains.services.codemodernizer.client.GumbyClient
48+
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.CodeModernizerUIConstants
4749
import software.aws.toolkits.jetbrains.services.codemodernizer.model.JobId
50+
import software.aws.toolkits.jetbrains.services.codemodernizer.model.MAVEN_CONFIGURATION_FILE_NAME
4851
import software.aws.toolkits.jetbrains.services.codemodernizer.state.CodeTransformTelemetryState
52+
import software.aws.toolkits.resources.message
4953
import software.aws.toolkits.telemetry.CodetransformTelemetry
54+
import java.io.File
5055
import java.io.FileOutputStream
5156
import java.lang.Thread.sleep
5257
import java.nio.file.Path
@@ -127,6 +132,17 @@ fun String.toTransformationLanguage() = when (this) {
127132
else -> TransformationLanguage.UNKNOWN_TO_SDK_VERSION
128133
}
129134

135+
fun getJdkVersionText(version: JavaSdkVersion?): String {
136+
val jdkVersionText: String = if (version == null) {
137+
message("codemodernizer.customerselectiondialog.unknown_jdk")
138+
} else if (CodeModernizerUIConstants.supportedSourceJDKs.contains(version)) { // detected java version is supported
139+
message("codemodernizer.customerselectiondialog.found_supported_jdk", version)
140+
} else {
141+
message("codemodernizer.customerselectiondialog.found_unsupported_jdk", version) // found the version, but unsupported
142+
}
143+
return jdkVersionText
144+
}
145+
130146
fun calculateTotalLatency(startTime: Instant, endTime: Instant) = (endTime.toEpochMilli() - startTime.toEpochMilli()).toInt()
131147

132148
data class PollingResult(
@@ -247,6 +263,29 @@ fun filterOnlyParentFiles(filePaths: Set<VirtualFile>): List<VirtualFile> {
247263
return shortestRoots.toList()
248264
}
249265

266+
/**
267+
* @description For every directory, check if any supported build files (pom.xml etc) exists.
268+
* If we find a valid build file, store it and stop further recursion.
269+
*/
270+
fun findBuildFiles(sourceFolder: File, supportedBuildFileNames: List<String>): List<File> {
271+
val buildFiles = mutableListOf<File>()
272+
sourceFolder.walkTopDown()
273+
.maxDepth(5)
274+
.onEnter { currentDir ->
275+
supportedBuildFileNames.forEach {
276+
val maybeSupportedFile = currentDir.resolve(MAVEN_CONFIGURATION_FILE_NAME)
277+
if (maybeSupportedFile.exists()) {
278+
buildFiles.add(maybeSupportedFile)
279+
return@onEnter false
280+
}
281+
}
282+
return@onEnter true
283+
}.forEach {
284+
// noop, collects the sequence
285+
}
286+
return buildFiles
287+
}
288+
250289
fun isIntellij(): Boolean {
251290
val productCode = ApplicationInfo.getInstance().build.productCode
252291
return productCode == "IC" || productCode == "IU"

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/ModuleUtils.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,35 @@ import com.intellij.openapi.roots.ModuleRootManager
1212
import com.intellij.openapi.roots.ProjectRootManager
1313
import com.intellij.openapi.vfs.VirtualFile
1414

15+
/**
16+
* @description Try to get the module SDK version and fallback to the project SDK version from the "project structure" settings.
17+
*/
1518
fun Module.tryGetJdk(project: Project): JavaSdkVersion? {
1619
val sdk = ModuleRootManager.getInstance(this).sdk ?: ProjectRootManager.getInstance(project).projectSdk ?: return null
1720
val javaSdk = JavaSdkImpl.getInstance()
1821
return javaSdk.getVersion(sdk)
1922
}
2023

21-
fun Project.getSupportedJavaMappingsForProject(supportedJavaMappings: Map<JavaSdkVersion, Set<JavaSdkVersion>>): List<String> {
22-
val projectSdk = ProjectRootManager.getInstance(this).projectSdk
23-
val javaSdk = JavaSdkImpl.getInstance()
24-
return if (projectSdk == null) {
25-
listOf()
26-
} else {
27-
supportedJavaMappings.getOrDefault(javaSdk.getVersion(projectSdk), listOf()).map { it.name }.toList()
24+
/**
25+
* @description Strategy:
26+
* 1. Find folders with pom.xml or build.gradle.kts or build.gradle
27+
* 2. Filter out subdirectories
28+
*/
29+
fun Project.getSupportedBuildModules(supportedBuildFileNames: List<String>): List<VirtualFile> {
30+
val projectRootManager = ProjectRootManager.getInstance(this)
31+
val probableProjectRoot = this.basePath?.toVirtualFile() // May point to only one intellij module (the first opened one)
32+
val probableContentRoots = projectRootManager.contentRoots.toMutableSet() // May not point to the topmost folder of modules
33+
probableContentRoots.add(probableProjectRoot) // dedupe
34+
val topLevelRoots = filterOnlyParentFiles(probableContentRoots)
35+
val detectedBuildFiles = topLevelRoots.flatMap { root ->
36+
findBuildFiles(root.toNioPath().toFile(), supportedBuildFileNames).mapNotNull { it.path.toVirtualFile() }
2837
}
38+
return detectedBuildFiles
2939
}
3040

41+
/**
42+
* @description Try to get the project SDK version from the "project structure" settings
43+
*/
3144
fun Project.tryGetJdk(): JavaSdkVersion? {
3245
val projectSdk = ProjectRootManager.getInstance(this).projectSdk
3346
val javaSdk = JavaSdkImpl.getInstance()

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/constants/CodeModernizerUIConstants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package software.aws.toolkits.jetbrains.services.codemodernizer.constants
55

6+
import com.intellij.openapi.projectRoots.JavaSdkVersion
67
import com.intellij.ui.JBColor
78
import icons.AwsIcons
89
import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererColorUtil
@@ -54,6 +55,7 @@ class CodeModernizerUIConstants {
5455
}
5556

5657
companion object {
58+
val supportedSourceJDKs = listOf(JavaSdkVersion.JDK_1_8, JavaSdkVersion.JDK_11)
5759
const val SINGLE_SPACE_STRING: String = " "
5860
const val EMPTY_SPACE_STRING: String = ""
5961
val transformationPlanPlaneConstraint = GridBagConstraints().apply {

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/model/CodeModernizerSessionContext.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,15 @@ data class CodeModernizerSessionContext(
176176
val installCommandList = listOf(
177177
"clean",
178178
"install",
179+
"-q",
179180
)
180181
val copyCommandList = listOf(
181182
"dependency:copy-dependencies",
182183
"-DoutputDirectory=$destinationDir",
183184
"-Dmdep.useRepositoryLayout=true",
184185
"-Dmdep.copyPom=true",
185-
"-Dmdep.addParentPoms=true"
186+
"-Dmdep.addParentPoms=true",
187+
"-q",
186188
)
187189
fun runInstallCommand(mavenCommand: String): ProcessOutput {
188190
buildlogBuilder.appendLine("Command Run: $mavenCommand clean install")

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codemodernizer/model/ZipManifest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
package software.aws.toolkits.jetbrains.services.codemodernizer.model
55

6+
import software.aws.toolkits.jetbrains.services.codemodernizer.BUILD_LOG_PATH
67
import software.aws.toolkits.jetbrains.services.codemodernizer.UPLOAD_ZIP_MANIFEST_VERSION
78
import software.aws.toolkits.jetbrains.services.codemodernizer.ZIP_SOURCES_PATH
89

910
data class ZipManifest(
1011
val sourcesRoot: String = ZIP_SOURCES_PATH,
1112
val dependenciesRoot: String? = null,
13+
val buildLogs: String = BUILD_LOG_PATH,
1214
val version: String = UPLOAD_ZIP_MANIFEST_VERSION.toString()
1315
)

0 commit comments

Comments
 (0)